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 03b17ad49..c647b34bd 100644 --- a/app/src/main/java/io/legado/app/base/BaseViewModel.kt +++ b/app/src/main/java/io/legado/app/base/BaseViewModel.kt @@ -10,13 +10,12 @@ open class BaseViewModel(application: Application) : AndroidViewModel(applicatio override val coroutineContext: CoroutineContext get() = Dispatchers.Main - private val launchManager: MutableList = mutableListOf() protected fun launchOnUI( - tryBlock: suspend CoroutineScope.() -> Unit,//成功 - errorBlock: suspend CoroutineScope.(Throwable) -> Unit,//失败 - finallyBlock: suspend CoroutineScope.() -> Unit//结束 + tryBlock: suspend CoroutineScope.() -> Unit, + errorBlock: (suspend CoroutineScope.(Throwable) -> Unit)? = null,//失败 + finallyBlock: (suspend CoroutineScope.() -> Unit)? = null//结束 ) { launchOnUI { tryCatch(tryBlock, errorBlock, finallyBlock) @@ -34,15 +33,15 @@ open class BaseViewModel(application: Application) : AndroidViewModel(applicatio private suspend fun tryCatch( tryBlock: suspend CoroutineScope.() -> Unit, - errorBlock: suspend CoroutineScope.(Throwable) -> Unit, - finallyBlock: suspend CoroutineScope.() -> Unit + errorBlock: (suspend CoroutineScope.(Throwable) -> Unit)? = null, + finallyBlock: (suspend CoroutineScope.() -> Unit)? = null ) { try { coroutineScope { tryBlock() } } catch (e: Throwable) { - coroutineScope { errorBlock(e) } + coroutineScope { errorBlock?.let { it(e) } } } finally { - coroutineScope { finallyBlock() } + coroutineScope { finallyBlock?.let { it() } } } } diff --git a/app/src/main/java/io/legado/app/ui/main/MainViewModel.kt b/app/src/main/java/io/legado/app/ui/main/MainViewModel.kt index 82a9146c5..cf2f8f600 100644 --- a/app/src/main/java/io/legado/app/ui/main/MainViewModel.kt +++ b/app/src/main/java/io/legado/app/ui/main/MainViewModel.kt @@ -1,9 +1,22 @@ package io.legado.app.ui.main import android.app.Application -import androidx.lifecycle.AndroidViewModel +import io.legado.app.App +import io.legado.app.base.BaseViewModel +import kotlinx.coroutines.async +import org.jetbrains.anko.toast -class MainViewModel(application: Application) : AndroidViewModel(application) { +class MainViewModel(application: Application) : BaseViewModel(application) { + fun test() { + launchOnUI({ + + val result = async { + "结果" + } + +// App.INSTANCE.toast("result: $result") + }) + } } \ No newline at end of file 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 4f7bc17c7..594672d74 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,7 +1,6 @@ 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 @@ -10,12 +9,13 @@ import io.legado.app.data.entities.SearchBook import io.legado.app.help.http.HttpHelper import kotlinx.coroutines.Dispatchers.IO import kotlinx.coroutines.withContext +import org.jetbrains.anko.error class SearchViewModel(application: Application) : BaseViewModel(application) { val searchBooks: LiveData> = MutableLiveData() - public fun search(start: () -> Unit, finally: () -> Unit) { + fun search(start: () -> Unit, finally: () -> Unit) { launchOnUI( { start() @@ -27,12 +27,8 @@ class SearchViewModel(application: Application) : BaseViewModel(application) { val result = searchResponse.await() }, - { Log.i("TAG", "${it.message}") }, + { error { "${it.message}" } }, { finally() }) - -// GlobalScope.launch { -// -// } } } diff --git a/app/src/main/java/io/legado/app/ui/sourceedit/SourceEditViewModel.kt b/app/src/main/java/io/legado/app/ui/sourceedit/SourceEditViewModel.kt index b1af68e88..950eeb0bd 100644 --- a/app/src/main/java/io/legado/app/ui/sourceedit/SourceEditViewModel.kt +++ b/app/src/main/java/io/legado/app/ui/sourceedit/SourceEditViewModel.kt @@ -1,19 +1,19 @@ package io.legado.app.ui.sourceedit import android.app.Application -import androidx.lifecycle.AndroidViewModel import androidx.lifecycle.MutableLiveData import io.legado.app.App +import io.legado.app.base.BaseViewModel import io.legado.app.data.entities.BookSource -import kotlinx.coroutines.GlobalScope +import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.launch -class SourceEditViewModel(application: Application) : AndroidViewModel(application) { +class SourceEditViewModel(application: Application) : BaseViewModel(application) { val sourceLiveData: MutableLiveData = MutableLiveData() fun setBookSource(key: String) { - GlobalScope.launch { + launch(Dispatchers.IO) { val source = App.db.bookSourceDao().findByKey(key) sourceLiveData.postValue(source) }