A cross platform Audiobookshelf client for Android and iOS
Dart
Fix CarPlay crashing when browsing the library
Barnabas5db543399bd4
diff --git a/lib/services/android_auto_service.dart b/lib/services/android_auto_service.dartindex 6aa8fd25..9fd83289 100644--- a/lib/services/android_auto_service.dart+++ b/lib/services/android_auto_service.dart@@ -1323,6 +1323,37 @@ class AndroidAutoService {}}+ /// Flatten the recursive prefix tree into a single ordered list of LEAF+ /// buckets (each <= [bucketThreshold] books). Used by CarPlay, where the+ /// recursive push drilldown isn't possible: CarPlay caps the navigation+ /// stack (~5 templates), so we show one bucket level then the books instead+ /// of pushing a template per letter the way Android Auto can.+ List<({String prefix, int count})> flattenedBookBuckets(List<AutoBookEntry> all) {+ final out = <({String prefix, int count})>[];+ void walk(String prefix) {+ final level = resolveBrowseLevel(all, prefix);+ if (level.books != null) {+ if (level.books!.isNotEmpty) {+ out.add((prefix: prefix, count: level.books!.length));+ }+ return;+ }+ for (final b in level.buckets!) {+ walk(b.prefix);+ }+ }++ walk('');+ return out;+ }++ /// All books whose title falls under [prefix] (a leaf bucket from+ /// [flattenedBookBuckets]). Empty prefix = the whole library.+ List<AutoBookEntry> booksForPrefix(List<AutoBookEntry> all, String prefix) {+ if (prefix.isEmpty) return all;+ return all.where((b) => _normTitle(b.title).startsWith(prefix)).toList();+ }+/// Android Auto children for a books browse level at [prefix] (empty = top).Future<List<MediaItem>> _fetchBooksAtPrefix(String libraryId, String prefix) async {final all = await fetchAllBooks(libraryId);diff --git a/lib/services/carplay_service.dart b/lib/services/carplay_service.dartindex 9750a0f2..54a382cf 100644--- a/lib/services/carplay_service.dart+++ b/lib/services/carplay_service.dart@@ -423,35 +423,33 @@ class CarPlayService {// ─── Books list ────────────────────────────────────────────────────- Future<CPListTemplate> _buildBooksList(String libraryId) =>- _buildBooksAtPrefix(libraryId, '');-- /// Recursive alphabetical drilldown: a leaf list of books, or deeper prefix- /// buckets when there are too many to show (so only one prefix's covers load- /// at a time). Mirrors the Android Auto side via the shared resolver.- Future<CPListTemplate> _buildBooksAtPrefix(String libraryId, String prefix) async {+ /// CarPlay books browse. Unlike Android Auto, CarPlay caps the navigation+ /// stack (~5 pushed templates), so a recursive letter drilldown (T -> Th ->+ /// The -> ...) pushes too deep and iOS kills the app. Instead we show ONE+ /// flat level of letter/prefix buckets (each <= bucketThreshold) and then+ /// the book list - a fixed depth of two pushes.+ Future<CPListTemplate> _buildBooksList(String libraryId) async {final api = await _autoService.getApi();final all = await _autoService.fetchAllBooks(libraryId);- final level = _autoService.resolveBrowseLevel(all, prefix);- final title = prefix.isEmpty ? 'Books' : prefix;- final books = level.books;- if (books != null) {- final items = books.map((e) => _playableListItem(e, api)).toList();+ // Small library: skip the bucket level, list the books directly.+ if (all.length <= AndroidAutoService.bucketThreshold) {+ final items = all.map((e) => _playableListItem(e, api)).toList();return CPListTemplate(sections: [CPListSection(items: items)],- title: title,+ title: 'Books',systemIcon: 'book.fill',);}- final items = level.buckets!.map((b) {+ final buckets = _autoService.flattenedBookBuckets(all);+ final items = buckets.map((b) {return CPListItem(- text: b.prefix,+ text: b.prefix.isEmpty ? '#' : b.prefix,detailText: '${b.count}',accessoryType: CPListItemAccessoryTypes.disclosureIndicator,onPress: (complete, self) async {- final template = await _buildBooksAtPrefix(libraryId, b.prefix);+ final template = await _buildBooksLeaf(libraryId, b.prefix);FlutterCarplay.push(template: template);complete();},@@ -459,7 +457,20 @@ class CarPlayService {}).toList();return CPListTemplate(sections: [CPListSection(items: items)],- title: title,+ title: 'Books',+ systemIcon: 'book.fill',+ );+ }++ /// Leaf book list for one flat bucket [prefix].+ Future<CPListTemplate> _buildBooksLeaf(String libraryId, String prefix) async {+ final api = await _autoService.getApi();+ final all = await _autoService.fetchAllBooks(libraryId);+ final books = _autoService.booksForPrefix(all, prefix);+ final items = books.map((e) => _playableListItem(e, api)).toList();+ return CPListTemplate(+ sections: [CPListSection(items: items)],+ title: prefix.isEmpty ? 'Books' : prefix,systemIcon: 'book.fill',);}diff --git a/pubspec.yaml b/pubspec.yamlindex 581f4fc7..a4a865f3 100644--- a/pubspec.yaml+++ b/pubspec.yaml@@ -1,7 +1,7 @@name: absorbdescription: An Audiobookshelf Clientpublish_to: 'none'-version: 1.8.97+168+version: 1.8.97+169environment:sdk: '>=3.1.0 <4.0.0'