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 852e99166..1d6fed36a 100644 --- a/app/src/main/java/io/legado/app/base/BaseViewModel.kt +++ b/app/src/main/java/io/legado/app/base/BaseViewModel.kt @@ -2,35 +2,17 @@ package io.legado.app.base import android.app.Application import androidx.lifecycle.AndroidViewModel -import kotlinx.coroutines.* +import io.legado.app.help.coroutine.Coroutine +import kotlinx.coroutines.CoroutineScope +import kotlinx.coroutines.MainScope +import kotlinx.coroutines.cancel import org.jetbrains.anko.AnkoLogger open class BaseViewModel(application: Application) : AndroidViewModel(application), CoroutineScope by MainScope(), AnkoLogger { - - protected fun launchOnUI( - tryBlock: suspend CoroutineScope.() -> Unit, - errorBlock: (suspend CoroutineScope.(Throwable) -> Unit)? = null,//失败 - finallyBlock: (suspend CoroutineScope.() -> Unit)? = null//结束 - ) { - launch { - tryCatch(tryBlock, errorBlock, finallyBlock) - } - } - - private suspend fun tryCatch( - tryBlock: suspend CoroutineScope.() -> Unit, - errorBlock: (suspend CoroutineScope.(Throwable) -> Unit)? = null, - finallyBlock: (suspend CoroutineScope.() -> Unit)? = null - ) { - try { - coroutineScope { tryBlock() } - } catch (e: Throwable) { - coroutineScope { errorBlock?.let { it(e) } } - } finally { - coroutineScope { finallyBlock?.let { it() } } - } + fun execute(domain: suspend CoroutineScope.() -> T): Coroutine { + return Coroutine.with(this).execute(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 2419afb4a..7eebfb59a 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,12 +2,12 @@ package io.legado.app.help.coroutine import kotlinx.coroutines.* -class Coroutine(private val domain: (suspend CoroutineScope.() -> T)? = null) : CoroutineScope by MainScope() { +class Coroutine(private val scope: CoroutineScope) { companion object { - fun of(value: suspend CoroutineScope.() -> T): Coroutine { - return Coroutine(value) + fun with(scope: CoroutineScope): Coroutine { + return Coroutine(scope) } } @@ -16,24 +16,20 @@ class Coroutine(private val domain: (suspend CoroutineScope.() -> T)? = null) private var error: ((Throwable) -> Unit)? = null private var finally: (() -> Unit)? = null - private var value: T? = null - - init { - val job: Job = launch { + fun execute(domain: suspend CoroutineScope.() -> T): Coroutine { + scope.launch { tryCatch( { start?.let { it() } val result: T? = withContext(Dispatchers.IO) { - domain?.let { - value = it() - return@let value - } + domain() } success?.let { it(result) } + }, { e -> error?.let { it(e) } @@ -42,20 +38,13 @@ class Coroutine(private val domain: (suspend CoroutineScope.() -> T)? = null) finally?.let { it() } }) } + return this@Coroutine } fun onStart(start: (() -> Unit)): Coroutine { this.start = start return this@Coroutine } -// -// fun map(func: Function): Coroutine { -// return of { func.apply(value) } -// } -// -// fun flatMap(func: Function>): Coroutine { -// return func.apply(value) -// } fun onSuccess(success: (T?) -> Unit): Coroutine { this.success = success diff --git a/app/src/main/java/io/legado/app/help/coroutine/Function.kt b/app/src/main/java/io/legado/app/help/coroutine/Function.kt deleted file mode 100644 index 78cea56db..000000000 --- a/app/src/main/java/io/legado/app/help/coroutine/Function.kt +++ /dev/null @@ -1,7 +0,0 @@ -package io.legado.app.help.coroutine - -interface Function { - - fun apply(t: T?): R - -} \ No newline at end of file diff --git a/app/src/main/java/io/legado/app/ui/search/SearchActivity.kt b/app/src/main/java/io/legado/app/ui/search/SearchActivity.kt index 34914296d..b2f662875 100644 --- a/app/src/main/java/io/legado/app/ui/search/SearchActivity.kt +++ b/app/src/main/java/io/legado/app/ui/search/SearchActivity.kt @@ -14,6 +14,7 @@ class SearchActivity : BaseActivity() { get() = R.layout.activity_search override fun onViewModelCreated(viewModel: SearchViewModel, savedInstanceState: Bundle?) { + viewModel.search() } } 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 c2466b084..acdc64c86 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 @@ -7,13 +7,9 @@ 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.coroutine.Coroutine -import io.legado.app.help.coroutine.Function import io.legado.app.help.http.HttpHelper -import kotlinx.coroutines.* -import kotlinx.coroutines.Dispatchers.IO +import kotlinx.coroutines.delay import org.jetbrains.anko.error -import java.lang.StringBuilder class SearchViewModel(application: Application) : BaseViewModel(application) { @@ -37,11 +33,13 @@ class SearchViewModel(application: Application) : BaseViewModel(application) { // { error { "${it.message}" } }, // { finally?.let { it() } }) - val task = Coroutine.of { + 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 @@ -50,7 +48,7 @@ class SearchViewModel(application: Application) : BaseViewModel(application) { Log.e("TAG!", "start") } .onSuccess { - Log.e("TAG!", "success: $it") + Log.e("TAG!", "success: $it") } .onError { error { "${it.message}" } @@ -59,7 +57,6 @@ class SearchViewModel(application: Application) : BaseViewModel(application) { Log.e("TAG!", "finally") } -// task.cancel() } }