Cancel previous search task before starting new one.

This commit is contained in:
Onuray Sahin 2020-09-30 11:45:46 +03:00 committed by Benoit Marty
parent 0838cbaf03
commit 08cc439306
1 changed files with 10 additions and 1 deletions

View File

@ -28,9 +28,11 @@ import com.squareup.inject.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.launch 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.Session
import org.matrix.android.sdk.api.session.room.Room 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 import org.matrix.android.sdk.internal.util.awaitCallback
class SearchViewModel @AssistedInject constructor( class SearchViewModel @AssistedInject constructor(
@ -39,6 +41,7 @@ class SearchViewModel @AssistedInject constructor(
) : VectorViewModel<SearchViewState, SearchAction, SearchViewEvents>(initialState) { ) : VectorViewModel<SearchViewState, SearchAction, SearchViewEvents>(initialState) {
private var room: Room? = null private var room: Room? = null
private var currentTask: Cancelable? = null
init { init {
room = initialState.roomId?.let { session.getRoom(it) } room = initialState.roomId?.let { session.getRoom(it) }
@ -98,10 +101,14 @@ class SearchViewModel @AssistedInject constructor(
} }
} }
if (state.asyncEventsRequest is Loading) {
currentTask?.cancel()
}
viewModelScope.launch { viewModelScope.launch {
try { try {
val result = awaitCallback<SearchResult> { val result = awaitCallback<SearchResult> {
room?.search( currentTask = room?.search(
searchTerm = state.searchTerm, searchTerm = state.searchTerm,
nextBatch = state.searchResult?.nextBatch, nextBatch = state.searchResult?.nextBatch,
orderByRecent = true, orderByRecent = true,
@ -114,6 +121,8 @@ class SearchViewModel @AssistedInject constructor(
} }
onSearchResultSuccess(result, isNextBatch) onSearchResultSuccess(result, isNextBatch)
} catch (failure: Throwable) { } catch (failure: Throwable) {
if (failure is Failure.Cancelled) return@launch
_viewEvents.post(SearchViewEvents.Failure(failure)) _viewEvents.post(SearchViewEvents.Failure(failure))
setState { setState {
copy( copy(