pull/47/head
kunfei 5 years ago
parent 16257b14cb
commit 972c0ec018
  1. 7
      app/src/main/java/io/legado/app/ui/chapterlist/ChapterListActivity.kt
  2. 19
      app/src/main/java/io/legado/app/ui/chapterlist/ChapterListFragment.kt
  3. 12
      app/src/main/java/io/legado/app/ui/chapterlist/ChapterListViewModel.kt

@ -27,9 +27,10 @@ class ChapterListActivity : VMBaseActivity<ChapterListViewModel>(R.layout.activi
override fun onActivityCreated(savedInstanceState: Bundle?) { override fun onActivityCreated(savedInstanceState: Bundle?) {
setSupportActionBar(toolbar) setSupportActionBar(toolbar)
viewModel.bookUrl = intent.getStringExtra("bookUrl") viewModel.bookUrl = intent.getStringExtra("bookUrl")
viewModel.loadBook() viewModel.loadBook {
view_pager.adapter = TabFragmentPageAdapter(supportFragmentManager) view_pager.adapter = TabFragmentPageAdapter(supportFragmentManager)
tab_layout.setupWithViewPager(view_pager) tab_layout.setupWithViewPager(view_pager)
}
} }
override fun onCompatCreateOptionsMenu(menu: Menu): Boolean { override fun onCompatCreateOptionsMenu(menu: Menu): Boolean {

@ -45,14 +45,13 @@ class ChapterListFragment : VMBaseFragment<ChapterListViewModel>(R.layout.fragme
} }
private fun initData() { private fun initData() {
viewModel.bookDate.observe(viewLifecycleOwner, Observer {
loadBookFinish(it)
})
viewModel.bookUrl?.let { bookUrl -> viewModel.bookUrl?.let { bookUrl ->
App.db.bookChapterDao().observeByBook(bookUrl).observe(viewLifecycleOwner, Observer { App.db.bookChapterDao().observeByBook(bookUrl).observe(viewLifecycleOwner, Observer {
adapter.setItems(it) adapter.setItems(it)
viewModel.bookDate.value?.let { book -> viewModel.book?.let {
loadBookFinish(book) durChapterIndex = it.durChapterIndex
tv_current_chapter_info.text = it.durChapterTitle
recycler_view.scrollToPosition(durChapterIndex)
} }
}) })
} }
@ -66,18 +65,12 @@ class ChapterListFragment : VMBaseFragment<ChapterListViewModel>(R.layout.fragme
} }
} }
tv_current_chapter_info.onClick { tv_current_chapter_info.onClick {
viewModel.bookDate.value?.let { viewModel.book?.let {
recycler_view.scrollToPosition(it.durChapterIndex) 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 { override fun durChapterIndex(): Int {
return durChapterIndex return durChapterIndex
} }
@ -88,6 +81,6 @@ class ChapterListFragment : VMBaseFragment<ChapterListViewModel>(R.layout.fragme
} }
override fun book(): Book? { override fun book(): Book? {
return viewModel.bookDate.value return viewModel.book
} }
} }

@ -2,23 +2,21 @@ package io.legado.app.ui.chapterlist
import android.app.Application import android.app.Application
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
class ChapterListViewModel(application: Application) : BaseViewModel(application) { class ChapterListViewModel(application: Application) : BaseViewModel(application) {
var bookDate = MutableLiveData<Book>()
var bookUrl: String? = null var bookUrl: String? = null
var book: Book? = null
fun loadBook() { fun loadBook(success: () -> Unit) {
execute { execute {
bookUrl?.let { bookUrl?.let {
App.db.bookDao().getBook(it)?.let { book -> book = App.db.bookDao().getBook(it)
bookDate.postValue(book)
}
} }
}.onSuccess {
success()
} }
} }
} }
Loading…
Cancel
Save