🐛 Bug fixes and optimization

This commit is contained in:
LittleSheep 2024-05-13 22:52:41 +08:00
parent f43f9e91f6
commit d46d584ff3
5 changed files with 52 additions and 16 deletions

View File

@ -28,9 +28,9 @@ class Notification {
}); });
factory Notification.fromJson(Map<String, dynamic> json) => Notification( factory Notification.fromJson(Map<String, dynamic> json) => Notification(
id: json['id'], id: json['id'] ?? 0,
createdAt: DateTime.parse(json['created_at']), createdAt: json['created_at'] == null ? DateTime.now() : DateTime.parse(json['created_at']),
updatedAt: DateTime.parse(json['updated_at']), updatedAt: json['updated_at'] == null ? DateTime.now() : DateTime.parse(json['updated_at']),
deletedAt: json['deleted_at'], deletedAt: json['deleted_at'],
subject: json['subject'], subject: json['subject'],
content: json['content'], content: json['content'],

View File

@ -33,7 +33,11 @@ class ChatProvider extends ChangeNotifier {
IOWebSocketChannel? _channel; IOWebSocketChannel? _channel;
Future<IOWebSocketChannel?> connect(AuthProvider auth, {noRetry = false}) async { Future<IOWebSocketChannel?> connect(
AuthProvider auth, {
Function(bool status)? onStateUpdated,
noRetry = false,
}) async {
if (auth.client == null) await auth.loadClient(); if (auth.client == null) await auth.loadClient();
if (!await auth.isAuthorized()) return null; if (!await auth.isAuthorized()) return null;
@ -52,7 +56,9 @@ class ChatProvider extends ChangeNotifier {
try { try {
_channel = IOWebSocketChannel.connect(uri); _channel = IOWebSocketChannel.connect(uri);
if (onStateUpdated != null) onStateUpdated(true);
await _channel!.ready; await _channel!.ready;
if (onStateUpdated != null) onStateUpdated(false);
} catch (e) { } catch (e) {
if (!noRetry) { if (!noRetry) {
await auth.client!.refreshToken(auth.client!.currentRefreshToken!); await auth.client!.refreshToken(auth.client!.currentRefreshToken!);

View File

@ -43,6 +43,7 @@ class KeypairProvider extends ChangeNotifier {
} }
void receiveKeypair(Keypair kp) { void receiveKeypair(Keypair kp) {
print('received ${kp.id}');
keys[kp.id] = kp; keys[kp.id] = kp;
requestingKeys.remove(kp.id); requestingKeys.remove(kp.id);
notifyListeners(); notifyListeners();
@ -94,6 +95,11 @@ class KeypairProvider extends ChangeNotifier {
)); ));
requestingKeys.add(id); requestingKeys.add(id);
Future.delayed(const Duration(seconds: 3), () {
requestingKeys.remove(id);
notifyListeners();
});
notifyListeners(); notifyListeners();
} }

View File

@ -66,9 +66,11 @@ class NotifyProvider extends ChangeNotifier {
IOWebSocketChannel? _channel; IOWebSocketChannel? _channel;
Future<IOWebSocketChannel?> connect(AuthProvider auth, { Future<IOWebSocketChannel?> connect(
AuthProvider auth, {
Keypair? Function(String id)? onKexRequest, Keypair? Function(String id)? onKexRequest,
Function(Keypair kp)? onKexProvide, Function(Keypair kp)? onKexProvide,
Function(bool status)? onStateUpdated,
bool noRetry = false, bool noRetry = false,
}) async { }) async {
if (auth.client == null) await auth.loadClient(); if (auth.client == null) await auth.loadClient();
@ -88,7 +90,9 @@ class NotifyProvider extends ChangeNotifier {
try { try {
_channel = IOWebSocketChannel.connect(uri); _channel = IOWebSocketChannel.connect(uri);
if (onStateUpdated != null) onStateUpdated(true);
await _channel!.ready; await _channel!.ready;
if (onStateUpdated != null) onStateUpdated(false);
} catch (e) { } catch (e) {
if (!noRetry) { if (!noRetry) {
await auth.client!.refreshToken(auth.client!.currentRefreshToken!); await auth.client!.refreshToken(auth.client!.currentRefreshToken!);
@ -103,11 +107,12 @@ class NotifyProvider extends ChangeNotifier {
final result = NetworkPackage.fromJson(jsonDecode(event)); final result = NetworkPackage.fromJson(jsonDecode(event));
switch (result.method) { switch (result.method) {
case 'notifications.new': case 'notifications.new':
final result = model.Notification.fromJson(jsonDecode(event)); if (result.payload == null) break;
final notification = model.Notification.fromJson(result.payload!);
unreadAmount++; unreadAmount++;
notifications.add(result); notifications.add(notification);
notifyListeners(); notifyListeners();
notifyMessage(result.subject, result.content); notifyMessage(notification.subject, notification.content);
break; break;
case 'kex.request': case 'kex.request':
if (onKexRequest == null || result.payload == null) break; if (onKexRequest == null || result.payload == null) break;

View File

@ -19,6 +19,21 @@ class ProviderInitializer extends StatefulWidget {
} }
class _ProviderInitializerState extends State<ProviderInitializer> { class _ProviderInitializerState extends State<ProviderInitializer> {
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 { void connect() async {
final auth = context.read<AuthProvider>(); final auth = context.read<AuthProvider>();
final nty = context.read<NotifyProvider>(); final nty = context.read<NotifyProvider>();
@ -28,7 +43,7 @@ class _ProviderInitializerState extends State<ProviderInitializer> {
final notify = ScaffoldMessenger.of(context).showSnackBar( final notify = ScaffoldMessenger.of(context).showSnackBar(
SnackBar( SnackBar(
content: Text(AppLocalizations.of(context)!.connectingServer), content: Text(AppLocalizations.of(context)!.connectingServer),
duration: const Duration(minutes: 1), duration: const Duration(seconds: 3),
), ),
); );
@ -38,17 +53,21 @@ class _ProviderInitializerState extends State<ProviderInitializer> {
await auth.loadClient(); await auth.loadClient();
} }
nty.fetch(auth); nty.connect(
chat.connect(auth);
keypair.channel = await nty.connect(
auth, auth,
onKexRequest: keypair.provideKeypair, onKexRequest: keypair.provideKeypair,
onKexProvide: keypair.receiveKeypair, 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) { Timer.periodic(const Duration(seconds: 1), (timer) {
nty.connect(auth); nty.connect(auth, onStateUpdated: showConnectionStatus);
chat.connect(auth); chat.connect(auth, onStateUpdated: showConnectionStatus);
}); });
} }
} catch (e) { } catch (e) {