A cross platform Audiobookshelf client for Android and iOS
Dart
Stop auto-download bringing back a book you deleted until you download it yourself
Barnabas76f95c132fb6
diff --git a/lib/providers/_lp_core.dart b/lib/providers/_lp_core.dartindex 64059c95..a0a696f1 100644--- a/lib/providers/_lp_core.dart+++ b/lib/providers/_lp_core.dart@@ -2577,6 +2577,7 @@ mixin _CoreMixin on ChangeNotifier, _StateMixin {dl.downloadItem(api: _api!,itemId: key,+ automatic: true,title: ep?['title'] as String? ?? 'Episode',author: metadata['title'] as String? ?? '',coverUrl: getCoverUrl(podcastId),@@ -3251,6 +3252,7 @@ mixin _CoreMixin on ChangeNotifier, _StateMixin {final error = await downloads.downloadItem(api: api,itemId: key,+ automatic: true,title: title,author: author,coverUrl: getCoverUrl(libraryItemId),@@ -3355,6 +3357,7 @@ mixin _CoreMixin on ChangeNotifier, _StateMixin {dl.downloadItem(api: _api!,itemId: bookId,+ automatic: true,title: md['title'] as String? ?? '',author: md['authorName'] as String? ?? '',coverUrl: getCoverUrl(bookId),@@ -3395,6 +3398,7 @@ mixin _CoreMixin on ChangeNotifier, _StateMixin {dl.downloadItem(api: _api!,itemId: id,+ automatic: true,title: metadata['title'] as String? ?? '',author: metadata['authorName'] as String? ?? '',coverUrl: getCoverUrl(id),@@ -3451,6 +3455,7 @@ mixin _CoreMixin on ChangeNotifier, _StateMixin {dl.downloadItem(api: _api!,itemId: compoundKey,+ automatic: true,title: curEp['title'] as String? ?? 'Episode',author: metadata['title'] as String? ?? '',coverUrl: getCoverUrl(showId),@@ -3476,6 +3481,7 @@ mixin _CoreMixin on ChangeNotifier, _StateMixin {dl.downloadItem(api: _api!,itemId: key,+ automatic: true,title: ep['title'] as String? ?? 'Episode',author: metadata['title'] as String? ?? '',coverUrl: getCoverUrl(showId),diff --git a/lib/screens/downloads_screen.dart b/lib/screens/downloads_screen.dartindex 7fdbf3a0..fe0b8c2f 100644--- a/lib/screens/downloads_screen.dart+++ b/lib/screens/downloads_screen.dart@@ -122,7 +122,7 @@ class _DownloadsScreenState extends State<DownloadsScreen> {if (confirmed != true || !mounted) return;for (final itemId in _selected.toList()) {- await DownloadService().deleteDownload(itemId);+ await DownloadService().deleteDownload(itemId, byUser: true);}_exitSelection();@@ -155,7 +155,7 @@ class _DownloadsScreenState extends State<DownloadsScreen> {),);if (confirmed != true || !mounted) return;- await DownloadService().deleteDownload(info.itemId);+ await DownloadService().deleteDownload(info.itemId, byUser: true);await _load();if (mounted) {showOverlayToast(context, l.downloadsRemovedTitle(info.title ?? ''),diff --git a/lib/services/download_service.dart b/lib/services/download_service.dartindex e1e82515..6a8e671c 100644--- a/lib/services/download_service.dart+++ b/lib/services/download_service.dart@@ -345,6 +345,14 @@ class DownloadService extends ChangeNotifier {/// Queue of pending download requests.final List<_QueuedDownload> _queue = [];+ // Items the user deleted by hand. Every auto-download planner (rolling+ // series and show, queue window, playlist and collection, new subscribed+ // episodes) funnels through [downloadItem] with automatic: true and skips+ // these, so a half-finished book that was deleted stops coming back on the+ // next launch. Global like the downloads themselves, not per account.+ final Set<String> _autoDownloadBlocked = {};+ static const _autoDownloadBlockedKey = 'auto_download_blocked';+/// The current Android SAF folder URI (content://), or null if using default.String? get customDownloadUri => _customDownloadUri;@@ -668,6 +676,7 @@ class DownloadService extends ChangeNotifier {_updatesSub ??= FileDownloader().updates.listen(_onTaskUpdate);await FileDownloader().trackTasks();await _loadPending();+ await _loadAutoDownloadBlocks();await _rehydratePending();await FileDownloader().resumeFromBackground();_startReconciler();@@ -1295,6 +1304,10 @@ class DownloadService extends ChangeNotifier {String? episodeId,String? libraryId,bool Function()? shouldStart,+ // Set by the auto-download planners. An item the user deleted by hand is+ // skipped when automatic; a manual (or download-on-stream) call clears+ // that block, since the user asked for it back.+ bool automatic = false,}) async {if (shouldStart?.call() == false) return null;try {@@ -1308,6 +1321,15 @@ class DownloadService extends ChangeNotifier {}if (shouldStart?.call() == false) return null;+ if (automatic && _autoDownloadBlocked.contains(itemId)) {+ debugPrint(+ '[Download] skipped auto-download of "$title" ($itemId): the user deleted it. A manual download, or playing it with download-on-stream on, brings it back');+ return null;+ }+ if (!automatic && _autoDownloadBlocked.remove(itemId)) {+ await _saveAutoDownloadBlocks();+ }+if (_activeDownloadIds.contains(itemId)) return null;if (isDownloaded(itemId)) return null;// Already queued — don't duplicate@@ -2327,6 +2349,23 @@ class DownloadService extends ChangeNotifier {}}+ Future<void> _loadAutoDownloadBlocks() async {+ final prefs = await SharedPreferences.getInstance();+ _autoDownloadBlocked+ ..clear()+ ..addAll(prefs.getStringList(_autoDownloadBlockedKey) ?? const []);+ }++ Future<void> _saveAutoDownloadBlocks() async {+ final prefs = await SharedPreferences.getInstance();+ if (_autoDownloadBlocked.isEmpty) {+ await prefs.remove(_autoDownloadBlockedKey);+ } else {+ await prefs.setStringList(+ _autoDownloadBlockedKey, _autoDownloadBlocked.toList());+ }+ }+/// Rebuild in-flight progress from the package task DB after a relaunch, then/// finalize books that finished while we were dead and drop ones whose tasks/// are gone. Tasks still in flight keep running; their updates (plus@@ -2382,10 +2421,22 @@ class DownloadService extends ChangeNotifier {}}- Future<void> deleteDownload(String itemId, {bool skipStopCheck = false}) async {- // If this is still downloading, cancel the in-flight transfer (which cleans- // up partial files and the background tasks) rather than deleting.- if (_pending.containsKey(itemId)) {+ /// [byUser] marks a deliberate removal: the auto-download planners then+ /// leave this item alone until a manual download, or a play with+ /// download-on-stream on, brings it back - see [downloadItem].+ Future<void> deleteDownload(+ String itemId, {+ bool skipStopCheck = false,+ bool byUser = false,+ }) async {+ if (byUser && _autoDownloadBlocked.add(itemId)) {+ debugPrint(+ '[Download] $itemId removed by the user - auto-download leaves it alone from now on');+ await _saveAutoDownloadBlocks();+ }+ // If this is still downloading or waiting for a slot, cancel it (which+ // cleans up partial files and the background tasks) rather than deleting.+ if (_pending.containsKey(itemId) || _queue.any((q) => q.itemId == itemId)) {cancelDownload(itemId);return;}diff --git a/lib/widgets/absorbing_shared.dart b/lib/widgets/absorbing_shared.dartindex 857c0fcf..27ba240f 100644--- a/lib/widgets/absorbing_shared.dart+++ b/lib/widgets/absorbing_shared.dart@@ -300,7 +300,7 @@ class _DownloadWideButtonState extends State<DownloadWideButton> {actions: [TextButton(onPressed: () => Navigator.pop(ctx), child: Text(l.cancel)),TextButton(onPressed: () {- _dl.deleteDownload(widget.itemId);+ _dl.deleteDownload(widget.itemId, byUser: true);Navigator.pop(ctx);showOverlayToast(context, l.downloadRemoved, icon: Icons.delete_outline_rounded);},diff --git a/lib/widgets/book_detail_sheet.dart b/lib/widgets/book_detail_sheet.dartindex 99174951..5ec1a738 100644--- a/lib/widgets/book_detail_sheet.dart+++ b/lib/widgets/book_detail_sheet.dart@@ -1857,7 +1857,7 @@ class _BookDetailSheetContentState extends State<_BookDetailSheetContent> {Future<void> _removeEbookOffline(BuildContext context) async {final l = AppLocalizations.of(context)!;- await DownloadService().deleteDownload(widget.itemId);+ await DownloadService().deleteDownload(widget.itemId, byUser: true);if (mounted) showOverlayToast(context, l.ebookRemovedOffline, icon: Icons.delete_outline_rounded);}diff --git a/lib/widgets/card_buttons.dart b/lib/widgets/card_buttons.dartindex 3131ccb1..c62ac099 100644--- a/lib/widgets/card_buttons.dart+++ b/lib/widgets/card_buttons.dart@@ -405,7 +405,7 @@ class CardDownloadButtonInline extends StatelessWidget {actions: [TextButton(onPressed: () => Navigator.pop(ctx), child: Text(l.cancel)),TextButton(onPressed: () {- dl.deleteDownload(_key);+ dl.deleteDownload(_key, byUser: true);Navigator.pop(ctx);showOverlayToast(context, l.downloadRemoved, icon: Icons.delete_outline_rounded);}, child: Text(l.remove, style: const TextStyle(color: Colors.redAccent))),@@ -1929,7 +1929,7 @@ class CardActionDelegate {actions: [TextButton(onPressed: () => Navigator.pop(dCtx), child: Text(l.cancel)),TextButton(onPressed: () {- dl.deleteDownload(dlKey);+ dl.deleteDownload(dlKey, byUser: true);Navigator.pop(dCtx);showOverlayToast(context, l.downloadRemoved, icon: Icons.delete_outline_rounded);}, child: Text(l.remove, style: const TextStyle(color: Colors.redAccent))),diff --git a/lib/widgets/episode_detail_sheet.dart b/lib/widgets/episode_detail_sheet.dartindex 43ce619d..1cbad0af 100644--- a/lib/widgets/episode_detail_sheet.dart+++ b/lib/widgets/episode_detail_sheet.dart@@ -306,7 +306,7 @@ class _EpisodeDetailSheetState extends State<EpisodeDetailSheet> {actions: [TextButton(onPressed: () => Navigator.pop(ctx), child: Text(l.cancel)),TextButton(onPressed: () {- DownloadService().deleteDownload(dlKey);+ DownloadService().deleteDownload(dlKey, byUser: true);Navigator.pop(ctx);showOverlayToast(context,