Skip to content

A cross platform Audiobookshelf client for Android and iOS

Dart

Show real names in the auto-download list and tuck it into a dropdown

Barnabasabc4e208921d
diff --git a/lib/providers/_lp_core.dart b/lib/providers/_lp_core.dart
index f6c90845..5799f116 100644
--- a/lib/providers/_lp_core.dart
+++ b/lib/providers/_lp_core.dart
@@ -6,8 +6,10 @@ mixin _CoreMixin on ChangeNotifier, _StateMixin {
bool isRollingDownloadEnabled(String seriesOrShowId) =>
_rollingDownloadSeries.contains(seriesOrShowId);
- Future<void> enableRollingDownload(String seriesOrShowId) async {
+ Future<void> enableRollingDownload(String seriesOrShowId,
+ {String? name, String? kind}) async {
_rollingDownloadSeries.add(seriesOrShowId);
+ _rememberRollingDownloadSource(seriesOrShowId, name: name, kind: kind);
await _saveRollingDownloadSeries();
notifyListeners();
final player = AudioPlayerService();
@@ -23,18 +25,36 @@ mixin _CoreMixin on ChangeNotifier, _StateMixin {
Future<void> disableRollingDownload(String seriesOrShowId) async {
_rollingDownloadSeries.remove(seriesOrShowId);
+ _rollingDownloadSourceNames.remove(seriesOrShowId);
+ unawaited(_saveRollingDownloadSourceNames());
await _saveRollingDownloadSeries();
notifyListeners();
}
- Future<void> toggleRollingDownload(String seriesOrShowId) async {
+ Future<void> toggleRollingDownload(String seriesOrShowId,
+ {String? name, String? kind}) async {
if (_rollingDownloadSeries.contains(seriesOrShowId)) {
await disableRollingDownload(seriesOrShowId);
} else {
- await enableRollingDownload(seriesOrShowId);
+ await enableRollingDownload(seriesOrShowId, name: name, kind: kind);
}
}
+ void _rememberRollingDownloadSource(String id, {String? name, String? kind}) {
+ final existing = _rollingDownloadSourceNames[id];
+ final mergedName =
+ (name != null && name.isNotEmpty) ? name : existing?['name'];
+ _rollingDownloadSourceNames[id] = {
+ if (mergedName != null) 'name': mergedName,
+ 'kind': kind ?? existing?['kind'] ?? 'unknown',
+ };
+ unawaited(_saveRollingDownloadSourceNames());
+ }
+
+ Future<void> _saveRollingDownloadSourceNames() => ScopedPrefs.setString(
+ 'rolling_download_source_names',
+ jsonEncode(_rollingDownloadSourceNames));
+
/// Everything auto-download is currently turned on for, resolved to a name
/// where we can. The stored set is bare ids with no type, so entries we
/// can't match against the cached series/playlist/collection/show data are
@@ -46,11 +66,21 @@ mixin _CoreMixin on ChangeNotifier, _StateMixin {
String? name;
var kind = 'unknown';
- for (final raw in _playlists) {
- if (raw is Map<String, dynamic> && raw['id'] == id) {
- name = raw['name'] as String?;
- kind = 'playlist';
- break;
+ // Names stored at enable time (or by an earlier resolve) win; the
+ // live caches below only cover the currently selected library.
+ final stored = _rollingDownloadSourceNames[id];
+ if (stored != null) {
+ name = stored['name'];
+ kind = stored['kind'] ?? 'unknown';
+ }
+
+ if (name == null) {
+ for (final raw in _playlists) {
+ if (raw is Map<String, dynamic> && raw['id'] == id) {
+ name = raw['name'] as String?;
+ kind = 'playlist';
+ break;
+ }
}
}
if (name == null) {
@@ -83,6 +113,10 @@ mixin _CoreMixin on ChangeNotifier, _StateMixin {
}
}
+ if (name != null && name.isNotEmpty && stored?['name'] == null) {
+ _rememberRollingDownloadSource(id, name: name, kind: kind);
+ }
+
sources.add({
'id': id,
'kind': kind,
@@ -103,12 +137,98 @@ mixin _CoreMixin on ChangeNotifier, _StateMixin {
Future<void> _loadRollingDownloadSeries() async {
_rollingDownloadSeries =
(await ScopedPrefs.getStringList('rolling_download_series')).toSet();
+ _rollingDownloadSourceNames = {};
+ final namesJson =
+ await ScopedPrefs.getString('rolling_download_source_names');
+ if (namesJson != null && namesJson.isNotEmpty) {
+ try {
+ final decoded = jsonDecode(namesJson);
+ if (decoded is Map<String, dynamic>) {
+ for (final entry in decoded.entries) {
+ final value = entry.value;
+ if (value is Map) {
+ _rollingDownloadSourceNames[entry.key] = {
+ for (final e in value.entries)
+ if (e.value is String) e.key.toString(): e.value as String,
+ };
+ }
+ }
+ }
+ } catch (_) {}
+ }
final prefs = await SharedPreferences.getInstance();
if (prefs.containsKey('rollingDownload')) {
await prefs.remove('rollingDownload');
}
}
+ bool _resolvingAutoDownloadNames = false;
+
+ /// Fetch names for auto-download ids enabled before names were stored.
+ /// The set holds bare UUIDs with no type, so try each endpoint until one
+ /// answers: playlist, collection, series, then podcast item.
+ Future<void> resolveAutoDownloadSourceNames() async {
+ final api = _api;
+ if (api == null || isOffline || _resolvingAutoDownloadNames) return;
+ final unknown = _rollingDownloadSeries
+ .where((id) => _rollingDownloadSourceNames[id]?['name'] == null)
+ .toList();
+ if (unknown.isEmpty) return;
+ _resolvingAutoDownloadNames = true;
+ try {
+ final bookLibraryIds = <String>[];
+ for (final l in _libraries) {
+ if (l is Map && (l['mediaType'] as String? ?? 'book') != 'podcast') {
+ final libraryId = l['id'];
+ if (libraryId is String) bookLibraryIds.add(libraryId);
+ }
+ }
+ var resolvedAny = false;
+ for (final id in unknown) {
+ String? name;
+ String? kind;
+ final playlist = await api.getPlaylist(id);
+ if (playlist != null) {
+ name = playlist['name'] as String?;
+ kind = 'playlist';
+ }
+ if (name == null) {
+ final collection = await api.getCollection(id);
+ if (collection != null) {
+ name = collection['name'] as String?;
+ kind = 'collection';
+ }
+ }
+ if (name == null) {
+ final series =
+ await api.getSeriesInfo(id, libraryIds: bookLibraryIds);
+ if (series != null) {
+ name = series['name'] as String?;
+ kind = 'series';
+ }
+ }
+ if (name == null) {
+ final item = await api.getLibraryItem(id);
+ if (item != null) {
+ final media = item['media'] as Map<String, dynamic>? ?? const {};
+ final metadata =
+ media['metadata'] as Map<String, dynamic>? ?? const {};
+ name = metadata['title'] as String?;
+ kind = item['mediaType'] == 'podcast' ? 'podcast' : 'unknown';
+ }
+ }
+ if (name != null && name.isNotEmpty) {
+ debugPrint('[AutoDL] Resolved $id -> $kind "$name"');
+ _rememberRollingDownloadSource(id, name: name, kind: kind);
+ resolvedAny = true;
+ }
+ }
+ if (resolvedAny) notifyListeners();
+ } finally {
+ _resolvingAutoDownloadNames = false;
+ }
+ }
+
Future<void> _saveRollingDownloadSeries() async {
await ScopedPrefs.setStringList(
'rolling_download_series', _rollingDownloadSeries.toList());
diff --git a/lib/providers/_lp_state.dart b/lib/providers/_lp_state.dart
index 08298210..c449b949 100644
--- a/lib/providers/_lp_state.dart
+++ b/lib/providers/_lp_state.dart
@@ -71,6 +71,8 @@ mixin _StateMixin on ChangeNotifier {
final Map<String, Map<String, dynamic>> _subSeriesCache = {};
Set<String> _rollingDownloadSeries = {};
+ // id -> {name, kind} for the settings list; the set above is bare ids.
+ Map<String, Map<String, String>> _rollingDownloadSourceNames = {};
Set<String> _subscribedPodcasts = {};
// Book ids the user hid from the local "finished this year" stats list.
// Purely local; the server's finished date is left untouched.
diff --git a/lib/screens/absorbing_screen.dart b/lib/screens/absorbing_screen.dart
index d050875b..255aef72 100644
--- a/lib/screens/absorbing_screen.dart
+++ b/lib/screens/absorbing_screen.dart
@@ -1228,7 +1228,14 @@ class _ReorderAbsorbingSheetState extends State<_ReorderAbsorbingSheet> {
setState(() => _queueAutoDownload = value);
if (sourceId != null) {
if (value) {
- await widget.lib.enableRollingDownload(sourceId);
+ final kind = _queueMode == 'playlist'
+ ? 'playlist'
+ : _queueMode == 'collection'
+ ? 'collection'
+ : _currentPodcastShowId != null
+ ? 'podcast'
+ : 'series';
+ await widget.lib.enableRollingDownload(sourceId, kind: kind);
} else {
await widget.lib.disableRollingDownload(sourceId);
}
diff --git a/lib/screens/settings_screen.dart b/lib/screens/settings_screen.dart
index f126feac..36fc4df2 100644
--- a/lib/screens/settings_screen.dart
+++ b/lib/screens/settings_screen.dart
@@ -1139,24 +1139,50 @@ class _SettingsScreenState extends State<SettingsScreen> {
}
}
+ bool _autoDownloadSourcesExpanded = false;
+
/// Auto-download is switched on per source from that source's own page, so
/// without this there is nowhere to see what it's on for - a list left
/// enabled keeps stocking itself with nothing pointing at it.
Widget _buildAutoDownloadSources(
LibraryProvider lib, AppLocalizations l, ColorScheme cs, TextTheme tt) {
final sources = lib.enabledAutoDownloadSources();
+ // Entries enabled before names were stored resolve against the server.
+ if (sources.any((s) => s['name'] == null)) {
+ lib.resolveAutoDownloadSourceNames();
+ }
return Padding(
padding: const EdgeInsets.fromLTRB(16, 0, 16, 8),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
- Text(l.autoDownloadEnabledFor,
- style: tt.bodyMedium?.copyWith(color: cs.onSurface)),
+ InkWell(
+ onTap: sources.isEmpty
+ ? null
+ : () => setState(() => _autoDownloadSourcesExpanded =
+ !_autoDownloadSourcesExpanded),
+ child: Row(
+ children: [
+ Text(
+ sources.isEmpty
+ ? l.autoDownloadEnabledFor
+ : '${l.autoDownloadEnabledFor} (${sources.length})',
+ style: tt.bodyMedium?.copyWith(color: cs.onSurface)),
+ if (sources.isNotEmpty)
+ AnimatedRotation(
+ turns: _autoDownloadSourcesExpanded ? 0.5 : 0,
+ duration: const Duration(milliseconds: 150),
+ child: Icon(Icons.keyboard_arrow_down_rounded,
+ size: 20, color: cs.onSurfaceVariant),
+ ),
+ ],
+ ),
+ ),
const SizedBox(height: 4),
if (sources.isEmpty)
Text(l.autoDownloadEnabledForNone,
style: tt.bodySmall?.copyWith(color: cs.onSurfaceVariant))
- else
+ else if (_autoDownloadSourcesExpanded)
...sources.map((source) {
final name = source['name'];
return Row(children: [
diff --git a/lib/services/api_service.dart b/lib/services/api_service.dart
index 89acf027..f3a25a7e 100644
--- a/lib/services/api_service.dart
+++ b/lib/services/api_service.dart
@@ -2101,6 +2101,34 @@ class ApiService {
/// Get a single series with its books (paginated).
/// If [onPageLoaded] is provided, it's called after each page with the
/// cumulative results and total so the UI can show books as they arrive.
+ /// Series metadata only, for ids stored without a name. Tries the direct
+ /// series endpoint first and falls back to the per-library one for servers
+ /// that don't expose it.
+ Future<Map<String, dynamic>?> getSeriesInfo(
+ String seriesId, {
+ List<String> libraryIds = const [],
+ }) async {
+ try {
+ final resp = await _authGet(
+ Uri.parse('$_cleanBaseUrl/api/series/$seriesId'),
+ );
+ if (resp.statusCode == 200) {
+ return jsonDecode(resp.body) as Map<String, dynamic>;
+ }
+ } catch (_) {}
+ for (final libraryId in libraryIds) {
+ try {
+ final resp = await _authGet(
+ Uri.parse('$_cleanBaseUrl/api/libraries/$libraryId/series/$seriesId'),
+ );
+ if (resp.statusCode == 200) {
+ return jsonDecode(resp.body) as Map<String, dynamic>;
+ }
+ } catch (_) {}
+ }
+ return null;
+ }
+
Future<Map<String, dynamic>?> getSeries(String seriesId, {String? libraryId, void Function(List<dynamic> books, int total, {double? totalDuration})? onPageLoaded}) async {
try {
Map<String, dynamic>? seriesMeta;
diff --git a/lib/widgets/collection_detail_sheet.dart b/lib/widgets/collection_detail_sheet.dart
index e364b086..9e97c609 100644
--- a/lib/widgets/collection_detail_sheet.dart
+++ b/lib/widgets/collection_detail_sheet.dart
@@ -264,7 +264,8 @@ class _CollectionDetailSheetState extends State<CollectionDetailSheet> {
lib.isRollingDownloadEnabled(widget.collectionId)
? Icons.downloading_rounded
: Icons.download_outlined,
- () => lib.toggleRollingDownload(widget.collectionId),
+ () => lib.toggleRollingDownload(widget.collectionId,
+ name: name, kind: 'collection'),
tooltip: lib.isRollingDownloadEnabled(widget.collectionId)
? l.turnAutoDownloadOff
: l.turnAutoDownloadOn),
diff --git a/lib/widgets/episode_list_sheet.dart b/lib/widgets/episode_list_sheet.dart
index 4f127712..e61b4c21 100644
--- a/lib/widgets/episode_list_sheet.dart
+++ b/lib/widgets/episode_list_sheet.dart
@@ -441,7 +441,8 @@ class _EpisodeListSheetState extends State<EpisodeListSheet> {
);
if (enable == true && mounted) {
final lib = context.read<LibraryProvider>();
- await lib.enableRollingDownload(_itemId);
+ await lib.enableRollingDownload(_itemId,
+ name: _title, kind: 'podcast');
setState(() => _autoDownloadEnabled = true);
}
}
@@ -525,7 +526,8 @@ class _EpisodeListSheetState extends State<EpisodeListSheet> {
onTap: () async {
Navigator.pop(ctx);
final lib = context.read<LibraryProvider>();
- await lib.toggleRollingDownload(_itemId);
+ await lib.toggleRollingDownload(_itemId,
+ name: _title, kind: 'podcast');
setState(() => _autoDownloadEnabled = lib.isRollingDownloadEnabled(_itemId));
}),
if (_itemId.isNotEmpty)
diff --git a/lib/widgets/playlist_detail_sheet.dart b/lib/widgets/playlist_detail_sheet.dart
index f482e0bf..dffa3402 100644
--- a/lib/widgets/playlist_detail_sheet.dart
+++ b/lib/widgets/playlist_detail_sheet.dart
@@ -395,7 +395,8 @@ class _PlaylistDetailSheetState extends State<PlaylistDetailSheet> {
lib.isRollingDownloadEnabled(widget.playlistId)
? Icons.downloading_rounded
: Icons.download_outlined,
- () => lib.toggleRollingDownload(widget.playlistId),
+ () => lib.toggleRollingDownload(widget.playlistId,
+ name: name, kind: 'playlist'),
tooltip: lib.isRollingDownloadEnabled(widget.playlistId)
? l.turnAutoDownloadOff
: l.turnAutoDownloadOn),
diff --git a/lib/widgets/series_books_sheet.dart b/lib/widgets/series_books_sheet.dart
index a7ae1f11..74f63da1 100644
--- a/lib/widgets/series_books_sheet.dart
+++ b/lib/widgets/series_books_sheet.dart
@@ -834,7 +834,8 @@ class _SeriesBooksSheetState extends State<SeriesBooksSheet> {
onTap: () async {
Navigator.pop(ctx);
final lib = context.read<LibraryProvider>();
- await lib.toggleRollingDownload(widget.seriesId!);
+ await lib.toggleRollingDownload(widget.seriesId!,
+ name: widget.seriesName, kind: 'series');
setState(() => _autoDownloadEnabled = lib.isRollingDownloadEnabled(widget.seriesId!));
}),
ActionPillData(icon: Icons.search_rounded, label: l.seriesBooksFindMissingTitle,
@@ -869,7 +870,8 @@ class _SeriesBooksSheetState extends State<SeriesBooksSheet> {
);
if (enable == true && mounted) {
final lib = context.read<LibraryProvider>();
- await lib.enableRollingDownload(seriesId);
+ await lib.enableRollingDownload(seriesId,
+ name: widget.seriesName, kind: 'series');
setState(() => _autoDownloadEnabled = true);
}
}