A cross platform Audiobookshelf client for Android and iOS
Dart
Don't overwrite offline progress with stale server position
Barnabas3c5ae951733b
diff --git a/lib/services/audio_player_service.dart b/lib/services/audio_player_service.dartindex 3ab6fe14..e838e451 100644--- a/lib/services/audio_player_service.dart+++ b/lib/services/audio_player_service.dart@@ -1844,8 +1844,11 @@ class AudioPlayerService extends ChangeNotifier {} else if (startTime > 0) {// Local is ahead — verify via timestamp that this isn't stale data.// Fetch the server's lastUpdate to compare with the local save time.+ // Skip the override if we have a pending local sync: local is the+ // truth, we just haven't shipped it to the server yet.bool useServer = false;- if (localTs > 0) {+ final hasPending = await _progressSync.hasPendingSync(pKey);+ if (localTs > 0 && !hasPending) {try {final serverProgress = await _api!.getItemProgress(pKey);final serverLastUpdate = (serverProgress?['lastUpdate'] as num?)?.toInt() ?? 0;@@ -1857,7 +1860,11 @@ class AudioPlayerService extends ChangeNotifier {} catch (_) {}}if (!useServer) {- debugPrint('[Player] Local position is ahead: local=${startTime}s vs server=${serverPos}s — keeping local');+ if (hasPending) {+ debugPrint('[Player] Local position is ahead: local=${startTime}s vs server=${serverPos}s — keeping local (pending sync)');+ } else {+ debugPrint('[Player] Local position is ahead: local=${startTime}s vs server=${serverPos}s — keeping local');+ }}} else if (serverPos > 0) {debugPrint('[Player] No local position, using server: ${serverPos}s');@@ -2237,8 +2244,13 @@ class AudioPlayerService extends ChangeNotifier {debugPrint('[Player] Server position is ahead: server=${serverPos}s vs local=${startTime}s — using server');startTime = serverPos;} else if (startTime > 0) {+ // Skip the staleness override if we have a pending local sync: the+ // server's lastUpdate can be newer than the local timestamp for reasons+ // unrelated to listening progress, so trusting it would clobber offline+ // playback we haven't shipped yet.bool useServer = false;- if (localTs > 0) {+ final hasPending = await _progressSync.hasPendingSync(pKey);+ if (localTs > 0 && !hasPending) {try {final serverProgress = await api.getItemProgress(pKey);final serverLastUpdate = (serverProgress?['lastUpdate'] as num?)?.toInt() ?? 0;@@ -2250,7 +2262,11 @@ class AudioPlayerService extends ChangeNotifier {} catch (_) {}}if (!useServer) {- debugPrint('[Player] Local position is ahead: local=${startTime}s vs server=${serverPos}s — keeping local');+ if (hasPending) {+ debugPrint('[Player] Local position is ahead: local=${startTime}s vs server=${serverPos}s — keeping local (pending sync)');+ } else {+ debugPrint('[Player] Local position is ahead: local=${startTime}s vs server=${serverPos}s — keeping local');+ }}} else if (serverPos > 0) {debugPrint('[Player] No local position, using server: ${serverPos}s');diff --git a/lib/services/progress_sync_service.dart b/lib/services/progress_sync_service.dartindex 72d97a7c..55010067 100644--- a/lib/services/progress_sync_service.dart+++ b/lib/services/progress_sync_service.dart@@ -117,6 +117,13 @@ class ProgressSyncService {return (data?['timestamp'] as num?)?.toInt() ?? 0;}+ /// Whether the given item has unsynced local progress. Callers can use this+ /// to avoid clobbering offline-listening progress with a stale server snapshot.+ Future<bool> hasPendingSync(String itemId) async {+ final pendingList = await ScopedPrefs.getStringList('pending_syncs');+ return pendingList.contains(itemId);+ }+/// Delete locally saved progress for an item.Future<void> deleteLocal(String itemId) async {await ScopedPrefs.remove('progress_$itemId');