From 06cf59bca71f6d146cdc8544e0e67adcb8f1572b Mon Sep 17 00:00:00 2001 From: Benoit Marty Date: Wed, 22 Apr 2020 19:20:13 +0200 Subject: [PATCH] Even if it's not happening, do not add the search term if already present in the results. --- .../createdirect/DirectoryUsersController.kt | 24 +++++++++++-------- 1 file changed, 14 insertions(+), 10 deletions(-) diff --git a/vector/src/main/java/im/vector/riotx/features/createdirect/DirectoryUsersController.kt b/vector/src/main/java/im/vector/riotx/features/createdirect/DirectoryUsersController.kt index fd943b6447..1c38e6f723 100644 --- a/vector/src/main/java/im/vector/riotx/features/createdirect/DirectoryUsersController.kt +++ b/vector/src/main/java/im/vector/riotx/features/createdirect/DirectoryUsersController.kt @@ -60,20 +60,24 @@ class DirectoryUsersController @Inject constructor(private val session: Session, when (val asyncUsers = currentState.directoryUsers) { is Uninitialized -> renderEmptyState(false) is Loading -> renderLoading() - is Success -> renderSuccess(getAsyncUsers(currentState), currentState.selectedUsers.map { it.userId }, hasSearch) + is Success -> renderSuccess( + computeUsersList(asyncUsers(), currentState.directorySearchTerm), + currentState.selectedUsers.map { it.userId }, + hasSearch + ) is Fail -> renderFailure(asyncUsers.error) } } - private fun getAsyncUsers(currentState: CreateDirectRoomViewState): List { - return currentState - .directoryUsers() - ?.toMutableList() - ?.apply { - currentState.directorySearchTerm - .takeIf { MatrixPatterns.isUserId(it) } - ?.let { add(User(it)) } - } ?: emptyList() + /** + * Eventually add the searched terms, if it is a userId, and if not already present in the result + */ + private fun computeUsersList(directoryUsers: List, searchTerms: String): List { + return directoryUsers + + searchTerms + .takeIf { terms -> MatrixPatterns.isUserId(terms) && !directoryUsers.any { it.userId == terms } } + ?.let { listOf(User(it)) } + .orEmpty() } private fun renderLoading() {