Skip to content

A cross platform Audiobookshelf client for Android and iOS

Dart

Show your downloaded books in the Continue list in Android Auto and CarPlay when you're offline

Barnabasc4342f48bfe0
diff --git a/lib/services/android_auto_service.dart b/lib/services/android_auto_service.dart
index 4c04c7ee..94c8654f 100644
--- a/lib/services/android_auto_service.dart
+++ b/lib/services/android_auto_service.dart
@@ -497,6 +497,46 @@ class AndroidAutoService {
_downloaded = entries;
}
+ /// Build the Continue shelf from in-progress downloaded books when the server
+ /// is unreachable, so the user can resume in Android Auto / CarPlay without
+ /// digging through the Downloads tab (Discord request). Ordered to match the
+ /// in-app Continue: items the user is actively absorbing first (persisted
+ /// `absorbing_seen_ids`), then most recently played. Relies on `_downloaded`
+ /// already being populated (it always is by the time this runs).
+ Future<void> _buildOfflineContinueFromDownloads() async {
+ if (_downloaded.isEmpty) {
+ _continueListening = [];
+ return;
+ }
+ final psync = ProgressSyncService();
+ final absorbingOrder = await ScopedPrefs.getStringList('absorbing_seen_ids');
+ final absorbingIndex = <String, int>{};
+ for (var i = 0; i < absorbingOrder.length; i++) {
+ absorbingIndex.putIfAbsent(absorbingOrder[i], () => i);
+ }
+
+ // (absorbing order, last-played ms, entry); a download's id is already the
+ // same key the absorbing list uses (bookId, or showId-episodeId).
+ final ranked = <(int, int, AutoBookEntry)>[];
+ for (final entry in _downloaded) {
+ final local = await psync.getLocal(entry.id);
+ if (local == null) continue;
+ final pos = (local['currentTime'] as num?)?.toDouble() ?? 0;
+ final finished = local['isFinished'] as bool? ?? false;
+ if (finished || pos <= 0) continue; // only books in progress
+ final ts = (local['timestamp'] as num?)?.toInt() ?? 0;
+ final order = absorbingIndex[entry.id] ?? (1 << 30);
+ ranked.add((order, ts, entry));
+ }
+ ranked.sort((a, b) {
+ if (a.$1 != b.$1) return a.$1.compareTo(b.$1);
+ return b.$2.compareTo(a.$2);
+ });
+ _continueListening = ranked.map((e) => e.$3).toList();
+ debugPrint(
+ '[AutoBrowse] Offline Continue from downloads: ${_continueListening.length} in-progress');
+ }
+
Future<void> _refreshFromServer() async {
final api = await getApi();
if (api == null) return;
@@ -508,7 +548,7 @@ class AndroidAutoService {
// the user opens the app or connects to the head unit while their
// server is unreachable.
if (manualOffline || AudioPlayerService().knownOffline) {
- _continueListening = [];
+ await _buildOfflineContinueFromDownloads();
_recentlyAdded = [];
_libraries = [];
return;
@@ -638,8 +678,9 @@ class AndroidAutoService {
recentRanked.sort((a, b) => b.$1.compareTo(a.$1));
_recentlyAdded = recentRanked.map((e) => e.$2).toList();
} catch (e) {
- // Server unreachable — clear stale tabs so only Downloads shows offline
- _continueListening = [];
+ // Server unreachable - fall back to in-progress downloads for Continue so
+ // the user can still resume offline, and clear the rest.
+ await _buildOfflineContinueFromDownloads();
_recentlyAdded = [];
_libraries = [];
debugPrint('[AutoBrowse] Server fetch error: $e');