diff --git a/app/src/main/java/io/legado/app/ui/chapterlist/ChapterListActivity.kt b/app/src/main/java/io/legado/app/ui/chapterlist/ChapterListActivity.kt index ef5be2761..514c2dc68 100644 --- a/app/src/main/java/io/legado/app/ui/chapterlist/ChapterListActivity.kt +++ b/app/src/main/java/io/legado/app/ui/chapterlist/ChapterListActivity.kt @@ -27,9 +27,10 @@ class ChapterListActivity : VMBaseActivity(R.layout.activi override fun onActivityCreated(savedInstanceState: Bundle?) { setSupportActionBar(toolbar) viewModel.bookUrl = intent.getStringExtra("bookUrl") - viewModel.loadBook() - view_pager.adapter = TabFragmentPageAdapter(supportFragmentManager) - tab_layout.setupWithViewPager(view_pager) + viewModel.loadBook { + view_pager.adapter = TabFragmentPageAdapter(supportFragmentManager) + tab_layout.setupWithViewPager(view_pager) + } } override fun onCompatCreateOptionsMenu(menu: Menu): Boolean { diff --git a/app/src/main/java/io/legado/app/ui/chapterlist/ChapterListFragment.kt b/app/src/main/java/io/legado/app/ui/chapterlist/ChapterListFragment.kt index 97092ab13..18449147e 100644 --- a/app/src/main/java/io/legado/app/ui/chapterlist/ChapterListFragment.kt +++ b/app/src/main/java/io/legado/app/ui/chapterlist/ChapterListFragment.kt @@ -45,14 +45,13 @@ class ChapterListFragment : VMBaseFragment(R.layout.fragme } private fun initData() { - viewModel.bookDate.observe(viewLifecycleOwner, Observer { - loadBookFinish(it) - }) viewModel.bookUrl?.let { bookUrl -> App.db.bookChapterDao().observeByBook(bookUrl).observe(viewLifecycleOwner, Observer { adapter.setItems(it) - viewModel.bookDate.value?.let { book -> - loadBookFinish(book) + viewModel.book?.let { + durChapterIndex = it.durChapterIndex + tv_current_chapter_info.text = it.durChapterTitle + recycler_view.scrollToPosition(durChapterIndex) } }) } @@ -66,18 +65,12 @@ class ChapterListFragment : VMBaseFragment(R.layout.fragme } } tv_current_chapter_info.onClick { - viewModel.bookDate.value?.let { + viewModel.book?.let { recycler_view.scrollToPosition(it.durChapterIndex) } } } - private fun loadBookFinish(book: Book) { - durChapterIndex = book.durChapterIndex - tv_current_chapter_info.text = book.durChapterTitle - recycler_view.scrollToPosition(durChapterIndex) - } - override fun durChapterIndex(): Int { return durChapterIndex } @@ -88,6 +81,6 @@ class ChapterListFragment : VMBaseFragment(R.layout.fragme } override fun book(): Book? { - return viewModel.bookDate.value + return viewModel.book } } \ No newline at end of file diff --git a/app/src/main/java/io/legado/app/ui/chapterlist/ChapterListViewModel.kt b/app/src/main/java/io/legado/app/ui/chapterlist/ChapterListViewModel.kt index 11d1b38e9..d11aa73dc 100644 --- a/app/src/main/java/io/legado/app/ui/chapterlist/ChapterListViewModel.kt +++ b/app/src/main/java/io/legado/app/ui/chapterlist/ChapterListViewModel.kt @@ -2,23 +2,21 @@ package io.legado.app.ui.chapterlist import android.app.Application -import androidx.lifecycle.MutableLiveData import io.legado.app.App import io.legado.app.base.BaseViewModel import io.legado.app.data.entities.Book class ChapterListViewModel(application: Application) : BaseViewModel(application) { - - var bookDate = MutableLiveData() var bookUrl: String? = null + var book: Book? = null - fun loadBook() { + fun loadBook(success: () -> Unit) { execute { bookUrl?.let { - App.db.bookDao().getBook(it)?.let { book -> - bookDate.postValue(book) - } + book = App.db.bookDao().getBook(it) } + }.onSuccess { + success() } } } \ No newline at end of file