diff --git a/app/src/main/java/io/legado/app/ui/book/read/ReadBookActivity.kt b/app/src/main/java/io/legado/app/ui/book/read/ReadBookActivity.kt index fdb8d1808..69e252234 100644 --- a/app/src/main/java/io/legado/app/ui/book/read/ReadBookActivity.kt +++ b/app/src/main/java/io/legado/app/ui/book/read/ReadBookActivity.kt @@ -74,7 +74,7 @@ class ReadBookActivity : VMBaseActivity(R.layout.activity_boo setSupportActionBar(toolbar) initView() viewModel.callBack = this - viewModel.bookData.observe(this, Observer { title_bar.title = it.name }) + viewModel.titleDate.observe(this, Observer { title_bar.title = it }) viewModel.initData(intent) } @@ -141,12 +141,12 @@ class ReadBookActivity : VMBaseActivity(R.layout.activity_boo when (item.itemId) { R.id.menu_change_source -> { read_menu.runMenuOut() - viewModel.bookData.value?.let { + viewModel.book?.let { ChangeSourceDialog.show(supportFragmentManager, it.name, it.author) } } R.id.menu_refresh -> { - viewModel.bookData.value?.let { + viewModel.book?.let { viewModel.curTextChapter = null page_view.upContent() viewModel.refreshContent(it) @@ -255,7 +255,7 @@ class ReadBookActivity : VMBaseActivity(R.layout.activity_boo * 加载章节内容 */ override fun loadContent() { - viewModel.bookData.value?.let { + viewModel.book?.let { viewModel.loadContent(it, viewModel.durChapterIndex) viewModel.loadContent(it, viewModel.durChapterIndex + 1) viewModel.loadContent(it, viewModel.durChapterIndex - 1) @@ -266,7 +266,7 @@ class ReadBookActivity : VMBaseActivity(R.layout.activity_boo * 加载章节内容, index章节序号 */ override fun loadContent(index: Int) { - viewModel.bookData.value?.let { + viewModel.book?.let { viewModel.loadContent(it, index) } } @@ -337,10 +337,10 @@ class ReadBookActivity : VMBaseActivity(R.layout.activity_boo } override val curOrigin: String? - get() = viewModel.bookData.value?.origin + get() = viewModel.book?.origin override val oldBook: Book? - get() = viewModel.bookData.value + get() = viewModel.book override fun changeTo(book: Book) { viewModel.changeTo(book) @@ -436,7 +436,7 @@ class ReadBookActivity : VMBaseActivity(R.layout.activity_boo } override fun openChapterList() { - viewModel.bookData.value?.let { + viewModel.book?.let { startActivityForResult( requestCodeChapterList, Pair("bookUrl", it.bookUrl) @@ -475,7 +475,7 @@ class ReadBookActivity : VMBaseActivity(R.layout.activity_boo * 朗读 */ private fun readAloud(play: Boolean = true) { - val book = viewModel.bookData.value + val book = viewModel.book val textChapter = viewModel.curTextChapter if (book != null && textChapter != null) { val key = IntentDataHelp.putData(textChapter) @@ -523,7 +523,7 @@ class ReadBookActivity : VMBaseActivity(R.layout.activity_boo } override fun finish() { - viewModel.bookData.value?.let { + viewModel.book?.let { if (!viewModel.inBookshelf) { this.alert(title = getString(R.string.add_to_shelf)) { message = getString(R.string.check_add_bookshelf, it.name) diff --git a/app/src/main/java/io/legado/app/ui/book/read/ReadBookViewModel.kt b/app/src/main/java/io/legado/app/ui/book/read/ReadBookViewModel.kt index 5880bec5c..4144bee78 100644 --- a/app/src/main/java/io/legado/app/ui/book/read/ReadBookViewModel.kt +++ b/app/src/main/java/io/legado/app/ui/book/read/ReadBookViewModel.kt @@ -21,8 +21,9 @@ import kotlinx.coroutines.launch import kotlinx.coroutines.withContext class ReadBookViewModel(application: Application) : BaseViewModel(application) { + var titleDate = MutableLiveData() + var book: Book? = null var inBookshelf = false - var bookData = MutableLiveData() var chapterSize = 0 var callBack: CallBack? = null var durChapterIndex = 0 @@ -38,22 +39,23 @@ class ReadBookViewModel(application: Application) : BaseViewModel(application) { execute { inBookshelf = intent.getBooleanExtra("inBookshelf", true) IntentDataHelp.getData(intent.getStringExtra("key"))?.let { - setBook(it) + initBook(it) } ?: intent.getStringExtra("bookUrl")?.let { App.db.bookDao().getBook(it)?.let { book -> - setBook(book) + initBook(book) } } ?: App.db.bookDao().lastReadBook?.let { - setBook(it) + initBook(it) } } } - private fun setBook(book: Book) { + private fun initBook(book: Book) { durChapterIndex = book.durChapterIndex durPageIndex = book.durChapterPos isLocalBook = book.origin == BookType.local - bookData.postValue(book) + this.book = book + titleDate.postValue(book.name) App.db.bookSourceDao().getBookSource(book.origin)?.let { webBook = WebBook(it) } @@ -72,7 +74,7 @@ class ReadBookViewModel(application: Application) : BaseViewModel(application) { callBack?.loadContent() } if (inBookshelf) { - saveRead(book) + saveRead() } } @@ -117,7 +119,7 @@ class ReadBookViewModel(application: Application) : BaseViewModel(application) { prevTextChapter = curTextChapter curTextChapter = nextTextChapter nextTextChapter = null - bookData.value?.let { + book?.let { if (curTextChapter == null) { loadContent(it, durChapterIndex) } else if (upContent) { @@ -127,7 +129,7 @@ class ReadBookViewModel(application: Application) : BaseViewModel(application) { launch(IO) { for (i in 2..10) { delay(100) - bookData.value?.let { book -> + book?.let { book -> download(book, durChapterIndex + i) } } @@ -140,7 +142,7 @@ class ReadBookViewModel(application: Application) : BaseViewModel(application) { nextTextChapter = curTextChapter curTextChapter = prevTextChapter prevTextChapter = null - bookData.value?.let { + book?.let { if (curTextChapter == null) { loadContent(it, durChapterIndex) } else if (upContent) { @@ -150,7 +152,7 @@ class ReadBookViewModel(application: Application) : BaseViewModel(application) { launch(IO) { for (i in -5..-2) { delay(100) - bookData.value?.let { book -> + book?.let { book -> download(book, durChapterIndex + i) } } @@ -225,19 +227,19 @@ class ReadBookViewModel(application: Application) : BaseViewModel(application) { if (chapter.index in durChapterIndex - 1..durChapterIndex + 1) { val c = BookHelp.disposeContent( chapter.title, - bookData.value!!.name, + book!!.name, webBook?.bookSource?.bookSourceUrl, content, - bookData.value!!.useReplaceRule + book!!.useReplaceRule ) callBack?.contentLoadFinish(chapter, c) } } } - fun changeTo(book: Book) { + fun changeTo(book1: Book) { execute { - bookData.value?.let { + book?.let { App.db.bookDao().delete(it.bookUrl) } prevTextChapter = null @@ -246,15 +248,15 @@ class ReadBookViewModel(application: Application) : BaseViewModel(application) { withContext(Main) { callBack?.upContent() } - App.db.bookDao().insert(book) - bookData.postValue(book) - App.db.bookSourceDao().getBookSource(book.origin)?.let { + App.db.bookDao().insert(book1) + book = book1 + App.db.bookSourceDao().getBookSource(book1.origin)?.let { webBook = WebBook(it) } - if (book.tocUrl.isEmpty()) { - loadBookInfo(book) { upChangeDurChapterIndex(book, it) } + if (book1.tocUrl.isEmpty()) { + loadBookInfo(book1) { upChangeDurChapterIndex(book1, it) } } else { - loadChapterList(book) { upChangeDurChapterIndex(book, it) } + loadChapterList(book1) { upChangeDurChapterIndex(book1, it) } } } } @@ -291,7 +293,7 @@ class ReadBookViewModel(application: Application) : BaseViewModel(application) { callBack?.loadContent() } - fun saveRead(book: Book? = bookData.value) { + fun saveRead() { execute { book?.let { book -> book.lastCheckCount = 0 @@ -308,7 +310,7 @@ class ReadBookViewModel(application: Application) : BaseViewModel(application) { fun removeFromBookshelf(success: (() -> Unit)?) { execute { - bookData.value?.let { + book?.let { App.db.bookDao().delete(it.bookUrl) } }.onSuccess { @@ -318,7 +320,7 @@ class ReadBookViewModel(application: Application) : BaseViewModel(application) { fun upBookSource() { execute { - bookData.value?.let { book -> + book?.let { book -> App.db.bookSourceDao().getBookSource(book.origin)?.let { webBook = WebBook(it) }