pull/94/head
kunfei 5 years ago
parent fdca17fc90
commit 4c60238789
  1. 4
      app/src/main/java/io/legado/app/ui/chapterlist/ChapterListActivity.kt
  2. 34
      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,12 +28,10 @@ class ChapterListActivity : VMBaseActivity<ChapterListViewModel>(R.layout.activi
override fun onActivityCreated(savedInstanceState: Bundle?) { override fun onActivityCreated(savedInstanceState: Bundle?) {
tab_layout.isTabIndicatorFullWidth = false tab_layout.isTabIndicatorFullWidth = false
tab_layout.setSelectedTabIndicatorColor(accentColor) tab_layout.setSelectedTabIndicatorColor(accentColor)
viewModel.bookUrl = intent.getStringExtra("bookUrl") viewModel.bookUrl = intent.getStringExtra("bookUrl") ?: ""
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 {
menuInflater.inflate(R.menu.search_view, menu) menuInflater.inflate(R.menu.search_view, menu)

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

@ -2,26 +2,13 @@ package io.legado.app.ui.chapterlist
import android.app.Application import android.app.Application
import io.legado.app.App
import io.legado.app.base.BaseViewModel import io.legado.app.base.BaseViewModel
import io.legado.app.data.entities.Book
class ChapterListViewModel(application: Application) : BaseViewModel(application) { class ChapterListViewModel(application: Application) : BaseViewModel(application) {
var bookUrl: String? = null var bookUrl: String = ""
var book: Book? = null
var chapterCallBack: ChapterListCallBack? = null var chapterCallBack: ChapterListCallBack? = null
var bookMarkCallBack: BookmarkCallBack? = null var bookMarkCallBack: BookmarkCallBack? = null
fun loadBook(success: () -> Unit) {
execute {
bookUrl?.let {
book = App.db.bookDao().getBook(it)
}
}.onSuccess {
success()
}
}
fun startChapterListSearch(newText: String?) { fun startChapterListSearch(newText: String?) {
chapterCallBack?.startChapterListSearch(newText) chapterCallBack?.startChapterListSearch(newText)
} }

Loading…
Cancel
Save