pull/32/head
Administrator 5 years ago
parent 216489288f
commit afd4b709a0
  1. 24
      app/src/main/java/io/legado/app/help/coroutine/Coroutine.kt
  2. 26
      app/src/main/java/io/legado/app/ui/search/SearchViewModel.kt

@ -20,21 +20,21 @@ class Coroutine<T>() {
private var interceptor: Coroutine<T>? = null
private var job: Job? = null
private var start: (() -> Unit)? = null
private var success: ((T?) -> Unit)? = null
private var error: ((Throwable) -> Unit)? = null
private var finally: (() -> Unit)? = null
private var start: (suspend CoroutineScope.() -> Unit)? = null
private var success: (suspend CoroutineScope.(T?) -> Unit)? = null
private var error: (suspend CoroutineScope.(Throwable) -> Unit)? = null
private var finally: (suspend CoroutineScope.() -> Unit)? = null
private var timeMillis: Long? = null
private var errorReturn: Result<T>? = null
private constructor(
scope: CoroutineScope? = null,
block: (suspend CoroutineScope.() -> T)? = null
scope: CoroutineScope,
block: suspend CoroutineScope.() -> T
) : this() {
this.job = scope?.launch {
block?.let { executeInternal(it) }
this.job = scope.launch {
executeInternal(block)
}
}
@ -61,7 +61,7 @@ class Coroutine<T>() {
return this@Coroutine
}
fun onStart(start: (() -> Unit)): Coroutine<T> {
fun onStart(start: (suspend CoroutineScope.() -> Unit)): Coroutine<T> {
if (this.interceptor != null) {
this.interceptor!!.start = start
} else {
@ -70,7 +70,7 @@ class Coroutine<T>() {
return this@Coroutine
}
fun onSuccess(success: (T?) -> Unit): Coroutine<T> {
fun onSuccess(success: suspend CoroutineScope.(T?) -> Unit): Coroutine<T> {
if (this.interceptor != null) {
this.interceptor!!.success = success
} else {
@ -79,7 +79,7 @@ class Coroutine<T>() {
return this@Coroutine
}
fun onError(error: (Throwable) -> Unit): Coroutine<T> {
fun onError(error: suspend CoroutineScope.(Throwable) -> Unit): Coroutine<T> {
if (this.interceptor != null) {
this.interceptor!!.error = error
} else {
@ -88,7 +88,7 @@ class Coroutine<T>() {
return this@Coroutine
}
fun onFinally(finally: () -> Unit): Coroutine<T> {
fun onFinally(finally: suspend CoroutineScope.() -> Unit): Coroutine<T> {
if (this.interceptor != null) {
this.interceptor!!.finally = finally
} else {

@ -6,11 +6,9 @@ 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.channels.Channel
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
@ -18,7 +16,14 @@ class SearchViewModel(application: Application) : BaseViewModel(application) {
val searchBooks: LiveData<List<SearchBook>> = MutableLiveData()
private val channel = Channel<Int>()//协程之间通信
fun search(start: (() -> Unit)? = null, finally: (() -> Unit)? = null) {
launch {
repeat(1000) {
channel.send(it)
}
}
val c = execute {
val response: String = HttpHelper.getApiService<CommonHttpApi>(
"http://www.baidu.com"
@ -49,13 +54,13 @@ class SearchViewModel(application: Application) : BaseViewModel(application) {
val c2 = plus(c)
// .timeout { 100L }
// .onErrorReturn { "error return2" }
.onStart {
.onStart {//会拦截掉c的onStart
Log.e("TAG!", "start2")
start?.let { it() }
}
.onSuccess {
Log.e("TAG!", "success2: $it")
}
// .onSuccess {
// Log.e("TAG!", "success2: $it")
// }
.onError {
Log.e("TAG!", "error2: $it")
}
@ -64,13 +69,16 @@ class SearchViewModel(application: Application) : BaseViewModel(application) {
if (finally != null) {
finally()
}
Log.e("TAG!", "rec2: " + channel.receive())
}
launch {
delay(1500L)
c2.cancel()
delay(1500L)
// c2.cancel()
// c.cancel()
}
}

Loading…
Cancel
Save