From d2a58422b43daf57d1c98ac2d9d5878fffe44be5 Mon Sep 17 00:00:00 2001 From: Krille Date: Tue, 20 Feb 2024 16:10:22 +0100 Subject: [PATCH] refactor: Use hashcode instead of string to id workaround for notifications --- lib/utils/background_push.dart | 3 +-- lib/utils/push_helper.dart | 30 ++---------------------------- 2 files changed, 3 insertions(+), 30 deletions(-) diff --git a/lib/utils/background_push.dart b/lib/utils/background_push.dart index 92e79fb1..dfdb171b 100644 --- a/lib/utils/background_push.dart +++ b/lib/utils/background_push.dart @@ -112,8 +112,7 @@ class BackgroundPush { Future cancelNotification(String roomId) async { Logs().v('Cancel notification for room', roomId); - final id = await mapRoomIdToInt(roomId); - await FlutterLocalNotificationsPlugin().cancel(id); + await FlutterLocalNotificationsPlugin().cancel(roomId.hashCode); // Workaround for app icon badge not updating if (Platform.isIOS) { diff --git a/lib/utils/push_helper.dart b/lib/utils/push_helper.dart index 78c60702..618410db 100644 --- a/lib/utils/push_helper.dart +++ b/lib/utils/push_helper.dart @@ -50,7 +50,7 @@ Future pushHelper( l10n ??= lookupL10n(const Locale('en')); flutterLocalNotificationsPlugin.show( - 0, + notification.roomId?.hashCode ?? 0, l10n.newMessageInFluffyChat, l10n.openAppToReadMessages, NotificationDetails( @@ -203,7 +203,7 @@ Future _tryPushHelper( Logs().e('Unable to get avatar picture', e, s); } - final id = await mapRoomIdToInt(event.room.id); + final id = notification.roomId.hashCode; // Show notification @@ -327,29 +327,3 @@ void _setShortcut( ), ); } - -/// Workaround for the problem that local notification IDs must be int but we -/// sort by [roomId] which is a String. To make sure that we don't have duplicated -/// IDs we map the [roomId] to a number and store this number. -Future mapRoomIdToInt(String roomId) async { - final store = await SharedPreferences.getInstance(); - final idMap = Map.from( - jsonDecode(store.getString(SettingKeys.notificationCurrentIds) ?? '{}'), - ); - int? currentInt; - try { - currentInt = idMap[roomId]; - } catch (_) { - currentInt = null; - } - if (currentInt != null) { - return currentInt; - } - var nCurrentInt = 0; - while (idMap.values.contains(nCurrentInt)) { - nCurrentInt++; - } - idMap[roomId] = nCurrentInt; - await store.setString(SettingKeys.notificationCurrentIds, json.encode(idMap)); - return nCurrentInt; -}