diff --git a/app/src/main/java/io/legado/app/ui/readbook/ReadBookActivity.kt b/app/src/main/java/io/legado/app/ui/readbook/ReadBookActivity.kt index 1d70e294a..ab6e1b036 100644 --- a/app/src/main/java/io/legado/app/ui/readbook/ReadBookActivity.kt +++ b/app/src/main/java/io/legado/app/ui/readbook/ReadBookActivity.kt @@ -49,6 +49,7 @@ class ReadBookActivity : VMBaseActivity(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(R.layout.activity_rea } override fun openChapterList() { - viewModel.book?.let { + viewModel.bookData.value?.let { startActivity(Pair("bookUrl", it.bookUrl)) } } @@ -182,7 +183,7 @@ class ReadBookActivity : VMBaseActivity(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(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(R.layout.activity_rea } when (readAloudStatus) { Status.STOP -> { - viewModel.book?.let { + viewModel.bookData.value?.let { ReadAloudService.paly(this, it.name, "", "") } } diff --git a/app/src/main/java/io/legado/app/ui/readbook/ReadBookViewModel.kt b/app/src/main/java/io/legado/app/ui/readbook/ReadBookViewModel.kt index 4bc182165..c345cccbc 100644 --- a/app/src/main/java/io/legado/app/ui/readbook/ReadBookViewModel.kt +++ b/app/src/main/java/io/legado/app/ui/readbook/ReadBookViewModel.kt @@ -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() var bookSource: BookSource? = null var chapterMaxIndex = MediatorLiveData() 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,11 +37,17 @@ 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) }