Merge pull request #2342 from vector-im/feature/ons/open_existing_dm
Navigate to an existing DM instead of creating a new one.
This commit is contained in:
commit
0753ba3495
@ -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
|
||||||
|
@ -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()
|
||||||
}
|
}
|
||||||
|
@ -91,7 +91,8 @@ class CreateDirectRoomActivity : SimpleFragmentActivity() {
|
|||||||
KnownUsersFragment::class.java,
|
KnownUsersFragment::class.java,
|
||||||
KnownUsersFragmentArgs(
|
KnownUsersFragmentArgs(
|
||||||
title = getString(R.string.fab_menu_create_chat),
|
title = getString(R.string.fab_menu_create_chat),
|
||||||
menuResId = R.menu.vector_create_direct_room
|
menuResId = R.menu.vector_create_direct_room,
|
||||||
|
isCreatingRoom = true
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
@ -121,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
|
||||||
|
))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -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,7 +57,22 @@ 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)
|
||||||
|
}.exhaustive
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* If users already have a DM room then navigate to it instead of creating a new room.
|
||||||
|
*/
|
||||||
|
private fun onSubmitInvitees(action: CreateDirectRoomAction.CreateRoomAndInviteSelectedUsers) {
|
||||||
|
if (action.existingDmRoomId != null) {
|
||||||
|
// Do not create a new DM, just tell that the creation is successful by passing the existing roomId
|
||||||
|
setState {
|
||||||
|
copy(createAndInviteState = Success(action.existingDmRoomId))
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
// Create the DM
|
||||||
|
createRoomAndInviteSelectedUsers(action.invitees)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -91,13 +91,20 @@ class KnownUsersFragment @Inject constructor(
|
|||||||
val showMenuItem = it.pendingInvitees.isNotEmpty()
|
val showMenuItem = it.pendingInvitees.isNotEmpty()
|
||||||
menu.forEach { menuItem ->
|
menu.forEach { menuItem ->
|
||||||
menuItem.isVisible = showMenuItem
|
menuItem.isVisible = showMenuItem
|
||||||
|
if (args.isCreatingRoom) {
|
||||||
|
menuItem.setTitle(if (it.existingDmRoomId != null) R.string.action_open else R.string.create_room_action_create)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
super.onPrepareOptionsMenu(menu)
|
super.onPrepareOptionsMenu(menu)
|
||||||
}
|
}
|
||||||
|
|
||||||
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
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -23,5 +23,6 @@ import kotlinx.android.parcel.Parcelize
|
|||||||
data class KnownUsersFragmentArgs(
|
data class KnownUsersFragmentArgs(
|
||||||
val title: String,
|
val title: String,
|
||||||
val menuResId: Int,
|
val menuResId: Int,
|
||||||
val excludedUserIds: Set<String>? = null
|
val excludedUserIds: Set<String>? = null,
|
||||||
|
val isCreatingRoom: Boolean = false
|
||||||
) : Parcelable
|
) : Parcelable
|
||||||
|
@ -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()
|
||||||
}
|
}
|
||||||
|
@ -87,14 +87,32 @@ class UserDirectoryViewModel @AssistedInject constructor(@Assisted
|
|||||||
|
|
||||||
private fun handleRemoveSelectedUser(action: UserDirectoryAction.RemovePendingInvitee) = withState { state ->
|
private fun handleRemoveSelectedUser(action: UserDirectoryAction.RemovePendingInvitee) = withState { state ->
|
||||||
val selectedUsers = state.pendingInvitees.minus(action.pendingInvitee)
|
val selectedUsers = state.pendingInvitees.minus(action.pendingInvitee)
|
||||||
setState { copy(pendingInvitees = selectedUsers) }
|
setState {
|
||||||
|
copy(
|
||||||
|
pendingInvitees = selectedUsers,
|
||||||
|
existingDmRoomId = getExistingDmRoomId(selectedUsers)
|
||||||
|
)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun handleSelectUser(action: UserDirectoryAction.SelectPendingInvitee) = withState { state ->
|
private fun handleSelectUser(action: UserDirectoryAction.SelectPendingInvitee) = withState { state ->
|
||||||
// Reset the filter asap
|
// Reset the filter asap
|
||||||
directoryUsersSearch.accept("")
|
directoryUsersSearch.accept("")
|
||||||
val selectedUsers = state.pendingInvitees.toggle(action.pendingInvitee)
|
val selectedUsers = state.pendingInvitees.toggle(action.pendingInvitee)
|
||||||
setState { copy(pendingInvitees = selectedUsers) }
|
setState {
|
||||||
|
copy(
|
||||||
|
pendingInvitees = selectedUsers,
|
||||||
|
existingDmRoomId = getExistingDmRoomId(selectedUsers)
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun getExistingDmRoomId(selectedUsers: Set<PendingInvitee>): String? {
|
||||||
|
return selectedUsers
|
||||||
|
.takeIf { it.size == 1 }
|
||||||
|
?.filterIsInstance(PendingInvitee.UserPendingInvitee::class.java)
|
||||||
|
?.firstOrNull()
|
||||||
|
?.let { invitee -> session.getExistingDirectRoomWithUser(invitee.user.userId) }
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun observeDirectoryUsers() = withState { state ->
|
private fun observeDirectoryUsers() = withState { state ->
|
||||||
|
@ -30,7 +30,8 @@ data class UserDirectoryViewState(
|
|||||||
val pendingInvitees: Set<PendingInvitee> = emptySet(),
|
val pendingInvitees: Set<PendingInvitee> = emptySet(),
|
||||||
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 existingDmRoomId: String? = null
|
||||||
) : MvRxState {
|
) : MvRxState {
|
||||||
|
|
||||||
constructor(args: KnownUsersFragmentArgs) : this(excludedUserIds = args.excludedUserIds)
|
constructor(args: KnownUsersFragmentArgs) : this(excludedUserIds = args.excludedUserIds)
|
||||||
|
Loading…
Reference in New Issue
Block a user