pull/32/head
Administrator 5 years ago
parent a4d60b4abe
commit df422bcb11
  1. 15
      app/src/main/java/io/legado/app/help/coroutine/Coroutine.kt
  2. 3
      app/src/main/java/io/legado/app/ui/search/SearchActivity.kt
  3. 5
      app/src/main/java/io/legado/app/ui/search/SearchViewModel.kt

@ -19,7 +19,7 @@ class Coroutine<T>() {
} }
private var interceptor: Coroutine<T>? = null private var interceptor: Coroutine<T>? = null
private var job: Job? = null private lateinit var job: Job
private var start: (suspend CoroutineScope.() -> Unit)? = null private var start: (suspend CoroutineScope.() -> Unit)? = null
private var execute: (suspend CoroutineScope.(T?) -> Unit)? = null private var execute: (suspend CoroutineScope.(T?) -> Unit)? = null
@ -32,13 +32,13 @@ class Coroutine<T>() {
private var errorReturn: Result<T>? = null private var errorReturn: Result<T>? = null
val isCancelled: Boolean val isCancelled: Boolean
get() = job?.isCancelled ?: false get() = job.isCancelled
val isActive: Boolean val isActive: Boolean
get() = job?.isActive ?: true get() = job.isActive
val isCompleted: Boolean val isCompleted: Boolean
get() = job?.isCompleted ?: false get() = job.isCompleted
private constructor( private constructor(
scope: CoroutineScope, scope: CoroutineScope,
@ -119,7 +119,11 @@ class Coroutine<T>() {
//取消当前任务 //取消当前任务
fun cancel(cause: CancellationException? = null) { 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) { private suspend fun executeInternal(scope: CoroutineScope, block: suspend CoroutineScope.() -> T) {
@ -134,6 +138,7 @@ class Coroutine<T>() {
success?.invoke(scope, value) success?.invoke(scope, value)
true true
} ?: false } ?: false
if (!consume) { if (!consume) {
error?.invoke(scope, e) error?.invoke(scope, e)
} }

@ -11,7 +11,6 @@ import androidx.recyclerview.widget.LinearLayoutManager
import io.legado.app.App import io.legado.app.App
import io.legado.app.R import io.legado.app.R
import io.legado.app.base.VMBaseActivity import io.legado.app.base.VMBaseActivity
import io.legado.app.data.entities.SearchBook
import io.legado.app.data.entities.SearchShow import io.legado.app.data.entities.SearchShow
import io.legado.app.lib.theme.ATH import io.legado.app.lib.theme.ATH
import io.legado.app.utils.getViewModel import io.legado.app.utils.getViewModel
@ -45,6 +44,8 @@ class SearchActivity : VMBaseActivity<SearchViewModel>(R.layout.activity_search)
viewModel.search(it, { startTime -> viewModel.search(it, { startTime ->
content_view.showContentView() content_view.showContentView()
initData(startTime) initData(startTime)
}, {
}) })
} }
return true return true

@ -1,6 +1,7 @@
package io.legado.app.ui.search package io.legado.app.ui.search
import android.app.Application import android.app.Application
import android.util.Log
import io.legado.app.App import io.legado.app.App
import io.legado.app.base.BaseViewModel import io.legado.app.base.BaseViewModel
import io.legado.app.help.coroutine.Coroutine import io.legado.app.help.coroutine.Coroutine
@ -36,6 +37,10 @@ class SearchViewModel(application: Application) : BaseViewModel(application) {
}.onError { }.onError {
it.printStackTrace() it.printStackTrace()
} }
task?.invokeOnCompletion {
Log.e("TAG", "complete")
}
} }
fun stop() { fun stop() {

Loading…
Cancel
Save