|
|
|
@ -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() { |
|
|
|
|