mirror of
https://github.com/krille-chan/fluffychat
synced 2024-09-17 08:15:09 +00:00
refactor: Use hashcode instead of string to id workaround for notifications
This commit is contained in:
parent
d6e797b04b
commit
d2a58422b4
2 changed files with 3 additions and 30 deletions
|
@ -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) {
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue