Merge remote-tracking branch 'origin/master'

pull/32/head
kunfei 5 years ago
commit b6e30b11bc
  1. 3
      app/src/main/java/io/legado/app/base/BaseViewModel.kt
  2. 20
      app/src/main/java/io/legado/app/help/coroutine/Coroutine.kt
  3. 5
      app/src/main/java/io/legado/app/ui/search/SearchViewModel.kt

@ -20,6 +20,9 @@ open class BaseViewModel(application: Application) : AndroidViewModel(applicatio
return Coroutine.launch(this) { block().await() } return Coroutine.launch(this) { block().await() }
} }
fun <T> plus(coroutine: Coroutine<T>): Coroutine<T> {
return Coroutine.plus(coroutine)
}
override fun onCleared() { override fun onCleared() {
super.onCleared() super.onCleared()

@ -2,7 +2,7 @@ package io.legado.app.help.coroutine
import kotlinx.coroutines.* import kotlinx.coroutines.*
class Coroutine<T>(private val scope: CoroutineScope, block: suspend CoroutineScope.() -> T) { class Coroutine<T>(private val scope: CoroutineScope, private val block: suspend CoroutineScope.() -> T) {
companion object { companion object {
@ -11,10 +11,22 @@ class Coroutine<T>(private val scope: CoroutineScope, block: suspend CoroutineSc
fun <T> launch(scope: CoroutineScope = DEFAULT, block: suspend CoroutineScope.() -> T): Coroutine<T> { fun <T> launch(scope: CoroutineScope = DEFAULT, block: suspend CoroutineScope.() -> T): Coroutine<T> {
return Coroutine(scope, block) return Coroutine(scope, block)
} }
fun <T> plus(coroutine: Coroutine<T>): Coroutine<T>{
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 { init {
scope.launch { job = scope.launch {
executeInternal(block) executeInternal(block)
} }
} }
@ -28,6 +40,7 @@ class Coroutine<T>(private val scope: CoroutineScope, block: suspend CoroutineSc
private var errorReturn: Result<T>? = null private var errorReturn: Result<T>? = null
fun timeout(timeMillis: () -> Long): Coroutine<T> { fun timeout(timeMillis: () -> Long): Coroutine<T> {
this.timeMillis = timeMillis() this.timeMillis = timeMillis()
return this@Coroutine return this@Coroutine
@ -58,8 +71,9 @@ class Coroutine<T>(private val scope: CoroutineScope, block: suspend CoroutineSc
return this@Coroutine return this@Coroutine
} }
//取消当前任务
fun cancel(cause: CancellationException? = null) { fun cancel(cause: CancellationException? = null) {
scope.cancel(cause) job.cancel(cause)
} }
private suspend fun executeInternal(block: suspend CoroutineScope.() -> T) { private suspend fun executeInternal(block: suspend CoroutineScope.() -> T) {

@ -6,8 +6,11 @@ import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData import androidx.lifecycle.MutableLiveData
import io.legado.app.base.BaseViewModel import io.legado.app.base.BaseViewModel
import io.legado.app.data.api.CommonHttpApi 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.data.entities.SearchBook
import io.legado.app.help.coroutine.Coroutine
import io.legado.app.help.http.HttpHelper import io.legado.app.help.http.HttpHelper
import io.legado.app.model.WebBook
import kotlinx.coroutines.delay import kotlinx.coroutines.delay
class SearchViewModel(application: Application) : BaseViewModel(application) { class SearchViewModel(application: Application) : BaseViewModel(application) {
@ -45,6 +48,8 @@ class SearchViewModel(application: Application) : BaseViewModel(application) {
finally() finally()
} }
} }
plus(WebBook(BookSource()).searchBook("keyword", 1))
} }
} }

Loading…
Cancel
Save