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:
@@ -21,6 +21,6 @@ import io.element.android.libraries.matrix.api.user.MatrixUser
|
||||
|
||||
interface UserListDataSource {
|
||||
//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?
|
||||
}
|
||||
|
||||
@@ -28,16 +28,12 @@ import javax.inject.Inject
|
||||
class MatrixUserListDataSource @Inject constructor(
|
||||
private val client: MatrixClient
|
||||
) : UserListDataSource {
|
||||
override suspend fun search(query: String): List<MatrixUser> {
|
||||
val res = client.searchUsers(query, MAX_SEARCH_RESULTS)
|
||||
override suspend fun search(query: String, count: Long): List<MatrixUser> {
|
||||
val res = client.searchUsers(query, count)
|
||||
return res.getOrNull()?.results.orEmpty()
|
||||
}
|
||||
|
||||
override suspend fun getProfile(userId: UserId): MatrixUser? {
|
||||
return client.getProfile(userId).getOrNull()
|
||||
}
|
||||
|
||||
companion object {
|
||||
private const val MAX_SEARCH_RESULTS = 5L
|
||||
}
|
||||
}
|
||||
|
||||
@@ -44,7 +44,7 @@ class MatrixUserRepository @Inject constructor(
|
||||
// Debounce
|
||||
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 (isUserId && results.none { it.userId.value == query }) {
|
||||
@@ -60,5 +60,6 @@ class MatrixUserRepository @Inject constructor(
|
||||
companion object {
|
||||
private const val DEBOUNCE_TIME_MILLIS = 250L
|
||||
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 results = dataSource.search("test")
|
||||
val results = dataSource.search("test", 2)
|
||||
Truth.assertThat(results).containsExactly(
|
||||
aMatrixUserProfile(),
|
||||
aMatrixUserProfile(userId = A_USER_ID_2)
|
||||
@@ -63,7 +63,7 @@ internal class MatrixUserListDataSourceTest {
|
||||
)
|
||||
val dataSource = MatrixUserListDataSource(matrixClient)
|
||||
|
||||
val results = dataSource.search("test")
|
||||
val results = dataSource.search("test", 2)
|
||||
Truth.assertThat(results).isEmpty()
|
||||
}
|
||||
|
||||
|
||||
@@ -25,7 +25,7 @@ class FakeUserListDataSource : UserListDataSource {
|
||||
private var searchResult: List<MatrixUser> = emptyList()
|
||||
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
|
||||
|
||||
|
||||
Reference in New Issue
Block a user