Skip to content

A cross platform Audiobookshelf client for Android and iOS

Dart

Show a book's Read button on the home card as soon as its ebook is added

Barnabas813e4f62a5cb
diff --git a/lib/providers/_lp_state.dart b/lib/providers/_lp_state.dart
index 08b6f0a0..4e97c0e3 100644
--- a/lib/providers/_lp_state.dart
+++ b/lib/providers/_lp_state.dart
@@ -253,7 +253,18 @@ mixin _StateMixin on ChangeNotifier {
_seriesTabCache[key] = {'items': items, 'total': total};
}
- void registerUpdatedAt(String id, int ts) => _itemUpdatedAt[id] = ts;
+ // Monotonic: only ever advances. A slow/stale personalized load that carries
+ // a pre-change snapshot must not write an older timestamp over a newer one, or
+ // it would mask a server change (e.g. an ebook file just added) from widgets
+ // watching the tick.
+ void registerUpdatedAt(String id, int ts) {
+ final cur = _itemUpdatedAt[id];
+ if (cur == null || ts > cur) _itemUpdatedAt[id] = ts;
+ }
+ /// Per-item "last changed on the server" tick, bumped on every socket
+ /// item_updated. Widgets watch this to re-fetch when an item changes (e.g. an
+ /// ebook file added to a book) without an app restart.
+ int? itemUpdatedAt(String id) => _itemUpdatedAt[id];
void registerHasCover(String id, bool hasCover) {
if (hasCover) {
diff --git a/lib/providers/library_provider.dart b/lib/providers/library_provider.dart
index cddde334..dfc59264 100644
--- a/lib/providers/library_provider.dart
+++ b/lib/providers/library_provider.dart
@@ -388,7 +388,7 @@ class LibraryProvider extends ChangeNotifier
if (b is Map<String, dynamic>) {
final id = b['id'] as String?;
final ts = b['updatedAt'] as num?;
- if (id != null && ts != null) _itemUpdatedAt[id] = ts.toInt();
+ if (id != null && ts != null) registerUpdatedAt(id, ts.toInt());
}
}
}
diff --git a/lib/widgets/absorbing_card.dart b/lib/widgets/absorbing_card.dart
index d6ff0de2..78c0a4b0 100644
--- a/lib/widgets/absorbing_card.dart
+++ b/lib/widgets/absorbing_card.dart
@@ -44,6 +44,11 @@ class AbsorbingCardState extends State<AbsorbingCard> with AutomaticKeepAliveCli
bool _isStarting = false;
List<dynamic>? _fetchedChapters;
Map<String, dynamic>? _fetchedEbookFile;
+ // Server-change tick last acted on, so a same-id item_updated (e.g. an ebook
+ // file added to this book) re-runs the full-item fetch. The card is kept
+ // alive with a stable key, so without this it never picks up a server change.
+ int? _lastSeenUpdatedAt;
+ bool _refetchingItem = false;
StreamSubscription<Duration>? _chapterTrackSub;
int _lastChapterIdx = -1;
ui.Image? _blurredCover; // Precached blurred background
@@ -147,6 +152,9 @@ class AbsorbingCardState extends State<AbsorbingCard> with AutomaticKeepAliveCli
@override
void initState() {
super.initState();
+ // Baseline the server-change tick so only a CHANGE after mount re-fetches
+ // (the initial fill is handled by _fetchChaptersIfNeeded below).
+ _lastSeenUpdatedAt = context.read<LibraryProvider>().itemUpdatedAt(_itemId);
_fetchChaptersIfNeeded();
_startChapterTracking();
ChromecastService().addListener(_onCastChanged);
@@ -252,6 +260,28 @@ class AbsorbingCardState extends State<AbsorbingCard> with AutomaticKeepAliveCli
} catch (_) {}
}
+ /// Re-fetch the full item when the server reports this book changed (socket
+ /// item_updated bumps the provider's per-item tick). Targets the case where
+ /// an ebook file is added while the app is open: the card's minified entity
+ /// never carries ebookFile and the one-time initState fetch found none, so
+ /// the "Read" action would stay disabled until a restart. Only fires while
+ /// the ebook is still missing, so ordinary cover/metadata updates don't
+ /// trigger needless fetches.
+ void _maybeRefetchOnServerChange(LibraryProvider lib) {
+ final ts = lib.itemUpdatedAt(_itemId);
+ if (ts == _lastSeenUpdatedAt) return;
+ _lastSeenUpdatedAt = ts;
+ if (_ebookFile != null || _refetchingItem) return;
+ _refetchingItem = true;
+ WidgetsBinding.instance.addPostFrameCallback((_) async {
+ try {
+ await _fetchChaptersIfNeeded();
+ } finally {
+ _refetchingItem = false;
+ }
+ });
+ }
+
void _startChapterTracking() {
_chapterTrackSub?.cancel();
@@ -335,6 +365,7 @@ class AbsorbingCardState extends State<AbsorbingCard> with AutomaticKeepAliveCli
_fetchedChapters = null;
_fetchedEbookFile = null;
_lastChapterIdx = -1;
+ _lastSeenUpdatedAt = context.read<LibraryProvider>().itemUpdatedAt(_itemId);
_fetchChaptersIfNeeded();
}
if (oldWidget.player != widget.player) _startChapterTracking();
@@ -448,6 +479,7 @@ class AbsorbingCardState extends State<AbsorbingCard> with AutomaticKeepAliveCli
final l = AppLocalizations.of(context)!;
final lib = context.watch<LibraryProvider>();
+ _maybeRefetchOnServerChange(lib);
// For podcast episodes, look up progress by compound key (itemId-episodeId)
final progress = (_episodeId != null)
? lib.getEpisodeProgress(_itemId, _episodeId!)