Even if it's not happening, do not add the search term if already present in the results.

This commit is contained in:
Benoit Marty 2020-04-22 19:20:13 +02:00
parent 3163bc8b80
commit 06cf59bca7
1 changed files with 14 additions and 10 deletions

View File

@ -60,20 +60,24 @@ class DirectoryUsersController @Inject constructor(private val session: Session,
when (val asyncUsers = currentState.directoryUsers) { when (val asyncUsers = currentState.directoryUsers) {
is Uninitialized -> renderEmptyState(false) is Uninitialized -> renderEmptyState(false)
is Loading -> renderLoading() 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) is Fail -> renderFailure(asyncUsers.error)
} }
} }
private fun getAsyncUsers(currentState: CreateDirectRoomViewState): List<User> { /**
return currentState * Eventually add the searched terms, if it is a userId, and if not already present in the result
.directoryUsers() */
?.toMutableList() private fun computeUsersList(directoryUsers: List<User>, searchTerms: String): List<User> {
?.apply { return directoryUsers +
currentState.directorySearchTerm searchTerms
.takeIf { MatrixPatterns.isUserId(it) } .takeIf { terms -> MatrixPatterns.isUserId(terms) && !directoryUsers.any { it.userId == terms } }
?.let { add(User(it)) } ?.let { listOf(User(it)) }
} ?: emptyList() .orEmpty()
} }
private fun renderLoading() { private fun renderLoading() {