updateNotificationIfEventTimeChanged method

Future<void> updateNotificationIfEventTimeChanged(
  1. List<ActivityModel> events,
  2. int minutesInAdvance
)

Updates the notification for an event if the event was edited.

Implementation

Future<void> updateNotificationIfEventTimeChanged(
  List<ActivityModel> events,
  int minutesInAdvance,
) async {
  final activeNotificationIds =
      (await flutterLocalNotificationsPlugin.pendingNotificationRequests())
          .map((e) => e.id)
          .toSet();

  for (final event in events) {
    // Check if the event has a notification scheduled
    if (activeNotificationIds.contains(event.id.hashCode)) {
      // Check if the event has been edited
      if (event.lastEdited == null ||
          event.postDate!.isAtSameMomentAs(event.lastEdited!)) {
        continue;
      }
      // If the event has been edited, cancel the old notification and schedule a new one
      await cancelEventNotification(event);
      await scheduleEventNotification(event, minutesInAdvance);
    }
  }
}