From b95bc32310e4ab10c7d28db79125780f4a3affda Mon Sep 17 00:00:00 2001 From: Yashraj Jain Date: Fri, 1 Nov 2024 19:33:03 +0530 Subject: [PATCH] fix(mobile): do not removed not backup asset when selecting the correspond options (#13256) * fixed the local ids selecting issue * code: updated impl inside deleteLocalOnlyAssets * fix: used png instead of jpg to maintain picture quality * Revert "fix: used png instead of jpg to maintain picture quality" This reverts commit 04f2ed54e40b454a057f97872836d71af50b06de. * fix: update logic from code-review perspective * refractor (mobile) : Dart fix applied * fix (mobile) : Updated multi grid as per requirement --------- Co-authored-by: Alex --- mobile/lib/providers/asset.provider.dart | 22 +++++++++++++++---- .../widgets/asset_grid/multiselect_grid.dart | 14 +++++++++++- 2 files changed, 31 insertions(+), 5 deletions(-) diff --git a/mobile/lib/providers/asset.provider.dart b/mobile/lib/providers/asset.provider.dart index c7e75df79b..3855a00b76 100644 --- a/mobile/lib/providers/asset.provider.dart +++ b/mobile/lib/providers/asset.provider.dart @@ -84,34 +84,48 @@ class AssetNotifier extends StateNotifier { _deleteInProgress = true; state = true; try { + // Filter the assets based on the backed-up status final assets = onlyBackedUp ? deleteAssets.where((e) => e.storage == AssetState.merged) : deleteAssets; + + if (assets.isEmpty) { + return false; // No assets to delete + } + + // Proceed with local deletion of the filtered assets final localDeleted = await _deleteLocalAssets(assets); + if (localDeleted.isNotEmpty) { - final localOnlyIds = deleteAssets + final localOnlyIds = assets .where((e) => e.storage == AssetState.local) .map((e) => e.id) .toList(); - // Update merged assets to remote only + + // Update merged assets to remote-only final mergedAssets = - deleteAssets.where((e) => e.storage == AssetState.merged).map((e) { + assets.where((e) => e.storage == AssetState.merged).map((e) { e.localId = null; return e; }).toList(); + + // Update the local database await _db.writeTxn(() async { if (mergedAssets.isNotEmpty) { - await _db.assets.putAll(mergedAssets); + await _db.assets + .putAll(mergedAssets); // Use the filtered merged assets } await _db.exifInfos.deleteAll(localOnlyIds); await _db.assets.deleteAll(localOnlyIds); }); + return true; } } finally { _deleteInProgress = false; state = false; } + return false; } diff --git a/mobile/lib/widgets/asset_grid/multiselect_grid.dart b/mobile/lib/widgets/asset_grid/multiselect_grid.dart index eeecfa9b58..2bceafb595 100644 --- a/mobile/lib/widgets/asset_grid/multiselect_grid.dart +++ b/mobile/lib/widgets/asset_grid/multiselect_grid.dart @@ -203,18 +203,30 @@ class MultiselectGrid extends HookConsumerWidget { void onDeleteLocal(bool onlyBackedUp) async { processing.value = true; try { + // Select only the local assets from the selection final localIds = selection.value.where((a) => a.isLocal).toList(); + // Delete only the backed-up assets if 'onlyBackedUp' is true final isDeleted = await ref .read(assetProvider.notifier) .deleteLocalOnlyAssets(localIds, onlyBackedUp: onlyBackedUp); + if (isDeleted) { + // Show a toast with the correct number of deleted assets + final deletedCount = localIds + .where( + (e) => !onlyBackedUp || e.isRemote, + ) // Only count backed-up assets + .length; + ImmichToast.show( context: context, msg: 'assets_removed_permanently_from_device' - .tr(args: ["${localIds.length}"]), + .tr(args: ["$deletedCount"]), gravity: ToastGravity.BOTTOM, ); + + // Reset the selection selectionEnabledHook.value = false; } } finally {