refactor: Use hashcode instead of string to id workaround for notifications

This commit is contained in:
Krille 2024-02-20 16:10:22 +01:00
parent d6e797b04b
commit d2a58422b4
No known key found for this signature in database
GPG key ID: E067ECD60F1A0652
2 changed files with 3 additions and 30 deletions

View file

@ -112,8 +112,7 @@ class BackgroundPush {
Future<void> 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) {

View file

@ -50,7 +50,7 @@ Future<void> pushHelper(
l10n ??= lookupL10n(const Locale('en'));
flutterLocalNotificationsPlugin.show(
0,
notification.roomId?.hashCode ?? 0,
l10n.newMessageInFluffyChat,
l10n.openAppToReadMessages,
NotificationDetails(
@ -203,7 +203,7 @@ Future<void> _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<int> mapRoomIdToInt(String roomId) async {
final store = await SharedPreferences.getInstance();
final idMap = Map<String, int>.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;
}