User search: provide 10 results not 5
Also refactor slightly so the caller specifies the number of results, which lets all the constants sit in the repository.
This commit is contained in:
parent
a456becc6e
commit
4e23550031
5 changed files with 8 additions and 11 deletions
|
|
@ -21,6 +21,6 @@ import io.element.android.libraries.matrix.api.user.MatrixUser
|
||||||
|
|
||||||
interface UserListDataSource {
|
interface UserListDataSource {
|
||||||
//TODO should probably have a flow
|
//TODO should probably have a flow
|
||||||
suspend fun search(query: String): List<MatrixUser>
|
suspend fun search(query: String, count: Long): List<MatrixUser>
|
||||||
suspend fun getProfile(userId: UserId): MatrixUser?
|
suspend fun getProfile(userId: UserId): MatrixUser?
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -28,16 +28,12 @@ import javax.inject.Inject
|
||||||
class MatrixUserListDataSource @Inject constructor(
|
class MatrixUserListDataSource @Inject constructor(
|
||||||
private val client: MatrixClient
|
private val client: MatrixClient
|
||||||
) : UserListDataSource {
|
) : UserListDataSource {
|
||||||
override suspend fun search(query: String): List<MatrixUser> {
|
override suspend fun search(query: String, count: Long): List<MatrixUser> {
|
||||||
val res = client.searchUsers(query, MAX_SEARCH_RESULTS)
|
val res = client.searchUsers(query, count)
|
||||||
return res.getOrNull()?.results.orEmpty()
|
return res.getOrNull()?.results.orEmpty()
|
||||||
}
|
}
|
||||||
|
|
||||||
override suspend fun getProfile(userId: UserId): MatrixUser? {
|
override suspend fun getProfile(userId: UserId): MatrixUser? {
|
||||||
return client.getProfile(userId).getOrNull()
|
return client.getProfile(userId).getOrNull()
|
||||||
}
|
}
|
||||||
|
|
||||||
companion object {
|
|
||||||
private const val MAX_SEARCH_RESULTS = 5L
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -44,7 +44,7 @@ class MatrixUserRepository @Inject constructor(
|
||||||
// Debounce
|
// Debounce
|
||||||
delay(DEBOUNCE_TIME_MILLIS)
|
delay(DEBOUNCE_TIME_MILLIS)
|
||||||
|
|
||||||
val results = dataSource.search(query).toMutableList()
|
val results = dataSource.search(query, MAXIMUM_SEARCH_RESULTS).toMutableList()
|
||||||
|
|
||||||
// If the query is a user ID and the result doesn't contain that user ID, query the profile information explicitly
|
// If the query is a user ID and the result doesn't contain that user ID, query the profile information explicitly
|
||||||
if (isUserId && results.none { it.userId.value == query }) {
|
if (isUserId && results.none { it.userId.value == query }) {
|
||||||
|
|
@ -60,5 +60,6 @@ class MatrixUserRepository @Inject constructor(
|
||||||
companion object {
|
companion object {
|
||||||
private const val DEBOUNCE_TIME_MILLIS = 250L
|
private const val DEBOUNCE_TIME_MILLIS = 250L
|
||||||
private const val MINIMUM_SEARCH_LENGTH = 3
|
private const val MINIMUM_SEARCH_LENGTH = 3
|
||||||
|
private const val MAXIMUM_SEARCH_RESULTS = 10L
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -47,7 +47,7 @@ internal class MatrixUserListDataSourceTest {
|
||||||
)
|
)
|
||||||
val dataSource = MatrixUserListDataSource(matrixClient)
|
val dataSource = MatrixUserListDataSource(matrixClient)
|
||||||
|
|
||||||
val results = dataSource.search("test")
|
val results = dataSource.search("test", 2)
|
||||||
Truth.assertThat(results).containsExactly(
|
Truth.assertThat(results).containsExactly(
|
||||||
aMatrixUserProfile(),
|
aMatrixUserProfile(),
|
||||||
aMatrixUserProfile(userId = A_USER_ID_2)
|
aMatrixUserProfile(userId = A_USER_ID_2)
|
||||||
|
|
@ -63,7 +63,7 @@ internal class MatrixUserListDataSourceTest {
|
||||||
)
|
)
|
||||||
val dataSource = MatrixUserListDataSource(matrixClient)
|
val dataSource = MatrixUserListDataSource(matrixClient)
|
||||||
|
|
||||||
val results = dataSource.search("test")
|
val results = dataSource.search("test", 2)
|
||||||
Truth.assertThat(results).isEmpty()
|
Truth.assertThat(results).isEmpty()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -25,7 +25,7 @@ class FakeUserListDataSource : UserListDataSource {
|
||||||
private var searchResult: List<MatrixUser> = emptyList()
|
private var searchResult: List<MatrixUser> = emptyList()
|
||||||
private var profile: MatrixUser? = null
|
private var profile: MatrixUser? = null
|
||||||
|
|
||||||
override suspend fun search(query: String): List<MatrixUser> = searchResult
|
override suspend fun search(query: String, count: Long): List<MatrixUser> = searchResult.take(count.toInt())
|
||||||
|
|
||||||
override suspend fun getProfile(userId: UserId): MatrixUser? = profile
|
override suspend fun getProfile(userId: UserId): MatrixUser? = profile
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue