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

+ 2 - 3
app/src/main/java/io/legado/app/data/dao/BookmarkDao.kt

@@ -2,7 +2,6 @@ package io.legado.app.data.dao
 
 import androidx.room.*
 import io.legado.app.data.entities.Bookmark
-import kotlinx.coroutines.flow.Flow
 
 
 @Dao
@@ -16,7 +15,7 @@ interface BookmarkDao {
         where bookName = :bookName and bookAuthor = :bookAuthor 
         order by chapterIndex"""
     )
-    fun flowByBook(bookName: String, bookAuthor: String): Flow<List<Bookmark>>
+    fun getByBook(bookName: String, bookAuthor: String): List<Bookmark>
 
     @Query(
         """SELECT * FROM bookmarks 
@@ -24,7 +23,7 @@ interface BookmarkDao {
         and chapterName like '%'||:key||'%' or content like '%'||:key||'%'
         order by chapterIndex"""
     )
-    fun flowSearch(bookName: String, bookAuthor: String, key: String): Flow<List<Bookmark>>
+    fun search(bookName: String, bookAuthor: String, key: String): List<Bookmark>
 
     @Insert(onConflict = OnConflictStrategy.REPLACE)
     fun insert(vararg bookmark: Bookmark)

+ 9 - 9
app/src/main/java/io/legado/app/ui/book/toc/BookmarkFragment.kt

@@ -16,10 +16,10 @@ import io.legado.app.ui.widget.recycler.VerticalDivider
 import io.legado.app.utils.setEdgeEffectColor
 import io.legado.app.utils.showDialogFragment
 import io.legado.app.utils.viewbindingdelegate.viewBinding
-import kotlinx.coroutines.Job
+import kotlinx.coroutines.Dispatchers.IO
 import kotlinx.coroutines.delay
-import kotlinx.coroutines.flow.conflate
 import kotlinx.coroutines.launch
+import kotlinx.coroutines.withContext
 
 
 class BookmarkFragment : VMBaseFragment<TocViewModel>(R.layout.fragment_bookmark),
@@ -28,7 +28,6 @@ class BookmarkFragment : VMBaseFragment<TocViewModel>(R.layout.fragment_bookmark
     override val viewModel by activityViewModels<TocViewModel>()
     private val binding by viewBinding(FragmentBookmarkBinding::bind)
     private val adapter by lazy { BookmarkAdapter(requireContext(), this) }
-    private var bookmarkFlowJob: Job? = null
 
     override fun onFragmentCreated(view: View, savedInstanceState: Bundle?) {
         viewModel.bookMarkCallBack = this
@@ -47,12 +46,13 @@ class BookmarkFragment : VMBaseFragment<TocViewModel>(R.layout.fragment_bookmark
 
     override fun upBookmark(searchKey: String?) {
         val book = viewModel.bookData.value ?: return
-        bookmarkFlowJob?.cancel()
-        bookmarkFlowJob = launch {
-            when {
-                searchKey.isNullOrBlank() -> appDb.bookmarkDao.flowByBook(book.name, book.author)
-                else -> appDb.bookmarkDao.flowSearch(book.name, book.author, searchKey)
-            }.conflate().collect {
+        launch {
+            withContext(IO) {
+                when {
+                    searchKey.isNullOrBlank() -> appDb.bookmarkDao.getByBook(book.name, book.author)
+                    else -> appDb.bookmarkDao.search(book.name, book.author, searchKey)
+                }
+            }.let {
                 adapter.setItems(it)
                 delay(100)
             }