Skip to content

A cross platform Audiobookshelf client for Android and iOS

Dart

Keep downloaded books on the Downloads shelf while offline, show their real progress on Home, and let them start from Home without the server

Barnabas3fa85d46d085
diff --git a/lib/providers/_lp_core.dart b/lib/providers/_lp_core.dart
index 15d8a866..118fcfd7 100644
--- a/lib/providers/_lp_core.dart
+++ b/lib/providers/_lp_core.dart
@@ -531,11 +531,8 @@ mixin _CoreMixin on ChangeNotifier, _StateMixin {
final isFinished =
_progressMap[dl.itemId]?['isFinished'] == true || progress >= 0.999;
- if (progress > 0 && !isFinished) {
- continueEntities.add(entity);
- } else {
- downloadedEntities.add(entity);
- }
+ if (progress > 0 && !isFinished) continueEntities.add(entity);
+ downloadedEntities.add(entity);
}
_personalizedSections = [
diff --git a/lib/screens/home_screen.dart b/lib/screens/home_screen.dart
index 84aa3488..a3540713 100644
--- a/lib/screens/home_screen.dart
+++ b/lib/screens/home_screen.dart
@@ -1,3 +1,4 @@
+import 'dart:convert';
import 'dart:io';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
@@ -969,6 +970,10 @@ class _ContinueListeningCardState extends State<_ContinueListeningCard> {
(recentEpisode != null
? (recentEpisode['duration'] as num?)?.toDouble() ?? 0
: (media['duration'] as num?)?.toDouble() ?? 0);
+ // Offline there is no server record, only what this phone saved.
+ if (progressData == null && progress > 0) {
+ currentTime = progress * totalDuration;
+ }
}
final accent = _accent ?? cs.primary;
@@ -1270,22 +1275,53 @@ class _ContinueListeningCardState extends State<_ContinueListeningCard> {
return;
}
- // Fetch full item data to get chapters
- final fullItem = await api.getLibraryItem(itemId);
- if (fullItem == null) {
- if (mounted) setState(() => _isLoading = false);
- return;
+ final String title;
+ final String author;
+ double duration;
+ List<dynamic> chapters;
+ String? libraryId;
+ final downloads = DownloadService();
+ if (downloads.isDownloaded(itemId)) {
+ // The download carries everything the player needs, so a book on the
+ // phone starts without asking the server, offline or not.
+ final media = widget.item['media'] as Map<String, dynamic>? ?? {};
+ final metadata = media['metadata'] as Map<String, dynamic>? ?? {};
+ final info = downloads.getInfo(itemId);
+ title = metadata['title'] as String? ?? info.title ?? '';
+ author = metadata['authorName'] as String? ?? info.author ?? '';
+ duration = (media['duration'] as num?)?.toDouble() ?? 0;
+ chapters = (media['chapters'] as List<dynamic>?) ?? [];
+ libraryId = widget.item['libraryId'] as String? ?? info.libraryId;
+ final cached = downloads.getCachedSessionData(itemId);
+ if (cached != null && (duration <= 0 || chapters.isEmpty)) {
+ try {
+ final session = jsonDecode(cached) as Map<String, dynamic>;
+ if (duration <= 0) {
+ duration = (session['duration'] as num?)?.toDouble() ?? 0;
+ }
+ if (chapters.isEmpty) {
+ chapters = session['chapters'] as List<dynamic>? ?? [];
+ }
+ } catch (_) {}
+ }
+ } else {
+ final fullItem = await api.getLibraryItem(itemId);
+ if (fullItem == null) {
+ if (mounted) {
+ showErrorToast(context, AppLocalizations.of(context)!.loginCouldNotReachServer);
+ setState(() => _isLoading = false);
+ }
+ return;
+ }
+ final media = fullItem['media'] as Map<String, dynamic>? ?? {};
+ final metadata = media['metadata'] as Map<String, dynamic>? ?? {};
+ title = metadata['title'] as String? ?? '';
+ author = metadata['authorName'] as String? ?? '';
+ duration = (media['duration'] as num?)?.toDouble() ?? 0;
+ chapters = (media['chapters'] as List<dynamic>?) ?? [];
+ libraryId = fullItem['libraryId'] as String?;
}
-
- final media = fullItem['media'] as Map<String, dynamic>? ?? {};
- final metadata = media['metadata'] as Map<String, dynamic>? ?? {};
- final title = metadata['title'] as String? ?? '';
- final author = metadata['authorName'] as String? ?? '';
final coverUrl = widget.lib.getCoverUrl(itemId);
- final duration = (media['duration'] is num)
- ? (media['duration'] as num).toDouble()
- : 0.0;
- final chapters = (media['chapters'] as List<dynamic>?) ?? [];
// Start playback
final error = await widget.player.playItem(
@@ -1296,7 +1332,7 @@ class _ContinueListeningCardState extends State<_ContinueListeningCard> {
coverUrl: coverUrl,
totalDuration: duration,
chapters: chapters,
- libraryId: fullItem['libraryId'] as String?,
+ libraryId: libraryId,
fromUi: true,
);
if (error != null && mounted) showErrorToast(context, error);