Skip to content

A cross platform Audiobookshelf client for Android and iOS

Dart

Keep progress saves queued when they never reach the server instead of dropping them

Barnabasa53f2f3ea3b4
diff --git a/lib/services/api_service.dart b/lib/services/api_service.dart
index c817dada..4cb3e1e9 100644
--- a/lib/services/api_service.dart
+++ b/lib/services/api_service.dart
@@ -2198,7 +2198,9 @@ class ApiService {
/// false only to deliberately un-finish: the server answers that by
/// clearing the position to 0, same as the web UI's "mark as not finished",
/// so [currentTime] is not sent in that case.
- Future<void> updateProgress(
+ /// Returns false on a 5xx, where a retry could still land; network errors
+ /// still throw.
+ Future<bool> updateProgress(
String itemId, {
required double currentTime,
required double duration,
@@ -2214,7 +2216,7 @@ class ApiService {
body: jsonEncode({'isFinished': false}),
timeout: const Duration(seconds: 10));
debugPrint('[API] updateProgress unfinish $progressPath: ${unfinish.statusCode}');
- return;
+ return unfinish.statusCode < 500;
}
final body = jsonEncode({
'currentTime': currentTime,
@@ -2228,6 +2230,7 @@ class ApiService {
body: body,
timeout: const Duration(seconds: 10));
debugPrint('[API] updateProgress response: ${resp.statusCode} ${resp.body}');
+ return resp.statusCode < 500;
} catch (e) {
debugPrint('[API] updateProgress error: $e');
rethrow;
@@ -2430,7 +2433,9 @@ class ApiService {
/// Same [isFinished] contract as [updateProgress]: null leaves the flag out
/// so the server writes the percent, true marks finished, false un-finishes
/// and lets the server clear the position to 0.
- Future<void> updateEpisodeProgress(
+ /// Returns false when it never reached the server (network error or 5xx),
+ /// so a queued save can stay queued.
+ Future<bool> updateEpisodeProgress(
String itemId,
String episodeId, {
required double currentTime,
@@ -2444,7 +2449,7 @@ class ApiService {
body: jsonEncode({'isFinished': false}),
timeout: const Duration(seconds: 10));
debugPrint('[API] updateEpisodeProgress unfinish $episodeId: ${unfinish.statusCode}');
- return;
+ return unfinish.statusCode < 500;
}
final resp = await _authPatch(url,
body: jsonEncode({
@@ -2457,8 +2462,10 @@ class ApiService {
if (resp.statusCode != 200) {
debugPrint('[API] updateEpisodeProgress $episodeId: HTTP ${resp.statusCode}');
}
+ return resp.statusCode < 500;
} catch (e) {
debugPrint('[API] updateEpisodeProgress error: $e');
+ return false;
}
}
diff --git a/lib/services/progress_sync_service.dart b/lib/services/progress_sync_service.dart
index bc1d0750..78484012 100644
--- a/lib/services/progress_sync_service.dart
+++ b/lib/services/progress_sync_service.dart
@@ -217,12 +217,16 @@ class ProgressSyncService {
duration: duration,
);
} else {
- await api.updateProgress(
+ final ok = await api.updateProgress(
itemId,
currentTime: currentTime,
duration: await serverTrustedDuration(api, itemId, duration),
isFinished: isFinished ? true : null,
);
+ if (!ok) {
+ debugPrint('[Sync] Server did not take $itemId - keeping it pending');
+ return false;
+ }
}
final pendingList = await ScopedPrefs.getStringList('pending_syncs');
@@ -348,21 +352,29 @@ class ProgressSyncService {
final localFinished = data['isFinished'] as bool? ?? false;
final syncDuration =
await serverTrustedDuration(api, itemId, localDuration);
+ final bool pushed;
if (episodeId != null) {
- await api.updateEpisodeProgress(
+ pushed = await api.updateEpisodeProgress(
apiItemId, episodeId,
currentTime: localTime,
duration: syncDuration,
isFinished: localFinished ? true : null,
);
} else {
- await api.updateProgress(
+ pushed = await api.updateProgress(
apiItemId,
currentTime: localTime,
duration: syncDuration,
isFinished: localFinished ? true : null,
);
}
+ if (!pushed) {
+ // Dropping it as flushed is how a refused push lost a position
+ // overnight: the next start resumed from the stale spot.
+ debugPrint('[Sync] Push for $itemId did not reach the server - keeping it for the next flush');
+ _consecutiveFailures++;
+ break;
+ }
debugPrint('[Sync] Flushed $itemId via progress update: ${localTime}s');
// Reset backoff on success - a successful response proves
// the server is reachable, so resume normal sync behaviour.