🐛 Fix compability issue on web platform
This commit is contained in:
@ -151,6 +151,16 @@ class AccountScreen extends HookConsumerWidget {
|
||||
context.router.push(WalletRoute());
|
||||
},
|
||||
),
|
||||
ListTile(
|
||||
minTileHeight: 48,
|
||||
leading: const Icon(Symbols.people),
|
||||
trailing: const Icon(Symbols.chevron_right),
|
||||
contentPadding: EdgeInsets.symmetric(horizontal: 24),
|
||||
title: Text('relationships').tr(),
|
||||
onTap: () {
|
||||
context.router.push(RelationshipRoute());
|
||||
},
|
||||
),
|
||||
ListTile(
|
||||
minTileHeight: 48,
|
||||
leading: const Icon(Symbols.edit),
|
||||
|
270
lib/screens/account/relationship.dart
Normal file
270
lib/screens/account/relationship.dart
Normal file
@ -0,0 +1,270 @@
|
||||
import 'package:auto_route/auto_route.dart';
|
||||
import 'package:easy_localization/easy_localization.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter/services.dart';
|
||||
import 'package:flutter_hooks/flutter_hooks.dart';
|
||||
import 'package:hooks_riverpod/hooks_riverpod.dart';
|
||||
import 'package:island/pods/userinfo.dart';
|
||||
import 'package:island/widgets/account/account_picker.dart';
|
||||
import 'package:island/widgets/alert.dart';
|
||||
import 'package:island/widgets/app_scaffold.dart';
|
||||
import 'package:island/widgets/content/cloud_files.dart';
|
||||
import 'package:material_symbols_icons/symbols.dart';
|
||||
import 'package:relative_time/relative_time.dart';
|
||||
import 'package:riverpod_annotation/riverpod_annotation.dart';
|
||||
import 'package:riverpod_paging_utils/riverpod_paging_utils.dart';
|
||||
import 'package:island/models/relationship.dart';
|
||||
import 'package:island/pods/network.dart';
|
||||
|
||||
part 'relationship.g.dart';
|
||||
|
||||
@riverpod
|
||||
Future<List<SnRelationship>> sendFriendRequest(Ref ref) async {
|
||||
final client = ref.read(apiClientProvider);
|
||||
final resp = await client.post('/relationships/requests');
|
||||
return resp.data
|
||||
.map((e) => SnRelationship.fromJson(e))
|
||||
.cast<SnRelationship>()
|
||||
.toList();
|
||||
}
|
||||
|
||||
@riverpod
|
||||
class RelationshipListNotifier extends _$RelationshipListNotifier
|
||||
with CursorPagingNotifierMixin<SnRelationship> {
|
||||
@override
|
||||
Future<CursorPagingData<SnRelationship>> build() => fetch(cursor: null);
|
||||
|
||||
@override
|
||||
Future<CursorPagingData<SnRelationship>> fetch({
|
||||
required String? cursor,
|
||||
}) async {
|
||||
final client = ref.read(apiClientProvider);
|
||||
final offset = cursor == null ? 0 : int.parse(cursor);
|
||||
final take = 20;
|
||||
|
||||
final response = await client.get(
|
||||
'/relationships',
|
||||
queryParameters: {'offset': offset, 'take': take},
|
||||
);
|
||||
|
||||
final List<SnRelationship> items =
|
||||
(response.data as List)
|
||||
.map((e) => SnRelationship.fromJson(e as Map<String, dynamic>))
|
||||
.toList();
|
||||
|
||||
final total = int.tryParse(response.headers['x-total']?.first ?? '') ?? 0;
|
||||
final hasMore = offset + items.length < total;
|
||||
final nextCursor = hasMore ? (offset + items.length).toString() : null;
|
||||
|
||||
return CursorPagingData(
|
||||
items: items,
|
||||
hasMore: hasMore,
|
||||
nextCursor: nextCursor,
|
||||
);
|
||||
}
|
||||
|
||||
void updateOne(int index, SnRelationship relationship) {
|
||||
final currentState = state.valueOrNull;
|
||||
if (currentState == null) return;
|
||||
|
||||
final updatedItems = [...currentState.items];
|
||||
updatedItems[index] = relationship;
|
||||
|
||||
state = AsyncData(
|
||||
CursorPagingData(
|
||||
items: updatedItems,
|
||||
hasMore: currentState.hasMore,
|
||||
nextCursor: currentState.nextCursor,
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@RoutePage()
|
||||
class RelationshipScreen extends HookConsumerWidget {
|
||||
const RelationshipScreen({super.key});
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context, WidgetRef ref) {
|
||||
final relationshipNotifier = ref.watch(
|
||||
relationshipListNotifierProvider.notifier,
|
||||
);
|
||||
|
||||
Future<void> addFriend() async {
|
||||
final result = await showModalBottomSheet(
|
||||
context: context,
|
||||
builder: (context) => AccountPickerSheet(),
|
||||
);
|
||||
if (result == null) return;
|
||||
|
||||
final client = ref.read(apiClientProvider);
|
||||
await client.post('/relationships/${result.id}/friends');
|
||||
relationshipNotifier.forceRefresh();
|
||||
}
|
||||
|
||||
final submitting = useState(false);
|
||||
|
||||
Future<void> handleFriendRequest(
|
||||
SnRelationship relationship,
|
||||
bool isAccept,
|
||||
) async {
|
||||
try {
|
||||
submitting.value = true;
|
||||
final client = ref.read(apiClientProvider);
|
||||
await client.post(
|
||||
'/relationships/${relationship.accountId}/friends/${isAccept ? 'accept' : 'decline'}',
|
||||
);
|
||||
relationshipNotifier.forceRefresh();
|
||||
if (!context.mounted) return;
|
||||
if (isAccept) {
|
||||
showSnackBar(
|
||||
context,
|
||||
'friendRequestAccepted'.tr(args: ['@${relationship.account.name}']),
|
||||
);
|
||||
} else {
|
||||
showSnackBar(
|
||||
context,
|
||||
'friendRequestDeclined'.tr(args: ['@${relationship.account.name}']),
|
||||
);
|
||||
}
|
||||
HapticFeedback.lightImpact();
|
||||
} catch (err) {
|
||||
showErrorAlert(err);
|
||||
} finally {
|
||||
submitting.value = false;
|
||||
}
|
||||
}
|
||||
|
||||
final user = ref.watch(userInfoProvider);
|
||||
final requests = ref.watch(sendFriendRequestProvider);
|
||||
|
||||
return AppScaffold(
|
||||
appBar: AppBar(title: Text('relationships').tr()),
|
||||
body: Column(
|
||||
children: [
|
||||
ListTile(
|
||||
leading: const Icon(Symbols.add),
|
||||
title: Text('addFriend').tr(),
|
||||
subtitle: Text('addFriendHint').tr(),
|
||||
contentPadding: const EdgeInsets.symmetric(horizontal: 24),
|
||||
onTap: addFriend,
|
||||
),
|
||||
if (requests.hasValue && requests.value!.isNotEmpty)
|
||||
ListTile(
|
||||
leading: const Icon(Symbols.add),
|
||||
title: Text('friendSentRequest').tr(),
|
||||
subtitle: Text(
|
||||
'friendSentRequestHint'.plural(requests.value!.length),
|
||||
),
|
||||
contentPadding: const EdgeInsets.symmetric(horizontal: 24),
|
||||
),
|
||||
const Divider(height: 1),
|
||||
Expanded(
|
||||
child: PagingHelperView(
|
||||
provider: relationshipListNotifierProvider,
|
||||
futureRefreshable: relationshipListNotifierProvider.future,
|
||||
notifierRefreshable: relationshipListNotifierProvider.notifier,
|
||||
contentBuilder:
|
||||
(data, widgetCount, endItemView) => ListView.builder(
|
||||
padding: EdgeInsets.zero,
|
||||
itemCount: widgetCount,
|
||||
itemBuilder: (context, index) {
|
||||
if (index == widgetCount - 1) {
|
||||
return endItemView;
|
||||
}
|
||||
|
||||
final relationship = data.items[index];
|
||||
final account = relationship.account;
|
||||
final isPending =
|
||||
relationship.status == 0 &&
|
||||
relationship.relatedId == user.value?.id;
|
||||
final isWaiting =
|
||||
relationship.status == 0 &&
|
||||
relationship.accountId == user.value?.id;
|
||||
|
||||
return ListTile(
|
||||
contentPadding: const EdgeInsets.only(
|
||||
left: 16,
|
||||
right: 12,
|
||||
),
|
||||
leading: ProfilePictureWidget(
|
||||
fileId: account.profile.pictureId,
|
||||
),
|
||||
title: Row(
|
||||
spacing: 6,
|
||||
children: [
|
||||
Flexible(child: Text(account.nick)),
|
||||
if (isPending) // Pending
|
||||
Badge(
|
||||
label: Text('pendingRequest').tr(),
|
||||
backgroundColor:
|
||||
Theme.of(context).colorScheme.primary,
|
||||
textColor:
|
||||
Theme.of(context).colorScheme.onPrimary,
|
||||
)
|
||||
else if (isWaiting) // Waiting
|
||||
Badge(
|
||||
label: Text('pendingRequest').tr(),
|
||||
backgroundColor:
|
||||
Theme.of(context).colorScheme.secondary,
|
||||
textColor:
|
||||
Theme.of(context).colorScheme.onSecondary,
|
||||
),
|
||||
if (relationship.expiredAt != null)
|
||||
Badge(
|
||||
label: Text(
|
||||
'requestExpiredIn'.tr(
|
||||
args: [
|
||||
RelativeTime(
|
||||
context,
|
||||
).format(relationship.expiredAt!),
|
||||
],
|
||||
),
|
||||
),
|
||||
backgroundColor:
|
||||
Theme.of(context).colorScheme.tertiary,
|
||||
textColor:
|
||||
Theme.of(context).colorScheme.onTertiary,
|
||||
),
|
||||
],
|
||||
),
|
||||
subtitle: Text('@${account.name}'),
|
||||
trailing: Row(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
children: [
|
||||
if (isPending)
|
||||
IconButton(
|
||||
padding: EdgeInsets.zero,
|
||||
onPressed:
|
||||
submitting.value
|
||||
? null
|
||||
: () => handleFriendRequest(
|
||||
relationship,
|
||||
true,
|
||||
),
|
||||
icon: const Icon(Symbols.check),
|
||||
),
|
||||
if (isPending)
|
||||
IconButton(
|
||||
padding: EdgeInsets.zero,
|
||||
onPressed:
|
||||
submitting.value
|
||||
? null
|
||||
: () => handleFriendRequest(
|
||||
relationship,
|
||||
false,
|
||||
),
|
||||
icon: const Icon(Symbols.close),
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
},
|
||||
),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
51
lib/screens/account/relationship.g.dart
Normal file
51
lib/screens/account/relationship.g.dart
Normal file
@ -0,0 +1,51 @@
|
||||
// GENERATED CODE - DO NOT MODIFY BY HAND
|
||||
|
||||
part of 'relationship.dart';
|
||||
|
||||
// **************************************************************************
|
||||
// RiverpodGenerator
|
||||
// **************************************************************************
|
||||
|
||||
String _$sendFriendRequestHash() => r'0fc0a3866b64df8b547f831fdb7db47929e2c9ff';
|
||||
|
||||
/// See also [sendFriendRequest].
|
||||
@ProviderFor(sendFriendRequest)
|
||||
final sendFriendRequestProvider =
|
||||
AutoDisposeFutureProvider<List<SnRelationship>>.internal(
|
||||
sendFriendRequest,
|
||||
name: r'sendFriendRequestProvider',
|
||||
debugGetCreateSourceHash:
|
||||
const bool.fromEnvironment('dart.vm.product')
|
||||
? null
|
||||
: _$sendFriendRequestHash,
|
||||
dependencies: null,
|
||||
allTransitiveDependencies: null,
|
||||
);
|
||||
|
||||
@Deprecated('Will be removed in 3.0. Use Ref instead')
|
||||
// ignore: unused_element
|
||||
typedef SendFriendRequestRef =
|
||||
AutoDisposeFutureProviderRef<List<SnRelationship>>;
|
||||
String _$relationshipListNotifierHash() =>
|
||||
r'ad352e8b10641820d5acac27b26ad1bb0b59b67f';
|
||||
|
||||
/// See also [RelationshipListNotifier].
|
||||
@ProviderFor(RelationshipListNotifier)
|
||||
final relationshipListNotifierProvider = AutoDisposeAsyncNotifierProvider<
|
||||
RelationshipListNotifier,
|
||||
CursorPagingData<SnRelationship>
|
||||
>.internal(
|
||||
RelationshipListNotifier.new,
|
||||
name: r'relationshipListNotifierProvider',
|
||||
debugGetCreateSourceHash:
|
||||
const bool.fromEnvironment('dart.vm.product')
|
||||
? null
|
||||
: _$relationshipListNotifierHash,
|
||||
dependencies: null,
|
||||
allTransitiveDependencies: null,
|
||||
);
|
||||
|
||||
typedef _$RelationshipListNotifier =
|
||||
AutoDisposeAsyncNotifier<CursorPagingData<SnRelationship>>;
|
||||
// ignore_for_file: type=lint
|
||||
// ignore_for_file: subtype_of_sealed_class, invalid_use_of_internal_member, invalid_use_of_visible_for_testing_member, deprecated_member_use_from_same_package
|
@ -1,17 +1,23 @@
|
||||
// ignore_for_file: invalid_runtime_check_with_js_interop_types
|
||||
|
||||
import 'dart:ui_web' as ui;
|
||||
import 'package:flutter_hooks/flutter_hooks.dart';
|
||||
import 'package:hooks_riverpod/hooks_riverpod.dart';
|
||||
import 'package:island/pods/config.dart';
|
||||
import 'package:island/widgets/app_scaffold.dart';
|
||||
import 'package:web/web.dart' as web;
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
class CaptchaScreen extends HookConsumerWidget {
|
||||
class CaptchaScreen extends ConsumerStatefulWidget {
|
||||
const CaptchaScreen({super.key});
|
||||
|
||||
void _setupWebListener(BuildContext context, String serverUrl) {
|
||||
@override
|
||||
ConsumerState<CaptchaScreen> createState() => _CaptchaScreenState();
|
||||
}
|
||||
|
||||
class _CaptchaScreenState extends ConsumerState<CaptchaScreen> {
|
||||
bool _isInitialized = false;
|
||||
|
||||
void _setupWebListener(String serverUrl) {
|
||||
web.window.onMessage.listen((event) {
|
||||
if (event.data != null && event.data is String) {
|
||||
final message = event.data as String;
|
||||
@ -24,7 +30,7 @@ class CaptchaScreen extends HookConsumerWidget {
|
||||
|
||||
final iframe =
|
||||
web.HTMLIFrameElement()
|
||||
..src = '$serverUrl/captcha'
|
||||
..src = '$serverUrl/auth/captcha'
|
||||
..style.border = 'none'
|
||||
..width = '100%'
|
||||
..height = '100%';
|
||||
@ -34,18 +40,29 @@ class CaptchaScreen extends HookConsumerWidget {
|
||||
'captcha-iframe',
|
||||
(int viewId) => iframe,
|
||||
);
|
||||
|
||||
setState(() {
|
||||
_isInitialized = true;
|
||||
});
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context, WidgetRef ref) {
|
||||
useCallback(() {
|
||||
void initState() {
|
||||
super.initState();
|
||||
Future.delayed(Duration.zero, () {
|
||||
final serverUrl = ref.watch(serverUrlProvider);
|
||||
_setupWebListener(context, serverUrl);
|
||||
}, []);
|
||||
_setupWebListener(serverUrl);
|
||||
});
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return AppScaffold(
|
||||
appBar: AppBar(title: Text("Anti-Robot")),
|
||||
body: HtmlElementView(viewType: 'captcha-iframe'),
|
||||
body:
|
||||
_isInitialized
|
||||
? HtmlElementView(viewType: 'captcha-iframe')
|
||||
: Center(child: CircularProgressIndicator()),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
@ -47,6 +47,7 @@ class CreateAccountScreen extends HookConsumerWidget {
|
||||
if (!context.mounted) return;
|
||||
|
||||
try {
|
||||
showLoadingModal(context);
|
||||
final client = ref.watch(apiClientProvider);
|
||||
await client.post(
|
||||
'/accounts',
|
||||
@ -59,10 +60,11 @@ class CreateAccountScreen extends HookConsumerWidget {
|
||||
'captcha_token': captchaTk,
|
||||
},
|
||||
);
|
||||
|
||||
if (!context.mounted) return;
|
||||
hideLoadingModal(context);
|
||||
showPostCreateModal();
|
||||
} catch (err) {
|
||||
if (context.mounted) hideLoadingModal(context);
|
||||
showErrorAlert(err);
|
||||
}
|
||||
}
|
||||
|
@ -4,7 +4,6 @@ import 'package:easy_localization/easy_localization.dart';
|
||||
import 'package:flutter/foundation.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_hooks/flutter_hooks.dart';
|
||||
import 'package:flutter_udid/flutter_udid.dart';
|
||||
import 'package:hooks_riverpod/hooks_riverpod.dart';
|
||||
import 'package:gap/gap.dart';
|
||||
import 'package:island/models/auth.dart';
|
||||
@ -13,6 +12,7 @@ import 'package:island/pods/network.dart';
|
||||
import 'package:island/pods/userinfo.dart';
|
||||
import 'package:island/pods/websocket.dart';
|
||||
import 'package:island/services/notify.dart';
|
||||
import 'package:island/services/udid.dart';
|
||||
import 'package:island/widgets/alert.dart';
|
||||
import 'package:island/widgets/app_scaffold.dart';
|
||||
import 'package:material_symbols_icons/symbols.dart';
|
||||
@ -150,8 +150,8 @@ class _LoginCheckScreen extends HookConsumerWidget {
|
||||
subscribePushNotification(apiClient);
|
||||
final wsNotifier = ref.read(websocketStateProvider.notifier);
|
||||
wsNotifier.connect();
|
||||
if (context.mounted) Navigator.pop(context, true);
|
||||
});
|
||||
Navigator.pop(context, true);
|
||||
} catch (err) {
|
||||
showErrorAlert(err);
|
||||
return;
|
||||
@ -379,7 +379,7 @@ class _LoginLookupScreen extends HookConsumerWidget {
|
||||
'/auth/challenge',
|
||||
data: {
|
||||
'account': uname,
|
||||
'device_id': await FlutterUdid.consistentUdid,
|
||||
'device_id': await getUdid(),
|
||||
'platform':
|
||||
kIsWeb
|
||||
? 1
|
||||
|
@ -59,6 +59,7 @@ class ExploreScreen extends ConsumerWidget {
|
||||
}
|
||||
|
||||
final item = data.items[index];
|
||||
if (item.data == null) return const SizedBox.shrink();
|
||||
Widget itemWidget;
|
||||
|
||||
switch (item.type) {
|
||||
|
Reference in New Issue
Block a user