From 34f6f482e0ccf55f2318d291a42630445eb13447 Mon Sep 17 00:00:00 2001 From: kunfei Date: Sun, 3 Apr 2022 18:48:44 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E6=89=B9=E9=87=8F=E6=8D=A2?= =?UTF-8?q?=E6=BA=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../io/legado/app/data/dao/BookSourceDao.kt | 25 +++++++++++++------ .../app/ui/book/arrange/SourcePickerDialog.kt | 23 +++++++++++++++-- .../book/source/manage/BookSourceActivity.kt | 4 +-- .../app/ui/main/explore/ExploreFragment.kt | 4 +-- 4 files changed, 43 insertions(+), 13 deletions(-) diff --git a/app/src/main/java/io/legado/app/data/dao/BookSourceDao.kt b/app/src/main/java/io/legado/app/data/dao/BookSourceDao.kt index f721dca47..1399eee93 100644 --- a/app/src/main/java/io/legado/app/data/dao/BookSourceDao.kt +++ b/app/src/main/java/io/legado/app/data/dao/BookSourceDao.kt @@ -12,15 +12,26 @@ interface BookSourceDao { @Query( """select * from book_sources - where bookSourceName like :searchKey - or bookSourceGroup like :searchKey - or bookSourceUrl like :searchKey - or bookSourceComment like :searchKey + where bookSourceName like '%' || :searchKey || '%' + or bookSourceGroup like '%' || :searchKey || '%' + or bookSourceUrl like '%' || :searchKey || '%' + or bookSourceComment like '%' || :searchKey || '%' order by customOrder asc""" ) fun flowSearch(searchKey: String): Flow> - @Query("select * from book_sources where bookSourceGroup like :searchKey order by customOrder asc") + @Query( + """select * from book_sources + where enabled = 1 and + (bookSourceName like '%' || :searchKey || '%' + or bookSourceGroup like '%' || :searchKey || '%' + or bookSourceUrl like '%' || :searchKey || '%' + or bookSourceComment like '%' || :searchKey || '%') + order by customOrder asc""" + ) + fun flowSearchEnabled(searchKey: String): Flow> + + @Query("select * from book_sources where bookSourceGroup like '%' || :searchKey || '%' order by customOrder asc") fun flowGroupSearch(searchKey: String): Flow> @Query("select * from book_sources where enabled = 1 order by customOrder asc") @@ -39,7 +50,7 @@ interface BookSourceDao { """select * from book_sources where enabledExplore = 1 and trim(exploreUrl) <> '' - and (bookSourceGroup like :key or bookSourceName like :key) + and (bookSourceGroup like '%' || :key || '%' or bookSourceName like '%' || :key || '%') order by customOrder asc""" ) fun flowExplore(key: String): Flow> @@ -48,7 +59,7 @@ interface BookSourceDao { """select * from book_sources where enabledExplore = 1 and trim(exploreUrl) <> '' - and (bookSourceGroup like :key) + and (bookSourceGroup like '%' || :key || '%') order by customOrder asc""" ) fun flowGroupExplore(key: String): Flow> diff --git a/app/src/main/java/io/legado/app/ui/book/arrange/SourcePickerDialog.kt b/app/src/main/java/io/legado/app/ui/book/arrange/SourcePickerDialog.kt index 56c8cccc8..ffe384fbd 100644 --- a/app/src/main/java/io/legado/app/ui/book/arrange/SourcePickerDialog.kt +++ b/app/src/main/java/io/legado/app/ui/book/arrange/SourcePickerDialog.kt @@ -10,14 +10,18 @@ import io.legado.app.R import io.legado.app.base.BaseDialogFragment import io.legado.app.base.adapter.ItemViewHolder import io.legado.app.base.adapter.RecyclerAdapter +import io.legado.app.data.appDb import io.legado.app.data.entities.BookSource import io.legado.app.databinding.DialogSourcePickerBinding import io.legado.app.databinding.ItemTextBinding import io.legado.app.lib.theme.primaryColor import io.legado.app.lib.theme.primaryTextColor import io.legado.app.utils.applyTint +import io.legado.app.utils.dpToPx import io.legado.app.utils.setLayout import io.legado.app.utils.viewbindingdelegate.viewBinding +import kotlinx.coroutines.Job +import kotlinx.coroutines.launch class SourcePickerDialog : BaseDialogFragment(R.layout.dialog_source_picker) { @@ -28,6 +32,7 @@ class SourcePickerDialog : BaseDialogFragment(R.layout.dialog_source_picker) { private val adapter by lazy { SourceAdapter(requireContext()) } + private var sourceFlowJob: Job? = null override fun onStart() { super.onStart() @@ -36,6 +41,7 @@ class SourcePickerDialog : BaseDialogFragment(R.layout.dialog_source_picker) { override fun onFragmentCreated(view: View, savedInstanceState: Bundle?) { initView() + initData() } private fun initView() { @@ -54,18 +60,31 @@ class SourcePickerDialog : BaseDialogFragment(R.layout.dialog_source_picker) { } override fun onQueryTextChange(newText: String?): Boolean { - + initData(newText) return false } }) } + private fun initData(searchKey: String? = null) { + sourceFlowJob?.cancel() + sourceFlowJob = launch { + when { + searchKey.isNullOrEmpty() -> appDb.bookSourceDao.flowEnabled() + else -> appDb.bookSourceDao.flowSearchEnabled(searchKey) + }.collect { + adapter.setItems(it) + } + } + } inner class SourceAdapter(context: Context) : RecyclerAdapter(context) { override fun getViewBinding(parent: ViewGroup): ItemTextBinding { - return ItemTextBinding.inflate(inflater, parent, false) + return ItemTextBinding.inflate(inflater, parent, false).apply { + root.setPadding(16.dpToPx(), 8.dpToPx(), 16.dpToPx(), 8.dpToPx()) + } } override fun convert( diff --git a/app/src/main/java/io/legado/app/ui/book/source/manage/BookSourceActivity.kt b/app/src/main/java/io/legado/app/ui/book/source/manage/BookSourceActivity.kt index 19b473a02..05b8d5000 100644 --- a/app/src/main/java/io/legado/app/ui/book/source/manage/BookSourceActivity.kt +++ b/app/src/main/java/io/legado/app/ui/book/source/manage/BookSourceActivity.kt @@ -230,10 +230,10 @@ class BookSourceActivity : VMBaseActivity { val key = searchKey.substringAfter("group:") - appDb.bookSourceDao.flowGroupSearch("%$key%") + appDb.bookSourceDao.flowGroupSearch(key) } else -> { - appDb.bookSourceDao.flowSearch("%$searchKey%") + appDb.bookSourceDao.flowSearch(searchKey) } }.conflate().map { data -> if (sortAscending) when (sort) { diff --git a/app/src/main/java/io/legado/app/ui/main/explore/ExploreFragment.kt b/app/src/main/java/io/legado/app/ui/main/explore/ExploreFragment.kt index 8e2b1e049..42c374329 100644 --- a/app/src/main/java/io/legado/app/ui/main/explore/ExploreFragment.kt +++ b/app/src/main/java/io/legado/app/ui/main/explore/ExploreFragment.kt @@ -123,10 +123,10 @@ class ExploreFragment : VMBaseFragment(R.layout.fragment_explo } searchKey.startsWith("group:") -> { val key = searchKey.substringAfter("group:") - appDb.bookSourceDao.flowGroupExplore("%$key%") + appDb.bookSourceDao.flowGroupExplore(key) } else -> { - appDb.bookSourceDao.flowExplore("%$searchKey%") + appDb.bookSourceDao.flowExplore(searchKey) } }.catch { AppLog.put("发现界面更新数据出错", it)