Skip to content

A cross platform Audiobookshelf client for Android and iOS

Dart

Make resetting a podcast episode actually clear it on the server instead of leaving the old percent behind

Barnabasc45beaa37a8c
diff --git a/lib/services/api_service.dart b/lib/services/api_service.dart
index 151be390..1cffd86b 100644
--- a/lib/services/api_service.dart
+++ b/lib/services/api_service.dart
@@ -2246,8 +2246,10 @@ class ApiService {
);
}
- /// Reset progress to zero.
- Future<bool> resetProgress(String itemId, double duration) async {
+ /// Reset progress to zero. [progressId] is the server's progress record id
+ /// when one is known; the delete route only accepts that.
+ Future<bool> resetProgress(String itemId, double duration,
+ {String? progressId}) async {
try {
final progressPath = itemId.length > 36
? '${itemId.substring(0, 36)}/${itemId.substring(37)}'
@@ -2256,10 +2258,7 @@ class ApiService {
final apiItemId = isCompound ? itemId.substring(0, 36) : itemId;
final episodeId = isCompound ? itemId.substring(37) : null;
- // DELETE progress entry
- await _authDelete(
- Uri.parse('$_cleanBaseUrl/api/me/progress/$progressPath'),
- timeout: const Duration(seconds: 10));
+ if (progressId != null) await deleteMediaProgress(progressId);
// Start session at 0 and close — forces server to update position
final sessionData = await startPlaybackSession(apiItemId, episodeId: episodeId);
@@ -2404,15 +2403,47 @@ class ApiService {
}
}
- /// DELETE /api/me/progress/:itemId/:episodeId
- Future<bool> deleteEpisodeProgress(String itemId, String episodeId) async {
+ /// DELETE /api/me/progress/:progressId
+ /// The server's delete route takes the progress record's own id (from the
+ /// user's mediaProgress list), not the item or episode id.
+ Future<bool> deleteMediaProgress(String progressId) async {
try {
final resp = await _authDelete(
- Uri.parse('$_cleanBaseUrl/api/me/progress/$itemId/$episodeId'),
+ Uri.parse('$_cleanBaseUrl/api/me/progress/$progressId'),
timeout: const Duration(seconds: 10));
+ debugPrint('[API] deleteMediaProgress $progressId: ${resp.statusCode}');
return resp.statusCode >= 200 && resp.statusCode < 300;
} catch (e) {
- debugPrint('[API] deleteEpisodeProgress error: $e');
+ debugPrint('[API] deleteMediaProgress error: $e');
+ return false;
+ }
+ }
+
+ /// Zero an episode's progress in place when there is no record id to delete.
+ /// ABS ignores `progress` in a PATCH that also carries `isFinished`, and only
+ /// zeroes an in-progress record when `isFinished` is absent, so this goes in
+ /// two steps: clear the finished flag, then write the zero position.
+ Future<bool> zeroEpisodeProgress(
+ String itemId,
+ String episodeId, {
+ required double duration,
+ }) async {
+ try {
+ final url = Uri.parse('$_cleanBaseUrl/api/me/progress/$itemId/$episodeId');
+ final unfinish = await _authPatch(url,
+ body: jsonEncode({'isFinished': false}),
+ timeout: const Duration(seconds: 10));
+ final zero = await _authPatch(url,
+ body: jsonEncode({
+ 'currentTime': 0,
+ 'duration': duration,
+ 'progress': 0,
+ }),
+ timeout: const Duration(seconds: 10));
+ debugPrint('[API] zeroEpisodeProgress: unfinish=${unfinish.statusCode} zero=${zero.statusCode}');
+ return unfinish.statusCode == 200 && zero.statusCode == 200;
+ } catch (e) {
+ debugPrint('[API] zeroEpisodeProgress error: $e');
return false;
}
}
diff --git a/lib/widgets/book_detail_sheet.dart b/lib/widgets/book_detail_sheet.dart
index 6f5f86fd..39e8fe2d 100644
--- a/lib/widgets/book_detail_sheet.dart
+++ b/lib/widgets/book_detail_sheet.dart
@@ -2367,7 +2367,13 @@ class _BookDetailSheetContentState extends State<_BookDetailSheetContent> {
await ProgressSyncService().deleteLocal(widget.itemId);
// Reset server progress (PATCH to zero + hide from continue listening)
- final serverSuccess = await api.resetProgress(widget.itemId, duration);
+ String? progressId;
+ if (context.mounted) {
+ final data = context.read<LibraryProvider>().getProgressData(widget.itemId);
+ progressId = data?['id'] as String?;
+ }
+ final serverSuccess =
+ await api.resetProgress(widget.itemId, duration, progressId: progressId);
// Clear from library provider (mark as reset — forces 0 progress)
if (context.mounted) context.read<LibraryProvider>().resetProgressFor(widget.itemId);
diff --git a/lib/widgets/episode_detail_sheet.dart b/lib/widgets/episode_detail_sheet.dart
index 1cbad0af..46ffa59a 100644
--- a/lib/widgets/episode_detail_sheet.dart
+++ b/lib/widgets/episode_detail_sheet.dart
@@ -805,15 +805,16 @@ class _EpisodeDetailSheetState extends State<EpisodeDetailSheet> {
}
final compoundKey = '$_itemId-$_episodeId';
+ final progressId = context
+ .read<LibraryProvider>()
+ .getEpisodeProgressData(_itemId, _episodeId)?['id'] as String?;
await ProgressSyncService().deleteLocal(compoundKey);
- final ok = await api.deleteEpisodeProgress(_itemId, _episodeId);
- // Mark as unfinished with zero progress on the server
- await api.updateEpisodeProgress(
- _itemId, _episodeId,
- currentTime: 0,
- duration: _duration,
- isFinished: false,
- );
+ // Deleting the record is what the web UI does. Fall back to zeroing it in
+ // place when there is no record id to delete or the delete is refused.
+ var ok = progressId != null && await api.deleteMediaProgress(progressId);
+ if (!ok) {
+ ok = await api.zeroEpisodeProgress(_itemId, _episodeId, duration: _duration);
+ }
if (context.mounted) {
context.read<LibraryProvider>().resetProgressFor(compoundKey);