A cross platform Audiobookshelf client for Android and iOS
Dart
Keep library switching and the podcast tab available when starting offline (#313)
Barnabas7986942d1f93
diff --git a/lib/providers/_lp_core.dart b/lib/providers/_lp_core.dartindex 34453911..4012b471 100644--- a/lib/providers/_lp_core.dart+++ b/lib/providers/_lp_core.dart@@ -254,9 +254,15 @@ mixin _CoreMixin on ChangeNotifier, _StateMixin {void _buildOfflineSections() {final isPodcast = isPodcastLibrary;final allDownloads = DownloadService().downloadedItems;- final downloads = allDownloads- .where((dl) => (dl.itemId.length > 36) == isPodcast)- .toList();+ final downloads = allDownloads.where((dl) {+ final libraryId = dl.libraryId;+ if (libraryId != null &&+ libraryId.isNotEmpty &&+ _selectedLibraryId != null) {+ return libraryId == _selectedLibraryId;+ }+ return (dl.itemId.length > 36) == isPodcast;+ }).toList();debugPrint('[Library] Building offline sections: ${downloads.length}/${allDownloads.length} downloads (${isPodcast ? "podcast" : "book"})');if (downloads.isEmpty) {@@ -367,9 +373,15 @@ mixin _CoreMixin on ChangeNotifier, _StateMixin {void _injectDownloadedSection() {final isPodcast = isPodcastLibrary;final allDownloads = DownloadService().downloadedItems;- final downloads = allDownloads- .where((dl) => (dl.itemId.length > 36) == isPodcast)- .toList();+ final downloads = allDownloads.where((dl) {+ final libraryId = dl.libraryId;+ if (libraryId != null &&+ libraryId.isNotEmpty &&+ _selectedLibraryId != null) {+ return libraryId == _selectedLibraryId;+ }+ return (dl.itemId.length > 36) == isPodcast;+ }).toList();if (downloads.isEmpty) return;final entities = <Map<String, dynamic>>[];diff --git a/lib/providers/library_provider.dart b/lib/providers/library_provider.dartindex 724052be..40c21dfc 100644--- a/lib/providers/library_provider.dart+++ b/lib/providers/library_provider.dart@@ -11,6 +11,7 @@ import '../services/api_service.dart';import '../services/progress_sync_service.dart';import '../services/local_session_service.dart';import '../services/download_service.dart';+import '../services/library_cache.dart';import '../services/android_auto_service.dart';import '../services/carplay_service.dart';import '../services/chromecast_service.dart';@@ -176,6 +177,7 @@ class LibraryProvider extends ChangeNotifierawait _loadSubscribedPodcasts();await _loadYearHidden();await _loadKnownEpisodeIds();+ await _restoreCachedLibraries();Future.microtask(() => checkSubscribedPodcasts());// Replay any book-finished event that fired before the absorbing@@ -283,28 +285,15 @@ class LibraryProvider extends ChangeNotifier_libraries = await _api!.getLibraries();debugPrint('[Library] loadLibraries: got ${_libraries.length} libraries');+ await LibraryCache.save(_libraries);if (_libraries.isNotEmpty) {- final savedId = await ScopedPrefs.getString('last_selected_library');- final defaultId = _auth?.defaultLibraryId;- if (savedId != null &&- _libraries.any((l) => l['id'] == savedId)) {- _selectedLibraryId = savedId;- } else if (defaultId != null &&- _libraries.any((l) => l['id'] == defaultId)) {- _selectedLibraryId = defaultId;- } else {- final bookLibraries = _libraries- .where((l) =>- (l['mediaType'] as String? ?? 'book') != 'podcast')- .toList();- _selectedLibraryId = bookLibraries.isNotEmpty- ? bookLibraries.first['id']- : _libraries.first['id'];- }+ await _restoreSelectedLibrary();await _loadSectionPrefs();await loadPersonalizedView(force: true);+ } else {+ _selectedLibraryId = null;}} catch (e) {if (_isLikelyNetworkError(e)) {@@ -322,6 +311,40 @@ class LibraryProvider extends ChangeNotifiercatchUpSubscribedPodcasts();}+ Future<void> _restoreCachedLibraries() async {+ if (_libraries.isNotEmpty) return;+ final cached = await LibraryCache.load();+ if (cached.isEmpty) return;+ _libraries = cached;+ await _restoreSelectedLibrary();+ debugPrint(+ '[Library] Restored ${_libraries.length} cached libraries for offline use');+ }++ Future<void> _restoreSelectedLibrary() async {+ if (_libraries.isEmpty) {+ _selectedLibraryId = null;+ return;+ }+ final savedId = await ScopedPrefs.getString('last_selected_library');+ final defaultId = _auth?.defaultLibraryId;+ if (savedId != null && _libraries.any((l) => l['id'] == savedId)) {+ _selectedLibraryId = savedId;+ return;+ }+ if (defaultId != null && _libraries.any((l) => l['id'] == defaultId)) {+ _selectedLibraryId = defaultId;+ return;+ }+ final bookLibraries = _libraries+ .where((l) =>+ (l['mediaType'] as String? ?? 'book') != 'podcast')+ .toList();+ _selectedLibraryId = bookLibraries.isNotEmpty+ ? bookLibraries.first['id'] as String?+ : _libraries.first['id'] as String?;+ }+Future<void> selectLibrary(String libraryId) async {// When Merge Libraries is on, the absorbing card stays visible across// libraries so the user can still control playback. Only stop on switchdiff --git a/lib/services/library_cache.dart b/lib/services/library_cache.dartnew file mode 100644index 00000000..3158d6b0--- /dev/null+++ b/lib/services/library_cache.dart@@ -0,0 +1,57 @@+import 'dart:convert';++import 'package:flutter/foundation.dart';++import 'scoped_prefs.dart';++class LibraryCache {+ static const _key = 'cached_libraries';++ static Future<void> save(List<dynamic> libraries) async {+ try {+ await ScopedPrefs.setString(_key, jsonEncode(_sanitize(libraries)));+ } catch (e) {+ debugPrint('[LibraryCache] Failed to save libraries: $e');+ }+ }++ static Future<List<Map<String, dynamic>>> load() async {+ try {+ return decode(await ScopedPrefs.getString(_key));+ } catch (e) {+ debugPrint('[LibraryCache] Failed to load libraries: $e');+ return [];+ }+ }++ @visibleForTesting+ static List<Map<String, dynamic>> decode(String? value) {+ if (value == null || value.isEmpty) return [];+ try {+ final decoded = jsonDecode(value);+ if (decoded is! List<dynamic>) return [];+ return _sanitize(decoded);+ } catch (_) {+ return [];+ }+ }++ static List<Map<String, dynamic>> _sanitize(Iterable<dynamic> libraries) {+ final sanitized = <Map<String, dynamic>>[];+ for (final library in libraries) {+ if (library is! Map) continue;+ final id = library['id'];+ if (id is! String || id.isEmpty) continue;+ final name = library['name'];+ final mediaType = library['mediaType'];+ sanitized.add({+ 'id': id,+ 'name': name is String && name.isNotEmpty ? name : 'Library',+ 'mediaType': mediaType is String && mediaType.isNotEmpty+ ? mediaType+ : 'book',+ });+ }+ return sanitized;+ }+}