A cross platform Audiobookshelf client for Android and iOS
Dart
Stop notifying about podcast episodes you already listened to or already saw in the app
Barnabas5f2d65e2a444
diff --git a/lib/providers/_lp_core.dart b/lib/providers/_lp_core.dartindex e931ca44..bf863820 100644--- a/lib/providers/_lp_core.dart+++ b/lib/providers/_lp_core.dart@@ -2287,13 +2287,37 @@ mixin _CoreMixin on ChangeNotifier, _StateMixin {return;}+ // "Already seen" is tracked per device (known_episodes_ is not synced or+ // backed up), so a phone that was off, or a second phone, meets episodes+ // another device already announced. Progress DOES sync, so use it as the+ // tiebreaker: an episode already listened to is not news, and re-downloading+ // it is worse than not mentioning it. Record it as known and move on.final newEpisodes = <Map<String, dynamic>>[];+ var alreadyHeard = 0;for (final ep in episodes) {final epMap = ep as Map<String, dynamic>;final epId = epMap['id'] as String?;- if (epId != null && !knownIds.contains(epId)) {- newEpisodes.add(epMap);+ if (epId == null || knownIds.contains(epId)) continue;+ final progress = getEpisodeProgressData(itemId, epId);+ final finished = progress?['isFinished'] == true;+ final started = ((progress?['currentTime'] as num?)?.toDouble() ?? 0) > 0;+ if (finished || started) {+ knownIds.add(epId);+ alreadyHeard++;+ continue;}+ newEpisodes.add(epMap);+ }+ if (alreadyHeard > 0) {+ debugPrint(+ '[Subscription] $alreadyHeard episode(s) for $itemId were already '+ 'listened to elsewhere - marking known, not announcing',+ );+ await _saveKnownEpisodeIds(itemId);+ await EpisodeNotificationService.markAlreadySurfaced(+ itemId,+ knownIds,+ );}if (newEpisodes.isNotEmpty) {@@ -2374,6 +2398,12 @@ mixin _CoreMixin on ChangeNotifier, _StateMixin {if (!queueless) {knownIds.addAll(freshEpIds);_saveKnownEpisodeIds(itemId);+ // The user is looking at these right now, queued and downloading, so+ // the background job must not announce them hours later as news.+ unawaited(EpisodeNotificationService.markAlreadySurfaced(+ itemId,+ freshEpIds,+ ));(this as _AbsorbingMixin)._saveManualAbsorbing();notifyListeners();_downloadSubscribedEpisodes(itemId, keys: freshKeys, meta: freshMeta);@@ -2388,6 +2418,10 @@ mixin _CoreMixin on ChangeNotifier, _StateMixin {if (started) {knownIds.addAll(freshEpIds);_saveKnownEpisodeIds(itemId);+ unawaited(EpisodeNotificationService.markAlreadySurfaced(+ itemId,+ freshEpIds,+ ));} else {debugPrint('[Subscription] $itemId download deferred - leaving ''${freshEpIds.length} episode(s) unseen so the next check retries');diff --git a/lib/providers/library_provider.dart b/lib/providers/library_provider.dartindex e2e5e31b..8e1915c9 100644--- a/lib/providers/library_provider.dart+++ b/lib/providers/library_provider.dart@@ -11,6 +11,7 @@ import '../services/api_service.dart';import '../services/progress_sync_service.dart';import '../services/local_session_service.dart';import '../services/download_service.dart';+import '../services/episode_notification_service.dart';import '../services/library_cache.dart';import '../services/android_auto_service.dart';import '../services/carplay_service.dart';@@ -549,4 +550,19 @@ class LibraryProvider extends ChangeNotifierreturn getProgressData(key)?['isFinished'] == true;}+ /// Re-read every prefs-backed list this provider holds in memory.+ ///+ /// Settings sync and the manual restore both rewrite SharedPreferences under+ /// a running app. Without this the provider keeps serving what it loaded at+ /// account load - and worse, the next `_saveManualAbsorbing()` writes that+ /// stale copy straight back over what just arrived, which is how a card+ /// removed on another phone came back and stayed back through a reboot.+ Future<void> reloadPrefsBackedState() async {+ await _loadManualAbsorbing();+ await _loadRollingDownloadSeries();+ await _loadSubscribedPodcasts();+ await _loadYearHidden();+ await _loadSectionPrefs();+ notifyListeners();+ }}diff --git a/lib/services/episode_notification_service.dart b/lib/services/episode_notification_service.dartindex 71565def..ed29e485 100644--- a/lib/services/episode_notification_service.dart+++ b/lib/services/episode_notification_service.dart@@ -28,6 +28,37 @@ class EpisodeNotificationService {static const _uniqueName = 'com.barnabas.absorb.episodeNotifCheck';static const channelId = 'new_episodes';+ static String notifiedKey(String showId) => 'notified_episodes_$showId';++ /// Record episodes as already announced without sending anything.+ ///+ /// The in-app check runs whenever the app opens, so a user who opens Absorb+ /// inside the polling window sees and downloads the new episodes there. The+ /// job would then still notify hours later, because its ledger is separate.+ /// Once the app has shown them, they are not news any more.+ ///+ /// Only ever adds. A show whose ledger is empty is one the job has never+ /// seeded, and seeding it here would be wrong - the job seeds silently on+ /// first sight, and writing the ids now would make that silent seed a no-op+ /// rather than a deliberate one.+ static Future<void> markAlreadySurfaced(+ String showId,+ Iterable<String> episodeIds,+ ) async {+ if (episodeIds.isEmpty) return;+ final key = notifiedKey(showId);+ final seen = (await ScopedPrefs.getStringList(key)).toSet();+ if (seen.isEmpty) return;+ final before = seen.length;+ seen.addAll(episodeIds);+ if (seen.length == before) return;+ await ScopedPrefs.setStringList(key, seen.toList());+ debugPrint(+ '[EpisodeNotif] ${seen.length - before} episode(s) for $showId already '+ 'surfaced in-app - will not be notified',+ );+ }+@visibleForTestingstatic Future<void> prepareBackgroundPreferences({Future<void> Function()? reloadPreferences,