Browse Source

Fix state refresh issue on collab add/remove

Neeraj Gupta 2 years ago
parent
commit
4143a14ab3

+ 22 - 8
lib/services/collections_service.dart

@@ -269,7 +269,7 @@ class CollectionsService {
     });
   }
 
-  Future<void> share(
+  Future<List<User>> share(
     int collectionID,
     String email,
     String publicKey,
@@ -280,7 +280,7 @@ class CollectionsService {
       Sodium.base642bin(publicKey),
     );
     try {
-      await _enteDio.post(
+      final response = await _enteDio.post(
         "/collections/share",
         data: {
           "collectionID": collectionID,
@@ -289,28 +289,42 @@ class CollectionsService {
           "role": role.toStringVal()
         },
       );
+      final sharees = <User>[];
+      for (final user in response.data["sharees"]) {
+        sharees.add(User.fromMap(user));
+      }
+      _collectionIDToCollections[collectionID] =
+          _collectionIDToCollections[collectionID].copyWith(sharees: sharees);
+      unawaited(_db.insert([_collectionIDToCollections[collectionID]]));
+      RemoteSyncService.instance.sync(silently: true).ignore();
+      return sharees;
     } on DioError catch (e) {
       if (e.response.statusCode == 402) {
         throw SharingNotPermittedForFreeAccountsError();
       }
       rethrow;
     }
-    RemoteSyncService.instance.sync(silently: true);
   }
 
-  Future<void> unshare(int collectionID, String email) async {
+  Future<List<User>> unshare(int collectionID, String email) async {
     try {
-      await _enteDio.post(
+      final response = await _enteDio.post(
         "/collections/unshare",
         data: {
           "collectionID": collectionID,
           "email": email,
         },
       );
-      _collectionIDToCollections[collectionID]
-          .sharees
-          .removeWhere((user) => user.email == email);
+      final sharees = <User>[];
+      for (final user in response.data["sharees"]) {
+        sharees.add(User.fromMap(user));
+      }
+
+      _collectionIDToCollections[collectionID] =
+          _collectionIDToCollections[collectionID].copyWith(sharees: sharees);
       unawaited(_db.insert([_collectionIDToCollections[collectionID]]));
+      RemoteSyncService.instance.sync(silently: true).ignore();
+      return sharees;
     } catch (e) {
       _logger.severe(e);
       rethrow;

+ 6 - 5
lib/ui/actions/collection/collection_sharing_actions.dart

@@ -54,7 +54,7 @@ class CollectionSharingActions {
       if (e is SharingNotPermittedForFreeAccountsError) {
         _showUnSupportedAlert(context);
       } else {
-        _logger.severe("failed to share collection", e);
+        _logger.severe("Failed to update shareUrl collection", e);
         showGenericErrorDialog(context);
       }
       return false;
@@ -83,8 +83,9 @@ class CollectionSharingActions {
     final dialog = createProgressDialog(context, "Please wait...");
     await dialog.show();
     try {
-      await CollectionsService.instance.unshare(collection.id, user.email);
-      collection.sharees!.removeWhere((u) => u!.email == user.email);
+      final newSharees =
+          await CollectionsService.instance.unshare(collection.id, user.email);
+      collection = collection.copyWith(sharees: newSharees);
       await dialog.hide();
       showToast(context, "Stopped sharing with " + user.email + ".");
       return true;
@@ -176,9 +177,9 @@ class CollectionSharingActions {
       final dialog = createProgressDialog(context, "Sharing...");
       await dialog.show();
       try {
-        await CollectionsService.instance
+        final newSharees = await CollectionsService.instance
             .share(collection.id, email, publicKey, role);
-        collection.sharees?.add((User(email: email, role: role.toStringVal())));
+        collection = collection.copyWith(sharees: newSharees);
         await dialog.hide();
         showShortToast(context, "Shared successfully!");
         return true;

+ 4 - 4
lib/ui/sharing/album_participants_page.dart

@@ -39,21 +39,21 @@ class _AlbumParticipantsPageState extends State<AlbumParticipantsPage> {
     if (user.id == currentUserID) {
       return;
     }
-    final result = await routeToPage(
+    await routeToPage(
       context,
       ManageIndividualParticipant(collection: widget.collection, user: user),
     );
-    if (result != null && mounted) {
+    if (mounted) {
       setState(() => {});
     }
   }
 
   Future<void> _navigateToAddUser(bool addingViewer) async {
-    final result = await routeToPage(
+    await routeToPage(
       context,
       AddParticipantPage(widget.collection),
     );
-    if (result != null && mounted) {
+    if (mounted) {
       setState(() => {});
     }
   }