pull/94/head
kunfei 5 years ago
parent fdca17fc90
commit 4c60238789
  1. 8
      app/src/main/java/io/legado/app/ui/chapterlist/ChapterListActivity.kt
  2. 46
      app/src/main/java/io/legado/app/ui/chapterlist/ChapterListFragment.kt
  3. 15
      app/src/main/java/io/legado/app/ui/chapterlist/ChapterListViewModel.kt

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

@ -4,6 +4,7 @@ import android.app.Activity.RESULT_OK
import android.content.Intent
import android.os.Bundle
import android.view.View
import androidx.lifecycle.LiveData
import androidx.lifecycle.Observer
import io.legado.app.App
import io.legado.app.R
@ -15,6 +16,8 @@ import io.legado.app.ui.widget.recycler.UpLinearLayoutManager
import io.legado.app.utils.getVerticalDivider
import io.legado.app.utils.getViewModelOfActivity
import kotlinx.android.synthetic.main.fragment_chapter_list.*
import kotlinx.coroutines.Dispatchers.IO
import kotlinx.coroutines.launch
import org.jetbrains.anko.sdk27.listeners.onClick
class ChapterListFragment : VMBaseFragment<ChapterListViewModel>(R.layout.fragment_chapter_list),
@ -24,15 +27,18 @@ class ChapterListFragment : VMBaseFragment<ChapterListViewModel>(R.layout.fragme
get() = getViewModelOfActivity(ChapterListViewModel::class.java)
lateinit var adapter: ChapterListAdapter
private var book: Book? = null
private var durChapterIndex = 0
private lateinit var mLayoutManager: UpLinearLayoutManager
private var tocLiveData: LiveData<List<BookChapter>>? = null
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
viewModel.chapterCallBack = this
initRecyclerView()
initView()
initData()
initBook()
initDoc()
}
private fun initRecyclerView() {
@ -43,20 +49,26 @@ class ChapterListFragment : VMBaseFragment<ChapterListViewModel>(R.layout.fragme
recycler_view.adapter = adapter
}
private fun initData() {
viewModel.bookUrl?.let { bookUrl ->
App.db.bookChapterDao().observeByBook(bookUrl).observe(viewLifecycleOwner, Observer {
adapter.setItems(it)
if (it.isEmpty()) return@Observer
viewModel.book?.let { book ->
durChapterIndex = book.durChapterIndex
tv_current_chapter_info.text = it[durChapterIndex()].title
mLayoutManager.scrollToPositionWithOffset(durChapterIndex, 0)
}
})
private fun initBook() {
launch(IO) {
book = App.db.bookDao().getBook(viewModel.bookUrl)
}
}
private fun initDoc() {
tocLiveData?.removeObservers(this@ChapterListFragment)
tocLiveData = App.db.bookChapterDao().observeByBook(viewModel.bookUrl)
tocLiveData?.observe(viewLifecycleOwner, Observer {
adapter.setItems(it)
if (it.isEmpty()) return@Observer
book?.let { book ->
durChapterIndex = book.durChapterIndex
tv_current_chapter_info.text = it[durChapterIndex()].title
mLayoutManager.scrollToPositionWithOffset(durChapterIndex, 0)
}
})
}
private fun initView() {
ll_chapter_base_info.setBackgroundColor(backgroundColor)
iv_chapter_top.onClick { mLayoutManager.scrollToPositionWithOffset(0, 0) }
@ -66,7 +78,7 @@ class ChapterListFragment : VMBaseFragment<ChapterListViewModel>(R.layout.fragme
}
}
tv_current_chapter_info.onClick {
viewModel.book?.let {
book?.let {
mLayoutManager.scrollToPositionWithOffset(it.durChapterIndex, 0)
}
}
@ -74,9 +86,11 @@ class ChapterListFragment : VMBaseFragment<ChapterListViewModel>(R.layout.fragme
override fun startChapterListSearch(newText: String?) {
if (newText.isNullOrBlank()) {
initData()
initDoc()
} else {
App.db.bookChapterDao().liveDataSearch(viewModel.bookUrl ?: "", newText).observe(viewLifecycleOwner, Observer {
tocLiveData?.removeObservers(this)
tocLiveData = App.db.bookChapterDao().liveDataSearch(viewModel.bookUrl, newText)
tocLiveData?.observe(viewLifecycleOwner, Observer {
adapter.setItems(it)
mLayoutManager.scrollToPositionWithOffset(0, 0)
})
@ -93,6 +107,6 @@ class ChapterListFragment : VMBaseFragment<ChapterListViewModel>(R.layout.fragme
}
override fun book(): Book? {
return viewModel.book
return book
}
}

@ -2,26 +2,13 @@ package io.legado.app.ui.chapterlist
import android.app.Application
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 bookUrl: String? = null
var book: Book? = null
var bookUrl: String = ""
var chapterCallBack: ChapterListCallBack? = null
var bookMarkCallBack: BookmarkCallBack? = null
fun loadBook(success: () -> Unit) {
execute {
bookUrl?.let {
book = App.db.bookDao().getBook(it)
}
}.onSuccess {
success()
}
}
fun startChapterListSearch(newText: String?) {
chapterCallBack?.startChapterListSearch(newText)
}

Loading…
Cancel
Save