From 649221176c201b14e596323205f16b9dbcfdddf2 Mon Sep 17 00:00:00 2001 From: Mert <101130780+mertalev@users.noreply.github.com> Date: Thu, 17 Jul 2025 00:25:41 +0300 Subject: [PATCH] refactor(mobile): delete local button in new timeline (#19961) * delete local action button * include source * move prompt * batch --- i18n/en.json | 1 + .../repositories/local_asset.repository.dart | 13 +++++++ .../delete_local_action_button.widget.dart | 35 ++++++++++++++++- .../asset_viewer/bottom_sheet.widget.dart | 2 +- .../archive_bottom_sheet.widget.dart | 2 +- .../favorite_bottom_sheet.widget.dart | 2 +- .../general_bottom_sheet.widget.dart | 2 +- .../local_album_bottom_sheet.widget.dart | 3 +- .../remote_album_bottom_sheet.widget.dart | 2 +- .../infrastructure/action.provider.dart | 38 +++++++++++++++---- mobile/lib/services/action.service.dart | 13 +++++++ 11 files changed, 99 insertions(+), 14 deletions(-) diff --git a/i18n/en.json b/i18n/en.json index 6b9a0bffe6..175399c7ab 100644 --- a/i18n/en.json +++ b/i18n/en.json @@ -749,6 +749,7 @@ "delete_key": "Delete key", "delete_library": "Delete Library", "delete_link": "Delete link", + "delete_local_action_prompt": "{count} deleted locally", "delete_local_dialog_ok_backed_up_only": "Delete Backed Up Only", "delete_local_dialog_ok_force": "Delete Anyway", "delete_others": "Delete others", diff --git a/mobile/lib/infrastructure/repositories/local_asset.repository.dart b/mobile/lib/infrastructure/repositories/local_asset.repository.dart index cb6871cd22..31a11f7047 100644 --- a/mobile/lib/infrastructure/repositories/local_asset.repository.dart +++ b/mobile/lib/infrastructure/repositories/local_asset.repository.dart @@ -1,3 +1,4 @@ +import 'package:collection/collection.dart'; import 'package:drift/drift.dart'; import 'package:immich_mobile/domain/models/asset/base_asset.model.dart'; import 'package:immich_mobile/infrastructure/entities/local_asset.entity.dart'; @@ -43,4 +44,16 @@ class DriftLocalAssetRepository extends DriftDatabaseRepository { } }); } + + Future delete(List ids) { + if (ids.isEmpty) { + return Future.value(); + } + + return _db.batch((batch) { + for (final slice in ids.slices(32000)) { + batch.deleteWhere(_db.localAssetEntity, (e) => e.id.isIn(slice)); + } + }); + } } diff --git a/mobile/lib/presentation/widgets/action_buttons/delete_local_action_button.widget.dart b/mobile/lib/presentation/widgets/action_buttons/delete_local_action_button.widget.dart index dfc84b4190..90534ca68c 100644 --- a/mobile/lib/presentation/widgets/action_buttons/delete_local_action_button.widget.dart +++ b/mobile/lib/presentation/widgets/action_buttons/delete_local_action_button.widget.dart @@ -1,10 +1,42 @@ import 'package:flutter/material.dart'; +import 'package:fluttertoast/fluttertoast.dart'; import 'package:hooks_riverpod/hooks_riverpod.dart'; +import 'package:immich_mobile/constants/enums.dart'; import 'package:immich_mobile/extensions/translate_extensions.dart'; import 'package:immich_mobile/presentation/widgets/action_buttons/base_action_button.widget.dart'; +import 'package:immich_mobile/providers/infrastructure/action.provider.dart'; +import 'package:immich_mobile/providers/timeline/multiselect.provider.dart'; +import 'package:immich_mobile/widgets/common/immich_toast.dart'; class DeleteLocalActionButton extends ConsumerWidget { - const DeleteLocalActionButton({super.key}); + final ActionSource source; + + const DeleteLocalActionButton({super.key, required this.source}); + + void _onTap(BuildContext context, WidgetRef ref) async { + if (!context.mounted) { + return; + } + + final result = await ref.read(actionProvider.notifier).deleteLocal(source); + ref.read(multiSelectProvider.notifier).reset(); + + final successMessage = 'delete_local_action_prompt'.t( + context: context, + args: {'count': result.count.toString()}, + ); + + if (context.mounted) { + ImmichToast.show( + context: context, + msg: result.success + ? successMessage + : 'scaffold_body_error_occurred'.t(context: context), + gravity: ToastGravity.BOTTOM, + toastType: result.success ? ToastType.success : ToastType.error, + ); + } + } @override Widget build(BuildContext context, WidgetRef ref) { @@ -12,6 +44,7 @@ class DeleteLocalActionButton extends ConsumerWidget { maxWidth: 95.0, iconData: Icons.no_cell_outlined, label: "control_bottom_app_bar_delete_from_local".t(context: context), + onPressed: () => _onTap(context, ref), ); } } diff --git a/mobile/lib/presentation/widgets/asset_viewer/bottom_sheet.widget.dart b/mobile/lib/presentation/widgets/asset_viewer/bottom_sheet.widget.dart index a7a2a57ce5..0ad87e05e8 100644 --- a/mobile/lib/presentation/widgets/asset_viewer/bottom_sheet.widget.dart +++ b/mobile/lib/presentation/widgets/asset_viewer/bottom_sheet.widget.dart @@ -58,7 +58,7 @@ class AssetDetailBottomSheet extends ConsumerWidget { ), ], if (asset.storage == AssetState.local) ...[ - const DeleteLocalActionButton(), + const DeleteLocalActionButton(source: ActionSource.viewer), const UploadActionButton(), ], ]; diff --git a/mobile/lib/presentation/widgets/bottom_sheet/archive_bottom_sheet.widget.dart b/mobile/lib/presentation/widgets/bottom_sheet/archive_bottom_sheet.widget.dart index c23f268465..3c21630d2e 100644 --- a/mobile/lib/presentation/widgets/bottom_sheet/archive_bottom_sheet.widget.dart +++ b/mobile/lib/presentation/widgets/bottom_sheet/archive_bottom_sheet.widget.dart @@ -52,7 +52,7 @@ class ArchiveBottomSheet extends ConsumerWidget { const StackActionButton(), ], if (multiselect.hasLocal) ...[ - const DeleteLocalActionButton(), + const DeleteLocalActionButton(source: ActionSource.timeline), const UploadActionButton(), ], ], diff --git a/mobile/lib/presentation/widgets/bottom_sheet/favorite_bottom_sheet.widget.dart b/mobile/lib/presentation/widgets/bottom_sheet/favorite_bottom_sheet.widget.dart index 2f8208a80b..d73d3c22e0 100644 --- a/mobile/lib/presentation/widgets/bottom_sheet/favorite_bottom_sheet.widget.dart +++ b/mobile/lib/presentation/widgets/bottom_sheet/favorite_bottom_sheet.widget.dart @@ -52,7 +52,7 @@ class FavoriteBottomSheet extends ConsumerWidget { const StackActionButton(), ], if (multiselect.hasLocal) ...[ - const DeleteLocalActionButton(), + const DeleteLocalActionButton(source: ActionSource.timeline), const UploadActionButton(), ], ], diff --git a/mobile/lib/presentation/widgets/bottom_sheet/general_bottom_sheet.widget.dart b/mobile/lib/presentation/widgets/bottom_sheet/general_bottom_sheet.widget.dart index 900adefd0b..6726f18246 100644 --- a/mobile/lib/presentation/widgets/bottom_sheet/general_bottom_sheet.widget.dart +++ b/mobile/lib/presentation/widgets/bottom_sheet/general_bottom_sheet.widget.dart @@ -52,7 +52,7 @@ class GeneralBottomSheet extends ConsumerWidget { const StackActionButton(), ], if (multiselect.hasLocal) ...[ - const DeleteLocalActionButton(), + const DeleteLocalActionButton(source: ActionSource.timeline), const UploadActionButton(), ], ], diff --git a/mobile/lib/presentation/widgets/bottom_sheet/local_album_bottom_sheet.widget.dart b/mobile/lib/presentation/widgets/bottom_sheet/local_album_bottom_sheet.widget.dart index 3fd717f516..d41f8ecb96 100644 --- a/mobile/lib/presentation/widgets/bottom_sheet/local_album_bottom_sheet.widget.dart +++ b/mobile/lib/presentation/widgets/bottom_sheet/local_album_bottom_sheet.widget.dart @@ -1,5 +1,6 @@ import 'package:flutter/material.dart'; import 'package:hooks_riverpod/hooks_riverpod.dart'; +import 'package:immich_mobile/constants/enums.dart'; import 'package:immich_mobile/presentation/widgets/action_buttons/delete_local_action_button.widget.dart'; import 'package:immich_mobile/presentation/widgets/action_buttons/share_action_button.widget.dart'; import 'package:immich_mobile/presentation/widgets/action_buttons/upload_action_button.widget.dart'; @@ -16,7 +17,7 @@ class LocalAlbumBottomSheet extends ConsumerWidget { shouldCloseOnMinExtent: false, actions: [ ShareActionButton(), - DeleteLocalActionButton(), + DeleteLocalActionButton(source: ActionSource.timeline), UploadActionButton(), ], ); diff --git a/mobile/lib/presentation/widgets/bottom_sheet/remote_album_bottom_sheet.widget.dart b/mobile/lib/presentation/widgets/bottom_sheet/remote_album_bottom_sheet.widget.dart index 0268a2b386..1ed662884a 100644 --- a/mobile/lib/presentation/widgets/bottom_sheet/remote_album_bottom_sheet.widget.dart +++ b/mobile/lib/presentation/widgets/bottom_sheet/remote_album_bottom_sheet.widget.dart @@ -55,7 +55,7 @@ class RemoteAlbumBottomSheet extends ConsumerWidget { const StackActionButton(), ], if (multiselect.hasLocal) ...[ - const DeleteLocalActionButton(), + const DeleteLocalActionButton(source: ActionSource.timeline), const UploadActionButton(), ], RemoveFromAlbumActionButton( diff --git a/mobile/lib/providers/infrastructure/action.provider.dart b/mobile/lib/providers/infrastructure/action.provider.dart index 49605e918a..25b5783137 100644 --- a/mobile/lib/providers/infrastructure/action.provider.dart +++ b/mobile/lib/providers/infrastructure/action.provider.dart @@ -41,7 +41,13 @@ class ActionNotifier extends Notifier { } List _getRemoteIdsForSource(ActionSource source) { - return _getIdsForSource(source).toIds().toList(); + return _getIdsForSource(source) + .toIds() + .toList(growable: false); + } + + List _getLocalIdsForSource(ActionSource source) { + return _getIdsForSource(source).toIds().toList(growable: false); } List _getOwnedRemoteForSource(ActionSource source) { @@ -49,23 +55,22 @@ class ActionNotifier extends Notifier { return _getIdsForSource(source) .ownedAssets(ownerId) .toIds() - .toList(); + .toList(growable: false); } Iterable _getIdsForSource(ActionSource source) { final Set assets = switch (source) { - ActionSource.timeline => - ref.read(multiSelectProvider.select((s) => s.selectedAssets)), + ActionSource.timeline => ref.read(multiSelectProvider).selectedAssets, ActionSource.viewer => switch (ref.read(currentAssetNotifier)) { BaseAsset asset => {asset}, - null => {}, + null => const {}, }, }; return switch (T) { const (RemoteAsset) => assets.whereType(), const (LocalAsset) => assets.whereType(), - _ => [], + _ => const [], } as Iterable; } @@ -207,6 +212,21 @@ class ActionNotifier extends Notifier { } } + Future deleteLocal(ActionSource source) async { + final ids = _getLocalIdsForSource(source); + try { + await _service.deleteLocal(ids); + return ActionResult(count: ids.length, success: true); + } catch (error, stack) { + _logger.severe('Failed to delete assets', error, stack); + return ActionResult( + count: ids.length, + success: false, + error: error.toString(), + ); + } + } + Future editLocation( ActionSource source, BuildContext context, @@ -252,7 +272,11 @@ extension on Iterable { Iterable toIds() => map((e) => e.id); Iterable ownedAssets(String? ownerId) { - if (ownerId == null) return []; + if (ownerId == null) return const []; return whereType().where((a) => a.ownerId == ownerId); } } + +extension on Iterable { + Iterable toIds() => map((e) => e.id); +} diff --git a/mobile/lib/services/action.service.dart b/mobile/lib/services/action.service.dart index d7c625b981..bb2e1514e8 100644 --- a/mobile/lib/services/action.service.dart +++ b/mobile/lib/services/action.service.dart @@ -2,11 +2,13 @@ import 'package:auto_route/auto_route.dart'; import 'package:flutter/material.dart'; import 'package:immich_mobile/constants/enums.dart'; import 'package:immich_mobile/domain/models/asset/base_asset.model.dart'; +import 'package:immich_mobile/infrastructure/repositories/local_asset.repository.dart'; import 'package:immich_mobile/infrastructure/repositories/remote_album.repository.dart'; import 'package:immich_mobile/infrastructure/repositories/remote_asset.repository.dart'; import 'package:immich_mobile/providers/infrastructure/album.provider.dart'; import 'package:immich_mobile/providers/infrastructure/asset.provider.dart'; import 'package:immich_mobile/repositories/asset_api.repository.dart'; +import 'package:immich_mobile/repositories/asset_media.repository.dart'; import 'package:immich_mobile/repositories/drift_album_api_repository.dart'; import 'package:immich_mobile/routing/router.dart'; import 'package:immich_mobile/widgets/common/location_picker.dart'; @@ -17,22 +19,28 @@ final actionServiceProvider = Provider( (ref) => ActionService( ref.watch(assetApiRepositoryProvider), ref.watch(remoteAssetRepositoryProvider), + ref.watch(localAssetRepository), ref.watch(driftAlbumApiRepositoryProvider), ref.watch(remoteAlbumRepository), + ref.watch(assetMediaRepositoryProvider), ), ); class ActionService { final AssetApiRepository _assetApiRepository; final RemoteAssetRepository _remoteAssetRepository; + final DriftLocalAssetRepository _localAssetRepository; final DriftAlbumApiRepository _albumApiRepository; final DriftRemoteAlbumRepository _remoteAlbumRepository; + final AssetMediaRepository _assetMediaRepository; const ActionService( this._assetApiRepository, this._remoteAssetRepository, + this._localAssetRepository, this._albumApiRepository, this._remoteAlbumRepository, + this._assetMediaRepository, ); Future shareLink(List remoteIds, BuildContext context) async { @@ -107,6 +115,11 @@ class ActionService { await _remoteAssetRepository.delete(remoteIds); } + Future deleteLocal(List localIds) async { + await _assetMediaRepository.deleteAll(localIds); + await _localAssetRepository.delete(localIds); + } + Future editLocation( List remoteIds, BuildContext context,