From d46d584ff301102a5a23122b5a0a9cb360f3ca5a Mon Sep 17 00:00:00 2001 From: LittleSheep Date: Mon, 13 May 2024 22:52:41 +0800 Subject: [PATCH] :bug: Bug fixes and optimization --- lib/models/notification.dart | 6 +++--- lib/providers/chat.dart | 8 +++++++- lib/providers/keypair.dart | 6 ++++++ lib/providers/notify.dart | 15 ++++++++++----- lib/widgets/provider_init.dart | 33 ++++++++++++++++++++++++++------- 5 files changed, 52 insertions(+), 16 deletions(-) diff --git a/lib/models/notification.dart b/lib/models/notification.dart index 751442e..6734539 100755 --- a/lib/models/notification.dart +++ b/lib/models/notification.dart @@ -28,9 +28,9 @@ class Notification { }); factory Notification.fromJson(Map json) => Notification( - id: json['id'], - createdAt: DateTime.parse(json['created_at']), - updatedAt: DateTime.parse(json['updated_at']), + id: json['id'] ?? 0, + createdAt: json['created_at'] == null ? DateTime.now() : DateTime.parse(json['created_at']), + updatedAt: json['updated_at'] == null ? DateTime.now() : DateTime.parse(json['updated_at']), deletedAt: json['deleted_at'], subject: json['subject'], content: json['content'], diff --git a/lib/providers/chat.dart b/lib/providers/chat.dart index 5a238f3..2f6625c 100644 --- a/lib/providers/chat.dart +++ b/lib/providers/chat.dart @@ -33,7 +33,11 @@ class ChatProvider extends ChangeNotifier { IOWebSocketChannel? _channel; - Future connect(AuthProvider auth, {noRetry = false}) async { + Future connect( + AuthProvider auth, { + Function(bool status)? onStateUpdated, + noRetry = false, + }) async { if (auth.client == null) await auth.loadClient(); if (!await auth.isAuthorized()) return null; @@ -52,7 +56,9 @@ class ChatProvider extends ChangeNotifier { try { _channel = IOWebSocketChannel.connect(uri); + if (onStateUpdated != null) onStateUpdated(true); await _channel!.ready; + if (onStateUpdated != null) onStateUpdated(false); } catch (e) { if (!noRetry) { await auth.client!.refreshToken(auth.client!.currentRefreshToken!); diff --git a/lib/providers/keypair.dart b/lib/providers/keypair.dart index 8bb4a42..586b18d 100644 --- a/lib/providers/keypair.dart +++ b/lib/providers/keypair.dart @@ -43,6 +43,7 @@ class KeypairProvider extends ChangeNotifier { } void receiveKeypair(Keypair kp) { + print('received ${kp.id}'); keys[kp.id] = kp; requestingKeys.remove(kp.id); notifyListeners(); @@ -94,6 +95,11 @@ class KeypairProvider extends ChangeNotifier { )); requestingKeys.add(id); + Future.delayed(const Duration(seconds: 3), () { + requestingKeys.remove(id); + notifyListeners(); + }); + notifyListeners(); } diff --git a/lib/providers/notify.dart b/lib/providers/notify.dart index 543e11e..e968d66 100644 --- a/lib/providers/notify.dart +++ b/lib/providers/notify.dart @@ -66,9 +66,11 @@ class NotifyProvider extends ChangeNotifier { IOWebSocketChannel? _channel; - Future connect(AuthProvider auth, { + Future connect( + AuthProvider auth, { Keypair? Function(String id)? onKexRequest, Function(Keypair kp)? onKexProvide, + Function(bool status)? onStateUpdated, bool noRetry = false, }) async { if (auth.client == null) await auth.loadClient(); @@ -88,7 +90,9 @@ class NotifyProvider extends ChangeNotifier { try { _channel = IOWebSocketChannel.connect(uri); + if (onStateUpdated != null) onStateUpdated(true); await _channel!.ready; + if (onStateUpdated != null) onStateUpdated(false); } catch (e) { if (!noRetry) { await auth.client!.refreshToken(auth.client!.currentRefreshToken!); @@ -99,15 +103,16 @@ class NotifyProvider extends ChangeNotifier { } _channel!.stream.listen( - (event) { + (event) { final result = NetworkPackage.fromJson(jsonDecode(event)); switch (result.method) { case 'notifications.new': - final result = model.Notification.fromJson(jsonDecode(event)); + if (result.payload == null) break; + final notification = model.Notification.fromJson(result.payload!); unreadAmount++; - notifications.add(result); + notifications.add(notification); notifyListeners(); - notifyMessage(result.subject, result.content); + notifyMessage(notification.subject, notification.content); break; case 'kex.request': if (onKexRequest == null || result.payload == null) break; diff --git a/lib/widgets/provider_init.dart b/lib/widgets/provider_init.dart index 26e1cbc..12ecd6b 100644 --- a/lib/widgets/provider_init.dart +++ b/lib/widgets/provider_init.dart @@ -19,6 +19,21 @@ class ProviderInitializer extends StatefulWidget { } class _ProviderInitializerState extends State { + void showConnectionStatus(bool status) { + if (status) { + showConnectionSnackbar(); + } else { + ScaffoldMessenger.of(context).clearSnackBars(); + } + } + + void showConnectionSnackbar() { + ScaffoldMessenger.of(context).showSnackBar(SnackBar( + content: Text(AppLocalizations.of(context)!.connectingServer), + duration: const Duration(minutes: 1), + )); + } + void connect() async { final auth = context.read(); final nty = context.read(); @@ -28,7 +43,7 @@ class _ProviderInitializerState extends State { final notify = ScaffoldMessenger.of(context).showSnackBar( SnackBar( content: Text(AppLocalizations.of(context)!.connectingServer), - duration: const Duration(minutes: 1), + duration: const Duration(seconds: 3), ), ); @@ -38,17 +53,21 @@ class _ProviderInitializerState extends State { await auth.loadClient(); } - nty.fetch(auth); - chat.connect(auth); - keypair.channel = await nty.connect( + nty.connect( auth, onKexRequest: keypair.provideKeypair, onKexProvide: keypair.receiveKeypair, - ); + onStateUpdated: showConnectionStatus + ).then((value) { + keypair.channel = value; + }); + chat.connect(auth, onStateUpdated: showConnectionStatus); + + nty.fetch(auth); Timer.periodic(const Duration(seconds: 1), (timer) { - nty.connect(auth); - chat.connect(auth); + nty.connect(auth, onStateUpdated: showConnectionStatus); + chat.connect(auth, onStateUpdated: showConnectionStatus); }); } } catch (e) {