Jelajahi Sumber

添加批量换源

kunfei 4 tahun lalu
induk
melakukan
7b61797af9

+ 18 - 7
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<List<BookSource>>
 
-    @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<List<BookSource>>
+
+    @Query("select * from book_sources where bookSourceGroup like '%' || :searchKey || '%' order by customOrder asc")
     fun flowGroupSearch(searchKey: String): Flow<List<BookSource>>
 
     @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<List<BookSource>>
@@ -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<List<BookSource>>

+ 21 - 2
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<BookSource, ItemTextBinding>(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(

+ 2 - 2
app/src/main/java/io/legado/app/ui/book/source/manage/BookSourceActivity.kt

@@ -230,10 +230,10 @@ class BookSourceActivity : VMBaseActivity<ActivityBookSourceBinding, BookSourceV
                 }
                 searchKey.startsWith("group:") -> {
                     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) {

+ 2 - 2
app/src/main/java/io/legado/app/ui/main/explore/ExploreFragment.kt

@@ -123,10 +123,10 @@ class ExploreFragment : VMBaseFragment<ExploreViewModel>(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)