allNotificationsProvider top-level property

AutoDisposeProvider<AsyncValue<List<AppNotification>>> allNotificationsProvider
final

Provides the list of all of the user's received app notifications

Implementation

final allNotificationsProvider =
    Provider.autoDispose<AsyncValue<List<AppNotification>>>((ref) {
  final serialNumbers = ref
          .watch(cachedDevicesProvider)
          .valueOrNull
          ?.map((d) => d.serialNumber)
          .toList() ??
      [];

  final notifications =
      ref.watch(notificationsApiNotifierProvider).value.mapValue(
            (notifications) =>
                notifications.map(dtoToAppNotification).whereNotNull().toList(),
          );

  // If all notifications are filtered out, load another page
  ref.listen<PagedValue<NotificationDTO>>(
    notificationsApiNotifierProvider,
    (previous, next) {
      final original = next.value.valueOrNull;
      final filtered = original
          ?.where(
            (notification) => serialNumbers.contains(notification.serialNumber),
          )
          .toList();

      if (original != null &&
          filtered != null &&
          original.isNotEmpty &&
          filtered.isEmpty &&
          !next.hasReachedEnd &&
          !next.value.isLoading) {
        ref.read(notificationsApiNotifierProvider.notifier).loadPage();
      }
    },
  );

  return notifications.mapValue(
    (notifications) => notifications
        .where(
          (notification) => serialNumbers.contains(notification.serialNumber),
        )
        .toList(),
  );
});