Skip to content

A cross platform Audiobookshelf client for Android and iOS

Dart

Open ebooks the server marks supplementary instead of saying there is no ebook file

Barnabasa07a09337c49
diff --git a/lib/services/download_service.dart b/lib/services/download_service.dart
index 42eeff4a..f66aa935 100644
--- a/lib/services/download_service.dart
+++ b/lib/services/download_service.dart
@@ -1406,8 +1406,8 @@ class DownloadService extends ChangeNotifier {
// Pull the companion ebook into the offline cache too, so a downloaded
// book is fully readable offline. Fire-and-forget - never blocks or fails
// the audio download.
- final ebookFile = ((sessionData['libraryItem'] as Map<String, dynamic>?)?['media']
- as Map<String, dynamic>?)?['ebookFile'] as Map<String, dynamic>?;
+ final ebookFile =
+ resolveEbookFile(sessionData['libraryItem'] as Map<String, dynamic>?);
if (ebookFile != null) {
unawaited(_cacheEbookForOffline(api, apiItemId, ebookFile, title));
}
diff --git a/lib/services/ebook_cache.dart b/lib/services/ebook_cache.dart
index a01348e8..c7569473 100644
--- a/lib/services/ebook_cache.dart
+++ b/lib/services/ebook_cache.dart
@@ -17,6 +17,38 @@ String ebookExtFromFile(Map<String, dynamic> ebookFile) {
return name.contains('.') ? name.substring(name.lastIndexOf('.')).toLowerCase() : '';
}
+/// Formats the in-app readers can open. EPUB uses the full reader, PDF the
+/// dedicated viewer, and foliate-js covers MOBI/AZW3/CBZ. CBR is RAR-based
+/// and foliate-js can't read it, so it's recognized but stays download-only.
+const foliateEbookFormats = {'mobi', 'azw3', 'cbz'};
+const readableEbookFormats = {'epub', 'pdf', ...foliateEbookFormats};
+const allEbookFormats = {...readableEbookFormats, 'cbr'};
+
+/// The ebook to use for a library item: the server's primary (media.ebookFile)
+/// when set, otherwise the best supplementary ebook from libraryFiles - epub
+/// first (mirroring the server's own primary pick), then the first readable
+/// format, then any ebook file (a CBR can still be saved to device).
+/// Audiobooks-only libraries mark every ebook supplementary and never set a
+/// primary, so without this fallback those books always showed "no ebook".
+Map<String, dynamic>? resolveEbookFile(Map<String, dynamic>? item) {
+ if (item == null) return null;
+ final media = item['media'] as Map<String, dynamic>?;
+ final primary = media?['ebookFile'] as Map<String, dynamic>?;
+ if (primary != null) return primary;
+ Map<String, dynamic>? firstReadable;
+ Map<String, dynamic>? firstEbook;
+ for (final f in (item['libraryFiles'] as List<dynamic>? ?? const [])) {
+ if (f is! Map<String, dynamic>) continue;
+ final ext = ebookExtFromFile(f);
+ final fmt = ext.startsWith('.') ? ext.substring(1) : ext;
+ if (!allEbookFormats.contains(fmt)) continue;
+ if (fmt == 'epub') return f;
+ if (readableEbookFormats.contains(fmt)) firstReadable ??= f;
+ firstEbook ??= f;
+ }
+ return firstReadable ?? firstEbook;
+}
+
Future<Directory> _ebookCacheDir() async {
final base = await getApplicationSupportDirectory();
final dir = Directory('${base.path}/ebook_cache');
diff --git a/lib/widgets/absorbing_card.dart b/lib/widgets/absorbing_card.dart
index 439d67d6..9f5936fd 100644
--- a/lib/widgets/absorbing_card.dart
+++ b/lib/widgets/absorbing_card.dart
@@ -12,6 +12,8 @@ import '../services/audio_player_service.dart';
import '../services/download_service.dart';
import 'absorbing_shared.dart';
import 'ebook_router.dart';
+import 'overlay_toast.dart';
+import '../services/ebook_cache.dart';
import 'card_edge_progress_bar.dart';
import 'card_progress_bar.dart';
import 'card_playback_controls.dart';
@@ -249,9 +251,11 @@ class AbsorbingCardState extends State<AbsorbingCard> with AutomaticKeepAliveCli
final fullItem = await api.getLibraryItem(_itemId);
if (fullItem != null && mounted) {
final media = fullItem['media'] as Map<String, dynamic>? ?? {};
- // Cache ebookFile if the inline item didn't have it
+ // Cache ebookFile if the inline item didn't have it. resolveEbookFile
+ // also covers supplementary-only books (audiobooks-only libraries
+ // never set media.ebookFile).
if (inlineEbook == null) {
- final ef = media['ebookFile'] as Map<String, dynamic>?;
+ final ef = resolveEbookFile(fullItem);
if (ef != null) setState(() => _fetchedEbookFile = ef);
}
// Books: chapters at media level
@@ -1044,9 +1048,19 @@ class AbsorbingCardState extends State<AbsorbingCard> with AutomaticKeepAliveCli
);
}
- void _openEbookReader() {
- final ebookFile = _ebookFile;
- if (ebookFile == null) return; // CardButtons already toasts when no ebook
+ void _openEbookReader() async {
+ var ebookFile = _ebookFile;
+ if (ebookFile == null) {
+ // The initState fetch is one-shot and can fail silently (network blip),
+ // so give it another chance before declaring there's no ebook.
+ await _fetchChaptersIfNeeded();
+ ebookFile = _ebookFile;
+ }
+ if (!mounted) return;
+ if (ebookFile == null) {
+ showOverlayToast(context, 'No ebook file for this book', icon: Icons.menu_book_outlined);
+ return;
+ }
openEbookReader(context, itemId: _itemId, title: _title, ebookFile: ebookFile);
}
@@ -1181,7 +1195,6 @@ class AbsorbingCardState extends State<AbsorbingCard> with AutomaticKeepAliveCli
PlayerSettings.setCardButtonOrder(newOrder);
PlayerSettings.setCardButtonVisibleCount(newCount);
},
- hasEbook: _ebookFile != null,
isEbookPdf: _ebookExt == 'pdf',
onEbookTap: _openEbookReader,
);
diff --git a/lib/widgets/book_detail_sheet.dart b/lib/widgets/book_detail_sheet.dart
index f55b1050..e31c4959 100644
--- a/lib/widgets/book_detail_sheet.dart
+++ b/lib/widgets/book_detail_sheet.dart
@@ -436,7 +436,7 @@ class _BookDetailSheetContentState extends State<_BookDetailSheetContent> {
final progressData = lib.getProgressData(widget.itemId);
final isFinished = progressData?['isFinished'] == true;
final currentTime = (progressData?['currentTime'] as num?)?.toDouble() ?? 0;
- final ebookFile = media['ebookFile'] as Map<String, dynamic>?;
+ final ebookFile = resolveEbookFile(_item);
final isEbookOnly = PlayerSettings.isEbookOnly(_item!);
@@ -830,7 +830,7 @@ class _BookDetailSheetContentState extends State<_BookDetailSheetContent> {
final authorName = metadata['authorName'] as String? ?? '';
final duration = (media['duration'] as num?)?.toDouble() ?? 0;
final serverPath = _item!['path'] as String? ?? _item!['relPath'] as String? ?? '';
- final ebookFile = media['ebookFile'] as Map<String, dynamic>?;
+ final ebookFile = resolveEbookFile(_item);
final isEbookOnly = PlayerSettings.isEbookOnly(_item!);
final lib = context.watch<LibraryProvider>();
diff --git a/lib/widgets/card_buttons.dart b/lib/widgets/card_buttons.dart
index 1f016aad..cdcb39a7 100644
--- a/lib/widgets/card_buttons.dart
+++ b/lib/widgets/card_buttons.dart
@@ -1393,7 +1393,6 @@ class CardActionDelegate {
final VoidCallback removeFromAbsorbing;
final VoidCallback? onRemoveExtra;
final void Function(List<String>, int) onReorder;
- final bool hasEbook;
final bool isEbookPdf;
final VoidCallback? onEbookTap;
@@ -1423,7 +1422,6 @@ class CardActionDelegate {
required this.removeFromAbsorbing,
this.onRemoveExtra,
required this.onReorder,
- this.hasEbook = false,
this.isEbookPdf = false,
this.onEbookTap,
});
@@ -1660,13 +1658,9 @@ class CardActionDelegate {
icon: isEbookPdf ? Icons.picture_as_pdf_rounded : Icons.menu_book_rounded,
label: 'Read',
accent: accent, isActive: true, alwaysEnabled: true, large: large, compact: compact, iconsOnly: iconsOnly,
- onTap: () {
- if (hasEbook) {
- onEbookTap?.call();
- } else {
- showOverlayToast(context, 'No ebook file for this book', icon: Icons.menu_book_outlined);
- }
- },
+ // The card handles the no-ebook case itself (it retries the item
+ // fetch before toasting).
+ onTap: () => onEbookTap?.call(),
);
default:
return const SizedBox.shrink();
@@ -1839,11 +1833,7 @@ class CardActionDelegate {
label: 'Read', accent: accent,
onTap: () {
Navigator.pop(ctx);
- if (hasEbook) {
- onEbookTap?.call();
- } else {
- showOverlayToast(context, 'No ebook file for this book', icon: Icons.menu_book_outlined);
- }
+ onEbookTap?.call();
},
);
default:
diff --git a/lib/widgets/ebook_router.dart b/lib/widgets/ebook_router.dart
index 4744768a..ad8eaad4 100644
--- a/lib/widgets/ebook_router.dart
+++ b/lib/widgets/ebook_router.dart
@@ -15,15 +15,8 @@ String ebookExt(Map<String, dynamic>? ebookFile) {
return ext.startsWith('.') ? ext.substring(1) : ext;
}
-/// Formats the in-app reader can open. EPUB uses the full reader, PDF the
-/// dedicated PDF viewer, and foliate-js covers MOBI/AZW3/CBZ. These match the
-/// ebook types Audiobookshelf actually serves (epub, pdf, mobi, azw3, cbr, cbz).
-/// CBR is RAR-based and foliate-js can't read it, so it stays a download.
-const _foliateFormats = {'mobi', 'azw3', 'cbz'};
-final _readableFormats = {'epub', 'pdf', ..._foliateFormats};
-
bool canReadEbook(Map<String, dynamic>? ebookFile) =>
- ebookFile != null && _readableFormats.contains(ebookExt(ebookFile));
+ ebookFile != null && readableEbookFormats.contains(ebookExt(ebookFile));
/// Routes an ebook to the right reader for its format, or toasts when the
/// format isn't supported in-app.
@@ -39,7 +32,7 @@ void openEbookReader(
viewer = EbookReaderView(itemId: itemId, title: title, ebookFile: ebookFile);
} else if (ext == 'pdf') {
viewer = PdfReaderView(itemId: itemId, title: title, ebookFile: ebookFile);
- } else if (_foliateFormats.contains(ext)) {
+ } else if (foliateEbookFormats.contains(ext)) {
viewer = FoliateReaderView(itemId: itemId, title: title, ebookFile: ebookFile);
} else {
showOverlayToast(context, AppLocalizations.of(context)!.readerFormatUnsupported,
diff --git a/lib/widgets/expanded_card.dart b/lib/widgets/expanded_card.dart
index 99eed710..4d48e272 100644
--- a/lib/widgets/expanded_card.dart
+++ b/lib/widgets/expanded_card.dart
@@ -15,6 +15,8 @@ import 'card_progress_bar.dart';
import 'card_playback_controls.dart';
import 'card_buttons.dart';
import 'ebook_router.dart';
+import 'overlay_toast.dart';
+import '../services/ebook_cache.dart';
import '../main.dart' show colorSourceNotifier, useColorEverywhereNotifier, manualSeedNotifier, manualColorScheme;
// ─── Custom route: slide-up + fade ────────────────────────────
@@ -457,8 +459,10 @@ class _ExpandedCardState extends State<ExpandedCard> {
final fullItem = await api.getLibraryItem(_itemId);
if (fullItem != null && mounted) {
final media = fullItem['media'] as Map<String, dynamic>? ?? {};
+ // resolveEbookFile also covers supplementary-only books
+ // (audiobooks-only libraries never set media.ebookFile).
if (inlineEbook == null) {
- final ef = media['ebookFile'] as Map<String, dynamic>?;
+ final ef = resolveEbookFile(fullItem);
if (ef != null) setState(() => _fetchedEbookFile = ef);
}
// Books: chapters at media level
@@ -1157,7 +1161,6 @@ class _ExpandedCardState extends State<ExpandedCard> {
PlayerSettings.setCardButtonOrder(newOrder);
PlayerSettings.setCardButtonVisibleCount(newCount);
},
- hasEbook: _ebookFile != null,
isEbookPdf: _ebookExt == 'pdf',
onEbookTap: _openReader,
);
@@ -1169,9 +1172,19 @@ class _ExpandedCardState extends State<ExpandedCard> {
return ext.isEmpty ? null : ext;
}
- void _openReader() {
- final ef = _ebookFile;
- if (ef == null) return;
+ void _openReader() async {
+ var ef = _ebookFile;
+ if (ef == null) {
+ // The initState fetch is one-shot and can fail silently (network blip),
+ // so give it another chance before declaring there's no ebook.
+ await _fetchChaptersIfNeeded();
+ ef = _ebookFile;
+ }
+ if (!mounted) return;
+ if (ef == null) {
+ showOverlayToast(context, 'No ebook file for this book', icon: Icons.menu_book_outlined);
+ return;
+ }
openEbookReader(context, itemId: _itemId, title: _title, ebookFile: ef);
}