From b95dfa4473f098947efd207dcf0045d5bc7dcd15 Mon Sep 17 00:00:00 2001 From: Benoit Marty Date: Tue, 25 Feb 2020 18:14:02 +0100 Subject: [PATCH 1/3] Create getBestName method for User --- .../vector/matrix/android/api/session/user/model/User.kt | 7 ++++++- .../createdirect/CreateDirectRoomKnownUsersFragment.kt | 2 +- .../features/notifications/NotificationDrawerManager.kt | 2 +- 3 files changed, 8 insertions(+), 3 deletions(-) diff --git a/matrix-sdk-android/src/main/java/im/vector/matrix/android/api/session/user/model/User.kt b/matrix-sdk-android/src/main/java/im/vector/matrix/android/api/session/user/model/User.kt index f569f5e47e..753c9b609c 100644 --- a/matrix-sdk-android/src/main/java/im/vector/matrix/android/api/session/user/model/User.kt +++ b/matrix-sdk-android/src/main/java/im/vector/matrix/android/api/session/user/model/User.kt @@ -24,4 +24,9 @@ data class User( val userId: String, val displayName: String? = null, val avatarUrl: String? = null -) +) { + /** + * Return the display name or the user id + */ + fun getBestName() = displayName?.takeIf { it.isNotEmpty() } ?: userId +} diff --git a/vector/src/main/java/im/vector/riotx/features/createdirect/CreateDirectRoomKnownUsersFragment.kt b/vector/src/main/java/im/vector/riotx/features/createdirect/CreateDirectRoomKnownUsersFragment.kt index fc1a4b9e65..57727dcfaf 100644 --- a/vector/src/main/java/im/vector/riotx/features/createdirect/CreateDirectRoomKnownUsersFragment.kt +++ b/vector/src/main/java/im/vector/riotx/features/createdirect/CreateDirectRoomKnownUsersFragment.kt @@ -159,7 +159,7 @@ class CreateDirectRoomKnownUsersFragment @Inject constructor( val chip = Chip(requireContext()) chip.setChipBackgroundColorResource(android.R.color.transparent) chip.chipStrokeWidth = dimensionConverter.dpToPx(1).toFloat() - chip.text = if (user.displayName.isNullOrBlank()) user.userId else user.displayName + chip.text = user.getBestName() chip.isClickable = true chip.isCheckable = false chip.isCloseIconVisible = true diff --git a/vector/src/main/java/im/vector/riotx/features/notifications/NotificationDrawerManager.kt b/vector/src/main/java/im/vector/riotx/features/notifications/NotificationDrawerManager.kt index 8173d24550..0f1d5f466e 100644 --- a/vector/src/main/java/im/vector/riotx/features/notifications/NotificationDrawerManager.kt +++ b/vector/src/main/java/im/vector/riotx/features/notifications/NotificationDrawerManager.kt @@ -203,7 +203,7 @@ class NotificationDrawerManager @Inject constructor(private val context: Context val user = session.getUser(session.myUserId) // myUserDisplayName cannot be empty else NotificationCompat.MessagingStyle() will crash - val myUserDisplayName = user?.displayName?.takeIf { it.isNotBlank() } ?: session.myUserId + val myUserDisplayName = user?.getBestName() ?: session.myUserId val myUserAvatarUrl = session.contentUrlResolver().resolveThumbnail(user?.avatarUrl, avatarSize, avatarSize, ContentUrlResolver.ThumbnailMethod.SCALE) synchronized(eventList) { Timber.v("%%%%%%%% REFRESH NOTIFICATION DRAWER ") From a55e0f1af4d4151da8ea14ca188c476e6041f9d7 Mon Sep 17 00:00:00 2001 From: Benoit Marty Date: Tue, 25 Feb 2020 18:17:40 +0100 Subject: [PATCH 2/3] Fix crash - workaround --- .../features/createdirect/CreateDirectRoomKnownUsersFragment.kt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/vector/src/main/java/im/vector/riotx/features/createdirect/CreateDirectRoomKnownUsersFragment.kt b/vector/src/main/java/im/vector/riotx/features/createdirect/CreateDirectRoomKnownUsersFragment.kt index 57727dcfaf..41855d824c 100644 --- a/vector/src/main/java/im/vector/riotx/features/createdirect/CreateDirectRoomKnownUsersFragment.kt +++ b/vector/src/main/java/im/vector/riotx/features/createdirect/CreateDirectRoomKnownUsersFragment.kt @@ -142,7 +142,7 @@ class CreateDirectRoomKnownUsersFragment @Inject constructor( if (data.isAdded) { addChipToGroup(data.user, chipGroup) } else { - if (chipGroup.size > data.index) { + if (data.index in 0..chipGroup.size) { chipGroup.removeViewAt(data.index) } } From 1072060cbbccc261e18e31d629a4c2dd3e58ff86 Mon Sep 17 00:00:00 2001 From: Benoit Marty Date: Tue, 25 Feb 2020 19:00:38 +0100 Subject: [PATCH 3/3] New direct chat: selecting a participant sometimes results in two breadcrumbs (#1022) --- CHANGES.md | 1 + .../CreateDirectRoomKnownUsersFragment.kt | 39 +++++++------------ .../CreateDirectRoomViewEvents.kt | 9 +---- .../createdirect/CreateDirectRoomViewModel.kt | 19 +-------- 4 files changed, 18 insertions(+), 50 deletions(-) diff --git a/CHANGES.md b/CHANGES.md index 90246416af..ffbaa42b3b 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -20,6 +20,7 @@ Bugfix 🐛: - Fix joining rooms from directory via federation isn't working. (#808) - Leaving a room creates a stuck "leaving room" loading screen. (#1041) - Fix some invitation handling issues (#1013) + - New direct chat: selecting a participant sometimes results in two breadcrumbs (#1022) Translations 🗣: - diff --git a/vector/src/main/java/im/vector/riotx/features/createdirect/CreateDirectRoomKnownUsersFragment.kt b/vector/src/main/java/im/vector/riotx/features/createdirect/CreateDirectRoomKnownUsersFragment.kt index 41855d824c..24b5394e5c 100644 --- a/vector/src/main/java/im/vector/riotx/features/createdirect/CreateDirectRoomKnownUsersFragment.kt +++ b/vector/src/main/java/im/vector/riotx/features/createdirect/CreateDirectRoomKnownUsersFragment.kt @@ -23,17 +23,14 @@ import android.view.Menu import android.view.MenuItem import android.view.View import android.widget.ScrollView -import androidx.core.view.size import com.airbnb.mvrx.activityViewModel import com.airbnb.mvrx.withState import com.google.android.material.chip.Chip -import com.google.android.material.chip.ChipGroup import com.jakewharton.rxbinding3.widget.textChanges import im.vector.matrix.android.api.session.user.model.User import im.vector.riotx.R import im.vector.riotx.core.extensions.cleanup import im.vector.riotx.core.extensions.configureWith -import im.vector.riotx.core.extensions.exhaustive import im.vector.riotx.core.extensions.hideKeyboard import im.vector.riotx.core.extensions.setupAsSearch import im.vector.riotx.core.platform.VectorBaseFragment @@ -61,11 +58,6 @@ class CreateDirectRoomKnownUsersFragment @Inject constructor( setupFilterView() setupAddByMatrixIdView() setupCloseView() - viewModel.observeViewEvents { - when (it) { - is CreateDirectRoomViewEvents.SelectUserAction -> updateChipsView(it) - }.exhaustive - } viewModel.selectSubscribe(this, CreateDirectRoomViewState::selectedUsers) { renderSelectedUsers(it) } @@ -138,24 +130,24 @@ class CreateDirectRoomKnownUsersFragment @Inject constructor( knownUsersController.setData(it) } - private fun updateChipsView(data: CreateDirectRoomViewEvents.SelectUserAction) { - if (data.isAdded) { - addChipToGroup(data.user, chipGroup) - } else { - if (data.index in 0..chipGroup.size) { - chipGroup.removeViewAt(data.index) + private fun renderSelectedUsers(selectedUsers: Set) { + invalidateOptionsMenu() + + val currentNumberOfChips = chipGroup.childCount + val newNumberOfChips = selectedUsers.size + + chipGroup.removeAllViews() + selectedUsers.forEach { addChipToGroup(it) } + + // Scroll to the bottom when adding chips. When removing chips, do not scroll + if (newNumberOfChips >= currentNumberOfChips) { + chipGroupScrollView.post { + chipGroupScrollView.fullScroll(ScrollView.FOCUS_DOWN) } } } - private fun renderSelectedUsers(selectedUsers: Set) { - invalidateOptionsMenu() - if (selectedUsers.isNotEmpty() && chipGroup.size == 0) { - selectedUsers.forEach { addChipToGroup(it, chipGroup) } - } - } - - private fun addChipToGroup(user: User, chipGroup: ChipGroup) { + private fun addChipToGroup(user: User) { val chip = Chip(requireContext()) chip.setChipBackgroundColorResource(android.R.color.transparent) chip.chipStrokeWidth = dimensionConverter.dpToPx(1).toFloat() @@ -167,9 +159,6 @@ class CreateDirectRoomKnownUsersFragment @Inject constructor( chip.setOnCloseIconClickListener { viewModel.handle(CreateDirectRoomAction.RemoveSelectedUser(user)) } - chipGroupScrollView.post { - chipGroupScrollView.fullScroll(ScrollView.FOCUS_DOWN) - } } override fun onItemClick(user: User) { diff --git a/vector/src/main/java/im/vector/riotx/features/createdirect/CreateDirectRoomViewEvents.kt b/vector/src/main/java/im/vector/riotx/features/createdirect/CreateDirectRoomViewEvents.kt index 168f23c9f7..0ed584ac6b 100644 --- a/vector/src/main/java/im/vector/riotx/features/createdirect/CreateDirectRoomViewEvents.kt +++ b/vector/src/main/java/im/vector/riotx/features/createdirect/CreateDirectRoomViewEvents.kt @@ -16,16 +16,9 @@ package im.vector.riotx.features.createdirect -import im.vector.matrix.android.api.session.user.model.User import im.vector.riotx.core.platform.VectorViewEvents /** * Transient events for create direct room screen */ -sealed class CreateDirectRoomViewEvents : VectorViewEvents { - data class SelectUserAction( - val user: User, - val isAdded: Boolean, - val index: Int - ) : CreateDirectRoomViewEvents() -} +sealed class CreateDirectRoomViewEvents : VectorViewEvents diff --git a/vector/src/main/java/im/vector/riotx/features/createdirect/CreateDirectRoomViewModel.kt b/vector/src/main/java/im/vector/riotx/features/createdirect/CreateDirectRoomViewModel.kt index b115a623a8..71fae11486 100644 --- a/vector/src/main/java/im/vector/riotx/features/createdirect/CreateDirectRoomViewModel.kt +++ b/vector/src/main/java/im/vector/riotx/features/createdirect/CreateDirectRoomViewModel.kt @@ -27,9 +27,9 @@ import com.squareup.inject.assisted.Assisted import com.squareup.inject.assisted.AssistedInject import im.vector.matrix.android.api.session.Session import im.vector.matrix.android.api.session.room.model.create.CreateRoomParams -import im.vector.matrix.android.api.session.user.model.User import im.vector.matrix.android.api.util.toMatrixItem import im.vector.matrix.rx.rx +import im.vector.riotx.core.extensions.toggle import im.vector.riotx.core.platform.VectorViewModel import io.reactivex.Single import io.reactivex.android.schedulers.AndroidSchedulers @@ -91,30 +91,15 @@ class CreateDirectRoomViewModel @AssistedInject constructor(@Assisted } private fun handleRemoveSelectedUser(action: CreateDirectRoomAction.RemoveSelectedUser) = withState { state -> - val index = state.selectedUsers.indexOfFirst { it.userId == action.user.userId } val selectedUsers = state.selectedUsers.minus(action.user) setState { copy(selectedUsers = selectedUsers) } - _viewEvents.post(CreateDirectRoomViewEvents.SelectUserAction(action.user, false, index)) } private fun handleSelectUser(action: CreateDirectRoomAction.SelectUser) = withState { state -> // Reset the filter asap directoryUsersSearch.accept("") - val isAddOperation: Boolean - val selectedUsers: Set - val indexOfUser = state.selectedUsers.indexOfFirst { it.userId == action.user.userId } - val changeIndex: Int - if (indexOfUser == -1) { - changeIndex = state.selectedUsers.size - selectedUsers = state.selectedUsers.plus(action.user) - isAddOperation = true - } else { - changeIndex = indexOfUser - selectedUsers = state.selectedUsers.minus(action.user) - isAddOperation = false - } + val selectedUsers = state.selectedUsers.toggle(action.user) setState { copy(selectedUsers = selectedUsers) } - _viewEvents.post(CreateDirectRoomViewEvents.SelectUserAction(action.user, isAddOperation, changeIndex)) } private fun observeDirectoryUsers() {