优化目录界面

pull/1693/head
kunfei 3 years ago
parent 7e97e60b58
commit 80716af733
  1. 6
      app/src/main/java/io/legado/app/data/dao/BookChapterDao.kt
  2. 20
      app/src/main/java/io/legado/app/ui/book/toc/ChapterListFragment.kt
  3. 3
      app/src/main/java/io/legado/app/ui/book/toc/TocActivity.kt
  4. 4
      app/src/main/java/io/legado/app/ui/book/toc/TocViewModel.kt

@ -2,16 +2,12 @@ package io.legado.app.data.dao
import androidx.room.* import androidx.room.*
import io.legado.app.data.entities.BookChapter import io.legado.app.data.entities.BookChapter
import kotlinx.coroutines.flow.Flow
@Dao @Dao
interface BookChapterDao { interface BookChapterDao {
@Query("select * from chapters where bookUrl = :bookUrl order by `index`")
fun flowByBook(bookUrl: String): Flow<List<BookChapter>>
@Query("SELECT * FROM chapters where bookUrl = :bookUrl and title like '%'||:key||'%' order by `index`") @Query("SELECT * FROM chapters where bookUrl = :bookUrl and title like '%'||:key||'%' order by `index`")
fun flowSearch(bookUrl: String, key: String): Flow<List<BookChapter>> fun search(bookUrl: String, key: String): List<BookChapter>
@Query("select * from chapters where bookUrl = :bookUrl order by `index`") @Query("select * from chapters where bookUrl = :bookUrl order by `index`")
fun getChapterList(bookUrl: String): List<BookChapter> fun getChapterList(bookUrl: String): List<BookChapter>

@ -21,10 +21,11 @@ import io.legado.app.ui.widget.recycler.VerticalDivider
import io.legado.app.utils.ColorUtils import io.legado.app.utils.ColorUtils
import io.legado.app.utils.observeEvent import io.legado.app.utils.observeEvent
import io.legado.app.utils.viewbindingdelegate.viewBinding import io.legado.app.utils.viewbindingdelegate.viewBinding
import kotlinx.coroutines.* import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers.IO import kotlinx.coroutines.Dispatchers.IO
import kotlinx.coroutines.Dispatchers.Main import kotlinx.coroutines.Dispatchers.Main
import kotlinx.coroutines.flow.conflate import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
class ChapterListFragment : VMBaseFragment<TocViewModel>(R.layout.fragment_chapter_list), class ChapterListFragment : VMBaseFragment<TocViewModel>(R.layout.fragment_chapter_list),
ChapterListAdapter.Callback, ChapterListAdapter.Callback,
@ -34,10 +35,9 @@ class ChapterListFragment : VMBaseFragment<TocViewModel>(R.layout.fragment_chapt
private val mLayoutManager by lazy { UpLinearLayoutManager(requireContext()) } private val mLayoutManager by lazy { UpLinearLayoutManager(requireContext()) }
private val adapter by lazy { ChapterListAdapter(requireContext(), this) } private val adapter by lazy { ChapterListAdapter(requireContext(), this) }
private var durChapterIndex = 0 private var durChapterIndex = 0
private var tocFlowJob: Job? = null
override fun onFragmentCreated(view: View, savedInstanceState: Bundle?) = binding.run { override fun onFragmentCreated(view: View, savedInstanceState: Bundle?) = binding.run {
viewModel.chapterCallBack = this@ChapterListFragment viewModel.chapterListCallBack = this@ChapterListFragment
val bbg = bottomBackground val bbg = bottomBackground
val btc = requireContext().getPrimaryTextColor(ColorUtils.isColorLight(bbg)) val btc = requireContext().getPrimaryTextColor(ColorUtils.isColorLight(bbg))
llChapterBaseInfo.setBackgroundColor(bbg) llChapterBaseInfo.setBackgroundColor(bbg)
@ -103,17 +103,17 @@ class ChapterListFragment : VMBaseFragment<TocViewModel>(R.layout.fragment_chapt
} }
override fun upChapterList(searchKey: String?) { override fun upChapterList(searchKey: String?) {
tocFlowJob?.cancel() launch {
tocFlowJob = launch { withContext(IO) {
when { when {
searchKey.isNullOrBlank() -> appDb.bookChapterDao.flowByBook(viewModel.bookUrl) searchKey.isNullOrBlank() -> appDb.bookChapterDao.getChapterList(viewModel.bookUrl)
else -> appDb.bookChapterDao.flowSearch(viewModel.bookUrl, searchKey) else -> appDb.bookChapterDao.search(viewModel.bookUrl, searchKey)
}.conflate().collect { }
}.let {
adapter.setItems(it) adapter.setItems(it)
if (searchKey.isNullOrBlank() && mLayoutManager.findFirstVisibleItemPosition() < 0) { if (searchKey.isNullOrBlank() && mLayoutManager.findFirstVisibleItemPosition() < 0) {
mLayoutManager.scrollToPositionWithOffset(durChapterIndex, 0) mLayoutManager.scrollToPositionWithOffset(durChapterIndex, 0)
} }
delay(100)
} }
} }
} }

@ -83,6 +83,7 @@ class TocActivity : VMBaseActivity<ActivityChapterListBinding, TocViewModel>() {
override fun onCompatOptionsItemSelected(item: MenuItem): Boolean { override fun onCompatOptionsItemSelected(item: MenuItem): Boolean {
when (item.itemId) { when (item.itemId) {
R.id.menu_reverse_toc -> viewModel.reverseToc { R.id.menu_reverse_toc -> viewModel.reverseToc {
viewModel.chapterListCallBack?.upChapterList(searchView?.query?.toString())
setResult(RESULT_OK, Intent().apply { setResult(RESULT_OK, Intent().apply {
putExtra("index", it.durChapterIndex) putExtra("index", it.durChapterIndex)
putExtra("chapterPos", 0) putExtra("chapterPos", 0)
@ -90,7 +91,7 @@ class TocActivity : VMBaseActivity<ActivityChapterListBinding, TocViewModel>() {
} }
R.id.menu_use_replace -> { R.id.menu_use_replace -> {
AppConfig.tocUiUseReplace = !item.isChecked AppConfig.tocUiUseReplace = !item.isChecked
viewModel.chapterCallBack?.clearDisplayTitle() viewModel.chapterListCallBack?.clearDisplayTitle()
} }
R.id.menu_log -> showDialogFragment<AppLogDialog>() R.id.menu_log -> showDialogFragment<AppLogDialog>()
} }

@ -10,7 +10,7 @@ import io.legado.app.data.entities.Book
class TocViewModel(application: Application) : BaseViewModel(application) { class TocViewModel(application: Application) : BaseViewModel(application) {
var bookUrl: String = "" var bookUrl: String = ""
var bookData = MutableLiveData<Book>() var bookData = MutableLiveData<Book>()
var chapterCallBack: ChapterListCallBack? = null var chapterListCallBack: ChapterListCallBack? = null
var bookMarkCallBack: BookmarkCallBack? = null var bookMarkCallBack: BookmarkCallBack? = null
fun initBook(bookUrl: String) { fun initBook(bookUrl: String) {
@ -39,7 +39,7 @@ class TocViewModel(application: Application) : BaseViewModel(application) {
} }
fun startChapterListSearch(newText: String?) { fun startChapterListSearch(newText: String?) {
chapterCallBack?.upChapterList(newText) chapterListCallBack?.upChapterList(newText)
} }
fun startBookmarkSearch(newText: String?) { fun startBookmarkSearch(newText: String?) {

Loading…
Cancel
Save