Cleanup after rebase - avoid computing twice the existing roomId

This commit is contained in:
Benoit Marty 2020-11-05 17:34:10 +01:00
parent 458b4259fe
commit 12d8bd1743
8 changed files with 36 additions and 38 deletions

View file

@ -5,7 +5,7 @@ Features ✨:
- -
Improvements 🙌: Improvements 🙌:
- - Open an existing DM instead of creating a new one (#2319)
Bugfix 🐛: Bugfix 🐛:
- Fix issue when updating the avatar of a room - Fix issue when updating the avatar of a room
@ -39,7 +39,6 @@ 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

@ -20,5 +20,8 @@ import im.vector.app.core.platform.VectorViewModelAction
import im.vector.app.features.userdirectory.PendingInvitee import im.vector.app.features.userdirectory.PendingInvitee
sealed class CreateDirectRoomAction : VectorViewModelAction { sealed class CreateDirectRoomAction : VectorViewModelAction {
data class CreateRoomAndInviteSelectedUsers(val invitees: Set<PendingInvitee>) : CreateDirectRoomAction() data class CreateRoomAndInviteSelectedUsers(
val invitees: Set<PendingInvitee>,
val existingDmRoomId: String?
) : CreateDirectRoomAction()
} }

View file

@ -122,7 +122,10 @@ class CreateDirectRoomActivity : SimpleFragmentActivity() {
private fun onMenuItemSelected(action: UserDirectorySharedAction.OnMenuItemSelected) { private fun onMenuItemSelected(action: UserDirectorySharedAction.OnMenuItemSelected) {
if (action.itemId == R.id.action_create_direct_room) { if (action.itemId == R.id.action_create_direct_room) {
viewModel.handle(CreateDirectRoomAction.CreateRoomAndInviteSelectedUsers(action.invitees)) viewModel.handle(CreateDirectRoomAction.CreateRoomAndInviteSelectedUsers(
action.invitees,
action.existingDmRoomId
))
} }
} }

View file

@ -57,31 +57,23 @@ class CreateDirectRoomViewModel @AssistedInject constructor(@Assisted
override fun handle(action: CreateDirectRoomAction) { override fun handle(action: CreateDirectRoomAction) {
when (action) { when (action) {
is CreateDirectRoomAction.CreateRoomAndInviteSelectedUsers -> onSubmitInvitees(action.invitees) is CreateDirectRoomAction.CreateRoomAndInviteSelectedUsers -> onSubmitInvitees(action)
} }.exhaustive
} }
/** /**
* If users already have a DM room then navigate to it instead of creating a new room. * If users already have a DM room then navigate to it instead of creating a new room.
*/ */
private fun onSubmitInvitees(invitees: Set<PendingInvitee>) { private fun onSubmitInvitees(action: CreateDirectRoomAction.CreateRoomAndInviteSelectedUsers) {
invitees if (action.existingDmRoomId != null) {
.takeIf { it.size == 1 } // Do not create a new DM, just tell that the creation is successful by passing the existing roomId
?.first() setState {
?.let { invitee -> copy(createAndInviteState = Success(action.existingDmRoomId))
when (invitee) { }
is PendingInvitee.UserPendingInvitee -> session.getExistingDirectRoomWithUser(invitee.user.userId) } else {
is PendingInvitee.ThreePidPendingInvitee -> null // Create the DM
}.exhaustive createRoomAndInviteSelectedUsers(action.invitees)
} }
?.let { roomId ->
setState {
copy(createAndInviteState = Success(roomId))
}
}
?: run {
createRoomAndInviteSelectedUsers(invitees)
}
} }
private fun createRoomAndInviteSelectedUsers(invitees: Set<PendingInvitee>) { private fun createRoomAndInviteSelectedUsers(invitees: Set<PendingInvitee>) {

View file

@ -92,7 +92,7 @@ class KnownUsersFragment @Inject constructor(
menu.forEach { menuItem -> menu.forEach { menuItem ->
menuItem.isVisible = showMenuItem menuItem.isVisible = showMenuItem
if (args.isCreatingRoom) { if (args.isCreatingRoom) {
menuItem.setTitle(if (it.isThereAnExistingRoom) R.string.action_open else R.string.create_room_action_create) menuItem.setTitle(if (it.existingDmRoomId != null) R.string.action_open else R.string.create_room_action_create)
} }
} }
} }
@ -100,7 +100,11 @@ class KnownUsersFragment @Inject constructor(
} }
override fun onOptionsItemSelected(item: MenuItem): Boolean = withState(viewModel) { override fun onOptionsItemSelected(item: MenuItem): Boolean = withState(viewModel) {
sharedActionViewModel.post(UserDirectorySharedAction.OnMenuItemSelected(item.itemId, it.pendingInvitees)) sharedActionViewModel.post(UserDirectorySharedAction.OnMenuItemSelected(
item.itemId,
it.pendingInvitees,
it.existingDmRoomId
))
return@withState true return@withState true
} }

View file

@ -23,5 +23,7 @@ sealed class UserDirectorySharedAction : VectorSharedAction {
object OpenPhoneBook : UserDirectorySharedAction() object OpenPhoneBook : UserDirectorySharedAction()
object Close : UserDirectorySharedAction() object Close : UserDirectorySharedAction()
object GoBack : UserDirectorySharedAction() object GoBack : UserDirectorySharedAction()
data class OnMenuItemSelected(val itemId: Int, val invitees: Set<PendingInvitee>) : UserDirectorySharedAction() data class OnMenuItemSelected(val itemId: Int,
val invitees: Set<PendingInvitee>,
val existingDmRoomId: String?) : UserDirectorySharedAction()
} }

View file

@ -90,7 +90,7 @@ class UserDirectoryViewModel @AssistedInject constructor(@Assisted
setState { setState {
copy( copy(
pendingInvitees = selectedUsers, pendingInvitees = selectedUsers,
isThereAnExistingRoom = isThereAnExistingRoom(selectedUsers) existingDmRoomId = getExistingDmRoomId(selectedUsers)
) )
} }
} }
@ -102,22 +102,17 @@ class UserDirectoryViewModel @AssistedInject constructor(@Assisted
setState { setState {
copy( copy(
pendingInvitees = selectedUsers, pendingInvitees = selectedUsers,
isThereAnExistingRoom = isThereAnExistingRoom(selectedUsers) existingDmRoomId = getExistingDmRoomId(selectedUsers)
) )
} }
} }
private fun isThereAnExistingRoom(selectedUsers: Set<PendingInvitee>): Boolean { private fun getExistingDmRoomId(selectedUsers: Set<PendingInvitee>): String? {
return selectedUsers return selectedUsers
.takeIf { it.size == 1 } .takeIf { it.size == 1 }
?.filterIsInstance(PendingInvitee.UserPendingInvitee::class.java)
?.firstOrNull() ?.firstOrNull()
?.let { invitee -> ?.let { invitee -> session.getExistingDirectRoomWithUser(invitee.user.userId) }
return when (invitee) {
is PendingInvitee.UserPendingInvitee -> session.getExistingDirectRoomWithUser(invitee.user.userId) != null
is PendingInvitee.ThreePidPendingInvitee -> false
}.exhaustive
}
?: false
} }
private fun observeDirectoryUsers() = withState { state -> private fun observeDirectoryUsers() = withState { state ->

View file

@ -31,7 +31,7 @@ data class UserDirectoryViewState(
val createAndInviteState: Async<String> = Uninitialized, val createAndInviteState: Async<String> = Uninitialized,
val directorySearchTerm: String = "", val directorySearchTerm: String = "",
val filterKnownUsersValue: Option<String> = Option.empty(), val filterKnownUsersValue: Option<String> = Option.empty(),
val isThereAnExistingRoom: Boolean = false val existingDmRoomId: String? = null
) : MvRxState { ) : MvRxState {
constructor(args: KnownUsersFragmentArgs) : this(excludedUserIds = args.excludedUserIds) constructor(args: KnownUsersFragmentArgs) : this(excludedUserIds = args.excludedUserIds)