From 1ac74ad5c068dbde5240ca1da70b890ec1eb50f7 Mon Sep 17 00:00:00 2001 From: kunfei Date: Thu, 16 Mar 2023 19:46:56 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/io/legado/app/data/dao/BookmarkDao.kt | 15 +++++++++++++++ .../io/legado/app/ui/book/toc/BookmarkFragment.kt | 11 ++++------- 2 files changed, 19 insertions(+), 7 deletions(-) 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 9d22cce20..7940139ff 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 @@ -18,6 +18,21 @@ interface BookmarkDao { @Query("select * from bookmarks order by bookName collate localized, bookAuthor collate localized, chapterIndex, chapterPos") fun flowAll(): Flow> + @Query( + """select * from bookmarks + where bookName = :bookName and bookAuthor = :bookAuthor + order by chapterIndex""" + ) + fun flowByBook(bookName: String, bookAuthor: String): Flow> + + @Query( + """SELECT * FROM bookmarks + where bookName = :bookName and bookAuthor = :bookAuthor + and chapterName like '%'||:key||'%' or content like '%'||:key||'%' + order by chapterIndex""" + ) + fun flowSearch(bookName: String, bookAuthor: String, key: String): Flow> + @Query( """select * from bookmarks where bookName = :bookName and bookAuthor = :bookAuthor 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 59f743174..2a7b6e5ae 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 @@ -18,7 +18,6 @@ import io.legado.app.utils.setEdgeEffectColor import io.legado.app.utils.showDialogFragment import io.legado.app.utils.viewbindingdelegate.viewBinding import kotlinx.coroutines.Dispatchers -import kotlinx.coroutines.Dispatchers.IO import kotlinx.coroutines.launch import kotlinx.coroutines.withContext @@ -51,12 +50,10 @@ class BookmarkFragment : VMBaseFragment(R.layout.fragment_bookmark override fun upBookmark(searchKey: String?) { val book = viewModel.bookData.value ?: return launch { - withContext(IO) { - when { - searchKey.isNullOrBlank() -> appDb.bookmarkDao.getByBook(book.name, book.author) - else -> appDb.bookmarkDao.search(book.name, book.author, searchKey) - } - }.let { + when { + searchKey.isNullOrBlank() -> appDb.bookmarkDao.flowByBook(book.name, book.author) + else -> appDb.bookmarkDao.flowSearch(book.name, book.author, searchKey) + }.collect { adapter.setItems(it) var scrollPos = 0 withContext(Dispatchers.Default) {