diff --git a/app/src/main/java/io/legado/app/help/coroutine/Coroutine.kt b/app/src/main/java/io/legado/app/help/coroutine/Coroutine.kt index fbb2cd873..d6aec0d0c 100644 --- a/app/src/main/java/io/legado/app/help/coroutine/Coroutine.kt +++ b/app/src/main/java/io/legado/app/help/coroutine/Coroutine.kt @@ -2,18 +2,18 @@ package io.legado.app.help.coroutine import kotlinx.coroutines.* -class Coroutine(scope: CoroutineScope, private val domain: suspend CoroutineScope.() -> T) { +class Coroutine(scope: CoroutineScope, private val block: suspend CoroutineScope.() -> T) { companion object { - fun with(scope: CoroutineScope, domain: suspend CoroutineScope.() -> T): Coroutine { - return Coroutine(scope, domain) + fun with(scope: CoroutineScope, block: suspend CoroutineScope.() -> T): Coroutine { + return Coroutine(scope, block) } } init { scope.launch { - executeInternal(domain) + executeInternal(block) } } @@ -24,8 +24,15 @@ class Coroutine(scope: CoroutineScope, private val domain: suspend CoroutineS private var timeMillis: Long? = null - fun timeout(timeMillis: Long): Coroutine { - this.timeMillis = timeMillis + private var errorReturn: Result? = null + + fun timeout(timeMillis: () -> Long): Coroutine { + this.timeMillis = timeMillis() + return this@Coroutine + } + + fun onErrorReturn(value: () -> T?): Coroutine { + errorReturn = Result(value()) return this@Coroutine } @@ -49,32 +56,40 @@ class Coroutine(scope: CoroutineScope, private val domain: suspend CoroutineS return this@Coroutine } - private suspend fun executeInternal(domain: suspend CoroutineScope.() -> T) { + private suspend fun executeInternal(block: suspend CoroutineScope.() -> T) { tryCatch( { start?.let { it() } - val result = if (timeMillis != null && timeMillis!! > 0) { - withTimeout(timeMillis!!) { - executeDomain(domain) + val time = timeMillis + val result = if (time != null && time > 0) { + withTimeout(time) { + executeBlock(block) } } else { - executeDomain(domain) + executeBlock(block) } success?.let { it(result) } }, { 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() } }) } - private suspend fun executeDomain(domain: suspend CoroutineScope.() -> T): T? { + private suspend fun executeBlock(block: suspend CoroutineScope.() -> T): T? { return withContext(Dispatchers.IO) { - domain() + block() } } @@ -92,4 +107,5 @@ class Coroutine(scope: CoroutineScope, private val domain: suspend CoroutineS } } + private data class Result(val value: T?) } diff --git a/app/src/main/java/io/legado/app/model/analyzeRule/AnalyzeRule.kt b/app/src/main/java/io/legado/app/model/analyzeRule/AnalyzeRule.kt index 0dbf26294..e8fc5bb81 100644 --- a/app/src/main/java/io/legado/app/model/analyzeRule/AnalyzeRule.kt +++ b/app/src/main/java/io/legado/app/model/analyzeRule/AnalyzeRule.kt @@ -146,7 +146,7 @@ class AnalyzeRule(private var book: BaseBook? = null) { } if (result == null) return ArrayList() if (result is String) { - result = listOf((result as String?)?.htmlFormat()?.split("\n")) + result = listOf((result as String).htmlFormat().split("\n")) } baseUrl?.let { if (isUrl && !TextUtils.isEmpty(it)) { diff --git a/app/src/main/java/io/legado/app/ui/search/SearchViewModel.kt b/app/src/main/java/io/legado/app/ui/search/SearchViewModel.kt index 629497204..376a47b76 100644 --- a/app/src/main/java/io/legado/app/ui/search/SearchViewModel.kt +++ b/app/src/main/java/io/legado/app/ui/search/SearchViewModel.kt @@ -5,7 +5,10 @@ import android.util.Log 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.SearchBook +import io.legado.app.help.http.HttpHelper +import kotlinx.coroutines.delay class SearchViewModel(application: Application) : BaseViewModel(application) { @@ -13,16 +16,19 @@ class SearchViewModel(application: Application) : BaseViewModel(application) { fun search(start: (() -> Unit)? = null, finally: (() -> Unit)? = null) { execute { - // val response: String = HttpHelper.getApiService( -// "http://www.baidu.com" -// ).get("http://www.baidu.com").await() -// -// delay(4000L) -// -// Log.e("TAG1", Thread.currentThread().name) - - null - }.timeout(30000L) + val response: String = HttpHelper.getApiService( + "http://www.baidu.com" + ).get("http://www.baidu.com").await() + + delay(4000L) + + Log.e("TAG1", Thread.currentThread().name) + + response + + } + .timeout { 100L } + .onErrorReturn { "error return" } .onStart { Log.e("TAG!", "start") }