Navigate to an existing DM instead of creating a new one.

This commit is contained in:
Onuray Sahin 2020-11-04 16:32:25 +03:00 committed by Benoit Marty
parent b4f9d40767
commit e7714da8e8
2 changed files with 26 additions and 1 deletions

View File

@ -39,6 +39,7 @@ Improvements 🙌:
- Add graphic resources for F-Droid (#812, #2220) - Add graphic resources for F-Droid (#812, #2220)
- Highlight text in the body of the displayed result (#2200) - Highlight text in the body of the displayed result (#2200)
- Considerably faster QR-code bitmap generation (#2331) - Considerably faster QR-code bitmap generation (#2331)
- Open an existing DM instead of creating a new one (#2319)
Bugfix 🐛: Bugfix 🐛:
- Fixed ringtone handling (#2100 & #2246) - Fixed ringtone handling (#2100 & #2246)

View File

@ -19,6 +19,7 @@ package im.vector.app.features.createdirect
import androidx.lifecycle.viewModelScope import androidx.lifecycle.viewModelScope
import com.airbnb.mvrx.ActivityViewModelContext import com.airbnb.mvrx.ActivityViewModelContext
import com.airbnb.mvrx.MvRxViewModelFactory import com.airbnb.mvrx.MvRxViewModelFactory
import com.airbnb.mvrx.Success
import com.airbnb.mvrx.ViewModelContext import com.airbnb.mvrx.ViewModelContext
import com.squareup.inject.assisted.Assisted import com.squareup.inject.assisted.Assisted
import com.squareup.inject.assisted.AssistedInject import com.squareup.inject.assisted.AssistedInject
@ -56,10 +57,33 @@ class CreateDirectRoomViewModel @AssistedInject constructor(@Assisted
override fun handle(action: CreateDirectRoomAction) { override fun handle(action: CreateDirectRoomAction) {
when (action) { 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<PendingInvitee>) {
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<PendingInvitee>) { private fun createRoomAndInviteSelectedUsers(invitees: Set<PendingInvitee>) {
viewModelScope.launch(Dispatchers.IO) { viewModelScope.launch(Dispatchers.IO) {
val adminE2EByDefault = rawService.getElementWellknown(session.myUserId) val adminE2EByDefault = rawService.getElementWellknown(session.myUserId)