kunfei 4 лет назад
Родитель
Сommit
bb9bcc9aa7

+ 1 - 5
app/src/main/java/io/legado/app/data/dao/BookChapterDao.kt

@@ -2,16 +2,12 @@ package io.legado.app.data.dao
 
 import androidx.room.*
 import io.legado.app.data.entities.BookChapter
-import kotlinx.coroutines.flow.Flow
 
 @Dao
 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`")
-    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`")
     fun getChapterList(bookUrl: String): List<BookChapter>

+ 11 - 11
app/src/main/java/io/legado/app/ui/book/toc/ChapterListFragment.kt

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

+ 2 - 1
app/src/main/java/io/legado/app/ui/book/toc/TocActivity.kt

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

+ 2 - 2
app/src/main/java/io/legado/app/ui/book/toc/TocViewModel.kt

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