A cross platform Audiobookshelf client for Android and iOS
Dart
Run server browse refresh in background so CarPlay/AA show downloads instantly
Barnabasebf07511b66e
diff --git a/lib/services/android_auto_service.dart b/lib/services/android_auto_service.dartindex 58de553d..f1f1e2ed 100644--- a/lib/services/android_auto_service.dart+++ b/lib/services/android_auto_service.dart@@ -280,10 +280,17 @@ class AndroidAutoService {});}+ /// Called once the background server-side refresh completes (success or+ /// failure) so listeners can re-render their browse trees with the new+ /// data. CarPlayService sets this in init() to call refreshTemplates();+ /// AndroidAuto uses notifyChildrenChanged below independently.+ static void Function()? onServerDataChanged;+Future<void> refresh({bool force = false}) async {// Always populate downloads immediately — no server needed.- // This ensures Android Auto can show the Downloads tab even if the- // server is unreachable (e.g. no remote access, offline-only users).+ // This ensures Android Auto / CarPlay can show the Downloads tab even+ // if the server is unreachable (e.g. no remote access, offline-only+ // users).if (!_downloadsReady || force) {await _refreshDownloaded();_downloadsReady = true;@@ -297,8 +304,16 @@ class AndroidAutoService {_isRefreshing = true;debugPrint('[AutoBrowse] Refreshing browse tree...');+ // Kick the server fetch off in the background so callers (CarPlay scene+ // init, Android Auto media browser) don't block on a 15s `getLibraries`+ // timeout when the server is unreachable. The downloads pass above is+ // already populated; that's enough to render a useful browse tree+ // immediately. When the server data arrives (or fails) we fire the+ // listener hook so the UI re-renders with the fresh sections.+ unawaited(_refreshServerInBackground());+ }- // Server fetch is best-effort — don't block the browse tree+ Future<void> _refreshServerInBackground() async {try {await _refreshDownloaded(); // re-fetch in case downloads changedawait _refreshFromServer();@@ -316,14 +331,17 @@ class AndroidAutoService {debugPrint('[AutoBrowse] Server refresh failed (downloads still available): $e');} finally {_isRefreshing = false;- // Tell Android Auto's MediaBrowser to re-fetch the root browse tree.- // iOS CarPlay uses this service for data but doesn't have a MediaBrowser.if (Platform.isAndroid) {try {// ignore: deprecated_member_useawait AudioServiceBackground.notifyChildrenChanged(AutoMediaIds.root);} catch (_) {}}+ try {+ onServerDataChanged?.call();+ } catch (e) {+ debugPrint('[AutoBrowse] onServerDataChanged listener threw: $e');+ }}}diff --git a/lib/services/carplay_service.dart b/lib/services/carplay_service.dartindex c1c6950b..c093f91b 100644--- a/lib/services/carplay_service.dart+++ b/lib/services/carplay_service.dart@@ -25,6 +25,16 @@ class CarPlayService {_initialized = true;_flutterCarplay.addListenerOnConnectionChange(_onConnectionChange);debugPrint('[CarPlay] Initialized');+ // Re-render the root template when the background server refresh+ // completes. AutoBrowse.refresh() returns immediately after downloads+ // are populated and continues the server fetch in the background;+ // this hook is how we know to swap the downloads-only tree for the+ // full one (or the other way for offline → online recovery).+ AndroidAutoService.onServerDataChanged = () {+ if (!_initialized || _rootTemplate == null) return;+ debugPrint('[CarPlay] onServerDataChanged - rebuilding root template');+ refreshTemplates();+ };// Eagerly load auto browse data so the first CarPlay connect lands with// full content already cached. Without this, the user's first open would// either show empty Continue Listening / Library tabs or wait the full