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 9013fc266..af62ac20e 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 @@ -19,7 +19,7 @@ class Coroutine() { } private var interceptor: Coroutine? = null - private var job: Job? = null + private lateinit var job: Job private var start: (suspend CoroutineScope.() -> Unit)? = null private var execute: (suspend CoroutineScope.(T?) -> Unit)? = null @@ -32,13 +32,13 @@ class Coroutine() { private var errorReturn: Result? = null val isCancelled: Boolean - get() = job?.isCancelled ?: false + get() = job.isCancelled val isActive: Boolean - get() = job?.isActive ?: true + get() = job.isActive val isCompleted: Boolean - get() = job?.isCompleted ?: false + get() = job.isCompleted private constructor( scope: CoroutineScope, @@ -119,7 +119,11 @@ class Coroutine() { //取消当前任务 fun cancel(cause: CancellationException? = null) { - job?.cancel(cause) + job.cancel(cause) + } + + fun invokeOnCompletion(handler: CompletionHandler): DisposableHandle{ + return job.invokeOnCompletion(handler) } private suspend fun executeInternal(scope: CoroutineScope, block: suspend CoroutineScope.() -> T) { @@ -134,6 +138,7 @@ class Coroutine() { success?.invoke(scope, value) true } ?: false + if (!consume) { error?.invoke(scope, e) } diff --git a/app/src/main/java/io/legado/app/ui/search/SearchActivity.kt b/app/src/main/java/io/legado/app/ui/search/SearchActivity.kt index 52222d541..74a51d172 100644 --- a/app/src/main/java/io/legado/app/ui/search/SearchActivity.kt +++ b/app/src/main/java/io/legado/app/ui/search/SearchActivity.kt @@ -11,7 +11,6 @@ import androidx.recyclerview.widget.LinearLayoutManager import io.legado.app.App import io.legado.app.R import io.legado.app.base.VMBaseActivity -import io.legado.app.data.entities.SearchBook import io.legado.app.data.entities.SearchShow import io.legado.app.lib.theme.ATH import io.legado.app.utils.getViewModel @@ -45,13 +44,15 @@ class SearchActivity : VMBaseActivity(R.layout.activity_search) viewModel.search(it, { startTime -> content_view.showContentView() initData(startTime) + }, { + }) } return true } override fun onQueryTextChange(newText: String?): Boolean { - if(newText.isNullOrBlank()) viewModel.stop() + if (newText.isNullOrBlank()) viewModel.stop() return false } 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 12f331fc4..096d8643e 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 @@ -1,6 +1,7 @@ package io.legado.app.ui.search import android.app.Application +import android.util.Log import io.legado.app.App import io.legado.app.base.BaseViewModel import io.legado.app.help.coroutine.Coroutine @@ -36,6 +37,10 @@ class SearchViewModel(application: Application) : BaseViewModel(application) { }.onError { it.printStackTrace() } + + task?.invokeOnCompletion { + Log.e("TAG", "complete") + } } fun stop() {