pull/32/head
kunfei 5 years ago
parent c56587894b
commit 507ac06777
  1. 3
      app/src/main/java/io/legado/app/base/BaseViewModel.kt
  2. 9
      app/src/main/java/io/legado/app/ui/changesource/ChangeSourceViewModel.kt

@ -1,6 +1,7 @@
package io.legado.app.base package io.legado.app.base
import android.app.Application import android.app.Application
import android.content.Context
import androidx.lifecycle.AndroidViewModel import androidx.lifecycle.AndroidViewModel
import io.legado.app.App import io.legado.app.App
import io.legado.app.help.coroutine.Coroutine import io.legado.app.help.coroutine.Coroutine
@ -11,6 +12,8 @@ import org.jetbrains.anko.toast
open class BaseViewModel(application: Application) : AndroidViewModel(application), CoroutineScope by MainScope(), open class BaseViewModel(application: Application) : AndroidViewModel(application), CoroutineScope by MainScope(),
AnkoLogger { AnkoLogger {
val context: Context = this.getApplication<App>()
fun <T> execute(scope: CoroutineScope = this, block: suspend CoroutineScope.() -> T): Coroutine<T> { fun <T> execute(scope: CoroutineScope = this, block: suspend CoroutineScope.() -> T): Coroutine<T> {
return Coroutine.async(scope) { block() } return Coroutine.async(scope) { block() }
} }

@ -9,6 +9,7 @@ import io.legado.app.data.entities.Book
import io.legado.app.data.entities.SearchBook import io.legado.app.data.entities.SearchBook
import io.legado.app.model.WebBook import io.legado.app.model.WebBook
import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.Dispatchers
import org.jetbrains.anko.debug
class ChangeSourceViewModel(application: Application) : BaseViewModel(application) { class ChangeSourceViewModel(application: Application) : BaseViewModel(application) {
var curBookUrl = "" var curBookUrl = ""
@ -56,9 +57,9 @@ class ChangeSourceViewModel(application: Application) : BaseViewModel(applicatio
.onSuccess { .onSuccess {
it?.let { loadChapter(it) } it?.let { loadChapter(it) }
}.onError { }.onError {
toast(R.string.error_get_book_info) debug { context.getString(R.string.error_get_book_info) }
} }
} ?: toast(R.string.error_no_source) } ?: debug { context.getString(R.string.error_no_source) }
} }
private fun loadChapter(book: Book) { private fun loadChapter(book: Book) {
@ -72,9 +73,9 @@ class ChangeSourceViewModel(application: Application) : BaseViewModel(applicatio
App.db.searchBookDao().insert(searchBook) App.db.searchBookDao().insert(searchBook)
} }
}.onError { }.onError {
toast(R.string.error_get_chapter_list) debug { context.getString(R.string.error_get_chapter_list) }
} }
} ?: toast(R.string.error_no_source) } ?: debug { R.string.error_no_source }
} }
fun screen(key: String?) { fun screen(key: String?) {

Loading…
Cancel
Save