From 9bff78b22facae0ad502952022c9824808c2e560 Mon Sep 17 00:00:00 2001 From: Administrator <1760316362@qq.com> Date: Sat, 6 Jul 2019 18:49:12 +0800 Subject: [PATCH] update --- .../io/legado/app/data/api/CommonHttpApi.kt | 2 + .../io/legado/app/help/coroutine/Coroutine.kt | 89 +++++++++++++++++++ .../io/legado/app/help/coroutine/Function.kt | 7 ++ .../io/legado/app/ui/search/SearchActivity.kt | 1 - .../legado/app/ui/search/SearchViewModel.kt | 63 ++++++++----- 5 files changed, 140 insertions(+), 22 deletions(-) create mode 100644 app/src/main/java/io/legado/app/help/coroutine/Coroutine.kt create mode 100644 app/src/main/java/io/legado/app/help/coroutine/Function.kt diff --git a/app/src/main/java/io/legado/app/data/api/CommonHttpApi.kt b/app/src/main/java/io/legado/app/data/api/CommonHttpApi.kt index 51d468469..0819b3281 100644 --- a/app/src/main/java/io/legado/app/data/api/CommonHttpApi.kt +++ b/app/src/main/java/io/legado/app/data/api/CommonHttpApi.kt @@ -13,4 +13,6 @@ interface CommonHttpApi { fun post(@Url url: String, @FieldMap map: Map): Deferred + @GET + fun get(@Url url: String) : Deferred } \ No newline at end of file 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 new file mode 100644 index 000000000..2419afb4a --- /dev/null +++ b/app/src/main/java/io/legado/app/help/coroutine/Coroutine.kt @@ -0,0 +1,89 @@ +package io.legado.app.help.coroutine + +import kotlinx.coroutines.* + +class Coroutine(private val domain: (suspend CoroutineScope.() -> T)? = null) : CoroutineScope by MainScope() { + + companion object { + + fun of(value: suspend CoroutineScope.() -> T): Coroutine { + return Coroutine(value) + } + } + + private var start: (() -> Unit)? = null + private var success: ((T?) -> Unit)? = null + private var error: ((Throwable) -> Unit)? = null + private var finally: (() -> Unit)? = null + + private var value: T? = null + + init { + val job: Job = launch { + tryCatch( + { + start?.let { it() } + + + val result: T? = withContext(Dispatchers.IO) { + domain?.let { + value = it() + return@let value + } + } + + success?.let { it(result) } + + }, + { e -> + error?.let { it(e) } + }, + { + finally?.let { it() } + }) + } + } + + 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 + return this@Coroutine + } + + fun onError(error: (Throwable) -> Unit): Coroutine { + this.error = error + return this@Coroutine + } + + fun onFinally(finally: () -> Unit): Coroutine { + this.finally = finally + return this@Coroutine + } + + 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() } } + } + } + +} 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 new file mode 100644 index 000000000..78cea56db --- /dev/null +++ b/app/src/main/java/io/legado/app/help/coroutine/Function.kt @@ -0,0 +1,7 @@ +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 6a6a02fb0..34914296d 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,7 +14,6 @@ class SearchActivity : BaseActivity() { get() = R.layout.activity_search override fun onViewModelCreated(viewModel: SearchViewModel, savedInstanceState: Bundle?) { - } } 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 649df0c09..c2466b084 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 @@ -1,44 +1,65 @@ package io.legado.app.ui.search import android.app.Application +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.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.coroutineScope -import kotlinx.coroutines.launch -import kotlinx.coroutines.withContext import org.jetbrains.anko.error +import java.lang.StringBuilder class SearchViewModel(application: Application) : BaseViewModel(application) { val searchBooks: LiveData> = MutableLiveData() - fun search(start: () -> Unit, finally: () -> Unit) { - launchOnUI( - { - start() - val searchResponse = withContext(IO) { - HttpHelper.getApiService( - "http:www.baidu.com" - ).post("", mutableMapOf(Pair("key", "value"))) - } + fun search(start: (() -> Unit)? = null, finally: (() -> Unit)? = null) { +// launchOnUI( +// { +// start?.let { it() } +// val searchResponse = withContext(IO) { +// val response: Deferred = HttpHelper.getApiService( +// "http://www.baidu.com" +// ).get("http://www.baidu.com") +// +// } +// +//// val result = searchResponse.await() +//// +//// println(result) +// }, +// { error { "${it.message}" } }, +// { finally?.let { it() } }) - val result = searchResponse.await() - }, - { error { "${it.message}" } }, - { finally() }) + val task = Coroutine.of { + val response: String = HttpHelper.getApiService( + "http://www.baidu.com" + ).get("http://www.baidu.com").await() - launchOnUI({ - //TODO - }) - - launch { + Log.e("TAG1", Thread.currentThread().name) + response } + .onStart { + Log.e("TAG!", "start") + } + .onSuccess { + Log.e("TAG!", "success: $it") + } + .onError { + error { "${it.message}" } + } + .onFinally { + Log.e("TAG!", "finally") + } + +// task.cancel() } }