try to dismiss notifications when read
This commit is contained in:
parent
b680add183
commit
322217fc20
2 changed files with 90 additions and 1 deletions
|
|
@ -2,10 +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: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/shared_prefs.dart";
|
||||||
import "package:nexus/controllers/sidebar_page.dart";
|
import "package:nexus/controllers/sidebar_page.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";
|
||||||
|
|
@ -58,13 +62,92 @@ class NotificationController
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Reading a room, here or on another client, dismisses its notifications
|
||||||
|
if (!isInBackground) {
|
||||||
|
ref.listen(
|
||||||
|
RoomsController.provider.select(
|
||||||
|
(rooms) => rooms
|
||||||
|
.where((_, room) => room.metadata?.unreadNotifications == 0)
|
||||||
|
.keys
|
||||||
|
.toISet(),
|
||||||
|
),
|
||||||
|
(previous, read) {
|
||||||
|
final newlyRead = read.difference(previous ?? const ISetConst({}));
|
||||||
|
if (newlyRead.isEmpty) return;
|
||||||
|
|
||||||
|
final accountId = ref.read(ActiveAccountController.provider);
|
||||||
|
dismiss(newlyRead.map((roomId) => threadIdFor(accountId, roomId)))
|
||||||
|
.onError(showError);
|
||||||
|
},
|
||||||
|
fireImmediately: true,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
return notifications;
|
return notifications;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static String threadIdFor(String accountId, String roomId) =>
|
||||||
|
"$accountId/$roomId";
|
||||||
|
|
||||||
static String payloadFor(String accountId, int rowId) => "$accountId:$rowId";
|
static String payloadFor(String accountId, int rowId) => "$accountId:$rowId";
|
||||||
|
|
||||||
static const _androidGroup = "nexus.federated.nexus.MESSAGES";
|
static const _androidGroup = "nexus.federated.nexus.MESSAGES";
|
||||||
static const _summaryPayload = "summary";
|
static const _summaryPayload = "summary";
|
||||||
|
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].
|
||||||
|
Future<void> dismiss(Iterable<String> threadIds) async {
|
||||||
|
final shown = await _shown();
|
||||||
|
final ids = threadIds.map((threadId) => shown[threadId]).nonNulls.flattened;
|
||||||
|
if (ids.isEmpty) return;
|
||||||
|
|
||||||
|
await _setShown(
|
||||||
|
shown.removeWhere((threadId, _) => threadIds.contains(threadId)),
|
||||||
|
);
|
||||||
|
|
||||||
|
if (Platform.isLinux) {
|
||||||
|
final portal = await ref.read(PortalController.provider.future);
|
||||||
|
await Future.wait(
|
||||||
|
ids.map((id) => portal.notification.removeNotification(id.toString())),
|
||||||
|
);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
final controller = await future;
|
||||||
|
await Future.wait(ids.map((id) => controller.cancel(id: id)));
|
||||||
|
|
||||||
|
// The summary would otherwise outlive its group
|
||||||
|
if (Platform.isAndroid &&
|
||||||
|
(await controller.getActiveNotifications()).every(
|
||||||
|
(notification) => notification.id == 0,
|
||||||
|
)) {
|
||||||
|
await controller.cancel(id: 0);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
void _open(String payload) {
|
void _open(String payload) {
|
||||||
if (payload == _summaryPayload) {
|
if (payload == _summaryPayload) {
|
||||||
|
|
@ -208,6 +291,7 @@ class NotificationController
|
||||||
// ),
|
// ),
|
||||||
],
|
],
|
||||||
);
|
);
|
||||||
|
if (threadId != null) await _track(threadId, id);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -293,6 +377,8 @@ class NotificationController
|
||||||
),
|
),
|
||||||
);
|
);
|
||||||
|
|
||||||
|
if (threadId != null) await _track(threadId, id);
|
||||||
|
|
||||||
if (Platform.isAndroid) {
|
if (Platform.isAndroid) {
|
||||||
await controller.show(
|
await controller.show(
|
||||||
id: 0,
|
id: 0,
|
||||||
|
|
|
||||||
|
|
@ -143,7 +143,10 @@ class UnifiedPushController extends AsyncNotifier<String?> {
|
||||||
"$accountId/${Platform.isAndroid ? notification.roomId : notification.eventId}"
|
"$accountId/${Platform.isAndroid ? notification.roomId : notification.eventId}"
|
||||||
.hashCode &
|
.hashCode &
|
||||||
0x7fffffff,
|
0x7fffffff,
|
||||||
threadId: "$accountId/${notification.roomId}",
|
threadId: NotificationController.threadIdFor(
|
||||||
|
accountId,
|
||||||
|
notification.roomId,
|
||||||
|
),
|
||||||
title: notification.roomName,
|
title: notification.roomName,
|
||||||
subtitle: hasOtherAccounts ? notification.self.id : null,
|
subtitle: hasOtherAccounts ? notification.self.id : null,
|
||||||
body: notification.text,
|
body: notification.text,
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue