Skip to content

A cross platform Audiobookshelf client for Android and iOS

Dart

Stop a headset or car play press restarting the episode that just finished

Barnabas9e5266c21d97
diff --git a/lib/services/home_widget_service.dart b/lib/services/home_widget_service.dart
index 6b0fb152..f6699a60 100644
--- a/lib/services/home_widget_service.dart
+++ b/lib/services/home_widget_service.dart
@@ -12,6 +12,7 @@ import 'package:shared_preferences/shared_preferences.dart';
import 'audio_player_service.dart';
import 'api_service.dart';
import 'download_service.dart';
+import 'progress_sync_service.dart';
import 'scoped_prefs.dart';
import 'user_account_service.dart';
import 'wear_player_service.dart';
@@ -192,6 +193,17 @@ class HomeWidgetService {
? stashedPosition
: null;
+ /// Whether the local progress record for the last-played item says it is
+ /// done: flagged finished, or saved within a second of the end, which is
+ /// the same cut-off the play path uses to start a book over.
+ static bool lastPlayedIsFinished(Map<String, dynamic>? local) {
+ if (local == null) return false;
+ if (local['isFinished'] == true) return true;
+ final duration = (local['duration'] as num?)?.toDouble() ?? 0;
+ final currentTime = (local['currentTime'] as num?)?.toDouble() ?? 0;
+ return duration > 0 && currentTime >= duration - 1.0;
+ }
+
/// The stash readers/writers below can run before [init] (the boot-time
/// engine adopt fires right after the player service starts), and the
/// HomeWidget plugin throws on iOS until it has been told the app group.
@@ -453,6 +465,23 @@ class HomeWidgetService {
final itemId = prefs.getString('widget_item_id');
debugPrint('[HomeWidget] play_pause: cold resume, itemId=$itemId');
if (itemId == null) return;
+ final episodeId = prefs.getString('widget_episode_id');
+
+ // The last-played marker still names an item after it finishes with
+ // nothing queued behind it. A headset or car play press then restarted
+ // it from the top (the play path treats a saved position at the end as
+ // "start over") and the first sync un-finished it on the server
+ // (GH #374). A finished item is nothing to resume.
+ final progressKey = episodeId != null ? '$itemId-$episodeId' : itemId;
+ if (lastPlayedIsFinished(
+ await ProgressSyncService().getLocal(progressKey),
+ )) {
+ debugPrint(
+ '[HomeWidget] play_pause: last played $progressKey is finished - '
+ 'nothing to resume',
+ );
+ return;
+ }
final api = _apiFromPrefs(prefs);
debugPrint(
@@ -460,8 +489,6 @@ class HomeWidgetService {
);
if (api == null) return;
- final episodeId = prefs.getString('widget_episode_id');
-
try {
// A downloaded item needs nothing from the server to start: the download
// record already carries title/author/duration/chapters, so play it now