Skip to content

A cross platform Audiobookshelf client for Android and iOS

Dart

Update podcast badges right after finishing an episode

Barnabas0be92c698828
diff --git a/lib/providers/_lp_absorbing.dart b/lib/providers/_lp_absorbing.dart
index a433322b..64c6912b 100644
--- a/lib/providers/_lp_absorbing.dart
+++ b/lib/providers/_lp_absorbing.dart
@@ -400,6 +400,9 @@ mixin _AbsorbingMixin on ChangeNotifier, _StateMixin, _CoreMixin {
{bool skipRefresh = false, bool skipAutoAdvance = false}) {
_resetItems.remove(itemId);
final existing = _progressMap[itemId] ?? {};
+ if (itemId.length > 36 && existing['isFinished'] != true) {
+ nudgeUnfinishedEpisodeCount(itemId.substring(0, 36), -1);
+ }
_progressMap[itemId] = {...existing, 'isFinished': true};
_localProgressOverrides[itemId] = 1.0;
_lastFinishedItemId = itemId;
diff --git a/lib/providers/_lp_core.dart b/lib/providers/_lp_core.dart
index 464ab614..45a3cff6 100644
--- a/lib/providers/_lp_core.dart
+++ b/lib/providers/_lp_core.dart
@@ -533,14 +533,16 @@ mixin _CoreMixin on ChangeNotifier, _StateMixin {
int getUnfinishedEpisodeCount(Map<String, dynamic>? show) {
if (show == null) return 0;
final media = show['media'] as Map<String, dynamic>? ?? const {};
+ final itemId = show['id'] as String? ?? '';
// Try the server-provided count first.
final serverCount = show['numEpisodesIncomplete'] as int? ??
media['numEpisodesIncomplete'] as int?;
- if (serverCount != null) return serverCount;
+ if (serverCount != null) {
+ return _adjustedUnfinishedCount(itemId, serverCount);
+ }
// Fallback: count from the loaded episodes array. Match server semantics
// — count any episode the user hasn't marked finished.
- final itemId = show['id'] as String? ?? '';
if (itemId.isEmpty) return 0;
final episodes = media['episodes'] as List<dynamic>? ?? const [];
if (episodes.isEmpty) return 0;
@@ -556,6 +558,33 @@ mixin _CoreMixin on ChangeNotifier, _StateMixin {
return count;
}
+ /// Apply (and fold pending) local badge nudges onto the server's count.
+ /// The cached items showing badges aren't all owned by the provider, so the
+ /// correction lives here instead of in the item maps. Self-discards when
+ /// the server count moves off the base it was recorded against.
+ int _adjustedUnfinishedCount(String showId, int serverCount) {
+ if (showId.isEmpty) return serverCount;
+ final pending = _pendingUnfinishedDeltas.remove(showId);
+ if (pending != null) {
+ final prior = _unfinishedCountAdjustments[showId];
+ final from = (prior != null && prior.$1 == serverCount) ? prior.$2 : serverCount;
+ _unfinishedCountAdjustments[showId] =
+ (serverCount, (from + pending).clamp(0, 1 << 30));
+ }
+ final adj = _unfinishedCountAdjustments[showId];
+ if (adj == null) return serverCount;
+ if (adj.$1 == serverCount) return adj.$2;
+ _unfinishedCountAdjustments.remove(showId);
+ return serverCount;
+ }
+
+ /// Record a local +/- nudge for a show's unfinished-episode badge.
+ void nudgeUnfinishedEpisodeCount(String showId, int delta) {
+ if (showId.isEmpty) return;
+ _pendingUnfinishedDeltas[showId] =
+ (_pendingUnfinishedDeltas[showId] ?? 0) + delta;
+ }
+
Future<void> refreshLocalProgress() async {
final sync = ProgressSyncService();
final itemIds = <String>{..._progressMap.keys};
@@ -589,6 +618,9 @@ mixin _CoreMixin on ChangeNotifier, _StateMixin {
}
void resetProgressFor(String itemId) {
+ if (itemId.length > 36 && _progressMap[itemId]?['isFinished'] == true) {
+ nudgeUnfinishedEpisodeCount(itemId.substring(0, 36), 1);
+ }
_progressMap.remove(itemId);
_localProgressOverrides.remove(itemId);
_locallyFinishedItems.remove(itemId);
diff --git a/lib/providers/_lp_state.dart b/lib/providers/_lp_state.dart
index ca250f0a..cadd9c08 100644
--- a/lib/providers/_lp_state.dart
+++ b/lib/providers/_lp_state.dart
@@ -15,6 +15,15 @@ mixin _StateMixin on ChangeNotifier {
final Map<String, int> _itemUpdatedAt = {};
final Set<String> _itemsWithoutCover = {};
+ /// Local nudges to podcast unfinished-episode badges after an episode is
+ /// finished/reset in-app, keyed by show id. Pending deltas get folded into
+ /// _unfinishedCountAdjustments against the server count on the next badge
+ /// read; an adjustment is dropped once the server reports a different base
+ /// count (fresh data caught up). Needed because show tiles can render from
+ /// caches the provider doesn't own (library screen pages).
+ final Map<String, int> _pendingUnfinishedDeltas = {};
+ final Map<String, (int, int)> _unfinishedCountAdjustments = {};
+
Future<void>? _personalizedInFlight;
Future<void>? _progressShelvesInFlight;
DateTime? _lastPersonalizedFetchAt;