mirror of
https://github.com/SchildiChat/SchildiChat-android.git
synced 2024-11-25 19:05:56 +03:00
Convert Room API to suspend (#24499)
This commit is contained in:
parent
79bee63515
commit
f49df59e10
5 changed files with 50 additions and 80 deletions
|
@ -17,30 +17,27 @@
|
|||
package org.matrix.android.sdk.session.search
|
||||
|
||||
import org.junit.Assert.assertTrue
|
||||
import org.junit.Assert.fail
|
||||
import org.junit.FixMethodOrder
|
||||
import org.junit.Test
|
||||
import org.junit.runner.RunWith
|
||||
import org.junit.runners.JUnit4
|
||||
import org.junit.runners.MethodSorters
|
||||
import org.matrix.android.sdk.InstrumentedTest
|
||||
import org.matrix.android.sdk.api.MatrixCallback
|
||||
import org.matrix.android.sdk.api.extensions.orFalse
|
||||
import org.matrix.android.sdk.api.session.events.model.toModel
|
||||
import org.matrix.android.sdk.api.session.room.model.message.MessageContent
|
||||
import org.matrix.android.sdk.api.session.room.timeline.TimelineSettings
|
||||
import org.matrix.android.sdk.api.session.search.SearchResult
|
||||
import org.matrix.android.sdk.common.CommonTestHelper
|
||||
import org.matrix.android.sdk.common.CryptoTestHelper
|
||||
import org.matrix.android.sdk.common.TestConstants
|
||||
import java.util.concurrent.CountDownLatch
|
||||
import java.util.concurrent.TimeUnit
|
||||
|
||||
@RunWith(JUnit4::class)
|
||||
@FixMethodOrder(MethodSorters.JVM)
|
||||
class SearchMessagesTest : InstrumentedTest {
|
||||
|
||||
private val MESSAGE = "Lorem ipsum dolor sit amet"
|
||||
companion object {
|
||||
private const val MESSAGE = "Lorem ipsum dolor sit amet"
|
||||
}
|
||||
|
||||
private val commonTestHelper = CommonTestHelper(context())
|
||||
private val cryptoTestHelper = CryptoTestHelper(commonTestHelper)
|
||||
|
@ -115,7 +112,7 @@ class SearchMessagesTest : InstrumentedTest {
|
|||
2)
|
||||
|
||||
run {
|
||||
var lock = CountDownLatch(1)
|
||||
val lock = CountDownLatch(1)
|
||||
|
||||
val eventListener = commonTestHelper.createEventListener(lock) { snapshot ->
|
||||
snapshot.count { it.root.content.toModel<MessageContent>()?.body?.startsWith(MESSAGE).orFalse() } == 2
|
||||
|
@ -124,37 +121,25 @@ class SearchMessagesTest : InstrumentedTest {
|
|||
aliceTimeline.addListener(eventListener)
|
||||
commonTestHelper.await(lock)
|
||||
|
||||
lock = CountDownLatch(1)
|
||||
roomFromAlicePOV
|
||||
.search(
|
||||
searchTerm = "lore",
|
||||
limit = 10,
|
||||
includeProfile = true,
|
||||
afterLimit = 0,
|
||||
beforeLimit = 10,
|
||||
orderByRecent = true,
|
||||
nextBatch = null,
|
||||
callback = object : MatrixCallback<SearchResult> {
|
||||
override fun onSuccess(data: SearchResult) {
|
||||
super.onSuccess(data)
|
||||
assertTrue(data.results?.size == 2)
|
||||
assertTrue(
|
||||
data.results
|
||||
?.all {
|
||||
(it.event.content?.get("body") as? String)?.startsWith(MESSAGE).orFalse()
|
||||
}.orFalse()
|
||||
)
|
||||
lock.countDown()
|
||||
}
|
||||
val data = commonTestHelper.runBlockingTest {
|
||||
roomFromAlicePOV.search(
|
||||
searchTerm = "lore",
|
||||
limit = 10,
|
||||
includeProfile = true,
|
||||
afterLimit = 0,
|
||||
beforeLimit = 10,
|
||||
orderByRecent = true,
|
||||
nextBatch = null
|
||||
)
|
||||
}
|
||||
|
||||
override fun onFailure(failure: Throwable) {
|
||||
super.onFailure(failure)
|
||||
fail(failure.localizedMessage)
|
||||
lock.countDown()
|
||||
}
|
||||
}
|
||||
)
|
||||
lock.await(TestConstants.timeOutMillis, TimeUnit.MILLISECONDS)
|
||||
assertTrue(data.results?.size == 2)
|
||||
assertTrue(
|
||||
data.results
|
||||
?.all {
|
||||
(it.event.content?.get("body") as? String)?.startsWith(MESSAGE).orFalse()
|
||||
}.orFalse()
|
||||
)
|
||||
|
||||
aliceTimeline.removeAllListeners()
|
||||
cryptoTestData.cleanUp(commonTestHelper)
|
||||
|
|
|
@ -86,12 +86,11 @@ interface Room :
|
|||
* @param includeProfile requests that the server returns the historic profile information for the users that sent the events that were returned.
|
||||
* @param callback Callback to get the search result
|
||||
*/
|
||||
fun search(searchTerm: String,
|
||||
suspend fun search(searchTerm: String,
|
||||
nextBatch: String?,
|
||||
orderByRecent: Boolean,
|
||||
limit: Int,
|
||||
beforeLimit: Int,
|
||||
afterLimit: Int,
|
||||
includeProfile: Boolean,
|
||||
callback: MatrixCallback<SearchResult>): Cancelable
|
||||
includeProfile: Boolean): SearchResult
|
||||
}
|
||||
|
|
|
@ -17,7 +17,6 @@
|
|||
package org.matrix.android.sdk.internal.session.room
|
||||
|
||||
import androidx.lifecycle.LiveData
|
||||
import org.matrix.android.sdk.api.MatrixCallback
|
||||
import org.matrix.android.sdk.api.session.crypto.CryptoService
|
||||
import org.matrix.android.sdk.api.session.events.model.EventType
|
||||
import org.matrix.android.sdk.api.session.room.Room
|
||||
|
@ -37,14 +36,11 @@ import org.matrix.android.sdk.api.session.room.timeline.TimelineService
|
|||
import org.matrix.android.sdk.api.session.room.typing.TypingService
|
||||
import org.matrix.android.sdk.api.session.room.uploads.UploadsService
|
||||
import org.matrix.android.sdk.api.session.search.SearchResult
|
||||
import org.matrix.android.sdk.api.util.Cancelable
|
||||
import org.matrix.android.sdk.api.util.Optional
|
||||
import org.matrix.android.sdk.internal.crypto.MXCRYPTO_ALGORITHM_MEGOLM
|
||||
import org.matrix.android.sdk.internal.session.room.state.SendStateTask
|
||||
import org.matrix.android.sdk.internal.session.room.summary.RoomSummaryDataSource
|
||||
import org.matrix.android.sdk.internal.session.search.SearchTask
|
||||
import org.matrix.android.sdk.internal.task.TaskExecutor
|
||||
import org.matrix.android.sdk.internal.task.configureWith
|
||||
import org.matrix.android.sdk.internal.util.awaitCallback
|
||||
import java.security.InvalidParameterException
|
||||
import javax.inject.Inject
|
||||
|
@ -66,7 +62,6 @@ internal class DefaultRoom @Inject constructor(override val roomId: String,
|
|||
private val relationService: RelationService,
|
||||
private val roomMembersService: MembershipService,
|
||||
private val roomPushRuleService: RoomPushRuleService,
|
||||
private val taskExecutor: TaskExecutor,
|
||||
private val sendStateTask: SendStateTask,
|
||||
private val searchTask: SearchTask) :
|
||||
Room,
|
||||
|
@ -133,16 +128,15 @@ internal class DefaultRoom @Inject constructor(override val roomId: String,
|
|||
}
|
||||
}
|
||||
|
||||
override fun search(searchTerm: String,
|
||||
nextBatch: String?,
|
||||
orderByRecent: Boolean,
|
||||
limit: Int,
|
||||
beforeLimit: Int,
|
||||
afterLimit: Int,
|
||||
includeProfile: Boolean,
|
||||
callback: MatrixCallback<SearchResult>): Cancelable {
|
||||
return searchTask
|
||||
.configureWith(SearchTask.Params(
|
||||
override suspend fun search(searchTerm: String,
|
||||
nextBatch: String?,
|
||||
orderByRecent: Boolean,
|
||||
limit: Int,
|
||||
beforeLimit: Int,
|
||||
afterLimit: Int,
|
||||
includeProfile: Boolean): SearchResult {
|
||||
return searchTask.execute(
|
||||
SearchTask.Params(
|
||||
searchTerm = searchTerm,
|
||||
roomId = roomId,
|
||||
nextBatch = nextBatch,
|
||||
|
@ -151,8 +145,7 @@ internal class DefaultRoom @Inject constructor(override val roomId: String,
|
|||
beforeLimit = beforeLimit,
|
||||
afterLimit = afterLimit,
|
||||
includeProfile = includeProfile
|
||||
)) {
|
||||
this.callback = callback
|
||||
}.executeBy(taskExecutor)
|
||||
)
|
||||
)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -36,7 +36,6 @@ import org.matrix.android.sdk.internal.session.room.timeline.DefaultTimelineServ
|
|||
import org.matrix.android.sdk.internal.session.room.typing.DefaultTypingService
|
||||
import org.matrix.android.sdk.internal.session.room.uploads.DefaultUploadsService
|
||||
import org.matrix.android.sdk.internal.session.search.SearchTask
|
||||
import org.matrix.android.sdk.internal.task.TaskExecutor
|
||||
import javax.inject.Inject
|
||||
|
||||
internal interface RoomFactory {
|
||||
|
@ -60,7 +59,6 @@ internal class DefaultRoomFactory @Inject constructor(private val cryptoService:
|
|||
private val relationServiceFactory: DefaultRelationService.Factory,
|
||||
private val membershipServiceFactory: DefaultMembershipService.Factory,
|
||||
private val roomPushRuleServiceFactory: DefaultRoomPushRuleService.Factory,
|
||||
private val taskExecutor: TaskExecutor,
|
||||
private val sendStateTask: SendStateTask,
|
||||
private val searchTask: SearchTask) :
|
||||
RoomFactory {
|
||||
|
@ -84,7 +82,6 @@ internal class DefaultRoomFactory @Inject constructor(private val cryptoService:
|
|||
relationService = relationServiceFactory.create(roomId),
|
||||
roomMembersService = membershipServiceFactory.create(roomId),
|
||||
roomPushRuleService = roomPushRuleServiceFactory.create(roomId),
|
||||
taskExecutor = taskExecutor,
|
||||
sendStateTask = sendStateTask,
|
||||
searchTask = searchTask
|
||||
)
|
||||
|
|
|
@ -24,26 +24,24 @@ import com.airbnb.mvrx.MvRxViewModelFactory
|
|||
import com.airbnb.mvrx.Success
|
||||
import com.airbnb.mvrx.ViewModelContext
|
||||
import dagger.assisted.Assisted
|
||||
import dagger.assisted.AssistedInject
|
||||
import dagger.assisted.AssistedFactory
|
||||
import dagger.assisted.AssistedInject
|
||||
import im.vector.app.core.extensions.exhaustive
|
||||
import im.vector.app.core.platform.VectorViewModel
|
||||
import kotlinx.coroutines.Job
|
||||
import kotlinx.coroutines.launch
|
||||
import org.matrix.android.sdk.api.failure.Failure
|
||||
import org.matrix.android.sdk.api.session.Session
|
||||
import org.matrix.android.sdk.api.session.room.Room
|
||||
import org.matrix.android.sdk.api.session.search.SearchResult
|
||||
import org.matrix.android.sdk.api.util.Cancelable
|
||||
import org.matrix.android.sdk.internal.util.awaitCallback
|
||||
|
||||
class SearchViewModel @AssistedInject constructor(
|
||||
@Assisted private val initialState: SearchViewState,
|
||||
session: Session
|
||||
) : VectorViewModel<SearchViewState, SearchAction, SearchViewEvents>(initialState) {
|
||||
|
||||
private var room: Room? = session.getRoom(initialState.roomId)
|
||||
private val room = session.getRoom(initialState.roomId)
|
||||
|
||||
private var currentTask: Cancelable? = null
|
||||
private var currentTask: Job? = null
|
||||
|
||||
private var nextBatch: String? = null
|
||||
|
||||
|
@ -92,6 +90,7 @@ class SearchViewModel @AssistedInject constructor(
|
|||
}
|
||||
|
||||
private fun startSearching(isNextBatch: Boolean) = withState { state ->
|
||||
if (room == null) return@withState
|
||||
if (state.searchTerm == null) return@withState
|
||||
|
||||
// There is no batch to retrieve
|
||||
|
@ -108,20 +107,17 @@ class SearchViewModel @AssistedInject constructor(
|
|||
|
||||
currentTask?.cancel()
|
||||
|
||||
viewModelScope.launch {
|
||||
currentTask = viewModelScope.launch {
|
||||
try {
|
||||
val result = awaitCallback<SearchResult> {
|
||||
currentTask = room?.search(
|
||||
searchTerm = state.searchTerm,
|
||||
nextBatch = nextBatch,
|
||||
orderByRecent = true,
|
||||
beforeLimit = 0,
|
||||
afterLimit = 0,
|
||||
includeProfile = true,
|
||||
limit = 20,
|
||||
callback = it
|
||||
)
|
||||
}
|
||||
val result = room.search(
|
||||
searchTerm = state.searchTerm,
|
||||
nextBatch = nextBatch,
|
||||
orderByRecent = true,
|
||||
beforeLimit = 0,
|
||||
afterLimit = 0,
|
||||
includeProfile = true,
|
||||
limit = 20
|
||||
)
|
||||
onSearchResultSuccess(result)
|
||||
} catch (failure: Throwable) {
|
||||
if (failure is Failure.Cancelled) return@launch
|
||||
|
|
Loading…
Reference in a new issue