A cross platform Audiobookshelf client for Android and iOS
Dart
Stop a reconnect dragging a paused book back to the server's old position, and keep syncing through the right address after leaving WiFi
Barnabas2488d78f5ede
diff --git a/lib/providers/_lp_core.dart b/lib/providers/_lp_core.dartindex 2bb2c15c..58d76924 100644--- a/lib/providers/_lp_core.dart+++ b/lib/providers/_lp_core.dart@@ -1537,6 +1537,11 @@ mixin _CoreMixin on ChangeNotifier, _StateMixin {final localUpd =((_progressMap[key]?['lastUpdate']) as num?)?.toInt() ?? 0;if (serverUpd <= localUpd) continue;+ // What this device saved itself counts too, not just the last+ // server value it happened to see - otherwise a reconnect after an+ // offline stretch replays the server's pre-outage position over it.+ final savedAt = await ProgressSyncService().getSavedTimestamp(key);+ if (serverUpd <= savedAt) continue;_onRemoteProgressUpdated(mp);applied++;}@@ -1586,13 +1591,29 @@ mixin _CoreMixin on ChangeNotifier, _StateMixin {newUpd > prevUpd &&!player.isPlaying &&!ChromecastService().isCasting) {- final posS = player.position.inMilliseconds / 1000.0;- if ((serverTime - posS).abs() > 5.0) {- debugPrint('[Sync] Adopting newer remote position for paused player: '- '${posS.toStringAsFixed(1)}s -> ${serverTime.toStringAsFixed(1)}s');- unawaited(player.seekTo(- Duration(milliseconds: (serverTime * 1000).round())));- }+ // "Newer than the last server value we saw" is not "newer than+ // where this phone got to": after an hour offline the server still+ // holds the position from before the outage, and adopting it drags+ // the paused player back and then syncs that old spot up over the+ // real one. The position saved here carries its own timestamp.+ unawaited(() async {+ final savedAt =+ await ProgressSyncService().getSavedTimestamp(key);+ if (newUpd <= savedAt) {+ debugPrint('[Sync] Ignoring remote position for paused player: '+ 'server ${serverTime.toStringAsFixed(1)}s ($newUpd) is older '+ 'than this device's save ($savedAt)');+ return;+ }+ if (player.isPlaying) return;+ final posS = player.position.inMilliseconds / 1000.0;+ if ((serverTime - posS).abs() > 5.0) {+ debugPrint('[Sync] Adopting newer remote position for paused player: '+ '${posS.toStringAsFixed(1)}s -> ${serverTime.toStringAsFixed(1)}s');+ await player.seekTo(+ Duration(milliseconds: (serverTime * 1000).round()));+ }+ }());}notifyListeners();}diff --git a/lib/providers/library_provider.dart b/lib/providers/library_provider.dartindex 91d1fb47..c2283160 100644--- a/lib/providers/library_provider.dart+++ b/lib/providers/library_provider.dart@@ -126,6 +126,10 @@ class LibraryProvider extends ChangeNotifier// or empty sections. Refresh once on each transition.if (_lastUseLocalServer != null && _lastUseLocalServer != auth.useLocalServer) {_lastUseLocalServer = auth.useLocalServer;+ // The player follows the address even while the library thinks it+ // is offline - that is exactly when the switch tends to happen.+ final switchedApi = auth.apiService;+ if (switchedApi != null) AudioPlayerService().useApi(switchedApi);if (!_networkOffline && !_manualOffline) {debugPrint('[Library] Active server switched - refreshing library data');AudioPlayerService().resetServerSyncBackoff();diff --git a/lib/services/audio_player_service.dart b/lib/services/audio_player_service.dartindex bcb15221..646480e3 100644--- a/lib/services/audio_player_service.dart+++ b/lib/services/audio_player_service.dart@@ -6261,6 +6261,19 @@ class AudioPlayerService extends ChangeNotifier {}}+ /// The active server address changed under a loaded book (local to remote+ /// or back). Sync, session pushes and the offline flush all go through+ /// [_api], which until now stayed on the address play started with - so+ /// after leaving WiFi every push went to a LAN address for as long as the+ /// book kept playing.+ void useApi(ApiService api) {+ final current = _api;+ if (current == null || identical(current, api)) return;+ if (current.cleanBaseUrl == api.cleanBaseUrl) return;+ debugPrint('[Player] Sync now goes to ${api.cleanBaseUrl}');+ _api = api;+ }+/// Forget any sync backoff so the next tick pushes progress right away./// Called when the network comes back or the active server changes.void resetServerSyncBackoff() {