From 629420e3d51e271379b541563ce5b58ddbb632dd Mon Sep 17 00:00:00 2001 From: gedoor Date: Wed, 23 Dec 2020 20:48:11 +0800 Subject: [PATCH] =?UTF-8?q?=E8=AE=A2=E9=98=85=E6=B7=BB=E5=8A=A0=E6=90=9C?= =?UTF-8?q?=E7=B4=A2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../io/legado/app/data/dao/RssSourceDao.kt | 6 ++-- .../io/legado/app/ui/main/rss/RssFragment.kt | 36 ++++++++++++++++--- app/src/main/res/layout/fragment_rss.xml | 1 + 3 files changed, 36 insertions(+), 7 deletions(-) diff --git a/app/src/main/java/io/legado/app/data/dao/RssSourceDao.kt b/app/src/main/java/io/legado/app/data/dao/RssSourceDao.kt index 5436ee26c..c2d1b80ee 100644 --- a/app/src/main/java/io/legado/app/data/dao/RssSourceDao.kt +++ b/app/src/main/java/io/legado/app/data/dao/RssSourceDao.kt @@ -11,7 +11,7 @@ interface RssSourceDao { fun getByKey(key: String): RssSource? @Query("select * from rssSources where sourceUrl in (:sourceUrls)") - fun getRssSources(vararg sourceUrls: String):List + fun getRssSources(vararg sourceUrls: String): List @get:Query("SELECT * FROM rssSources") val all: List @@ -25,8 +25,8 @@ interface RssSourceDao { @Query("SELECT * FROM rssSources where sourceName like :key or sourceUrl like :key or sourceGroup like :key order by customOrder") fun liveSearch(key: String): LiveData> - @Query("SELECT * FROM rssSources where enabled = 1 order by customOrder") - fun liveEnabled(): LiveData> + @Query("SELECT * FROM rssSources where enabled = 1 and (sourceName like '%'||:searchKey||'%' or sourceGroup like '%'||:searchKey||'%' or sourceUrl like '%'||:searchKey||'%') order by customOrder") + fun liveEnabled(searchKey: String): LiveData> @Query("select sourceGroup from rssSources where trim(sourceGroup) <> ''") fun liveGroup(): LiveData> diff --git a/app/src/main/java/io/legado/app/ui/main/rss/RssFragment.kt b/app/src/main/java/io/legado/app/ui/main/rss/RssFragment.kt index c8cc6e723..8fbb92c7b 100644 --- a/app/src/main/java/io/legado/app/ui/main/rss/RssFragment.kt +++ b/app/src/main/java/io/legado/app/ui/main/rss/RssFragment.kt @@ -4,6 +4,8 @@ import android.os.Bundle import android.view.Menu import android.view.MenuItem import android.view.View +import androidx.appcompat.widget.SearchView +import androidx.lifecycle.LiveData import io.legado.app.App import io.legado.app.R import io.legado.app.base.VMBaseFragment @@ -11,6 +13,7 @@ import io.legado.app.data.entities.RssSource import io.legado.app.databinding.FragmentRssBinding import io.legado.app.databinding.ItemRssBinding import io.legado.app.lib.theme.ATH +import io.legado.app.lib.theme.primaryTextColor import io.legado.app.ui.rss.article.RssSortActivity import io.legado.app.ui.rss.favorites.RssFavoritesActivity import io.legado.app.ui.rss.read.ReadRssActivity @@ -30,11 +33,15 @@ class RssFragment : VMBaseFragment(R.layout.fragment_rss), RssAdapter.CallBack { private val binding by viewBinding(FragmentRssBinding::bind) private lateinit var adapter: RssAdapter + private lateinit var searchView: SearchView override val viewModel: RssSourceViewModel get() = getViewModel(RssSourceViewModel::class.java) + private var liveRssData: LiveData>? = null override fun onFragmentCreated(view: View, savedInstanceState: Bundle?) { + searchView = binding.titleBar.findViewById(R.id.search_view) setSupportToolbar(binding.titleBar.toolbar) + initSearchView() initRecyclerView() initData() } @@ -51,6 +58,24 @@ class RssFragment : VMBaseFragment(R.layout.fragment_rss), } } + private fun initSearchView() { + ATH.setTint(searchView, primaryTextColor) + searchView.onActionViewExpanded() + searchView.isSubmitButtonEnabled = true + searchView.queryHint = getString(R.string.rss) + searchView.clearFocus() + searchView.setOnQueryTextListener(object : SearchView.OnQueryTextListener { + override fun onQueryTextSubmit(query: String?): Boolean { + return false + } + + override fun onQueryTextChange(newText: String?): Boolean { + initData(newText ?: "") + return false + } + }) + } + private fun initRecyclerView() { ATH.applyEdgeEffectColor(binding.recyclerView) adapter = RssAdapter(requireContext(), this) @@ -66,10 +91,13 @@ class RssFragment : VMBaseFragment(R.layout.fragment_rss), } } - private fun initData() { - App.db.rssSourceDao.liveEnabled().observe(viewLifecycleOwner, { - adapter.setItems(it) - }) + private fun initData(searchKey: String = "") { + liveRssData?.removeObservers(this) + liveRssData = App.db.rssSourceDao.liveEnabled(searchKey).apply { + observe(viewLifecycleOwner, { + adapter.setItems(it) + }) + } } override fun openRss(rssSource: RssSource) { diff --git a/app/src/main/res/layout/fragment_rss.xml b/app/src/main/res/layout/fragment_rss.xml index 49456ce20..f972ab758 100644 --- a/app/src/main/res/layout/fragment_rss.xml +++ b/app/src/main/res/layout/fragment_rss.xml @@ -11,6 +11,7 @@ android:layout_width="match_parent" android:layout_height="wrap_content" app:attachToActivity="false" + app:contentLayout="@layout/view_search" app:layout_constraintTop_toTopOf="parent" app:title="@string/rss" />