diff --git a/app/src/main/java/io/legado/app/ui/book/toc/BookmarkFragment.kt b/app/src/main/java/io/legado/app/ui/book/toc/BookmarkFragment.kt index 4c4188a3f..73235d31a 100644 --- a/app/src/main/java/io/legado/app/ui/book/toc/BookmarkFragment.kt +++ b/app/src/main/java/io/legado/app/ui/book/toc/BookmarkFragment.kt @@ -13,6 +13,7 @@ import androidx.recyclerview.widget.LinearLayoutManager import io.legado.app.R import io.legado.app.base.VMBaseFragment import io.legado.app.data.appDb +import io.legado.app.data.entities.Book import io.legado.app.data.entities.Bookmark import io.legado.app.databinding.DialogBookmarkBinding import io.legado.app.databinding.FragmentBookmarkBinding @@ -35,7 +36,9 @@ class BookmarkFragment : VMBaseFragment(R.layout.fragment_ override fun onFragmentCreated(view: View, savedInstanceState: Bundle?) { viewModel.bookMarkCallBack = this initRecyclerView() - initData() + viewModel.bookData.observe(this) { + initData(it) + } } private fun initRecyclerView() { @@ -46,20 +49,20 @@ class BookmarkFragment : VMBaseFragment(R.layout.fragment_ binding.recyclerView.adapter = adapter } - private fun initData() { - viewModel.book?.let { book -> - bookmarkLiveData?.removeObservers(viewLifecycleOwner) - bookmarkLiveData = - LivePagedListBuilder( - appDb.bookmarkDao.observeByBook(book.bookUrl, book.name, book.author), 20 - ).build() - bookmarkLiveData?.observe(viewLifecycleOwner, { adapter.submitList(it) }) - } + private fun initData(book: Book) { + bookmarkLiveData?.removeObservers(viewLifecycleOwner) + bookmarkLiveData = + LivePagedListBuilder( + appDb.bookmarkDao.observeByBook(book.bookUrl, book.name, book.author), 20 + ).build() + bookmarkLiveData?.observe(viewLifecycleOwner, { adapter.submitList(it) }) } override fun startBookmarkSearch(newText: String?) { if (newText.isNullOrBlank()) { - initData() + viewModel.bookData.value?.let { + initData(it) + } } else { bookmarkLiveData?.removeObservers(viewLifecycleOwner) bookmarkLiveData = LivePagedListBuilder( diff --git a/app/src/main/java/io/legado/app/ui/book/toc/ChapterListActivity.kt b/app/src/main/java/io/legado/app/ui/book/toc/ChapterListActivity.kt index 94077fba5..4f429447a 100644 --- a/app/src/main/java/io/legado/app/ui/book/toc/ChapterListActivity.kt +++ b/app/src/main/java/io/legado/app/ui/book/toc/ChapterListActivity.kt @@ -34,16 +34,15 @@ class ChapterListActivity : VMBaseActivity - when (position) { - 0 -> tab.setText(R.string.chapter_list) - else -> tab.setText(R.string.bookmark) - } - } + binding.viewPager.adapter = TabFragmentPageAdapter() + TabLayoutMediator(tabLayout, binding.viewPager) { tab, position -> + when (position) { + 0 -> tab.setText(R.string.chapter_list) + else -> tab.setText(R.string.bookmark) } + }.attach() + intent.getStringExtra("bookUrl")?.let { + viewModel.initBook(it) } } diff --git a/app/src/main/java/io/legado/app/ui/book/toc/ChapterListFragment.kt b/app/src/main/java/io/legado/app/ui/book/toc/ChapterListFragment.kt index 72bca9746..57ccc8305 100644 --- a/app/src/main/java/io/legado/app/ui/book/toc/ChapterListFragment.kt +++ b/app/src/main/java/io/legado/app/ui/book/toc/ChapterListFragment.kt @@ -49,7 +49,9 @@ class ChapterListFragment : VMBaseFragment(R.layout.fragme ivChapterBottom.setColorFilter(btc) initRecyclerView() initView() - initBook() + viewModel.bookData.observe(this@ChapterListFragment) { + initBook(it) + } } private fun initRecyclerView() { @@ -73,15 +75,13 @@ class ChapterListFragment : VMBaseFragment(R.layout.fragme } @SuppressLint("SetTextI18n") - private fun initBook() { + private fun initBook(book: Book) { launch { initDoc() - viewModel.book?.let { - durChapterIndex = it.durChapterIndex - binding.tvCurrentChapterInfo.text = - "${it.durChapterTitle}(${it.durChapterIndex + 1}/${it.totalChapterNum})" - initCacheFileNames(it) - } + durChapterIndex = book.durChapterIndex + binding.tvCurrentChapterInfo.text = + "${book.durChapterTitle}(${book.durChapterIndex + 1}/${book.totalChapterNum})" + initCacheFileNames(book) } } @@ -108,7 +108,7 @@ class ChapterListFragment : VMBaseFragment(R.layout.fragme override fun observeLiveBus() { observeEvent(EventBus.SAVE_CONTENT) { chapter -> - viewModel.book?.bookUrl?.let { bookUrl -> + viewModel.bookData.value?.bookUrl?.let { bookUrl -> if (chapter.bookUrl == bookUrl) { adapter.cacheFileNames.add(chapter.getFileName()) adapter.notifyItemChanged(chapter.index, true) @@ -130,7 +130,7 @@ class ChapterListFragment : VMBaseFragment(R.layout.fragme } override val isLocalBook: Boolean - get() = viewModel.book?.isLocalBook() == true + get() = viewModel.bookData.value?.isLocalBook() == true override fun durChapterIndex(): Int { return min(durChapterIndex, adapter.itemCount) diff --git a/app/src/main/java/io/legado/app/ui/book/toc/ChapterListViewModel.kt b/app/src/main/java/io/legado/app/ui/book/toc/ChapterListViewModel.kt index 27f6011ae..f85ff5749 100644 --- a/app/src/main/java/io/legado/app/ui/book/toc/ChapterListViewModel.kt +++ b/app/src/main/java/io/legado/app/ui/book/toc/ChapterListViewModel.kt @@ -2,22 +2,23 @@ package io.legado.app.ui.book.toc import android.app.Application +import androidx.lifecycle.MutableLiveData import io.legado.app.base.BaseViewModel import io.legado.app.data.appDb import io.legado.app.data.entities.Book class ChapterListViewModel(application: Application) : BaseViewModel(application) { var bookUrl: String = "" - var book: Book? = null + var bookData = MutableLiveData() var chapterCallBack: ChapterListCallBack? = null var bookMarkCallBack: BookmarkCallBack? = null - fun initBook(bookUrl: String, success: () -> Unit) { + fun initBook(bookUrl: String) { this.bookUrl = bookUrl execute { - book = appDb.bookDao.getBook(bookUrl) - }.onSuccess { - success.invoke() + appDb.bookDao.getBook(bookUrl)?.let { + bookData.postValue(it) + } } } diff --git a/app/src/main/java/io/legado/app/ui/rss/article/RssSortActivity.kt b/app/src/main/java/io/legado/app/ui/rss/article/RssSortActivity.kt index f8356ae0c..3ebe5e74e 100644 --- a/app/src/main/java/io/legado/app/ui/rss/article/RssSortActivity.kt +++ b/app/src/main/java/io/legado/app/ui/rss/article/RssSortActivity.kt @@ -41,7 +41,7 @@ class RssSortActivity : VMBaseActivity tab.text = fragments.keys.elementAt(position) - } + }.attach() viewModel.titleLiveData.observe(this, { binding.titleBar.title = it })