From b5d7c7c0faedf34a8aa9c10eff0ce79a4cc1c189 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hans-J=C3=BCrgen=20Tappe?= Date: Wed, 25 Dec 2024 16:59:22 +0100 Subject: [PATCH] #10: Improve events notifications and add test data. --- lib/data/dataProviders/events_provider.dart | 19 ++++++++++++------ .../dataProviders/favorites_provider.dart | 20 +++++++------------ .../local_notification_service.dart | 9 +++++++++ 3 files changed, 29 insertions(+), 19 deletions(-) diff --git a/lib/data/dataProviders/events_provider.dart b/lib/data/dataProviders/events_provider.dart index 388dd82..ea5dda2 100644 --- a/lib/data/dataProviders/events_provider.dart +++ b/lib/data/dataProviders/events_provider.dart @@ -15,7 +15,7 @@ class EventsProvider with ChangeNotifier { static String get worksheetTitle => "Sessions"; String get _cacheTitle => "_eventDataCache"; final GsheetsProvider _gsheetsProvider; - final List itemIDs = []; + final List _notificationIDs = []; final List _cache = []; @@ -112,21 +112,28 @@ class EventsProvider with ChangeNotifier { } void _registerNotifications() { - for (int id in itemIDs) { + for (int id in _notificationIDs) { LocalNotificationService.cancelNotification(id, null); } - itemIDs.clear(); + _notificationIDs.clear(); + DateTime now = DateTime.now(); for (EventData item in _items.where( (item) => - (item.forceNotify ?? false)).toList() + (item.forceNotify ?? false == true && + item.start.isAfter(now))).toList() ) { - itemIDs.add(item.id ?? 0); + _notificationIDs.add(item.id ?? 0); + DateTime notificationTime = LocalNotificationService.scheduleAhead( + time: item.start, + ); + Debug.msg('ANNOUNCE ${item.name} at ${item + .start} ($notificationTime) with ID ${item.id ?? 0}'); LocalNotificationService.scheduleNotification( title: 'Upcoming: ${item.name}', body: item.details, id: item.id, - scheduledDate: item.start.subtract(Duration(minutes: 3))); + scheduledDate: notificationTime); } } diff --git a/lib/data/dataProviders/favorites_provider.dart b/lib/data/dataProviders/favorites_provider.dart index 8ca220f..3277ef4 100644 --- a/lib/data/dataProviders/favorites_provider.dart +++ b/lib/data/dataProviders/favorites_provider.dart @@ -10,7 +10,7 @@ import 'package:shared_preferences/shared_preferences.dart'; class FavoritesProvider with ChangeNotifier { String get _cacheTitle => "_favoritesDataCache"; - final List _registeredIDs = []; + final List _notificationIDs = []; final List _cache = []; @@ -81,18 +81,20 @@ class FavoritesProvider with ChangeNotifier { void _registerNotifications() { // Selectively cancel the favorites notifications from this class. - for (int id in _registeredIDs) { + for (int id in _notificationIDs) { Debug.msg('CANCEL ID $id'); LocalNotificationService.cancelNotification(id, null); } - _registeredIDs.clear(); + _notificationIDs.clear(); DateTime now = DateTime.now(); for (FavoritesData item in _items) { // Debug.msg('PROCESSING ${item.name}: ADD ${item.id ?? 0} TO _registeredIDs'); if (item.start.isAfter(now)) { - _registeredIDs.add(item.id ?? 0); - DateTime notificationTime = _scheduleAhead(time: item.start); + _notificationIDs.add(item.id ?? 0); + DateTime notificationTime = LocalNotificationService.scheduleAhead( + time: item.start, + ); Debug.msg('NOTIFY ${item.name} at ${item.start} ($notificationTime) with ID ${item.id ?? 0}'); LocalNotificationService.scheduleNotification( title: 'Upcoming: ${item.name}', @@ -106,14 +108,6 @@ class FavoritesProvider with ChangeNotifier { } } - DateTime _scheduleAhead ({ - required DateTime time, - Duration? before, - }) { - before ??= Duration(minutes: 3); - return time.subtract(before); - } - bool isInFavorites({ required String name, required DateTime start, diff --git a/lib/data/notifications/local_notification_service.dart b/lib/data/notifications/local_notification_service.dart index 3f9fed4..0a9db17 100644 --- a/lib/data/notifications/local_notification_service.dart +++ b/lib/data/notifications/local_notification_service.dart @@ -196,4 +196,13 @@ class LocalNotificationService { static Future onDidReceiveBackgroundNotificationResponse( NotificationResponse notificationResponse) async {} + + + static DateTime scheduleAhead ({ + required DateTime time, + Duration? before, + }) { + before ??= Duration(minutes: 3); + return time.subtract(before); + } } \ No newline at end of file