2024-04-24 15:19:26 +00:00
|
|
|
import 'dart:convert';
|
|
|
|
|
|
|
|
import 'package:flutter/material.dart';
|
2024-04-28 13:49:03 +00:00
|
|
|
import 'package:flutter_local_notifications/flutter_local_notifications.dart';
|
2024-04-29 12:22:06 +00:00
|
|
|
import 'package:livekit_client/livekit_client.dart';
|
2024-04-28 13:49:03 +00:00
|
|
|
import 'package:permission_handler/permission_handler.dart';
|
2024-04-24 15:19:26 +00:00
|
|
|
import 'package:solian/models/pagination.dart';
|
|
|
|
import 'package:solian/providers/auth.dart';
|
|
|
|
import 'package:solian/utils/service_url.dart';
|
|
|
|
import 'package:solian/models/notification.dart' as model;
|
|
|
|
import 'package:web_socket_channel/web_socket_channel.dart';
|
2024-04-28 13:49:03 +00:00
|
|
|
import 'dart:math' as math;
|
2024-04-24 15:19:26 +00:00
|
|
|
|
|
|
|
class NotifyProvider extends ChangeNotifier {
|
|
|
|
bool isOpened = false;
|
2024-04-28 13:49:03 +00:00
|
|
|
int unreadAmount = 0;
|
2024-04-24 15:19:26 +00:00
|
|
|
|
|
|
|
List<model.Notification> notifications = List.empty(growable: true);
|
|
|
|
|
2024-05-01 09:37:34 +00:00
|
|
|
final FlutterLocalNotificationsPlugin localNotify =
|
|
|
|
FlutterLocalNotificationsPlugin();
|
2024-04-28 13:49:03 +00:00
|
|
|
|
|
|
|
NotifyProvider() {
|
|
|
|
initNotify();
|
|
|
|
requestPermissions();
|
|
|
|
}
|
|
|
|
|
|
|
|
void initNotify() {
|
2024-04-29 12:22:06 +00:00
|
|
|
const androidSettings = AndroidInitializationSettings('app_icon');
|
|
|
|
const darwinSettings = DarwinInitializationSettings(
|
|
|
|
notificationCategories: [
|
2024-05-01 16:49:38 +00:00
|
|
|
DarwinNotificationCategory('general'),
|
2024-04-29 12:22:06 +00:00
|
|
|
],
|
|
|
|
);
|
2024-05-01 09:37:34 +00:00
|
|
|
const linuxSettings =
|
|
|
|
LinuxInitializationSettings(defaultActionName: 'Open notification');
|
|
|
|
const InitializationSettings initializationSettings =
|
|
|
|
InitializationSettings(
|
2024-04-29 12:22:06 +00:00
|
|
|
android: androidSettings,
|
|
|
|
iOS: darwinSettings,
|
|
|
|
macOS: darwinSettings,
|
|
|
|
linux: linuxSettings,
|
2024-04-28 13:49:03 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
localNotify.initialize(initializationSettings);
|
|
|
|
}
|
|
|
|
|
|
|
|
Future<void> requestPermissions() async {
|
2024-05-01 16:49:38 +00:00
|
|
|
if (lkPlatformIs(PlatformType.macOS) || lkPlatformIs(PlatformType.linux)) {
|
2024-05-01 09:37:34 +00:00
|
|
|
return;
|
2024-05-01 16:49:38 +00:00
|
|
|
}
|
2024-04-28 13:49:03 +00:00
|
|
|
await Permission.notification.request();
|
|
|
|
}
|
|
|
|
|
2024-04-24 15:19:26 +00:00
|
|
|
Future<void> fetch(AuthProvider auth) async {
|
|
|
|
if (!await auth.isAuthorized()) return;
|
|
|
|
|
|
|
|
var uri = getRequestUri('passport', '/api/notifications?skip=0&take=25');
|
|
|
|
var res = await auth.client!.get(uri);
|
|
|
|
if (res.statusCode == 200) {
|
2024-05-01 09:37:34 +00:00
|
|
|
final result =
|
|
|
|
PaginationResult.fromJson(jsonDecode(utf8.decode(res.bodyBytes)));
|
|
|
|
notifications =
|
|
|
|
result.data?.map((x) => model.Notification.fromJson(x)).toList() ??
|
|
|
|
List.empty(growable: true);
|
2024-04-24 15:19:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
notifyListeners();
|
|
|
|
}
|
|
|
|
|
|
|
|
Future<WebSocketChannel?> connect(AuthProvider auth) async {
|
2024-05-01 03:27:48 +00:00
|
|
|
if (auth.client == null) await auth.loadClient();
|
2024-04-24 15:19:26 +00:00
|
|
|
if (!await auth.isAuthorized()) return null;
|
|
|
|
|
2024-05-02 04:16:01 +00:00
|
|
|
await auth.client!.refreshToken(auth.client!.currentRefreshToken!);
|
2024-04-24 15:19:26 +00:00
|
|
|
|
|
|
|
var ori = getRequestUri('passport', '/api/notifications/listen');
|
|
|
|
var uri = Uri(
|
|
|
|
scheme: ori.scheme.replaceFirst('http', 'ws'),
|
|
|
|
host: ori.host,
|
|
|
|
path: ori.path,
|
2024-05-01 09:37:34 +00:00
|
|
|
queryParameters: {
|
2024-05-02 04:16:01 +00:00
|
|
|
'tk': Uri.encodeComponent(auth.client!.currentToken!)
|
2024-05-01 09:37:34 +00:00
|
|
|
},
|
2024-04-24 15:19:26 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
final channel = WebSocketChannel.connect(uri);
|
|
|
|
await channel.ready;
|
|
|
|
|
|
|
|
return channel;
|
|
|
|
}
|
|
|
|
|
|
|
|
void onRemoteMessage(model.Notification item) {
|
2024-04-28 13:49:03 +00:00
|
|
|
unreadAmount++;
|
2024-04-24 15:19:26 +00:00
|
|
|
notifications.add(item);
|
|
|
|
notifyListeners();
|
|
|
|
}
|
|
|
|
|
2024-04-28 13:49:03 +00:00
|
|
|
void notifyMessage(String title, String body) {
|
2024-04-29 12:22:06 +00:00
|
|
|
const androidSettings = AndroidNotificationDetails(
|
|
|
|
'general',
|
|
|
|
'General',
|
|
|
|
importance: Importance.high,
|
|
|
|
priority: Priority.high,
|
|
|
|
silent: true,
|
|
|
|
);
|
|
|
|
const darwinSettings = DarwinNotificationDetails(
|
|
|
|
presentAlert: true,
|
|
|
|
presentBanner: true,
|
|
|
|
presentBadge: true,
|
|
|
|
presentSound: false,
|
|
|
|
);
|
|
|
|
const linuxSettings = LinuxNotificationDetails();
|
|
|
|
|
2024-04-28 13:49:03 +00:00
|
|
|
localNotify.show(
|
|
|
|
math.max(1, math.Random().nextInt(100000000)),
|
|
|
|
title,
|
|
|
|
body,
|
|
|
|
const NotificationDetails(
|
2024-04-29 12:22:06 +00:00
|
|
|
android: androidSettings,
|
|
|
|
iOS: darwinSettings,
|
|
|
|
macOS: darwinSettings,
|
|
|
|
linux: linuxSettings,
|
2024-04-28 13:49:03 +00:00
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2024-04-25 15:03:16 +00:00
|
|
|
void clearAt(int index) {
|
|
|
|
notifications.removeAt(index);
|
|
|
|
notifyListeners();
|
|
|
|
}
|
|
|
|
|
2024-04-28 15:05:59 +00:00
|
|
|
void clearRealtime() {
|
2024-04-24 15:19:26 +00:00
|
|
|
notifications = notifications.where((x) => !x.isRealtime).toList();
|
2024-04-28 15:05:59 +00:00
|
|
|
notifyListeners();
|
2024-04-24 15:19:26 +00:00
|
|
|
}
|
2024-04-28 13:49:03 +00:00
|
|
|
|
|
|
|
void allRead() {
|
|
|
|
unreadAmount = 0;
|
|
|
|
notifyListeners();
|
|
|
|
}
|
2024-04-24 15:19:26 +00:00
|
|
|
}
|