pull/32/head
Invinciblelee 6 years ago
parent 3f3593c616
commit 09e94d8b5c
  1. 15
      app/src/main/java/io/legado/app/base/BaseViewModel.kt
  2. 17
      app/src/main/java/io/legado/app/ui/main/MainViewModel.kt
  3. 10
      app/src/main/java/io/legado/app/ui/search/SearchViewModel.kt
  4. 8
      app/src/main/java/io/legado/app/ui/sourceedit/SourceEditViewModel.kt

@ -10,13 +10,12 @@ open class BaseViewModel(application: Application) : AndroidViewModel(applicatio
override val coroutineContext: CoroutineContext override val coroutineContext: CoroutineContext
get() = Dispatchers.Main get() = Dispatchers.Main
private val launchManager: MutableList<Job> = mutableListOf() private val launchManager: MutableList<Job> = mutableListOf()
protected fun launchOnUI( protected fun launchOnUI(
tryBlock: suspend CoroutineScope.() -> Unit,//成功 tryBlock: suspend CoroutineScope.() -> Unit,
errorBlock: suspend CoroutineScope.(Throwable) -> Unit,//失败 errorBlock: (suspend CoroutineScope.(Throwable) -> Unit)? = null,//失败
finallyBlock: suspend CoroutineScope.() -> Unit//结束 finallyBlock: (suspend CoroutineScope.() -> Unit)? = null//结束
) { ) {
launchOnUI { launchOnUI {
tryCatch(tryBlock, errorBlock, finallyBlock) tryCatch(tryBlock, errorBlock, finallyBlock)
@ -34,15 +33,15 @@ open class BaseViewModel(application: Application) : AndroidViewModel(applicatio
private suspend fun tryCatch( private suspend fun tryCatch(
tryBlock: suspend CoroutineScope.() -> Unit, tryBlock: suspend CoroutineScope.() -> Unit,
errorBlock: suspend CoroutineScope.(Throwable) -> Unit, errorBlock: (suspend CoroutineScope.(Throwable) -> Unit)? = null,
finallyBlock: suspend CoroutineScope.() -> Unit finallyBlock: (suspend CoroutineScope.() -> Unit)? = null
) { ) {
try { try {
coroutineScope { tryBlock() } coroutineScope { tryBlock() }
} catch (e: Throwable) { } catch (e: Throwable) {
coroutineScope { errorBlock(e) } coroutineScope { errorBlock?.let { it(e) } }
} finally { } finally {
coroutineScope { finallyBlock() } coroutineScope { finallyBlock?.let { it() } }
} }
} }

@ -1,9 +1,22 @@
package io.legado.app.ui.main package io.legado.app.ui.main
import android.app.Application 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")
})
}
} }

@ -1,7 +1,6 @@
package io.legado.app.ui.search package io.legado.app.ui.search
import android.app.Application import android.app.Application
import android.util.Log
import androidx.lifecycle.LiveData import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData import androidx.lifecycle.MutableLiveData
import io.legado.app.base.BaseViewModel 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 io.legado.app.help.http.HttpHelper
import kotlinx.coroutines.Dispatchers.IO import kotlinx.coroutines.Dispatchers.IO
import kotlinx.coroutines.withContext import kotlinx.coroutines.withContext
import org.jetbrains.anko.error
class SearchViewModel(application: Application) : BaseViewModel(application) { class SearchViewModel(application: Application) : BaseViewModel(application) {
val searchBooks: LiveData<List<SearchBook>> = MutableLiveData() val searchBooks: LiveData<List<SearchBook>> = MutableLiveData()
public fun search(start: () -> Unit, finally: () -> Unit) { fun search(start: () -> Unit, finally: () -> Unit) {
launchOnUI( launchOnUI(
{ {
start() start()
@ -27,12 +27,8 @@ class SearchViewModel(application: Application) : BaseViewModel(application) {
val result = searchResponse.await() val result = searchResponse.await()
}, },
{ Log.i("TAG", "${it.message}") }, { error { "${it.message}" } },
{ finally() }) { finally() })
// GlobalScope.launch {
//
// }
} }
} }

@ -1,19 +1,19 @@
package io.legado.app.ui.sourceedit package io.legado.app.ui.sourceedit
import android.app.Application import android.app.Application
import androidx.lifecycle.AndroidViewModel
import androidx.lifecycle.MutableLiveData import androidx.lifecycle.MutableLiveData
import io.legado.app.App import io.legado.app.App
import io.legado.app.base.BaseViewModel
import io.legado.app.data.entities.BookSource import io.legado.app.data.entities.BookSource
import kotlinx.coroutines.GlobalScope import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch import kotlinx.coroutines.launch
class SourceEditViewModel(application: Application) : AndroidViewModel(application) { class SourceEditViewModel(application: Application) : BaseViewModel(application) {
val sourceLiveData: MutableLiveData<BookSource> = MutableLiveData() val sourceLiveData: MutableLiveData<BookSource> = MutableLiveData()
fun setBookSource(key: String) { fun setBookSource(key: String) {
GlobalScope.launch { launch(Dispatchers.IO) {
val source = App.db.bookSourceDao().findByKey(key) val source = App.db.bookSourceDao().findByKey(key)
sourceLiveData.postValue(source) sourceLiveData.postValue(source)
} }

Loading…
Cancel
Save