Change menu action title for existing rooms.

This commit is contained in:
Onuray Sahin 2020-11-04 17:51:31 +03:00 committed by Benoit Marty
parent e7714da8e8
commit 458b4259fe
5 changed files with 34 additions and 5 deletions

View File

@ -91,7 +91,8 @@ class CreateDirectRoomActivity : SimpleFragmentActivity() {
KnownUsersFragment::class.java,
KnownUsersFragmentArgs(
title = getString(R.string.fab_menu_create_chat),
menuResId = R.menu.vector_create_direct_room
menuResId = R.menu.vector_create_direct_room,
isCreatingRoom = true
)
)
}

View File

@ -91,6 +91,9 @@ class KnownUsersFragment @Inject constructor(
val showMenuItem = it.pendingInvitees.isNotEmpty()
menu.forEach { menuItem ->
menuItem.isVisible = showMenuItem
if (args.isCreatingRoom) {
menuItem.setTitle(if (it.isThereAnExistingRoom) R.string.action_open else R.string.create_room_action_create)
}
}
}
super.onPrepareOptionsMenu(menu)

View File

@ -23,5 +23,6 @@ import kotlinx.android.parcel.Parcelize
data class KnownUsersFragmentArgs(
val title: String,
val menuResId: Int,
val excludedUserIds: Set<String>? = null
val excludedUserIds: Set<String>? = null,
val isCreatingRoom: Boolean = false
) : Parcelable

View File

@ -87,14 +87,37 @@ class UserDirectoryViewModel @AssistedInject constructor(@Assisted
private fun handleRemoveSelectedUser(action: UserDirectoryAction.RemovePendingInvitee) = withState { state ->
val selectedUsers = state.pendingInvitees.minus(action.pendingInvitee)
setState { copy(pendingInvitees = selectedUsers) }
setState {
copy(
pendingInvitees = selectedUsers,
isThereAnExistingRoom = isThereAnExistingRoom(selectedUsers)
)
}
}
private fun handleSelectUser(action: UserDirectoryAction.SelectPendingInvitee) = withState { state ->
// Reset the filter asap
directoryUsersSearch.accept("")
val selectedUsers = state.pendingInvitees.toggle(action.pendingInvitee)
setState { copy(pendingInvitees = selectedUsers) }
setState {
copy(
pendingInvitees = selectedUsers,
isThereAnExistingRoom = isThereAnExistingRoom(selectedUsers)
)
}
}
private fun isThereAnExistingRoom(selectedUsers: Set<PendingInvitee>): Boolean {
return selectedUsers
.takeIf { it.size == 1 }
?.firstOrNull()
?.let { invitee ->
return when (invitee) {
is PendingInvitee.UserPendingInvitee -> session.getExistingDirectRoomWithUser(invitee.user.userId) != null
is PendingInvitee.ThreePidPendingInvitee -> false
}.exhaustive
}
?: false
}
private fun observeDirectoryUsers() = withState { state ->

View File

@ -30,7 +30,8 @@ data class UserDirectoryViewState(
val pendingInvitees: Set<PendingInvitee> = emptySet(),
val createAndInviteState: Async<String> = Uninitialized,
val directorySearchTerm: String = "",
val filterKnownUsersValue: Option<String> = Option.empty()
val filterKnownUsersValue: Option<String> = Option.empty(),
val isThereAnExistingRoom: Boolean = false
) : MvRxState {
constructor(args: KnownUsersFragmentArgs) : this(excludedUserIds = args.excludedUserIds)