π End-to-end encrypted cloud for everything.
Dart 2faandroidauthye2eeencryptionend-to-end-encryptionfluttergolanggoogle-photosgoogle-photos-alternativeiosopensourcephotosprivacyprivacy-toolstypescriptzero-knowledge
refactor: enhance collection trashing logic with bulk deletion support and event handling
AmanRajSinghMouryaf135da97b51c
diff --git a/mobile/apps/locker/lib/services/collections/collections_api_client.dart b/mobile/apps/locker/lib/services/collections/collections_api_client.dartindex bcba3c24b3..d601deb282 100644--- a/mobile/apps/locker/lib/services/collections/collections_api_client.dart+++ b/mobile/apps/locker/lib/services/collections/collections_api_client.dart@@ -257,6 +257,7 @@ class CollectionApiClient {Future<void> trashCollection(Collection collection, {bool keepFiles = false,+ bool skipEventFiring = false,}) async {try {await _enteDio.delete(@@ -264,7 +265,13 @@ class CollectionApiClient {"?keepFiles=${keepFiles ? "True" : "False"}""&collectionID=${collection.id}",);- await _handleCollectionDeletion(collection);+ if (skipEventFiring) {+ await _db.deleteCollection(collection);+ final deletedCollection = collection.copyWith(isDeleted: true);+ await _updateCollectionInDB(deletedCollection);+ } else {+ await _handleCollectionDeletion(collection);+ }} catch (e) {_logger.severe('failed to trash collection', e);rethrow;diff --git a/mobile/apps/locker/lib/services/collections/collections_service.dart b/mobile/apps/locker/lib/services/collections/collections_service.dartindex 738860dbc7..90bb3e3bff 100644--- a/mobile/apps/locker/lib/services/collections/collections_service.dart+++ b/mobile/apps/locker/lib/services/collections/collections_service.dart@@ -419,6 +419,33 @@ class CollectionService {}}+ /// Trash an empty collection directly without moving files.+ /// The server will verify that the collection is actually empty before+ /// deleting. If keepFiles is set as False and the collection is not empty,+ /// then the files in the collection will be moved to trash.+ ///+ /// [isBulkDelete] - During bulk deletion, this event is not fired to avoid+ /// quick refresh of the collection gallery+ Future<void> trashEmptyCollection(+ Collection collection, {+ bool isBulkDelete = false,+ }) async {+ try {+ await _apiClient.trashCollection(+ collection,+ keepFiles: true,+ skipEventFiring: isBulkDelete,+ );+ if (!isBulkDelete) {+ await sync();+ await TrashService.instance.syncTrash();+ }+ } catch (e) {+ _logger.severe("Failed to trash empty collection: $e");+ rethrow;+ }+ }+Future<void> moveFilesFromCurrentCollection(Collection collection,List<EnteFile> files,diff --git a/mobile/apps/locker/lib/ui/components/collection_popup_menu_widget.dart b/mobile/apps/locker/lib/ui/components/collection_popup_menu_widget.dartindex 9e35a8822a..47c0f486ac 100644--- a/mobile/apps/locker/lib/ui/components/collection_popup_menu_widget.dart+++ b/mobile/apps/locker/lib/ui/components/collection_popup_menu_widget.dart@@ -1,8 +1,6 @@-import "package:ente_events/event_bus.dart";import "package:ente_ui/theme/ente_theme.dart";import "package:flutter/material.dart";import "package:hugeicons/hugeicons.dart";-import "package:locker/events/collections_updated_event.dart";import "package:locker/l10n/l10n.dart";import "package:locker/services/collections/models/collection.dart";import "package:locker/services/collections/models/collection_view_type.dart";@@ -171,38 +169,14 @@ class CollectionPopupMenuWidget extends StatelessWidget {}Future<void> _editCollection(BuildContext context) async {- await CollectionActions.editCollection(- context,- collection,- onSuccess: () {- Bus.instance.fire(- CollectionsUpdatedEvent("rename_collection"),- );- },- );+ await CollectionActions.editCollection(context, collection);}Future<void> _deleteCollection(BuildContext context) async {- await CollectionActions.deleteCollection(- context,- collection,- onSuccess: () {- Bus.instance.fire(- CollectionsUpdatedEvent("delete_collection"),- );- },- );+ await CollectionActions.deleteCollection(context, collection);}Future<void> _leaveCollection(BuildContext context) async {- await CollectionActions.leaveCollection(- context,- collection,- onSuccess: () {- Bus.instance.fire(- CollectionsUpdatedEvent("leave_collection"),- );- },- );+ await CollectionActions.leaveCollection(context, collection);}}diff --git a/mobile/apps/locker/lib/ui/viewer/actions/file_selection_overlay_bar.dart b/mobile/apps/locker/lib/ui/viewer/actions/file_selection_overlay_bar.dartindex 1beb016b3e..ea332259e6 100644--- a/mobile/apps/locker/lib/ui/viewer/actions/file_selection_overlay_bar.dart+++ b/mobile/apps/locker/lib/ui/viewer/actions/file_selection_overlay_bar.dart@@ -431,7 +431,7 @@ class _FileSelectionOverlayBarState extends State<FileSelectionOverlayBar> {}Future<void> _shareFileLink(BuildContext context, EnteFile file) async {- await FileActions.shareFileLink(context, file,);+ await FileActions.shareFileLink(context, file);}Future<void> _editFile(BuildContext context, EnteFile file) async {diff --git a/mobile/apps/locker/lib/utils/collection_actions.dart b/mobile/apps/locker/lib/utils/collection_actions.dartindex 1c08610832..74c4c1cff5 100644--- a/mobile/apps/locker/lib/utils/collection_actions.dart+++ b/mobile/apps/locker/lib/utils/collection_actions.dart@@ -20,6 +20,7 @@ import "package:locker/services/collections/collections_api_client.dart";import 'package:locker/services/collections/collections_service.dart';import 'package:locker/services/collections/models/collection.dart';import "package:locker/services/configuration.dart";+import "package:locker/services/trash/trash_service.dart";import "package:locker/ui/components/delete_confirmation_dialog.dart";import "package:locker/ui/components/input_dialog_sheet.dart";import 'package:locker/utils/snack_bar_utils.dart';@@ -141,6 +142,9 @@ class CollectionActions {bool isFavoriteCollection = false;final bool keepFiles = !(dialogChoice?.deleteFromAllCollections ?? false);+ final List<Collection> emptyCollections = [];+ final List<Collection> nonEmptyCollections = [];+ final List<dynamic> errors = [];try {for (final collection in collections) {@@ -148,16 +152,58 @@ class CollectionActions {isFavoriteCollection = true;continue;}- if (collection.type.canDelete) {+ if (!collection.type.canDelete) {+ continue;+ }++ final fileCount =+ await CollectionService.instance.getFileCount(collection);++ if (fileCount == 0) {+ emptyCollections.add(collection);+ } else {+ nonEmptyCollections.add(collection);+ }+ }++ for (final collection in emptyCollections) {+ try {+ await CollectionService.instance.trashEmptyCollection(+ collection,+ isBulkDelete: true,+ );+ } catch (e, s) {+ _logger.severe("Failed to trash empty collection", e, s);+ errors.add(e);+ }+ }++ if (emptyCollections.isNotEmpty) {+ await CollectionService.instance.sync();+ await TrashService.instance.syncTrash();+ }++ for (final collection in nonEmptyCollections) {+ try {await CollectionService.instance.trashCollection(collection,keepFiles: keepFiles,);+ } catch (e, s) {+ _logger.severe("Failed to trash collection", e, s);+ errors.add(e);}}await progressDialog.hide();+ if (errors.isNotEmpty) {+ await showGenericErrorDialog(+ context: context,+ error: errors.first,+ );+ }+SnackBarUtils.showInfoSnackBar(context,"${collections.length} collections deleted successfully",@@ -196,6 +242,35 @@ class CollectionActions {return;}+ final fileCount = await CollectionService.instance.getFileCount(collection);++ if (fileCount == 0) {+ final progressDialog = createProgressDialog(context, l10n.pleaseWait);+ await progressDialog.show();++ try {+ await CollectionService.instance.trashEmptyCollection(collection);++ await progressDialog.hide();++ SnackBarUtils.showInfoSnackBar(+ context,+ l10n.collectionDeletedSuccessfully,+ );++ // Call success callback if provided+ onSuccess?.call();+ } catch (error) {+ await progressDialog.hide();++ SnackBarUtils.showWarningSnackBar(+ context,+ l10n.failedToDeleteCollection(error.toString()),+ );+ }+ return;+ }+final collectionName = collection.name ?? 'this collection';final result = await showDeleteConfirmationDialog(