From ae918028bd8c765fed8ebf7be5745429e90b6965 Mon Sep 17 00:00:00 2001 From: zarzet <42882290+zarzet@users.noreply.github.com> Date: Tue, 6 Oct 2026 13:08:13 +0700 Subject: [PATCH] fix(history): preserve bulk deletion failure handling --- lib/providers/download_history_provider.dart | 42 ++++++++++---------- 1 file changed, 21 insertions(+), 21 deletions(-) diff --git a/lib/providers/download_history_provider.dart b/lib/providers/download_history_provider.dart index 2f2a1a8c..9272d39f 100644 --- a/lib/providers/download_history_provider.dart +++ b/lib/providers/download_history_provider.dart @@ -426,35 +426,35 @@ class DownloadHistoryNotifier extends Notifier { // Serialize with reloads so a snapshot read before deletion cannot restore // the removed entries after both indexes have been cleaned. return _enqueueHistoryWrite(() async { - await removeManyFromHistory(await _db.getPhysicalFileIds(paths)); + await _removePersistedHistoryItems(await _db.getPhysicalFileIds(paths)); }); } Future removeManyFromHistory(Iterable ids) async { + try { + await _removePersistedHistoryItems(ids); + } catch (error, stack) { + _historyLog.e('Failed to delete from database: $error', error, stack); + } + } + + Future _removePersistedHistoryItems(Iterable ids) async { final removedIds = ids.toSet(); if (removedIds.isEmpty) return; // Persist first: a failed deletion must not invalidate every Library view // or silently remove an entry from the visible history snapshot. - try { - final deletedCount = await _db.deleteByIds(removedIds.toList()); - if (deletedCount == 0) return; - state = state.copyWith( - items: state.items - .where((item) => !removedIds.contains(item.id)) - .toList(), - totalCount: (state.totalCount - deletedCount).clamp( - 0, - state.totalCount, - ), - lookupItems: state.lookupItems - .where((item) => !removedIds.contains(item.id)) - .toList(growable: false), - loadedIndexVersion: state.loadedIndexVersion + 1, - ); - } catch (error) { - _historyLog.e('Failed to delete from database: $error'); - rethrow; - } + final deletedCount = await _db.deleteByIds(removedIds.toList()); + if (deletedCount == 0) return; + state = state.copyWith( + items: state.items + .where((item) => !removedIds.contains(item.id)) + .toList(), + totalCount: (state.totalCount - deletedCount).clamp(0, state.totalCount), + lookupItems: state.lookupItems + .where((item) => !removedIds.contains(item.id)) + .toList(growable: false), + loadedIndexVersion: state.loadedIndexVersion + 1, + ); } DownloadHistoryItem? getBySpotifyId(String spotifyId) {