diff --git a/app/src/main/java/io/legado/app/data/dao/BookmarkDao.kt b/app/src/main/java/io/legado/app/data/dao/BookmarkDao.kt index 830e83d6c..4fd2e82e5 100644 --- a/app/src/main/java/io/legado/app/data/dao/BookmarkDao.kt +++ b/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> + fun getByBook(bookName: String, bookAuthor: String): List @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> + fun search(bookName: String, bookAuthor: String, key: String): List @Insert(onConflict = OnConflictStrategy.REPLACE) fun insert(vararg bookmark: Bookmark) diff --git a/app/src/main/java/io/legado/app/ui/book/toc/BookmarkFragment.kt b/app/src/main/java/io/legado/app/ui/book/toc/BookmarkFragment.kt index 11dc587f0..e4e7f6e83 100644 --- a/app/src/main/java/io/legado/app/ui/book/toc/BookmarkFragment.kt +++ b/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(R.layout.fragment_bookmark), @@ -28,7 +28,6 @@ class BookmarkFragment : VMBaseFragment(R.layout.fragment_bookmark override val viewModel by activityViewModels() 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(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) }