diff --git a/mobile/lib/providers/asset.provider.dart b/mobile/lib/providers/asset.provider.dart index c7e75df79b27f..3855a00b76a61 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 eeecfa9b58435..2bceafb595d3d 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 {