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 f24e5876c..655cd7097 100644 --- a/app/src/main/java/io/legado/app/base/BaseViewModel.kt +++ b/app/src/main/java/io/legado/app/base/BaseViewModel.kt @@ -20,10 +20,6 @@ open class BaseViewModel(application: Application) : AndroidViewModel(applicatio return Coroutine.async(scope) { block().await() } } - fun plus(coroutine: Coroutine): Coroutine { - return Coroutine.plus(coroutine) - } - override fun onCleared() { super.onCleared() cancel() 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 af62ac20e..14dd69f14 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 @@ -3,7 +3,7 @@ package io.legado.app.help.coroutine import kotlinx.coroutines.* -class Coroutine() { +class Coroutine(scope: CoroutineScope, block: suspend CoroutineScope.() -> T) { companion object { @@ -13,13 +13,9 @@ class Coroutine() { return Coroutine(scope, block) } - fun plus(coroutine: Coroutine): Coroutine { - return Coroutine(coroutine) - } } - private var interceptor: Coroutine? = null - private lateinit var job: Job + private val job: Job private var start: (suspend CoroutineScope.() -> Unit)? = null private var execute: (suspend CoroutineScope.(T?) -> Unit)? = null @@ -40,80 +36,54 @@ class Coroutine() { val isCompleted: Boolean get() = job.isCompleted - private constructor( - scope: CoroutineScope, - block: suspend CoroutineScope.() -> T - ) : this() { + init { this.job = scope.plus(Dispatchers.Main).launch { executeInternal(this@launch, block) } } - private constructor(coroutine: Coroutine) : this() { - this.interceptor = coroutine - this.job = coroutine.job + fun timeout(timeMillis: () -> Long): Coroutine { + this.timeMillis = timeMillis() + return this@Coroutine } - fun timeout(timeMillis: () -> Long): Coroutine { - if (this.interceptor != null) { - this.interceptor!!.timeMillis = timeMillis() - } else { - this.timeMillis = timeMillis() - } + fun timeout(timeMillis: Long): Coroutine { + this.timeMillis = timeMillis return this@Coroutine } fun onErrorReturn(value: () -> T?): Coroutine { - if (this.interceptor != null) { - this.interceptor!!.errorReturn = Result(value()) - } else { - errorReturn = Result(value()) - } + this.errorReturn = Result(value()) + return this@Coroutine + } + + fun onErrorReturn(value: T?): Coroutine { + this.errorReturn = Result(value) return this@Coroutine } fun onStart(start: (suspend CoroutineScope.() -> Unit)): Coroutine { - if (this.interceptor != null) { - this.interceptor!!.start = start - } else { - this.start = start - } + this.start = start return this@Coroutine } fun onExecute(execute: suspend CoroutineScope.(T?) -> Unit): Coroutine { - if (this.interceptor != null) { - this.interceptor!!.execute = execute - } else { - this.execute = execute - } + this.execute = execute return this@Coroutine } fun onSuccess(success: suspend CoroutineScope.(T?) -> Unit): Coroutine { - if (this.interceptor != null) { - this.interceptor!!.success = success - } else { - this.success = success - } + this.success = success return this@Coroutine } fun onError(error: suspend CoroutineScope.(Throwable) -> Unit): Coroutine { - if (this.interceptor != null) { - this.interceptor!!.error = error - } else { - this.error = error - } + this.error = error return this@Coroutine } fun onFinally(finally: suspend CoroutineScope.() -> Unit): Coroutine { - if (this.interceptor != null) { - this.interceptor!!.finally = finally - } else { - this.finally = finally - } + this.finally = finally return this@Coroutine } @@ -122,7 +92,7 @@ class Coroutine() { job.cancel(cause) } - fun invokeOnCompletion(handler: CompletionHandler): DisposableHandle{ + fun invokeOnCompletion(handler: CompletionHandler): DisposableHandle { return job.invokeOnCompletion(handler) } 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 8fdf5e22d..f4faf69f7 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 @@ -4,8 +4,10 @@ import android.app.Application import android.util.Log import io.legado.app.App import io.legado.app.base.BaseViewModel +import io.legado.app.data.entities.SearchBook import io.legado.app.help.coroutine.Coroutine import io.legado.app.model.WebBook +import kotlinx.coroutines.CoroutineScope class SearchViewModel(application: Application) : BaseViewModel(application) { private var task: Coroutine<*>? = null @@ -26,8 +28,8 @@ class SearchViewModel(application: Application) : BaseViewModel(application) { for (item in bookSourceList) { //task取消时自动取消 by (scope = this@execute) WebBook(item).searchBook(key, searchPage, scope = this@execute) - .timeout { 30000L } - .onExecute { + .timeout(30000L) + .onExecute{ it?.let { list -> App.db.searchBookDao().insert(*list.toTypedArray()) } @@ -39,7 +41,6 @@ class SearchViewModel(application: Application) : BaseViewModel(application) { } task?.invokeOnCompletion { - Log.e("TAG", "complete") finally?.invoke() } }