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