refactor notif code
This commit is contained in:
parent
23d77ddc60
commit
c3b39a6294
2 changed files with 121 additions and 128 deletions
|
|
@ -2,15 +2,14 @@ import "dart:async";
|
||||||
import "dart:convert";
|
import "dart:convert";
|
||||||
import "dart:io";
|
import "dart:io";
|
||||||
|
|
||||||
import "package:collection/collection.dart";
|
|
||||||
import "package:fast_immutable_collections/fast_immutable_collections.dart";
|
import "package:fast_immutable_collections/fast_immutable_collections.dart";
|
||||||
import "package:flutter/services.dart";
|
import "package:flutter/services.dart";
|
||||||
import "package:material_ui/material_ui.dart";
|
import "package:material_ui/material_ui.dart";
|
||||||
import "package:nexus/controllers/active_account.dart";
|
import "package:nexus/controllers/active_account.dart";
|
||||||
import "package:nexus/controllers/portal.dart";
|
import "package:nexus/controllers/portal.dart";
|
||||||
import "package:nexus/controllers/rooms.dart";
|
import "package:nexus/controllers/rooms.dart";
|
||||||
import "package:nexus/controllers/shared_prefs.dart";
|
|
||||||
import "package:nexus/controllers/sidebar_page.dart";
|
import "package:nexus/controllers/sidebar_page.dart";
|
||||||
|
import "package:nexus/helpers/shown_notifications_helper.dart";
|
||||||
import "package:nexus/main.dart";
|
import "package:nexus/main.dart";
|
||||||
import "package:flutter_local_notifications/flutter_local_notifications.dart";
|
import "package:flutter_local_notifications/flutter_local_notifications.dart";
|
||||||
import "package:flutter_riverpod/flutter_riverpod.dart";
|
import "package:flutter_riverpod/flutter_riverpod.dart";
|
||||||
|
|
@ -30,7 +29,17 @@ class NotificationController
|
||||||
Future<FlutterLocalNotificationsPlugin> build() async {
|
Future<FlutterLocalNotificationsPlugin> build() async {
|
||||||
final notifications = FlutterLocalNotificationsPlugin();
|
final notifications = FlutterLocalNotificationsPlugin();
|
||||||
|
|
||||||
if (!Platform.isLinux) {
|
if (Platform.isLinux) {
|
||||||
|
const MethodChannel("nexus/notifications")
|
||||||
|
.setMethodCallHandler((call) async {
|
||||||
|
if (call case MethodCall(
|
||||||
|
method: "notificationClicked",
|
||||||
|
arguments: final String payload,
|
||||||
|
)) {
|
||||||
|
_open(payload);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
} else {
|
||||||
final darwin = DarwinInitializationSettings();
|
final darwin = DarwinInitializationSettings();
|
||||||
|
|
||||||
await notifications.initialize(
|
await notifications.initialize(
|
||||||
|
|
@ -56,18 +65,6 @@ class NotificationController
|
||||||
?.deleteNotificationChannel(channelId: "messages");
|
?.deleteNotificationChannel(channelId: "messages");
|
||||||
}
|
}
|
||||||
|
|
||||||
if (Platform.isLinux) {
|
|
||||||
const notificationChannel = MethodChannel("nexus/notifications");
|
|
||||||
|
|
||||||
notificationChannel.setMethodCallHandler((call) async {
|
|
||||||
if (call.method != "notificationClicked") {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (call.arguments case final String payload) _open(payload);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
// Reading a room, here or on another client, dismisses its notifications
|
// Reading a room, here or on another client, dismisses its notifications
|
||||||
if (!isInBackground) {
|
if (!isInBackground) {
|
||||||
ref.listen(
|
ref.listen(
|
||||||
|
|
@ -82,8 +79,9 @@ class NotificationController
|
||||||
if (newlyRead.isEmpty) return;
|
if (newlyRead.isEmpty) return;
|
||||||
|
|
||||||
final accountId = ref.read(ActiveAccountController.provider);
|
final accountId = ref.read(ActiveAccountController.provider);
|
||||||
dismiss(newlyRead.map((roomId) => threadIdFor(accountId, roomId)))
|
dismiss(
|
||||||
.onError(showError);
|
newlyRead.map((roomId) => threadIdFor(accountId, roomId)).toISet(),
|
||||||
|
).onError(showError);
|
||||||
},
|
},
|
||||||
fireImmediately: true,
|
fireImmediately: true,
|
||||||
);
|
);
|
||||||
|
|
@ -101,42 +99,14 @@ class NotificationController
|
||||||
static const _summaryPayload = "summary";
|
static const _summaryPayload = "summary";
|
||||||
|
|
||||||
static const _messagesChannel = "messages_v2";
|
static const _messagesChannel = "messages_v2";
|
||||||
static const _shownKey = "shownNotifications";
|
|
||||||
|
|
||||||
/// IDs of the notifications shown, by thread. Stored, since they're often
|
|
||||||
/// shown by a background instance.
|
|
||||||
Future<IMap<String, IList<int>>> _shown() async => IMap<String, dynamic>(
|
|
||||||
json.decode(
|
|
||||||
await ref.read(SharedPrefsController.provider).getString(_shownKey) ??
|
|
||||||
"{}",
|
|
||||||
),
|
|
||||||
).map((threadId, ids) => .new(threadId, IList((ids as List).cast<int>())));
|
|
||||||
|
|
||||||
Future<void> _setShown(IMap<String, IList<int>> shown) => ref
|
|
||||||
.read(SharedPrefsController.provider)
|
|
||||||
.setString(
|
|
||||||
_shownKey,
|
|
||||||
json.encode(
|
|
||||||
shown.map((threadId, ids) => .new(threadId, ids.unlock)).unlock,
|
|
||||||
),
|
|
||||||
);
|
|
||||||
|
|
||||||
Future<void> _track(String threadId, int id) async {
|
|
||||||
final shown = await _shown();
|
|
||||||
final ids = shown[threadId] ?? const IListConst([]);
|
|
||||||
if (!ids.contains(id)) await _setShown(shown.add(threadId, ids.add(id)));
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Dismisses the notifications shown for [threadIds].
|
/// Dismisses the notifications shown for [threadIds].
|
||||||
Future<void> dismiss(Iterable<String> threadIds) async {
|
Future<void> dismiss(ISet<String> threadIds) async {
|
||||||
final shown = await _shown();
|
final ids = await ref
|
||||||
final ids = threadIds.map((threadId) => shown[threadId]).nonNulls.flattened;
|
.read(ShownNotificationsHelper.provider)
|
||||||
|
.remove(threadIds);
|
||||||
if (ids.isEmpty) return;
|
if (ids.isEmpty) return;
|
||||||
|
|
||||||
await _setShown(
|
|
||||||
shown.removeWhere((threadId, _) => threadIds.contains(threadId)),
|
|
||||||
);
|
|
||||||
|
|
||||||
if (Platform.isLinux) {
|
if (Platform.isLinux) {
|
||||||
final portal = await ref.read(PortalController.provider.future);
|
final portal = await ref.read(PortalController.provider.future);
|
||||||
await Future.wait(
|
await Future.wait(
|
||||||
|
|
@ -158,12 +128,10 @@ class NotificationController
|
||||||
}
|
}
|
||||||
|
|
||||||
void _open(String payload) {
|
void _open(String payload) {
|
||||||
if (payload == _summaryPayload) {
|
|
||||||
navigatorKey.currentState?.popUntil((route) => route.isFirst);
|
navigatorKey.currentState?.popUntil((route) => route.isFirst);
|
||||||
ref.read(SidebarPageController.provider.notifier).set(.notifications);
|
ref.read(SidebarPageController.provider.notifier).set(.notifications);
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
// The summary's payload has no row, so it only opens the notifications
|
||||||
final (accountId, rowId) = switch (payload.split(":")) {
|
final (accountId, rowId) = switch (payload.split(":")) {
|
||||||
[final accountId, final rowId] => (accountId, int.tryParse(rowId)),
|
[final accountId, final rowId] => (accountId, int.tryParse(rowId)),
|
||||||
// From before notifications were per account
|
// From before notifications were per account
|
||||||
|
|
@ -172,9 +140,6 @@ class NotificationController
|
||||||
};
|
};
|
||||||
if (rowId == null) return;
|
if (rowId == null) return;
|
||||||
|
|
||||||
navigatorKey.currentState?.popUntil((route) => route.isFirst);
|
|
||||||
ref.read(SidebarPageController.provider.notifier).set(.notifications);
|
|
||||||
|
|
||||||
ref
|
ref
|
||||||
.read(ActiveAccountController.provider.notifier)
|
.read(ActiveAccountController.provider.notifier)
|
||||||
.switchTo(
|
.switchTo(
|
||||||
|
|
@ -184,29 +149,21 @@ class NotificationController
|
||||||
.onError(showError);
|
.onError(showError);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Each resolves to null except on its own platform
|
||||||
Future<bool> requestPermissions() async {
|
Future<bool> requestPermissions() async {
|
||||||
if (Platform.isLinux) {
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
final controller = await future;
|
final controller = await future;
|
||||||
|
|
||||||
if (Platform.isIOS) {
|
|
||||||
return await controller
|
return await controller
|
||||||
.resolvePlatformSpecificImplementation<
|
.resolvePlatformSpecificImplementation<
|
||||||
IOSFlutterLocalNotificationsPlugin
|
IOSFlutterLocalNotificationsPlugin
|
||||||
>()
|
>()
|
||||||
?.requestPermissions(alert: true, badge: true, sound: true) ??
|
?.requestPermissions(alert: true, badge: true, sound: true) ??
|
||||||
true;
|
await controller
|
||||||
} else if (Platform.isMacOS) {
|
|
||||||
return await controller
|
|
||||||
.resolvePlatformSpecificImplementation<
|
.resolvePlatformSpecificImplementation<
|
||||||
MacOSFlutterLocalNotificationsPlugin
|
MacOSFlutterLocalNotificationsPlugin
|
||||||
>()
|
>()
|
||||||
?.requestPermissions(alert: true, badge: true, sound: true) ??
|
?.requestPermissions(alert: true, badge: true, sound: true) ??
|
||||||
true;
|
await controller
|
||||||
} else if (Platform.isAndroid) {
|
|
||||||
return await controller
|
|
||||||
.resolvePlatformSpecificImplementation<
|
.resolvePlatformSpecificImplementation<
|
||||||
AndroidFlutterLocalNotificationsPlugin
|
AndroidFlutterLocalNotificationsPlugin
|
||||||
>()
|
>()
|
||||||
|
|
@ -214,35 +171,23 @@ class NotificationController
|
||||||
true;
|
true;
|
||||||
}
|
}
|
||||||
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Whether notifications can currently be shown, without prompting
|
/// Whether notifications can currently be shown, without prompting
|
||||||
Future<bool> permissionsGranted() async {
|
Future<bool> permissionsGranted() async {
|
||||||
if (Platform.isLinux) {
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
final controller = await future;
|
final controller = await future;
|
||||||
|
|
||||||
if (Platform.isIOS) {
|
|
||||||
return (await controller
|
return (await controller
|
||||||
.resolvePlatformSpecificImplementation<
|
.resolvePlatformSpecificImplementation<
|
||||||
IOSFlutterLocalNotificationsPlugin
|
IOSFlutterLocalNotificationsPlugin
|
||||||
>()
|
>()
|
||||||
?.checkPermissions())
|
?.checkPermissions())
|
||||||
?.isEnabled ??
|
?.isEnabled ??
|
||||||
true;
|
(await controller
|
||||||
} else if (Platform.isMacOS) {
|
|
||||||
return (await controller
|
|
||||||
.resolvePlatformSpecificImplementation<
|
.resolvePlatformSpecificImplementation<
|
||||||
MacOSFlutterLocalNotificationsPlugin
|
MacOSFlutterLocalNotificationsPlugin
|
||||||
>()
|
>()
|
||||||
?.checkPermissions())
|
?.checkPermissions())
|
||||||
?.isEnabled ??
|
?.isEnabled ??
|
||||||
true;
|
await controller
|
||||||
} else if (Platform.isAndroid) {
|
|
||||||
return await controller
|
|
||||||
.resolvePlatformSpecificImplementation<
|
.resolvePlatformSpecificImplementation<
|
||||||
AndroidFlutterLocalNotificationsPlugin
|
AndroidFlutterLocalNotificationsPlugin
|
||||||
>()
|
>()
|
||||||
|
|
@ -250,9 +195,6 @@ class NotificationController
|
||||||
true;
|
true;
|
||||||
}
|
}
|
||||||
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Passing [message] shows it as a chat message in the conversation for
|
/// Passing [message] shows it as a chat message in the conversation for
|
||||||
/// [threadId], naming the sender in group chats. [subtitle] is shown on
|
/// [threadId], naming the sender in group chats. [subtitle] is shown on
|
||||||
/// platforms that support it.
|
/// platforms that support it.
|
||||||
|
|
@ -273,6 +215,7 @@ class NotificationController
|
||||||
// In DMs the title is already the sender
|
// In DMs the title is already the sender
|
||||||
final sender = message == null || message.isDm ? null : message.sender;
|
final sender = message == null || message.isDm ? null : message.sender;
|
||||||
final prefixedBody = sender == null ? body : "${sender.name}: $body";
|
final prefixedBody = sender == null ? body : "${sender.name}: $body";
|
||||||
|
final shown = ref.read(ShownNotificationsHelper.provider);
|
||||||
|
|
||||||
if (Platform.isLinux) {
|
if (Platform.isLinux) {
|
||||||
final portal = await ref.read(PortalController.provider.future);
|
final portal = await ref.read(PortalController.provider.future);
|
||||||
|
|
@ -299,7 +242,7 @@ class NotificationController
|
||||||
// ),
|
// ),
|
||||||
],
|
],
|
||||||
);
|
);
|
||||||
if (threadId != null) await _track(threadId, id);
|
if (threadId != null) await shown.add(threadId, id);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -315,6 +258,7 @@ class NotificationController
|
||||||
.onError((_, _) => null)
|
.onError((_, _) => null)
|
||||||
: null;
|
: null;
|
||||||
|
|
||||||
|
final largeIcon = icon == null ? null : FilePathAndroidBitmap(icon.path);
|
||||||
final darwin = DarwinNotificationDetails(
|
final darwin = DarwinNotificationDetails(
|
||||||
subtitle: subtitle,
|
subtitle: subtitle,
|
||||||
presentSound: sound,
|
presentSound: sound,
|
||||||
|
|
@ -335,9 +279,7 @@ class NotificationController
|
||||||
subText: subtitle,
|
subText: subtitle,
|
||||||
silent: !sound,
|
silent: !sound,
|
||||||
when: timestamp?.millisecondsSinceEpoch,
|
when: timestamp?.millisecondsSinceEpoch,
|
||||||
largeIcon: icon == null
|
largeIcon: largeIcon,
|
||||||
? null
|
|
||||||
: FilePathAndroidBitmap(icon.path),
|
|
||||||
styleInformation: BigTextStyleInformation(body),
|
styleInformation: BigTextStyleInformation(body),
|
||||||
)
|
)
|
||||||
: .new(
|
: .new(
|
||||||
|
|
@ -350,9 +292,7 @@ class NotificationController
|
||||||
silent: !sound,
|
silent: !sound,
|
||||||
when: timestamp?.millisecondsSinceEpoch,
|
when: timestamp?.millisecondsSinceEpoch,
|
||||||
category: .message,
|
category: .message,
|
||||||
largeIcon: icon == null
|
largeIcon: largeIcon,
|
||||||
? null
|
|
||||||
: FilePathAndroidBitmap(icon.path),
|
|
||||||
styleInformation: MessagingStyleInformation(
|
styleInformation: MessagingStyleInformation(
|
||||||
.new(key: message.self.id, name: message.self.name),
|
.new(key: message.self.id, name: message.self.name),
|
||||||
conversationTitle: message.isDm ? null : title,
|
conversationTitle: message.isDm ? null : title,
|
||||||
|
|
@ -365,11 +305,12 @@ class NotificationController
|
||||||
.new(
|
.new(
|
||||||
key: message.sender.id,
|
key: message.sender.id,
|
||||||
name: message.sender.name,
|
name: message.sender.name,
|
||||||
icon: message.senderAvatar == null
|
icon: switch (message.senderAvatar) {
|
||||||
? null
|
final avatar? => BitmapFilePathAndroidIcon(
|
||||||
: BitmapFilePathAndroidIcon(
|
avatar.path,
|
||||||
message.senderAvatar!.path,
|
|
||||||
),
|
),
|
||||||
|
null => null,
|
||||||
|
},
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
],
|
],
|
||||||
|
|
@ -387,7 +328,7 @@ class NotificationController
|
||||||
),
|
),
|
||||||
);
|
);
|
||||||
|
|
||||||
if (threadId != null) await _track(threadId, id);
|
if (threadId != null) await shown.add(threadId, id);
|
||||||
|
|
||||||
if (Platform.isAndroid) {
|
if (Platform.isAndroid) {
|
||||||
await controller.show(
|
await controller.show(
|
||||||
|
|
|
||||||
52
lib/helpers/shown_notifications_helper.dart
Normal file
52
lib/helpers/shown_notifications_helper.dart
Normal file
|
|
@ -0,0 +1,52 @@
|
||||||
|
import "dart:convert";
|
||||||
|
|
||||||
|
import "package:collection/collection.dart";
|
||||||
|
import "package:fast_immutable_collections/fast_immutable_collections.dart";
|
||||||
|
import "package:flutter_riverpod/flutter_riverpod.dart";
|
||||||
|
import "package:nexus/controllers/shared_prefs.dart";
|
||||||
|
|
||||||
|
class ShownNotificationsHelper(final Ref ref) {
|
||||||
|
static const _key = "shownNotifications";
|
||||||
|
|
||||||
|
/// Applies [update], returning the IDs from before it.
|
||||||
|
Future<IMap<String, ISet<int>>> _update(
|
||||||
|
IMap<String, ISet<int>> Function(IMap<String, ISet<int>> shown) update,
|
||||||
|
) async {
|
||||||
|
final prefs = ref.read(SharedPrefsController.provider);
|
||||||
|
final shown = IMap<String, ISet<int>>.fromJson(
|
||||||
|
json.decode(await prefs.getString(_key) ?? "{}"),
|
||||||
|
(threadId) => threadId as String,
|
||||||
|
(ids) => ISet.fromJson(ids, (id) => id as int),
|
||||||
|
);
|
||||||
|
|
||||||
|
await prefs.setString(
|
||||||
|
_key,
|
||||||
|
json.encode(
|
||||||
|
update(shown)
|
||||||
|
.toJson((threadId) => threadId, (ids) => ids.toJson((id) => id)),
|
||||||
|
),
|
||||||
|
);
|
||||||
|
return shown;
|
||||||
|
}
|
||||||
|
|
||||||
|
Future<void> add(String threadId, int id) => _update(
|
||||||
|
(shown) => shown.update(
|
||||||
|
threadId,
|
||||||
|
(ids) => ids.add(id),
|
||||||
|
ifAbsent: () => ISet({id}),
|
||||||
|
),
|
||||||
|
);
|
||||||
|
|
||||||
|
/// Forgets the notifications shown for [threadIds], returning their IDs.
|
||||||
|
Future<Iterable<int>> remove(ISet<String> threadIds) async {
|
||||||
|
final shown = await _update(
|
||||||
|
(shown) =>
|
||||||
|
shown.removeWhere((threadId, _) => threadIds.contains(threadId)),
|
||||||
|
);
|
||||||
|
return threadIds.map((threadId) => shown[threadId]).nonNulls.flattened;
|
||||||
|
}
|
||||||
|
|
||||||
|
static final provider = Provider<ShownNotificationsHelper>(
|
||||||
|
ShownNotificationsHelper.new,
|
||||||
|
);
|
||||||
|
}
|
||||||
Loading…
Reference in a new issue