diff --git a/app/src/main/java/io/legado/app/base/BaseViewModel.kt b/app/src/main/java/io/legado/app/base/BaseViewModel.kt index 1d6fed36a..a54dce392 100644 --- a/app/src/main/java/io/legado/app/base/BaseViewModel.kt +++ b/app/src/main/java/io/legado/app/base/BaseViewModel.kt @@ -12,7 +12,7 @@ open class BaseViewModel(application: Application) : AndroidViewModel(applicatio AnkoLogger { fun execute(domain: suspend CoroutineScope.() -> T): Coroutine { - return Coroutine.with(this).execute(domain) + return Coroutine.with(this) { domain() } } override fun onCleared() { 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 7eebfb59a..e56b9d2e9 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 @@ -1,13 +1,20 @@ package io.legado.app.help.coroutine +import android.util.Log import kotlinx.coroutines.* -class Coroutine(private val scope: CoroutineScope) { +class Coroutine(scope: CoroutineScope, private val domain: suspend CoroutineScope.() -> T) { companion object { - fun with(scope: CoroutineScope): Coroutine { - return Coroutine(scope) + fun with(scope: CoroutineScope, domain: suspend CoroutineScope.() -> T): Coroutine { + return Coroutine(scope, domain) + } + } + + init { + scope.launch { + executeInternal(domain) } } @@ -16,28 +23,10 @@ class Coroutine(private val scope: CoroutineScope) { private var error: ((Throwable) -> Unit)? = null private var finally: (() -> Unit)? = null - fun execute(domain: suspend CoroutineScope.() -> T): Coroutine { - scope.launch { - tryCatch( - { - start?.let { it() } - - - val result: T? = withContext(Dispatchers.IO) { - domain() - } - - success?.let { it(result) } + private var timeMillis: Long? = null - - }, - { e -> - error?.let { it(e) } - }, - { - finally?.let { it() } - }) - } + fun timeout(timeMillis: Long): Coroutine { + this.timeMillis = timeMillis return this@Coroutine } @@ -61,6 +50,36 @@ class Coroutine(private val scope: CoroutineScope) { return this@Coroutine } + private suspend fun executeInternal(domain: suspend CoroutineScope.() -> T) { + tryCatch( + { + start?.let { it() } + + val result = if (timeMillis != null && timeMillis!! > 0) { + withTimeout(timeMillis!!) { + executeDomain(domain) + } + } else { + executeDomain(domain) + } + + success?.let { it(result) } + }, + { e -> + error?.let { it(e) } + }, + { + finally?.let { it() } + }) + } + + private suspend fun executeDomain(domain: suspend CoroutineScope.() -> T): T? { + return withContext(Dispatchers.IO) { + Log.e("TAG!", "executeDomain") + domain() + } + } + private suspend fun tryCatch( tryBlock: suspend CoroutineScope.() -> Unit, errorBlock: (suspend CoroutineScope.(Throwable) -> Unit)? = null, 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 e47c57a0d..0dbf26294 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 @@ -152,7 +152,7 @@ class AnalyzeRule(private var book: BaseBook? = null) { if (isUrl && !TextUtils.isEmpty(it)) { val urlList = ArrayList() if (result is List<*>) { - for (url in result) { + for (url in result as List<*>) { val absoluteURL = NetworkUtils.getAbsoluteURL(it, url.toString()) if (!urlList.contains(absoluteURL)) { urlList.add(absoluteURL) 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 86df445b5..fb0364917 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 @@ -9,6 +9,7 @@ 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 +import kotlinx.coroutines.withTimeout import org.jetbrains.anko.error class SearchViewModel(application: Application) : BaseViewModel(application) { @@ -17,16 +18,16 @@ 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) - - response - } +// 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) .onStart { Log.e("TAG!", "start") } @@ -34,12 +35,11 @@ class SearchViewModel(application: Application) : BaseViewModel(application) { Log.e("TAG!", "success: $it") } .onError { - error { "${it.message}" } + Log.e("TAG!", "error: $it") } .onFinally { Log.e("TAG!", "finally") } - } }