From bd73e265cc8ef7a9fa668e7ec287372613fea6c4 Mon Sep 17 00:00:00 2001 From: Pierre HUBERT Date: Sat, 13 Mar 2021 10:52:53 +0100 Subject: [PATCH] Can add members to conversation --- lib/helpers/conversations_helper.dart | 9 +++++++ .../screens/update_conversation_screen.dart | 27 +++++++++++++------ 2 files changed, 28 insertions(+), 8 deletions(-) diff --git a/lib/helpers/conversations_helper.dart b/lib/helpers/conversations_helper.dart index 8662c48..22f5154 100644 --- a/lib/helpers/conversations_helper.dart +++ b/lib/helpers/conversations_helper.dart @@ -49,6 +49,15 @@ class ConversationsHelper { return response.getObject()["conversationID"]; } + /// Add a member to a conversation. + /// + /// Throws in case of failure + static Future addMember(int convID, int userID) async => + await APIRequest.withLogin("conversations/addMember") + .addInt("convID", convID) + .addInt("userID", userID) + .execWithThrow(); + /// Remove a member from a conversation. /// /// Throws in case of failure diff --git a/lib/ui/screens/update_conversation_screen.dart b/lib/ui/screens/update_conversation_screen.dart index 1f9a21d..0086733 100644 --- a/lib/ui/screens/update_conversation_screen.dart +++ b/lib/ui/screens/update_conversation_screen.dart @@ -166,14 +166,11 @@ class _UpdateConversationScreen extends State { // Add a member to the conversation PickUserWidget( - resetOnChoose: true, - keepFocusOnChoose: true, - label: tr("Add member"), - enabled: _canAddMembers, - onSelectUser: (user) => setState(() { - if (!_members.contains(user)) _members.insert(0, user); - }), - ), + resetOnChoose: true, + keepFocusOnChoose: true, + label: tr("Add member"), + enabled: _canAddMembers, + onSelectUser: (user) => _addMember(user)), //Conversation members Column( @@ -229,6 +226,20 @@ class _UpdateConversationScreen extends State { } } + void _addMember(User user) async { + try { + if (_members.contains(user)) return; + + if (isUpdating) + await ConversationsHelper.addMember(_conversation.id, user.id); + + setState(() => _members.insert(0, user)); + } catch (e, s) { + logError(e, s); + snack(context, tr("Failed to add member to conversation!")); + } + } + void _removeMember(User user) async { try { if (isUpdating)