A cross platform Audiobookshelf client for Android and iOS
Dart
Catch up on server changes made while the app was in the background
Barnabas55ec3c3c61eb
diff --git a/lib/providers/_lp_core.dart b/lib/providers/_lp_core.dartindex 6ee852ba..2040e417 100644--- a/lib/providers/_lp_core.dart+++ b/lib/providers/_lp_core.dart@@ -1025,6 +1025,7 @@ mixin _CoreMixin on ChangeNotifier, _StateMixin {void onAppBackgrounded() {_isBackgrounded = true;+ _backgroundedAt = DateTime.now();_stopServerPingTimer();_stopHealthCheckTimer();if (!AudioPlayerService().isPlaying) {@@ -1049,9 +1050,51 @@ mixin _CoreMixin on ChangeNotifier, _StateMixin {_startLocalProbeTimer();if (!isOffline && !_manualOffline) {(this as LibraryProvider).checkSubscribedPodcasts();+ // Item events emitted while backgrounded were missed (the socket was+ // down) and are never replayed, so anything edited from another device+ // would stay stale forever. Skip quick app switches - a fresh socket+ // has nothing to have missed.+ final away = _backgroundedAt == null+ ? null+ : DateTime.now().difference(_backgroundedAt!);+ if (away != null && away > const Duration(seconds: 30)) {+ _catchUpAfterBackground();+ }}}+ /// Re-sync what the missed socket events would have delivered: reload the+ /// home shelves, and sweep the most recently updated items in the current+ /// library to bump cover cache-busters and patch the search index.+ Future<void> _catchUpAfterBackground() async {+ loadPersonalizedView(force: true);+ final api = _api;+ final libId = _selectedLibraryId;+ if (api == null || libId == null) return;+ try {+ final data = await api.getLibraryItems(libId,+ sort: 'updatedAt', desc: 1, limit: 50);+ final results = data?['results'] as List? ?? const [];+ var changed = false;+ for (final r in results) {+ final item = (r is Map<String, dynamic>)+ ? (r['libraryItem'] as Map<String, dynamic>? ?? r)+ : null;+ if (item == null) continue;+ final id = item['id'] as String?;+ final ts = item['updatedAt'] as num?;+ if (id == null || ts == null) continue;+ final cur = _itemUpdatedAt[id];+ if (cur == null || ts.toInt() > cur) {+ registerUpdatedAt(id, ts.toInt());+ changed = true;+ }+ BookSearchIndex().patchItem(item);+ }+ if (changed) notifyListeners();+ } catch (_) {}+ }+void onPlaybackStarted() {if (!_isBackgrounded) {_softReconnectSocket();diff --git a/lib/providers/_lp_state.dart b/lib/providers/_lp_state.dartindex 053f0d9d..4ff6dff5 100644--- a/lib/providers/_lp_state.dart+++ b/lib/providers/_lp_state.dart@@ -82,6 +82,7 @@ mixin _StateMixin on ChangeNotifier {bool _isBackgrounded = false;bool _socketSoftDisconnected = false;+ DateTime? _backgroundedAt;Map<String, Map<String, dynamic>> _progressMap = {};final Map<String, double> _localProgressOverrides = {};diff --git a/lib/providers/library_provider.dart b/lib/providers/library_provider.dartindex 15510655..6ebc0338 100644--- a/lib/providers/library_provider.dart+++ b/lib/providers/library_provider.dart@@ -18,6 +18,7 @@ import '../services/bookmark_service.dart';import '../services/metadata_override_service.dart';import '../services/session_cache.dart';import '../services/socket_service.dart';+import '../services/book_search_index.dart';import '../services/home_widget_service.dart';import '../l10n/app_localizations.dart';import '../main.dart' show scaffoldMessengerKey, rootNavigatorKey;diff --git a/lib/services/book_search_index.dart b/lib/services/book_search_index.dartindex 7df95382..6911e17d 100644--- a/lib/services/book_search_index.dart+++ b/lib/services/book_search_index.dart@@ -39,12 +39,17 @@ class BookSearchIndex {final Map<String, List<_Indexed>> _cache = {};final Map<String, Future<void>> _inFlight = {};- void _onItemUpdated(Map<String, dynamic> item) {+ void _onItemUpdated(Map<String, dynamic> item) => patchItem(item);++ /// Patch or insert a single item, e.g. from a socket event or the+ /// foreground catch-up sweep. No-op for libraries that aren't indexed yet -+ /// those build fresh on demand anyway.+ void patchItem(Map<String, dynamic> item) {final libId = item['libraryId'] as String?;final id = item['id'] as String?;if (libId == null || id == null) return;final idx = _cache[libId];- if (idx == null) return; // library not indexed yet - built fresh on demand+ if (idx == null) return;final entry = _indexItem(item);final i = idx.indexWhere((e) => e.item['id'] == id);if (i >= 0) {