From 5036a8da5928176f18a2f2f477184d680da05db1 Mon Sep 17 00:00:00 2001 From: AmanRajSinghMourya Date: Fri, 29 Aug 2025 20:20:16 +0530 Subject: [PATCH] Add method to leave collection --- .../collections/collections_api_client.dart | 27 +++++++++- .../collections/collections_service.dart | 5 +- .../locker/lib/utils/collection_actions.dart | 50 ++++++++++++++++++- 3 files changed, 77 insertions(+), 5 deletions(-) diff --git a/mobile/apps/locker/lib/services/collections/collections_api_client.dart b/mobile/apps/locker/lib/services/collections/collections_api_client.dart index 9867e3880b..e4e0d1fc60 100644 --- a/mobile/apps/locker/lib/services/collections/collections_api_client.dart +++ b/mobile/apps/locker/lib/services/collections/collections_api_client.dart @@ -171,6 +171,26 @@ class CollectionApiClient { } } + Future leaveCollection(Collection collection) async { + try { + await _enteDio.post( + "/collections/leave/${collection.id}", + ); + await _handleCollectionDeletion(collection); + Bus.instance.fire(CollectionsUpdatedEvent()); + } catch (e, s) { + _logger.severe("failed to leave collection", e, s); + rethrow; + } + } + + Future _handleCollectionDeletion(Collection collection) async { + await _db.deleteCollection(collection); + final deletedCollection = collection.copyWith(isDeleted: true); + unawaited(_db.updateCollections([deletedCollection])); + CollectionService.instance.updateCollectionCache(deletedCollection); + } + Future move( EnteFile file, Collection fromCollection, @@ -423,8 +443,13 @@ class CollectionApiClient { await _db.updateCollections([collection]); CollectionService.instance.updateCollectionCache(collection); Bus.instance.fire(CollectionsUpdatedEvent()); + } on DioException catch (e) { + if (e.response?.statusCode == 402) { + throw SharingNotPermittedForFreeAccountsError(); + } + rethrow; } catch (e, s) { - _logger.severe('Failed to create share URL for collection', e, s); + _logger.severe("failed to rename collection", e, s); rethrow; } } diff --git a/mobile/apps/locker/lib/services/collections/collections_service.dart b/mobile/apps/locker/lib/services/collections/collections_service.dart index c2d92a9798..b92b2b568d 100644 --- a/mobile/apps/locker/lib/services/collections/collections_service.dart +++ b/mobile/apps/locker/lib/services/collections/collections_service.dart @@ -59,6 +59,9 @@ class CollectionService { _init(); }); } + Bus.instance.on().listen((event) { + _init(); + }); } Future sync() async { @@ -401,8 +404,6 @@ class CollectionService { /// - Owners of collections shared to user. /// - All collaborators of collections in which user is a collaborator or /// a viewer. - /// - All family members of user. - /// - All contacts linked to a person. List getRelevantContacts() { final List relevantUsers = []; final existingEmails = {}; diff --git a/mobile/apps/locker/lib/utils/collection_actions.dart b/mobile/apps/locker/lib/utils/collection_actions.dart index ce17a4420e..d0a014e573 100644 --- a/mobile/apps/locker/lib/utils/collection_actions.dart +++ b/mobile/apps/locker/lib/utils/collection_actions.dart @@ -171,6 +171,53 @@ class CollectionActions { } } + static Future leaveCollection( + BuildContext context, + Collection collection, { + VoidCallback? onSuccess, + }) async { + final actionResult = await showActionSheet( + context: context, + buttons: [ + ButtonWidget( + buttonType: ButtonType.critical, + isInAlert: true, + shouldStickToDarkTheme: true, + buttonAction: ButtonAction.first, + shouldSurfaceExecutionStates: true, + labelText: context.l10n.leaveCollection, + onTap: () async { + await CollectionApiClient.instance.leaveCollection(collection); + }, + ), + ButtonWidget( + buttonType: ButtonType.secondary, + buttonAction: ButtonAction.cancel, + isInAlert: true, + shouldStickToDarkTheme: true, + labelText: context.l10n.cancel, + ), + ], + title: context.l10n.leaveCollection, + body: context.l10n.filesAddedByYouWillBeRemovedFromTheCollection, + ); + if (actionResult?.action != null && context.mounted) { + if (actionResult!.action == ButtonAction.error) { + await showGenericErrorDialog( + context: context, + error: actionResult.exception, + ); + } else if (actionResult.action == ButtonAction.first) { + onSuccess?.call(); + Navigator.of(context).pop(); + SnackBarUtils.showInfoSnackBar( + context, + "Leave collection successfully", + ); + } + } + } + static Future enableUrl( BuildContext context, Collection collection, { @@ -411,7 +458,7 @@ class CollectionActions { } } - // removeParticipant remove the user from a share album + // removeParticipant remove the user from a share album Future removeParticipant( BuildContext context, Collection collection, @@ -455,5 +502,4 @@ class CollectionActions { } return false; } - }