Skip to content

A cross platform Audiobookshelf client for Android and iOS

Dart

Keep pushing progress for downloaded books after a few failed updates instead of giving up until the app is opened

Barnabasf13fca6ab9a6
diff --git a/lib/providers/_lp_core.dart b/lib/providers/_lp_core.dart
index 02549219..934b3e3d 100644
--- a/lib/providers/_lp_core.dart
+++ b/lib/providers/_lp_core.dart
@@ -432,6 +432,7 @@ mixin _CoreMixin on ChangeNotifier, _StateMixin {
unawaited(_auth?.ensureUserInfoLoaded() ?? Future.value());
if (_api != null) {
debugPrint('[Library] Back online — flushing pending syncs');
+ AudioPlayerService().resetServerSyncBackoff();
ProgressSyncService().flushPendingSync(api: _api!);
ProgressSyncService().flushOfflineListeningTime(api: _api!);
LocalSessionService().flushPending(api: _api!);
diff --git a/lib/providers/library_provider.dart b/lib/providers/library_provider.dart
index 8e1915c9..91d1fb47 100644
--- a/lib/providers/library_provider.dart
+++ b/lib/providers/library_provider.dart
@@ -128,6 +128,7 @@ class LibraryProvider extends ChangeNotifier
_lastUseLocalServer = auth.useLocalServer;
if (!_networkOffline && !_manualOffline) {
debugPrint('[Library] Active server switched - refreshing library data');
+ AudioPlayerService().resetServerSyncBackoff();
refresh();
}
} else {
diff --git a/lib/services/audio_player_service.dart b/lib/services/audio_player_service.dart
index 7adb28fe..156f7359 100644
--- a/lib/services/audio_player_service.dart
+++ b/lib/services/audio_player_service.dart
@@ -3317,7 +3317,7 @@ class AudioPlayerService extends ChangeNotifier {
'[ClickDebug] App foregrounded: sincePrevPauseMs=$sincePrevPauseMs, '
'sincePrevPlayMs=$sincePrevPlayMs, aaDisconnectSuspect=$aaDisconnectSuspect',
);
- service._positionSyncFailures = 0; // retry on foreground
+ service.resetServerSyncBackoff();
if (Platform.isIOS && service._iosResyncPending) {
unawaited(service._iosForegroundResyncIfNeeded());
}
@@ -5493,6 +5493,7 @@ class AudioPlayerService extends ChangeNotifier {
_lastAccrualPos = null;
_positionSyncInProgress = false;
_positionSyncFailures = 0;
+ _noSessionSyncRetryAt = null;
// Cache prefs in background - not needed synchronously here
if (_prefs == null) {
SharedPreferences.getInstance().then((p) => _prefs = p);
@@ -5844,9 +5845,12 @@ class AudioPlayerService extends ChangeNotifier {
// Back off when the server is unreachable to avoid hammering
// every sync interval with requests that will just timeout.
- if (_positionSyncFailures >= 3) {
- // Skip server sync - will retry after connectivity change
- // or app foreground resets the counter.
+ // The pause is timed and grows with each failure, so a bad
+ // minute during a WiFi-to-cellular handover doesn't leave the
+ // server stale for the rest of the listen.
+ if (_positionSyncFailures >= 3 &&
+ _noSessionSyncRetryAt != null &&
+ DateTime.now().isBefore(_noSessionSyncRetryAt!)) {
_lastServerSync = DateTime.now();
} else if (manualOffline) {
// Manual offline - local save only, no server sync
@@ -5868,17 +5872,12 @@ class AudioPlayerService extends ChangeNotifier {
if (ok) {
debugPrint('[Player] No-session sync succeeded');
_positionSyncFailures = 0;
+ _noSessionSyncRetryAt = null;
} else {
- _positionSyncFailures++;
- debugPrint(
- '[Player] No-session sync returned false (failures=$_positionSyncFailures)',
- );
+ _noteNoSessionSyncFailure('returned false');
}
} catch (e) {
- _positionSyncFailures++;
- debugPrint(
- '[Player] No-session sync error (failures=$_positionSyncFailures): $e',
- );
+ _noteNoSessionSyncFailure('error: $e');
}
}
} finally {
@@ -6243,7 +6242,33 @@ class AudioPlayerService extends ChangeNotifier {
bool _syncRecoveryInProgress = false;
bool _positionSyncInProgress = false;
int _positionSyncFailures = 0;
+ DateTime? _noSessionSyncRetryAt;
bool _recreatingSession = false;
+
+ void _noteNoSessionSyncFailure(String what) {
+ _positionSyncFailures++;
+ if (_positionSyncFailures >= 3) {
+ final waitSec = (60 << (_positionSyncFailures - 3)).clamp(60, 300);
+ _noSessionSyncRetryAt = DateTime.now().add(Duration(seconds: waitSec));
+ debugPrint(
+ '[Player] No-session sync $what (failures=$_positionSyncFailures), '
+ 'backing off ${waitSec}s',
+ );
+ } else {
+ debugPrint(
+ '[Player] No-session sync $what (failures=$_positionSyncFailures)',
+ );
+ }
+ }
+
+ /// 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() {
+ if (_positionSyncFailures == 0 && _noSessionSyncRetryAt == null) return;
+ debugPrint('[Player] Sync backoff cleared (failures=$_positionSyncFailures)');
+ _positionSyncFailures = 0;
+ _noSessionSyncRetryAt = null;
+ }
int _playbackGeneration = 0;
int? _cachedStartReconcileGeneration;
diff --git a/lib/services/local_session_service.dart b/lib/services/local_session_service.dart
index 765b0117..80672a5a 100644
--- a/lib/services/local_session_service.dart
+++ b/lib/services/local_session_service.dart
@@ -287,9 +287,19 @@ class LocalSessionService {
await _legacyFlush(api, _active!);
return true;
}
+ final ct = (_active!['currentTime'] as num?)?.toDouble() ?? 0;
+ if (!result.ok) {
+ debugPrint('[LocalSession] Push failed for ${_active!['id']} at ${ct.toStringAsFixed(0)}s');
+ } else if (_lastPushLogAt == null ||
+ clock().difference(_lastPushLogAt!) >= const Duration(minutes: 5)) {
+ _lastPushLogAt = clock();
+ debugPrint('[LocalSession] Pushed ${_active!['id']} at ${ct.toStringAsFixed(0)}s');
+ }
return result.ok;
}
+ DateTime? _lastPushLogAt;
+
/// Clear the active session (stop / finish / item change). When [pushed] is
/// false (offline, or the push failed) it's queued for replay; when true it's
/// already on the server so we just drop it.