Skip to content

A cross platform Audiobookshelf client for Android and iOS

Dart

Add a book preview button and show audio tracks and library files on the detail page

Barnabas099dbfb8e7e2
diff --git a/lib/l10n/app_en.arb b/lib/l10n/app_en.arb
index 9b2d16d4..108409af 100644
--- a/lib/l10n/app_en.arb
+++ b/lib/l10n/app_en.arb
@@ -1353,6 +1353,22 @@
}
}
},
+ "audioTracksCount": "Audio Tracks ({count})",
+ "@audioTracksCount": {
+ "placeholders": {
+ "count": {
+ "type": "int"
+ }
+ }
+ },
+ "libraryFilesCount": "Library Files ({count})",
+ "@libraryFilesCount": {
+ "placeholders": {
+ "count": {
+ "type": "int"
+ }
+ }
+ },
"chapters": "Chapters",
"noChaptersBook": "This book has no chapters",
"noChaptersPodcast": "This podcast has no chapters",
@@ -3947,6 +3963,7 @@
"bookmarkListen": "Listen",
"bookmarkPause": "Pause",
"bookmarkPreviewFailed": "Couldn't play this spot.",
+ "previewSample": "Preview",
"clipExport": "Export clip",
"clipJumpToStart": "Jump to start",
diff --git a/lib/l10n/app_localizations.dart b/lib/l10n/app_localizations.dart
index cfa7c54a..3aa7b6a5 100644
--- a/lib/l10n/app_localizations.dart
+++ b/lib/l10n/app_localizations.dart
@@ -4996,6 +4996,18 @@ abstract class AppLocalizations {
/// **'Chapters ({count})'**
String chaptersCount(int count);
+ /// No description provided for @audioTracksCount.
+ ///
+ /// In en, this message translates to:
+ /// **'Audio Tracks ({count})'**
+ String audioTracksCount(int count);
+
+ /// No description provided for @libraryFilesCount.
+ ///
+ /// In en, this message translates to:
+ /// **'Library Files ({count})'**
+ String libraryFilesCount(int count);
+
/// No description provided for @chapters.
///
/// In en, this message translates to:
@@ -11577,6 +11589,12 @@ abstract class AppLocalizations {
/// **'Couldn\'t play this spot.'**
String get bookmarkPreviewFailed;
+ /// No description provided for @previewSample.
+ ///
+ /// In en, this message translates to:
+ /// **'Preview'**
+ String get previewSample;
+
/// No description provided for @clipExport.
///
/// In en, this message translates to:
diff --git a/lib/l10n/app_localizations_de.dart b/lib/l10n/app_localizations_de.dart
index bc1c6437..d0aaf4dd 100644
--- a/lib/l10n/app_localizations_de.dart
+++ b/lib/l10n/app_localizations_de.dart
@@ -2765,6 +2765,16 @@ class AppLocalizationsDe extends AppLocalizations {
return 'Kapitel ($count)';
}
+ @override
+ String audioTracksCount(int count) {
+ return 'Audio Tracks ($count)';
+ }
+
+ @override
+ String libraryFilesCount(int count) {
+ return 'Library Files ($count)';
+ }
+
@override
String get chapters => 'Kapitel';
@@ -6751,6 +6761,9 @@ class AppLocalizationsDe extends AppLocalizations {
@override
String get bookmarkPreviewFailed => 'Stelle konnte nicht wiedergeben werden.';
+ @override
+ String get previewSample => 'Preview';
+
@override
String get clipExport => 'Clip exportieren';
diff --git a/lib/l10n/app_localizations_el.dart b/lib/l10n/app_localizations_el.dart
index 9aaacda6..a9712263 100644
--- a/lib/l10n/app_localizations_el.dart
+++ b/lib/l10n/app_localizations_el.dart
@@ -2731,6 +2731,16 @@ class AppLocalizationsEl extends AppLocalizations {
return 'Chapters ($count)';
}
+ @override
+ String audioTracksCount(int count) {
+ return 'Audio Tracks ($count)';
+ }
+
+ @override
+ String libraryFilesCount(int count) {
+ return 'Library Files ($count)';
+ }
+
@override
String get chapters => 'Chapters';
@@ -6656,6 +6666,9 @@ class AppLocalizationsEl extends AppLocalizations {
@override
String get bookmarkPreviewFailed => 'Couldn\'t play this spot.';
+ @override
+ String get previewSample => 'Preview';
+
@override
String get clipExport => 'Export clip';
diff --git a/lib/l10n/app_localizations_en.dart b/lib/l10n/app_localizations_en.dart
index 769db5e4..3cc268ae 100644
--- a/lib/l10n/app_localizations_en.dart
+++ b/lib/l10n/app_localizations_en.dart
@@ -2731,6 +2731,16 @@ class AppLocalizationsEn extends AppLocalizations {
return 'Chapters ($count)';
}
+ @override
+ String audioTracksCount(int count) {
+ return 'Audio Tracks ($count)';
+ }
+
+ @override
+ String libraryFilesCount(int count) {
+ return 'Library Files ($count)';
+ }
+
@override
String get chapters => 'Chapters';
@@ -6656,6 +6666,9 @@ class AppLocalizationsEn extends AppLocalizations {
@override
String get bookmarkPreviewFailed => 'Couldn\'t play this spot.';
+ @override
+ String get previewSample => 'Preview';
+
@override
String get clipExport => 'Export clip';
diff --git a/lib/l10n/app_localizations_es.dart b/lib/l10n/app_localizations_es.dart
index 2a003a0b..91775a47 100644
--- a/lib/l10n/app_localizations_es.dart
+++ b/lib/l10n/app_localizations_es.dart
@@ -2733,6 +2733,16 @@ class AppLocalizationsEs extends AppLocalizations {
return 'Chapters ($count)';
}
+ @override
+ String audioTracksCount(int count) {
+ return 'Audio Tracks ($count)';
+ }
+
+ @override
+ String libraryFilesCount(int count) {
+ return 'Library Files ($count)';
+ }
+
@override
String get chapters => 'Chapters';
@@ -6658,6 +6668,9 @@ class AppLocalizationsEs extends AppLocalizations {
@override
String get bookmarkPreviewFailed => 'Couldn\'t play this spot.';
+ @override
+ String get previewSample => 'Preview';
+
@override
String get clipExport => 'Export clip';
diff --git a/lib/l10n/app_localizations_fr.dart b/lib/l10n/app_localizations_fr.dart
index 6edb26ad..ae53f7f7 100644
--- a/lib/l10n/app_localizations_fr.dart
+++ b/lib/l10n/app_localizations_fr.dart
@@ -2751,6 +2751,16 @@ class AppLocalizationsFr extends AppLocalizations {
return 'Chapitres ($count)';
}
+ @override
+ String audioTracksCount(int count) {
+ return 'Audio Tracks ($count)';
+ }
+
+ @override
+ String libraryFilesCount(int count) {
+ return 'Library Files ($count)';
+ }
+
@override
String get chapters => 'Chapitres';
@@ -6676,6 +6686,9 @@ class AppLocalizationsFr extends AppLocalizations {
@override
String get bookmarkPreviewFailed => 'Couldn\'t play this spot.';
+ @override
+ String get previewSample => 'Preview';
+
@override
String get clipExport => 'Export clip';
diff --git a/lib/l10n/app_localizations_it.dart b/lib/l10n/app_localizations_it.dart
index e1e25b7b..96020903 100644
--- a/lib/l10n/app_localizations_it.dart
+++ b/lib/l10n/app_localizations_it.dart
@@ -2731,6 +2731,16 @@ class AppLocalizationsIt extends AppLocalizations {
return 'Chapters ($count)';
}
+ @override
+ String audioTracksCount(int count) {
+ return 'Audio Tracks ($count)';
+ }
+
+ @override
+ String libraryFilesCount(int count) {
+ return 'Library Files ($count)';
+ }
+
@override
String get chapters => 'Chapters';
@@ -6656,6 +6666,9 @@ class AppLocalizationsIt extends AppLocalizations {
@override
String get bookmarkPreviewFailed => 'Couldn\'t play this spot.';
+ @override
+ String get previewSample => 'Preview';
+
@override
String get clipExport => 'Export clip';
diff --git a/lib/l10n/app_localizations_ja.dart b/lib/l10n/app_localizations_ja.dart
index 25229ea4..933ffad4 100644
--- a/lib/l10n/app_localizations_ja.dart
+++ b/lib/l10n/app_localizations_ja.dart
@@ -2731,6 +2731,16 @@ class AppLocalizationsJa extends AppLocalizations {
return 'Chapters ($count)';
}
+ @override
+ String audioTracksCount(int count) {
+ return 'Audio Tracks ($count)';
+ }
+
+ @override
+ String libraryFilesCount(int count) {
+ return 'Library Files ($count)';
+ }
+
@override
String get chapters => 'Chapters';
@@ -6656,6 +6666,9 @@ class AppLocalizationsJa extends AppLocalizations {
@override
String get bookmarkPreviewFailed => 'Couldn\'t play this spot.';
+ @override
+ String get previewSample => 'Preview';
+
@override
String get clipExport => 'Export clip';
diff --git a/lib/l10n/app_localizations_no.dart b/lib/l10n/app_localizations_no.dart
index b63eae3a..171a5754 100644
--- a/lib/l10n/app_localizations_no.dart
+++ b/lib/l10n/app_localizations_no.dart
@@ -2731,6 +2731,16 @@ class AppLocalizationsNo extends AppLocalizations {
return 'Chapters ($count)';
}
+ @override
+ String audioTracksCount(int count) {
+ return 'Audio Tracks ($count)';
+ }
+
+ @override
+ String libraryFilesCount(int count) {
+ return 'Library Files ($count)';
+ }
+
@override
String get chapters => 'Chapters';
@@ -6656,6 +6666,9 @@ class AppLocalizationsNo extends AppLocalizations {
@override
String get bookmarkPreviewFailed => 'Couldn\'t play this spot.';
+ @override
+ String get previewSample => 'Preview';
+
@override
String get clipExport => 'Export clip';
diff --git a/lib/l10n/app_localizations_pt.dart b/lib/l10n/app_localizations_pt.dart
index aec3119d..02c6d026 100644
--- a/lib/l10n/app_localizations_pt.dart
+++ b/lib/l10n/app_localizations_pt.dart
@@ -2731,6 +2731,16 @@ class AppLocalizationsPt extends AppLocalizations {
return 'Chapters ($count)';
}
+ @override
+ String audioTracksCount(int count) {
+ return 'Audio Tracks ($count)';
+ }
+
+ @override
+ String libraryFilesCount(int count) {
+ return 'Library Files ($count)';
+ }
+
@override
String get chapters => 'Chapters';
@@ -6656,6 +6666,9 @@ class AppLocalizationsPt extends AppLocalizations {
@override
String get bookmarkPreviewFailed => 'Couldn\'t play this spot.';
+ @override
+ String get previewSample => 'Preview';
+
@override
String get clipExport => 'Export clip';
diff --git a/lib/l10n/app_localizations_ro.dart b/lib/l10n/app_localizations_ro.dart
index e669a084..1804fea9 100644
--- a/lib/l10n/app_localizations_ro.dart
+++ b/lib/l10n/app_localizations_ro.dart
@@ -2731,6 +2731,16 @@ class AppLocalizationsRo extends AppLocalizations {
return 'Chapters ($count)';
}
+ @override
+ String audioTracksCount(int count) {
+ return 'Audio Tracks ($count)';
+ }
+
+ @override
+ String libraryFilesCount(int count) {
+ return 'Library Files ($count)';
+ }
+
@override
String get chapters => 'Chapters';
@@ -6656,6 +6666,9 @@ class AppLocalizationsRo extends AppLocalizations {
@override
String get bookmarkPreviewFailed => 'Couldn\'t play this spot.';
+ @override
+ String get previewSample => 'Preview';
+
@override
String get clipExport => 'Export clip';
diff --git a/lib/l10n/app_localizations_ru.dart b/lib/l10n/app_localizations_ru.dart
index 3a908f3a..66891b6d 100644
--- a/lib/l10n/app_localizations_ru.dart
+++ b/lib/l10n/app_localizations_ru.dart
@@ -2735,6 +2735,16 @@ class AppLocalizationsRu extends AppLocalizations {
return 'Chapters ($count)';
}
+ @override
+ String audioTracksCount(int count) {
+ return 'Audio Tracks ($count)';
+ }
+
+ @override
+ String libraryFilesCount(int count) {
+ return 'Library Files ($count)';
+ }
+
@override
String get chapters => 'Chapters';
@@ -6661,6 +6671,9 @@ class AppLocalizationsRu extends AppLocalizations {
@override
String get bookmarkPreviewFailed => 'Не удалось воспроизвести это место.';
+ @override
+ String get previewSample => 'Preview';
+
@override
String get clipExport => 'Export clip';
diff --git a/lib/l10n/app_localizations_zh.dart b/lib/l10n/app_localizations_zh.dart
index d6a35891..4ba7b410 100644
--- a/lib/l10n/app_localizations_zh.dart
+++ b/lib/l10n/app_localizations_zh.dart
@@ -2670,6 +2670,16 @@ class AppLocalizationsZh extends AppLocalizations {
return '章节 ($count)';
}
+ @override
+ String audioTracksCount(int count) {
+ return 'Audio Tracks ($count)';
+ }
+
+ @override
+ String libraryFilesCount(int count) {
+ return 'Library Files ($count)';
+ }
+
@override
String get chapters => '章节';
@@ -6564,6 +6574,9 @@ class AppLocalizationsZh extends AppLocalizations {
@override
String get bookmarkPreviewFailed => 'Couldn\'t play this spot.';
+ @override
+ String get previewSample => 'Preview';
+
@override
String get clipExport => 'Export clip';
diff --git a/lib/widgets/book_detail_sheet.dart b/lib/widgets/book_detail_sheet.dart
index 1ecd91b4..ac40fa82 100644
--- a/lib/widgets/book_detail_sheet.dart
+++ b/lib/widgets/book_detail_sheet.dart
@@ -24,6 +24,7 @@ import '../providers/library_provider.dart';
import '../services/audio_player_service.dart';
import 'card_buttons.dart';
import '../services/api_service.dart';
+import '../services/bookmark_preview_player.dart';
import '../services/bookmark_service.dart';
import '../services/download_service.dart';
import '../services/ebook_cache.dart';
@@ -98,6 +99,9 @@ class _BookDetailSheetContentState extends State<_BookDetailSheetContent> {
bool _isLoading = true;
bool _chaptersExpanded = false;
bool _bookmarksExpanded = false;
+ bool _tracksExpanded = false;
+ bool _filesExpanded = false;
+ BookmarkPreviewPlayer? _preview;
List<Bookmark> _bookmarks = [];
bool _isAbsorbing = false;
bool _hasLocalOverride = false;
@@ -146,6 +150,8 @@ class _BookDetailSheetContentState extends State<_BookDetailSheetContent> {
void dispose() {
SocketService().removeItemUpdatedListener(_onSocketItemUpdated);
_liveRefreshDebounce?.cancel();
+ _preview?.removeListener(_onPreviewChanged);
+ _preview?.dispose();
super.dispose();
}
@@ -413,6 +419,8 @@ class _BookDetailSheetContentState extends State<_BookDetailSheetContent> {
final media = _item!['media'] as Map<String, dynamic>? ?? {};
final metadata = media['metadata'] as Map<String, dynamic>? ?? {};
final chapters = media['chapters'] as List<dynamic>? ?? [];
+ final audioFiles = media['audioFiles'] as List<dynamic>? ?? [];
+ final libraryFiles = _item!['libraryFiles'] as List<dynamic>? ?? [];
final title = metadata['title'] as String? ?? l.unknown;
final authorName = metadata['authorName'] as String? ?? '';
final descRaw = metadata['description'] as String? ?? '';
@@ -439,6 +447,9 @@ class _BookDetailSheetContentState extends State<_BookDetailSheetContent> {
final ebookFile = resolveEbookFile(_item);
final isEbookOnly = PlayerSettings.isEbookOnly(_item!);
+ // Preview only makes sense before the book has been started. The 60s
+ // grace means an accidental tap-and-close doesn't hide it forever.
+ final showPreview = !isEbookOnly && !isFinished && currentTime < 60 && duration > 0;
return ListView(controller: widget.scrollController, padding: EdgeInsets.fromLTRB(20, 8, 20, 32 + MediaQuery.of(context).viewPadding.bottom), children: [
Center(child: Container(width: 40, height: 4, margin: const EdgeInsets.only(bottom: 16),
@@ -699,6 +710,30 @@ class _BookDetailSheetContentState extends State<_BookDetailSheetContent> {
)),
],
]),
+ if (showPreview) ...[
+ const SizedBox(height: 8),
+ GestureDetector(
+ onTap: () => _togglePreview(context, chapters, duration),
+ child: Container(
+ height: 36,
+ decoration: BoxDecoration(
+ color: cs.onSurface.withValues(alpha: 0.06),
+ borderRadius: BorderRadius.circular(14),
+ border: Border.all(color: cs.onSurface.withValues(alpha: 0.08)),
+ ),
+ child: Row(mainAxisAlignment: MainAxisAlignment.center, children: [
+ if (_preview?.isLoading ?? false)
+ SizedBox(width: 16, height: 16, child: CircularProgressIndicator(strokeWidth: 2, color: cs.onSurfaceVariant))
+ else
+ Icon((_preview?.isPlaying ?? false) ? Icons.stop_rounded : Icons.headphones_rounded,
+ size: 16, color: cs.onSurfaceVariant),
+ const SizedBox(width: 6),
+ Text((_preview?.isPlaying ?? false) ? l.chapterStopPreview : l.previewSample,
+ style: TextStyle(color: cs.onSurfaceVariant, fontSize: 12, fontWeight: FontWeight.w500)),
+ ]),
+ ),
+ ),
+ ],
// More button below primary row
const SizedBox(height: 8),
GestureDetector(
@@ -812,6 +847,45 @@ class _BookDetailSheetContentState extends State<_BookDetailSheetContent> {
])),
]));
})]],
+ if (audioFiles.isNotEmpty) ...[const SizedBox(height: 16),
+ GestureDetector(onTap: () => setState(() => _tracksExpanded = !_tracksExpanded),
+ child: Row(children: [
+ Text(l.audioTracksCount(audioFiles.length), style: tt.titleSmall?.copyWith(color: cs.onSurfaceVariant, fontWeight: FontWeight.w600)),
+ const Spacer(), Icon(_tracksExpanded ? Icons.expand_less : Icons.expand_more, color: cs.onSurface.withValues(alpha: 0.3), size: 20)])),
+ if (_tracksExpanded) ...[const SizedBox(height: 8),
+ ...audioFiles.asMap().entries.map((e) {
+ final af = e.value as Map<String, dynamic>;
+ final afMeta = af['metadata'] as Map<String, dynamic>? ?? {};
+ final trackDuration = (af['duration'] as num?)?.toDouble() ?? 0;
+ return Padding(padding: const EdgeInsets.symmetric(vertical: 3),
+ child: Row(children: [
+ SizedBox(width: 28, child: Text('${(af['index'] as num?)?.toInt() ?? e.key + 1}', style: tt.labelSmall?.copyWith(color: cs.onSurface.withValues(alpha: 0.3)))),
+ Expanded(child: Text(afMeta['filename'] as String? ?? '', maxLines: 1, overflow: TextOverflow.ellipsis, style: tt.bodySmall?.copyWith(color: cs.onSurface.withValues(alpha: 0.6)))),
+ if (trackDuration > 0)
+ Text(formatHm(trackDuration), style: tt.labelSmall?.copyWith(color: cs.onSurface.withValues(alpha: 0.3))),
+ ]));
+ })]],
+ if (libraryFiles.isNotEmpty) ...[const SizedBox(height: 16),
+ GestureDetector(onTap: () => setState(() => _filesExpanded = !_filesExpanded),
+ child: Row(children: [
+ Text(l.libraryFilesCount(libraryFiles.length), style: tt.titleSmall?.copyWith(color: cs.onSurfaceVariant, fontWeight: FontWeight.w600)),
+ const Spacer(), Icon(_filesExpanded ? Icons.expand_less : Icons.expand_more, color: cs.onSurface.withValues(alpha: 0.3), size: 20)])),
+ if (_filesExpanded) ...[const SizedBox(height: 8),
+ ...libraryFiles.map((f) {
+ final lf = f as Map<String, dynamic>;
+ final lfMeta = lf['metadata'] as Map<String, dynamic>? ?? {};
+ final size = (lfMeta['size'] as num?)?.toInt() ?? 0;
+ return Padding(padding: const EdgeInsets.symmetric(vertical: 3),
+ child: Row(crossAxisAlignment: CrossAxisAlignment.start, children: [
+ Expanded(child: Column(crossAxisAlignment: CrossAxisAlignment.start, children: [
+ Text(lfMeta['relPath'] as String? ?? lfMeta['filename'] as String? ?? '', maxLines: 2, overflow: TextOverflow.ellipsis, style: tt.bodySmall?.copyWith(color: cs.onSurface.withValues(alpha: 0.6))),
+ if ((lf['fileType'] as String?)?.isNotEmpty ?? false)
+ Text(lf['fileType'] as String, style: tt.labelSmall?.copyWith(color: cs.onSurface.withValues(alpha: 0.35))),
+ ])),
+ if (size > 0) ...[const SizedBox(width: 8),
+ Text(_fmtSize(size), style: tt.labelSmall?.copyWith(color: cs.onSurface.withValues(alpha: 0.3)))],
+ ]));
+ })]],
]);
}
@@ -1827,7 +1901,56 @@ class _BookDetailSheetContentState extends State<_BookDetailSheetContent> {
);
}
+ /// Where a preview should start. Front matter (credits, dedication, intro)
+ /// is always short, so the first chapter long enough to be real prose is the
+ /// sample point - capped to the first 10% of the book so a preview can never
+ /// spoil. Books without a usable chapter land shortly past the credits.
+ double _previewStartSeconds(List<dynamic> chapters, double duration) {
+ const minRealChapter = 300.0;
+ const fallback = 90.0;
+ final clampedFallback = duration * 0.25 < fallback ? duration * 0.25 : fallback;
+ final spoilerCap = duration * 0.10;
+ for (final c in chapters) {
+ if (c is! Map<String, dynamic>) continue;
+ final start = (c['start'] as num?)?.toDouble() ?? 0;
+ final end = (c['end'] as num?)?.toDouble() ?? 0;
+ if (end - start >= minRealChapter && start <= spoilerCap) {
+ // A qualifying chapter at 0:00 means the credits are baked into it
+ // (single-file books) - nudge past them instead.
+ return start == 0 ? clampedFallback : start;
+ }
+ }
+ return clampedFallback;
+ }
+
+ Future<void> _togglePreview(BuildContext context, List<dynamic> chapters, double duration) async {
+ final l = AppLocalizations.of(context)!;
+ if (_preview?.isLoading ?? false) return;
+ if (_preview?.isPlaying ?? false) {
+ await _preview!.stop();
+ return;
+ }
+ _preview ??= BookmarkPreviewPlayer(itemId: widget.itemId, api: context.read<AuthProvider>().apiService)
+ ..clipLength = const Duration(minutes: 5)
+ ..addListener(_onPreviewChanged);
+ final startAt = _previewStartSeconds(chapters, duration);
+ debugPrint('[BookDetail] preview start at ${startAt.toStringAsFixed(1)}s of ${duration.toStringAsFixed(0)}s');
+ try {
+ await _preview!.toggleAt(startAt);
+ } catch (e) {
+ debugPrint('[BookDetail] preview failed: $e');
+ if (mounted) {
+ showOverlayToast(context, l.bookmarkPreviewFailed, icon: Icons.error_outline_rounded);
+ }
+ }
+ }
+
+ void _onPreviewChanged() {
+ if (mounted) setState(() {});
+ }
+
Future<void> _startAbsorb(BuildContext context, {required AuthProvider auth, required String title, required String author, required String? coverUrl, required double duration, required List<dynamic> chapters}) async {
+ await _preview?.stop();
final player = AudioPlayerService();
// Grab the root navigator before we pop the sheet
final rootNav = Navigator.of(context, rootNavigator: true);