pull/47/head
kunfei 5 years ago
parent 16257b14cb
commit 972c0ec018
  1. 3
      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,10 +27,11 @@ class ChapterListActivity : VMBaseActivity<ChapterListViewModel>(R.layout.activi
override fun onActivityCreated(savedInstanceState: Bundle?) {
setSupportActionBar(toolbar)
viewModel.bookUrl = intent.getStringExtra("bookUrl")
viewModel.loadBook()
viewModel.loadBook {
view_pager.adapter = TabFragmentPageAdapter(supportFragmentManager)
tab_layout.setupWithViewPager(view_pager)
}
}
override fun onCompatCreateOptionsMenu(menu: Menu): Boolean {
menuInflater.inflate(R.menu.search_view, menu)

@ -45,14 +45,13 @@ class ChapterListFragment : VMBaseFragment<ChapterListViewModel>(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<ChapterListViewModel>(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<ChapterListViewModel>(R.layout.fragme
}
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 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<Book>()
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()
}
}
}
Loading…
Cancel
Save