diff --git a/app/src/main/java/io/legado/app/base/BaseViewModel.kt b/app/src/main/java/io/legado/app/base/BaseViewModel.kt index f915480d0..dcd19b586 100644 --- a/app/src/main/java/io/legado/app/base/BaseViewModel.kt +++ b/app/src/main/java/io/legado/app/base/BaseViewModel.kt @@ -20,6 +20,9 @@ open class BaseViewModel(application: Application) : AndroidViewModel(applicatio return Coroutine.launch(this) { block().await() } } + fun plus(coroutine: Coroutine): Coroutine { + return Coroutine.plus(coroutine) + } override fun onCleared() { super.onCleared() diff --git a/app/src/main/java/io/legado/app/help/coroutine/Coroutine.kt b/app/src/main/java/io/legado/app/help/coroutine/Coroutine.kt index a0c64eb43..6a471cdb7 100644 --- a/app/src/main/java/io/legado/app/help/coroutine/Coroutine.kt +++ b/app/src/main/java/io/legado/app/help/coroutine/Coroutine.kt @@ -2,7 +2,7 @@ package io.legado.app.help.coroutine import kotlinx.coroutines.* -class Coroutine(private val scope: CoroutineScope, block: suspend CoroutineScope.() -> T) { +class Coroutine(private val scope: CoroutineScope, private val block: suspend CoroutineScope.() -> T) { companion object { @@ -11,10 +11,22 @@ class Coroutine(private val scope: CoroutineScope, block: suspend CoroutineSc fun launch(scope: CoroutineScope = DEFAULT, block: suspend CoroutineScope.() -> T): Coroutine { return Coroutine(scope, block) } + + fun plus(coroutine: Coroutine): Coroutine{ + return Coroutine(coroutine.scope, coroutine.block) + .timeout{ coroutine.timeMillis?:0 } + .onErrorReturn { coroutine.errorReturn?.value } + .onStart { coroutine.start } + .onSuccess { coroutine.success } + .onError { coroutine.error } + .onFinally { coroutine.finally } + } } + private val job: Job + init { - scope.launch { + job = scope.launch { executeInternal(block) } } @@ -28,6 +40,7 @@ class Coroutine(private val scope: CoroutineScope, block: suspend CoroutineSc private var errorReturn: Result? = null + fun timeout(timeMillis: () -> Long): Coroutine { this.timeMillis = timeMillis() return this@Coroutine @@ -58,8 +71,9 @@ class Coroutine(private val scope: CoroutineScope, block: suspend CoroutineSc return this@Coroutine } + //取消当前任务 fun cancel(cause: CancellationException? = null) { - scope.cancel(cause) + job.cancel(cause) } private suspend fun executeInternal(block: suspend CoroutineScope.() -> T) { diff --git a/app/src/main/java/io/legado/app/ui/search/SearchViewModel.kt b/app/src/main/java/io/legado/app/ui/search/SearchViewModel.kt index 4dc615062..2674ce70e 100644 --- a/app/src/main/java/io/legado/app/ui/search/SearchViewModel.kt +++ b/app/src/main/java/io/legado/app/ui/search/SearchViewModel.kt @@ -6,8 +6,11 @@ import androidx.lifecycle.LiveData import androidx.lifecycle.MutableLiveData import io.legado.app.base.BaseViewModel import io.legado.app.data.api.CommonHttpApi +import io.legado.app.data.entities.BookSource import io.legado.app.data.entities.SearchBook +import io.legado.app.help.coroutine.Coroutine import io.legado.app.help.http.HttpHelper +import io.legado.app.model.WebBook import kotlinx.coroutines.delay class SearchViewModel(application: Application) : BaseViewModel(application) { @@ -45,6 +48,8 @@ class SearchViewModel(application: Application) : BaseViewModel(application) { finally() } } + + plus(WebBook(BookSource()).searchBook("keyword", 1)) } }