🐛 Bug fixes and optimization
This commit is contained in:
parent
f43f9e91f6
commit
d46d584ff3
@ -28,9 +28,9 @@ class Notification {
|
||||
});
|
||||
|
||||
factory Notification.fromJson(Map<String, dynamic> 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'],
|
||||
|
@ -33,7 +33,11 @@ class ChatProvider extends ChangeNotifier {
|
||||
|
||||
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 (!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!);
|
||||
|
@ -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();
|
||||
}
|
||||
|
||||
|
@ -66,9 +66,11 @@ class NotifyProvider extends ChangeNotifier {
|
||||
|
||||
IOWebSocketChannel? _channel;
|
||||
|
||||
Future<IOWebSocketChannel?> connect(AuthProvider auth, {
|
||||
Future<IOWebSocketChannel?> 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;
|
||||
|
@ -19,6 +19,21 @@ class ProviderInitializer extends StatefulWidget {
|
||||
}
|
||||
|
||||
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 {
|
||||
final auth = context.read<AuthProvider>();
|
||||
final nty = context.read<NotifyProvider>();
|
||||
@ -28,7 +43,7 @@ class _ProviderInitializerState extends State<ProviderInitializer> {
|
||||
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<ProviderInitializer> {
|
||||
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) {
|
||||
|
Loading…
Reference in New Issue
Block a user