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. 11
      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 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 <T> execute(domain: suspend CoroutineScope.() -> T): Coroutine<T> {
return Coroutine.with<T>(this).execute(domain)
}
override fun onCleared() {

@ -2,12 +2,12 @@ package io.legado.app.help.coroutine
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 {
fun <T> of(value: suspend CoroutineScope.() -> T): Coroutine<T> {
return Coroutine(value)
fun <T> with(scope: CoroutineScope): Coroutine<T> {
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 finally: (() -> Unit)? = null
private var value: T? = null
init {
val job: Job = launch {
fun execute(domain: suspend CoroutineScope.() -> T): Coroutine<T> {
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<T>(private val domain: (suspend CoroutineScope.() -> T)? = null)
finally?.let { it() }
})
}
return this@Coroutine
}
fun onStart(start: (() -> Unit)): Coroutine<T> {
this.start = start
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> {
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
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.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<CommonHttpApi>(
"http://www.baidu.com"
).get("http://www.baidu.com").await()
delay(4000L)
Log.e("TAG1", Thread.currentThread().name)
response
@ -59,7 +57,6 @@ class SearchViewModel(application: Application) : BaseViewModel(application) {
Log.e("TAG!", "finally")
}
// task.cancel()
}
}

Loading…
Cancel
Save