pull/32/head
Administrator 5 years ago
parent 0f70ab546b
commit 2bf60b6248
  1. 30
      app/src/main/java/io/legado/app/base/BaseViewModel.kt
  2. 27
      app/src/main/java/io/legado/app/help/coroutine/Coroutine.kt
  3. 7
      app/src/main/java/io/legado/app/help/coroutine/Function.kt
  4. 1
      app/src/main/java/io/legado/app/ui/search/SearchActivity.kt
  5. 13
      app/src/main/java/io/legado/app/ui/search/SearchViewModel.kt

@ -2,35 +2,17 @@ package io.legado.app.base
import android.app.Application import android.app.Application
import androidx.lifecycle.AndroidViewModel 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 import org.jetbrains.anko.AnkoLogger
open class BaseViewModel(application: Application) : AndroidViewModel(application), CoroutineScope by MainScope(), open class BaseViewModel(application: Application) : AndroidViewModel(application), CoroutineScope by MainScope(),
AnkoLogger { AnkoLogger {
fun <T> execute(domain: suspend CoroutineScope.() -> T): Coroutine<T> {
protected fun launchOnUI( return Coroutine.with<T>(this).execute(domain)
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() } }
}
} }
override fun onCleared() { override fun onCleared() {

@ -2,12 +2,12 @@ package io.legado.app.help.coroutine
import kotlinx.coroutines.* import kotlinx.coroutines.*
class Coroutine<T>(private val domain: (suspend CoroutineScope.() -> T)? = null) : CoroutineScope by MainScope() { class Coroutine<T>(private val scope: CoroutineScope) {
companion object { companion object {
fun <T> of(value: suspend CoroutineScope.() -> T): Coroutine<T> { fun <T> with(scope: CoroutineScope): Coroutine<T> {
return Coroutine(value) return Coroutine(scope)
} }
} }
@ -16,24 +16,20 @@ class Coroutine<T>(private val domain: (suspend CoroutineScope.() -> T)? = null)
private var error: ((Throwable) -> Unit)? = null private var error: ((Throwable) -> Unit)? = null
private var finally: (() -> Unit)? = null private var finally: (() -> Unit)? = null
private var value: T? = null fun execute(domain: suspend CoroutineScope.() -> T): Coroutine<T> {
scope.launch {
init {
val job: Job = launch {
tryCatch( tryCatch(
{ {
start?.let { it() } start?.let { it() }
val result: T? = withContext(Dispatchers.IO) { val result: T? = withContext(Dispatchers.IO) {
domain?.let { domain()
value = it()
return@let value
}
} }
success?.let { it(result) } success?.let { it(result) }
}, },
{ e -> { e ->
error?.let { it(e) } error?.let { it(e) }
@ -42,20 +38,13 @@ class Coroutine<T>(private val domain: (suspend CoroutineScope.() -> T)? = null)
finally?.let { it() } finally?.let { it() }
}) })
} }
return this@Coroutine
} }
fun onStart(start: (() -> Unit)): Coroutine<T> { fun onStart(start: (() -> Unit)): Coroutine<T> {
this.start = start this.start = start
return this@Coroutine return this@Coroutine
} }
//
// fun <U> map(func: Function<T, U>): Coroutine<U> {
// return of { func.apply(value) }
// }
//
// fun <U> flatMap(func: Function<T, Coroutine<U>>): Coroutine<U> {
// return func.apply(value)
// }
fun onSuccess(success: (T?) -> Unit): Coroutine<T> { fun onSuccess(success: (T?) -> Unit): Coroutine<T> {
this.success = success this.success = success

@ -1,7 +0,0 @@
package io.legado.app.help.coroutine
interface Function<T, R> {
fun apply(t: T?): R
}

@ -14,6 +14,7 @@ class SearchActivity : BaseActivity<SearchViewModel>() {
get() = R.layout.activity_search get() = R.layout.activity_search
override fun onViewModelCreated(viewModel: SearchViewModel, savedInstanceState: Bundle?) { override fun onViewModelCreated(viewModel: SearchViewModel, savedInstanceState: Bundle?) {
viewModel.search()
} }
} }

@ -7,13 +7,9 @@ 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.api.CommonHttpApi
import io.legado.app.data.entities.SearchBook 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 io.legado.app.help.http.HttpHelper
import kotlinx.coroutines.* import kotlinx.coroutines.delay
import kotlinx.coroutines.Dispatchers.IO
import org.jetbrains.anko.error import org.jetbrains.anko.error
import java.lang.StringBuilder
class SearchViewModel(application: Application) : BaseViewModel(application) { class SearchViewModel(application: Application) : BaseViewModel(application) {
@ -37,11 +33,13 @@ class SearchViewModel(application: Application) : BaseViewModel(application) {
// { error { "${it.message}" } }, // { error { "${it.message}" } },
// { finally?.let { it() } }) // { finally?.let { it() } })
val task = Coroutine.of { 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)
Log.e("TAG1", Thread.currentThread().name) Log.e("TAG1", Thread.currentThread().name)
response response
@ -50,7 +48,7 @@ class SearchViewModel(application: Application) : BaseViewModel(application) {
Log.e("TAG!", "start") Log.e("TAG!", "start")
} }
.onSuccess { .onSuccess {
Log.e("TAG!", "success: $it") Log.e("TAG!", "success: $it")
} }
.onError { .onError {
error { "${it.message}" } error { "${it.message}" }
@ -59,7 +57,6 @@ class SearchViewModel(application: Application) : BaseViewModel(application) {
Log.e("TAG!", "finally") Log.e("TAG!", "finally")
} }
// task.cancel()
} }
} }

Loading…
Cancel
Save