Skip to content

Commit

Permalink
#10: Improve events notifications and add test data.
Browse files Browse the repository at this point in the history
  • Loading branch information
hjtappe committed Dec 25, 2024
1 parent 25feab9 commit b5d7c7c
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 19 deletions.
19 changes: 13 additions & 6 deletions lib/data/dataProviders/events_provider.dart
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class EventsProvider with ChangeNotifier {
static String get worksheetTitle => "Sessions";
String get _cacheTitle => "_eventDataCache";
final GsheetsProvider _gsheetsProvider;
final List<int> itemIDs = [];
final List<int> _notificationIDs = [];

final List<EventData> _cache = [];

Expand Down Expand Up @@ -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);
}
}

Expand Down
20 changes: 7 additions & 13 deletions lib/data/dataProviders/favorites_provider.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import 'package:shared_preferences/shared_preferences.dart';

class FavoritesProvider with ChangeNotifier {
String get _cacheTitle => "_favoritesDataCache";
final List<int> _registeredIDs = [];
final List<int> _notificationIDs = [];

final List<FavoritesData> _cache = [];

Expand Down Expand Up @@ -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}',
Expand All @@ -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,
Expand Down
9 changes: 9 additions & 0 deletions lib/data/notifications/local_notification_service.dart
Original file line number Diff line number Diff line change
Expand Up @@ -196,4 +196,13 @@ class LocalNotificationService {

static Future<void> onDidReceiveBackgroundNotificationResponse(
NotificationResponse notificationResponse) async {}


static DateTime scheduleAhead ({
required DateTime time,
Duration? before,
}) {
before ??= Duration(minutes: 3);
return time.subtract(before);
}
}

0 comments on commit b5d7c7c

Please sign in to comment.