Skip to content

A cross platform Audiobookshelf client for Android and iOS

Dart

Fix mark-finished server sync, auto-play on manual finish, and continue series not updating

Nathan Poulsonabaa42ad07c9
diff --git a/lib/providers/library_provider.dart b/lib/providers/library_provider.dart
index c274abfc..5398ed6d 100644
--- a/lib/providers/library_provider.dart
+++ b/lib/providers/library_provider.dart
@@ -41,6 +41,7 @@ class LibraryProvider extends ChangeNotifier {
String? _lastRssHydrationLibraryId;
static const _personalizedFetchCooldown = Duration(seconds: 5);
static const _rssHydrationCooldown = Duration(minutes: 10);
+ Timer? _progressRefreshDebounce;
// Per-series/show rolling download opt-in
Set<String> _rollingDownloadSeries = {};
@@ -484,6 +485,7 @@ class LibraryProvider extends ChangeNotifier {
_selectedLibraryId = null;
_errorMessage = null;
_connectivitySub?.cancel();
+ _progressRefreshDebounce?.cancel();
_stopServerPingTimer();
SocketService().disconnect();
_personalizedInFlight = null;
@@ -585,10 +587,20 @@ class LibraryProvider extends ChangeNotifier {
final episodeId = mp['episodeId'] as String?;
if (itemId == null) return;
final key = episodeId != null ? '$itemId-$episodeId' : itemId;
+ // Don't let a stale socket event overwrite a local mark-finished.
+ // The server will confirm isFinished on the next full refresh.
+ if (_locallyFinishedItems.contains(key) && mp['isFinished'] != true) return;
_progressMap[key] = mp;
_localProgressOverrides.remove(key);
_resetItems.remove(key);
notifyListeners();
+
+ // Debounce a personalized view refresh so sections like "continue series"
+ // update when progress changes (e.g. a book finished on another device).
+ _progressRefreshDebounce?.cancel();
+ _progressRefreshDebounce = Timer(const Duration(seconds: 2), () {
+ loadPersonalizedView(force: true);
+ });
}
/// Handle library item added or updated from socket.
@@ -1416,7 +1428,7 @@ class LibraryProvider extends ChangeNotifier {
/// [itemId] can be a plain book ID or a compound "showId-episodeId" key.
/// If [skipRefresh] is true, the caller handles refreshing (e.g.
/// book_detail_sheet calls refresh() after api.markFinished).
- void markFinishedLocally(String itemId, {bool skipRefresh = false}) {
+ void markFinishedLocally(String itemId, {bool skipRefresh = false, bool skipAutoAdvance = false}) {
if (_resetItems.contains(itemId)) return;
final existing = _progressMap[itemId] ?? {};
_progressMap[itemId] = {...existing, 'isFinished': true};
@@ -1440,14 +1452,17 @@ class LibraryProvider extends ChangeNotifier {
// Instant local auto-advance — uses cached metadata + download info,
// no server call needed. Fires first so playback starts immediately,
// then the server refresh still runs in the background when online.
- PlayerSettings.getQueueMode().then((mode) {
- if (mode == 'manual') {
- _manualQueueAdvance(itemId);
- } else if (mode == 'auto_next') {
- _autoAdvanceOffline(itemId);
- }
- // mode == 'off': do nothing, playback stops
- });
+ // Skip when user manually marks an item finished (not natural playback completion).
+ if (!skipAutoAdvance) {
+ PlayerSettings.getQueueMode().then((mode) {
+ if (mode == 'manual') {
+ _manualQueueAdvance(itemId);
+ } else if (mode == 'auto_next') {
+ _autoAdvanceOffline(itemId);
+ }
+ // mode == 'off': do nothing, playback stops
+ });
+ }
// Slide the rolling download window forward for the finished item's series
_checkRollingDownloads(itemId);
diff --git a/lib/screens/home_screen.dart b/lib/screens/home_screen.dart
index 44cbff1a..bfa508b0 100644
--- a/lib/screens/home_screen.dart
+++ b/lib/screens/home_screen.dart
@@ -27,7 +27,7 @@ class _HomeScreenState extends State<HomeScreen> {
// Cached filtered sections — invalidated when source data or settings change.
List<dynamic>? _cachedSections;
List<dynamic>? _cachedClItems;
- int _lastSectionsLength = -1;
+ List<dynamic>? _lastSectionsRef;
bool _lastHideEbook = false;
bool _lastIsPodcast = false;
@@ -77,13 +77,13 @@ class _HomeScreenState extends State<HomeScreen> {
void _refreshFilteredCache(LibraryProvider lib) {
final sections = lib.personalizedSections;
final isPod = lib.isPodcastLibrary;
- if (sections.length == _lastSectionsLength &&
+ if (identical(sections, _lastSectionsRef) &&
_hideEbookOnly == _lastHideEbook &&
isPod == _lastIsPodcast &&
_cachedSections != null) {
return; // cache is still valid
}
- _lastSectionsLength = sections.length;
+ _lastSectionsRef = sections;
_lastHideEbook = _hideEbookOnly;
_lastIsPodcast = isPod;
@@ -253,7 +253,7 @@ class _HomeScreenState extends State<HomeScreen> {
// (where the new user may have the same number of sections).
_cachedSections = null;
_cachedClItems = null;
- _lastSectionsLength = -1;
+ _lastSectionsRef = null;
} else {
_refreshFilteredCache(lib);
}
diff --git a/lib/widgets/book_detail_sheet.dart b/lib/widgets/book_detail_sheet.dart
index 2533a350..8a30fe8c 100644
--- a/lib/widgets/book_detail_sheet.dart
+++ b/lib/widgets/book_detail_sheet.dart
@@ -744,9 +744,9 @@ class _BookDetailSheetContentState extends State<_BookDetailSheetContent> {
// Mark finished locally first so the absorbing card shows the overlay
// immediately when the player stops (which triggers the expanded card to pop)
if (context.mounted) {
- context.read<LibraryProvider>().markFinishedLocally(widget.itemId, skipRefresh: true);
+ context.read<LibraryProvider>().markFinishedLocally(widget.itemId, skipRefresh: true, skipAutoAdvance: true);
}
- if (player.currentItemId == widget.itemId) await player.stop();
+ if (player.currentItemId == widget.itemId) await player.stopWithoutSaving();
try {
await api.markFinished(widget.itemId, duration);
await ProgressSyncService().deleteLocal(widget.itemId);
diff --git a/lib/widgets/episode_list_sheet.dart b/lib/widgets/episode_list_sheet.dart
index f5b73724..8da4b83b 100644
--- a/lib/widgets/episode_list_sheet.dart
+++ b/lib/widgets/episode_list_sheet.dart
@@ -630,7 +630,7 @@ class _EpisodeDetailSheetState extends State<EpisodeDetailSheet> {
duration: _duration,
isFinished: true,
);
- lib.markFinishedLocally(key);
+ lib.markFinishedLocally(key, skipAutoAdvance: true);
if (mounted) {
ScaffoldMessenger.of(context).showSnackBar(SnackBar(
content: const Text('Marked as finished — nice!'),
diff --git a/lib/widgets/series_books_sheet.dart b/lib/widgets/series_books_sheet.dart
index f766bccf..21ab5f61 100644
--- a/lib/widgets/series_books_sheet.dart
+++ b/lib/widgets/series_books_sheet.dart
@@ -212,7 +212,7 @@ class _SeriesBooksSheetState extends State<SeriesBooksSheet> {
? (media['duration'] as num).toDouble()
: 0.0;
await api.markFinished(bookId, duration);
- lib.markFinishedLocally(bookId, skipRefresh: true);
+ lib.markFinishedLocally(bookId, skipRefresh: true, skipAutoAdvance: true);
}
if (mounted) {