🐛 Bug fixes

This commit is contained in:
LittleSheep 2024-05-11 23:22:35 +08:00
parent 5ce6543275
commit 08d0a99b10
2 changed files with 27 additions and 32 deletions

View File

@ -4,6 +4,7 @@ import 'package:flutter/material.dart';
import 'package:flutter_local_notifications/flutter_local_notifications.dart'; import 'package:flutter_local_notifications/flutter_local_notifications.dart';
import 'package:livekit_client/livekit_client.dart'; import 'package:livekit_client/livekit_client.dart';
import 'package:permission_handler/permission_handler.dart'; import 'package:permission_handler/permission_handler.dart';
import 'package:solian/models/packet.dart';
import 'package:solian/models/pagination.dart'; import 'package:solian/models/pagination.dart';
import 'package:solian/providers/auth.dart'; import 'package:solian/providers/auth.dart';
import 'package:solian/utils/services_url.dart'; import 'package:solian/utils/services_url.dart';
@ -17,8 +18,7 @@ class NotifyProvider extends ChangeNotifier {
List<model.Notification> notifications = List.empty(growable: true); List<model.Notification> notifications = List.empty(growable: true);
final FlutterLocalNotificationsPlugin localNotify = final FlutterLocalNotificationsPlugin localNotify = FlutterLocalNotificationsPlugin();
FlutterLocalNotificationsPlugin();
NotifyProvider() { NotifyProvider() {
initNotify(); initNotify();
@ -32,10 +32,8 @@ class NotifyProvider extends ChangeNotifier {
DarwinNotificationCategory('general'), DarwinNotificationCategory('general'),
], ],
); );
const linuxSettings = const linuxSettings = LinuxInitializationSettings(defaultActionName: 'Open notification');
LinuxInitializationSettings(defaultActionName: 'Open notification'); const InitializationSettings initializationSettings = InitializationSettings(
const InitializationSettings initializationSettings =
InitializationSettings(
android: androidSettings, android: androidSettings,
iOS: darwinSettings, iOS: darwinSettings,
macOS: darwinSettings, macOS: darwinSettings,
@ -58,19 +56,16 @@ class NotifyProvider extends ChangeNotifier {
var uri = getRequestUri('passport', '/api/notifications?skip=0&take=25'); var uri = getRequestUri('passport', '/api/notifications?skip=0&take=25');
var res = await auth.client!.get(uri); var res = await auth.client!.get(uri);
if (res.statusCode == 200) { if (res.statusCode == 200) {
final result = final result = PaginationResult.fromJson(jsonDecode(utf8.decode(res.bodyBytes)));
PaginationResult.fromJson(jsonDecode(utf8.decode(res.bodyBytes))); notifications = result.data?.map((x) => model.Notification.fromJson(x)).toList() ?? List.empty(growable: true);
notifications =
result.data?.map((x) => model.Notification.fromJson(x)).toList() ??
List.empty(growable: true);
} }
notifyListeners(); notifyListeners();
} }
Future<WebSocketChannel?> connect(AuthProvider auth) async { Future<void> connect(AuthProvider auth) 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;
await auth.client!.refreshToken(auth.client!.currentRefreshToken!); await auth.client!.refreshToken(auth.client!.currentRefreshToken!);
@ -80,15 +75,25 @@ class NotifyProvider extends ChangeNotifier {
host: ori.host, host: ori.host,
port: ori.port, port: ori.port,
path: ori.path, path: ori.path,
queryParameters: { queryParameters: {'tk': Uri.encodeComponent(auth.client!.currentToken!)},
'tk': Uri.encodeComponent(auth.client!.currentToken!)
},
); );
final channel = WebSocketChannel.connect(uri); final channel = WebSocketChannel.connect(uri);
await channel.ready; await channel.ready;
return channel; channel.stream.listen(
(event) {
final result = NetworkPackage.fromJson(jsonDecode(event));
switch (result.method) {
case 'notifications.new':
final result = model.Notification.fromJson(jsonDecode(event));
onRemoteMessage(result);
notifyMessage(result.subject, result.content);
}
},
onError: (_, __) => connect(auth),
onDone: () => connect(auth),
);
} }
void onRemoteMessage(model.Notification item) { void onRemoteMessage(model.Notification item) {

View File

@ -32,22 +32,12 @@ class _NotificationNotifierState extends State<NotificationNotifier> {
final nty = context.read<NotifyProvider>(); final nty = context.read<NotifyProvider>();
if (await auth.isAuthorized()) { if (await auth.isAuthorized()) {
if (auth.client == null) {
await auth.loadClient();
}
nty.fetch(auth); nty.fetch(auth);
nty.connect(auth).then((snapshot) { nty.connect(auth);
snapshot!.stream.listen(
(event) {
final result = NetworkPackage.fromJson(jsonDecode(event));
switch (result.method) {
case 'notifications.new':
final result = model.Notification.fromJson(jsonDecode(event));
nty.onRemoteMessage(result);
nty.notifyMessage(result.subject, result.content);
}
},
onError: (_, __) => connect(),
onDone: () => connect(),
);
});
} }
notify.close(); notify.close();