Coroutine onErrorReturn

pull/32/head
Administrator 6 years ago
parent 0e2636068a
commit eea048dc70
  1. 44
      app/src/main/java/io/legado/app/help/coroutine/Coroutine.kt
  2. 2
      app/src/main/java/io/legado/app/model/analyzeRule/AnalyzeRule.kt
  3. 26
      app/src/main/java/io/legado/app/ui/search/SearchViewModel.kt

@ -2,18 +2,18 @@ package io.legado.app.help.coroutine
import kotlinx.coroutines.* import kotlinx.coroutines.*
class Coroutine<T>(scope: CoroutineScope, private val domain: suspend CoroutineScope.() -> T) { class Coroutine<T>(scope: CoroutineScope, private val block: suspend CoroutineScope.() -> T) {
companion object { companion object {
fun <T> with(scope: CoroutineScope, domain: suspend CoroutineScope.() -> T): Coroutine<T> { fun <T> with(scope: CoroutineScope, block: suspend CoroutineScope.() -> T): Coroutine<T> {
return Coroutine(scope, domain) return Coroutine(scope, block)
} }
} }
init { init {
scope.launch { scope.launch {
executeInternal(domain) executeInternal(block)
} }
} }
@ -24,8 +24,15 @@ class Coroutine<T>(scope: CoroutineScope, private val domain: suspend CoroutineS
private var timeMillis: Long? = null private var timeMillis: Long? = null
fun timeout(timeMillis: Long): Coroutine<T> { private var errorReturn: Result<T>? = null
this.timeMillis = timeMillis
fun timeout(timeMillis: () -> Long): Coroutine<T> {
this.timeMillis = timeMillis()
return this@Coroutine
}
fun onErrorReturn(value: () -> T?): Coroutine<T> {
errorReturn = Result(value())
return this@Coroutine return this@Coroutine
} }
@ -49,32 +56,40 @@ class Coroutine<T>(scope: CoroutineScope, private val domain: suspend CoroutineS
return this@Coroutine return this@Coroutine
} }
private suspend fun executeInternal(domain: suspend CoroutineScope.() -> T) { private suspend fun executeInternal(block: suspend CoroutineScope.() -> T) {
tryCatch( tryCatch(
{ {
start?.let { it() } start?.let { it() }
val result = if (timeMillis != null && timeMillis!! > 0) { val time = timeMillis
withTimeout(timeMillis!!) { val result = if (time != null && time > 0) {
executeDomain(domain) withTimeout(time) {
executeBlock(block)
} }
} else { } else {
executeDomain(domain) executeBlock(block)
} }
success?.let { it(result) } success?.let { it(result) }
}, },
{ e -> { e ->
error?.let { it(e) } val consume: Boolean = errorReturn?.value?.let { value ->
success?.let { it(value) }
true
} ?: false
if (!consume) {
error?.let { it(e) }
}
}, },
{ {
finally?.let { it() } finally?.let { it() }
}) })
} }
private suspend fun executeDomain(domain: suspend CoroutineScope.() -> T): T? { private suspend fun executeBlock(block: suspend CoroutineScope.() -> T): T? {
return withContext(Dispatchers.IO) { return withContext(Dispatchers.IO) {
domain() block()
} }
} }
@ -92,4 +107,5 @@ class Coroutine<T>(scope: CoroutineScope, private val domain: suspend CoroutineS
} }
} }
private data class Result<out T>(val value: T?)
} }

@ -146,7 +146,7 @@ class AnalyzeRule(private var book: BaseBook? = null) {
} }
if (result == null) return ArrayList() if (result == null) return ArrayList()
if (result is String) { if (result is String) {
result = listOf((result as String?)?.htmlFormat()?.split("\n")) result = listOf((result as String).htmlFormat().split("\n"))
} }
baseUrl?.let { baseUrl?.let {
if (isUrl && !TextUtils.isEmpty(it)) { if (isUrl && !TextUtils.isEmpty(it)) {

@ -5,7 +5,10 @@ import android.util.Log
import androidx.lifecycle.LiveData 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.entities.SearchBook import io.legado.app.data.entities.SearchBook
import io.legado.app.help.http.HttpHelper
import kotlinx.coroutines.delay
class SearchViewModel(application: Application) : BaseViewModel(application) { class SearchViewModel(application: Application) : BaseViewModel(application) {
@ -13,16 +16,19 @@ class SearchViewModel(application: Application) : BaseViewModel(application) {
fun search(start: (() -> Unit)? = null, finally: (() -> Unit)? = null) { fun search(start: (() -> Unit)? = null, finally: (() -> Unit)? = null) {
execute { execute {
// val response: String = HttpHelper.getApiService<CommonHttpApi>( val response: String = HttpHelper.getApiService<CommonHttpApi>(
// "http://www.baidu.com" "http://www.baidu.com"
// ).get("http://www.baidu.com").await() ).get("http://www.baidu.com").await()
//
// delay(4000L) delay(4000L)
//
// Log.e("TAG1", Thread.currentThread().name) Log.e("TAG1", Thread.currentThread().name)
null response
}.timeout(30000L)
}
.timeout { 100L }
.onErrorReturn { "error return" }
.onStart { .onStart {
Log.e("TAG!", "start") Log.e("TAG!", "start")
} }

Loading…
Cancel
Save