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

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

Loading…
Cancel
Save