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

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

Loading…
Cancel
Save