shouldProcessAppNotification function

Future<bool> shouldProcessAppNotification(
  1. ProviderContainer container,
  2. AppNotification notification
)

Returns whether the app should process the app notification, based on the user's preferences.

Returns true if the user's preferences indicate that they care about the notification. The notification should be saved locally and displayed accordingly.

Implementation

Future<bool> shouldProcessAppNotification(
  ProviderContainer container,
  AppNotification notification,
) async {
  // Load shared preferences using the core shared_preferences package
  // This avoids issues with doing background work in an isolated thread.
  final prefs = await SharedPreferences.getInstance();
  await prefs.reload(); // Force latest values

  // Load the streaming shared preferences so preference notifiers
  // are setup correctly.
  await container.read(sharedPrefProvider.future);

  // Read the relevant preference notifier, but only to get its key and
  // default value.
  PreferenceNotifier<bool>? notifier;
  switch (notification.type) {
    case AppNotificationType.shutdown:
      notifier = container.read(showShutdownNotificationsPrefProvider.notifier);
      break;
    case AppNotificationType.warning:
      notifier = container.read(showWarningNotificationsPrefProvider.notifier);
      break;
    case AppNotificationType.maintenanceNotice:
      notifier =
          container.read(showMaintenanceNotificationsPrefProvider.notifier);
      break;
    case AppNotificationType.longRunningNotice:
      notifier =
          container.read(showLongRunningNotificationsPrefProvider.notifier);
      break;
    case AppNotificationType.engineStartedNotice:
    case AppNotificationType.engineStoppedNotice:
    case AppNotificationType.exerciseEndedNotice:
    case AppNotificationType.exerciseStartedNotice:
      notifier = container
          .read(showGeneratorRunningNotificationsPrefProvider.notifier);
      break;
    case AppNotificationType.offlineNotice:
    case AppNotificationType.onlineNotice:
      notifier =
          container.read(showConnectionNotificationsPrefProvider.notifier);
      break;
    case AppNotificationType.notice:
      notifier = container.read(showNoticeNotificationsPrefProvider.notifier);
      break;
  }

  return notifier != null
      ? (prefs.getBool(notifier.key) ?? notifier.defaultValue)
      : false;
}