pull/32/head
kunfei 5 years ago
parent 0061ab721a
commit 471a08a7b1
  1. 9
      app/src/main/java/io/legado/app/ui/readbook/ReadBookActivity.kt
  2. 17
      app/src/main/java/io/legado/app/ui/readbook/ReadBookViewModel.kt

@ -49,6 +49,7 @@ class ReadBookActivity : VMBaseActivity<ReadBookViewModel>(R.layout.activity_rea
initView()
viewModel.callBack = this
viewModel.chapterMaxIndex.observe(this, Observer { bookLoadFinish() })
viewModel.bookData.observe(this, Observer { title_bar.title = it.name })
viewModel.initData(intent)
savedInstanceState?.let {
changeSourceDialog = supportFragmentManager.findFragmentByTag(ChangeSourceDialog.tag) as? ChangeSourceDialog
@ -145,7 +146,7 @@ class ReadBookActivity : VMBaseActivity<ReadBookViewModel>(R.layout.activity_rea
}
override fun openChapterList() {
viewModel.book?.let {
viewModel.bookData.value?.let {
startActivity<ChapterListActivity>(Pair("bookUrl", it.bookUrl))
}
}
@ -182,7 +183,7 @@ class ReadBookActivity : VMBaseActivity<ReadBookViewModel>(R.layout.activity_rea
when (item.itemId) {
R.id.menu_change_source -> {
if (changeSourceDialog == null) {
viewModel.book?.let {
viewModel.bookData.value?.let {
changeSourceDialog = ChangeSourceDialog.newInstance(it.name, it.author)
changeSourceDialog?.callBack = this
}
@ -229,7 +230,7 @@ class ReadBookActivity : VMBaseActivity<ReadBookViewModel>(R.layout.activity_rea
}
private fun bookLoadFinish() {
viewModel.book?.let {
viewModel.bookData.value?.let {
viewModel.loadContent(it, it.durChapterIndex)
}
}
@ -252,7 +253,7 @@ class ReadBookActivity : VMBaseActivity<ReadBookViewModel>(R.layout.activity_rea
}
when (readAloudStatus) {
Status.STOP -> {
viewModel.book?.let {
viewModel.bookData.value?.let {
ReadAloudService.paly(this, it.name, "", "")
}
}

@ -3,6 +3,7 @@ package io.legado.app.ui.readbook
import android.app.Application
import android.content.Intent
import androidx.lifecycle.MediatorLiveData
import androidx.lifecycle.MutableLiveData
import io.legado.app.App
import io.legado.app.base.BaseViewModel
import io.legado.app.data.entities.Book
@ -14,7 +15,7 @@ import kotlinx.coroutines.Dispatchers.IO
class ReadBookViewModel(application: Application) : BaseViewModel(application) {
var book: Book? = null
var bookData = MutableLiveData<Book>()
var bookSource: BookSource? = null
var chapterMaxIndex = MediatorLiveData<Int>()
var webBook: WebBook? = null
@ -24,8 +25,10 @@ class ReadBookViewModel(application: Application) : BaseViewModel(application) {
val bookUrl = intent.getStringExtra("bookUrl")
if (!bookUrl.isNullOrEmpty()) {
execute {
book = App.db.bookDao().getBook(bookUrl)
book?.let { book ->
App.db.bookDao().getBook(bookUrl).let {
bookData.postValue(it)
}
bookData.value?.let { book ->
bookSource = App.db.bookSourceDao().getBookSource(book.origin)
bookSource?.let {
webBook = WebBook(it)
@ -34,10 +37,16 @@ class ReadBookViewModel(application: Application) : BaseViewModel(application) {
if (count == 0) {
webBook?.getChapterList(book)
?.onSuccess(IO) { cList ->
cList?.let {
if (!cList.isNullOrEmpty()) {
App.db.bookChapterDao().insert(*cList.toTypedArray())
chapterMaxIndex.postValue(cList.size)
} else {
}
}?.onError {
} ?: let {
}
} else {
chapterMaxIndex.postValue(count)

Loading…
Cancel
Save