From e7714da8e8f65b8764daf762f9e3b9d4cf2df04a Mon Sep 17 00:00:00 2001 From: Onuray Sahin Date: Wed, 4 Nov 2020 16:32:25 +0300 Subject: [PATCH] Navigate to an existing DM instead of creating a new one. --- CHANGES.md | 1 + .../createdirect/CreateDirectRoomViewModel.kt | 26 ++++++++++++++++++- 2 files changed, 26 insertions(+), 1 deletion(-) diff --git a/CHANGES.md b/CHANGES.md index 79ea40de59..31dd8388e0 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -39,6 +39,7 @@ Improvements 🙌: - Add graphic resources for F-Droid (#812, #2220) - Highlight text in the body of the displayed result (#2200) - Considerably faster QR-code bitmap generation (#2331) + - Open an existing DM instead of creating a new one (#2319) Bugfix 🐛: - Fixed ringtone handling (#2100 & #2246) diff --git a/vector/src/main/java/im/vector/app/features/createdirect/CreateDirectRoomViewModel.kt b/vector/src/main/java/im/vector/app/features/createdirect/CreateDirectRoomViewModel.kt index c42e10686f..7d8b8c0c82 100644 --- a/vector/src/main/java/im/vector/app/features/createdirect/CreateDirectRoomViewModel.kt +++ b/vector/src/main/java/im/vector/app/features/createdirect/CreateDirectRoomViewModel.kt @@ -19,6 +19,7 @@ package im.vector.app.features.createdirect import androidx.lifecycle.viewModelScope import com.airbnb.mvrx.ActivityViewModelContext import com.airbnb.mvrx.MvRxViewModelFactory +import com.airbnb.mvrx.Success import com.airbnb.mvrx.ViewModelContext import com.squareup.inject.assisted.Assisted import com.squareup.inject.assisted.AssistedInject @@ -56,10 +57,33 @@ class CreateDirectRoomViewModel @AssistedInject constructor(@Assisted override fun handle(action: CreateDirectRoomAction) { when (action) { - is CreateDirectRoomAction.CreateRoomAndInviteSelectedUsers -> createRoomAndInviteSelectedUsers(action.invitees) + is CreateDirectRoomAction.CreateRoomAndInviteSelectedUsers -> onSubmitInvitees(action.invitees) } } + /** + * If users already have a DM room then navigate to it instead of creating a new room. + */ + private fun onSubmitInvitees(invitees: Set) { + invitees + .takeIf { it.size == 1 } + ?.first() + ?.let { invitee -> + when (invitee) { + is PendingInvitee.UserPendingInvitee -> session.getExistingDirectRoomWithUser(invitee.user.userId) + is PendingInvitee.ThreePidPendingInvitee -> null + }.exhaustive + } + ?.let { roomId -> + setState { + copy(createAndInviteState = Success(roomId)) + } + } + ?: run { + createRoomAndInviteSelectedUsers(invitees) + } + } + private fun createRoomAndInviteSelectedUsers(invitees: Set) { viewModelScope.launch(Dispatchers.IO) { val adminE2EByDefault = rawService.getElementWellknown(session.myUserId)