订阅添加搜索

pull/754/head^2
gedoor 4 years ago
parent ccd06501d9
commit 629420e3d5
  1. 4
      app/src/main/java/io/legado/app/data/dao/RssSourceDao.kt
  2. 32
      app/src/main/java/io/legado/app/ui/main/rss/RssFragment.kt
  3. 1
      app/src/main/res/layout/fragment_rss.xml

@ -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") @Query("SELECT * FROM rssSources where sourceName like :key or sourceUrl like :key or sourceGroup like :key order by customOrder")
fun liveSearch(key: String): LiveData<List<RssSource>> fun liveSearch(key: String): LiveData<List<RssSource>>
@Query("SELECT * FROM rssSources where enabled = 1 order by customOrder") @Query("SELECT * FROM rssSources where enabled = 1 and (sourceName like '%'||:searchKey||'%' or sourceGroup like '%'||:searchKey||'%' or sourceUrl like '%'||:searchKey||'%') order by customOrder")
fun liveEnabled(): LiveData<List<RssSource>> fun liveEnabled(searchKey: String): LiveData<List<RssSource>>
@Query("select sourceGroup from rssSources where trim(sourceGroup) <> ''") @Query("select sourceGroup from rssSources where trim(sourceGroup) <> ''")
fun liveGroup(): LiveData<List<String>> fun liveGroup(): LiveData<List<String>>

@ -4,6 +4,8 @@ import android.os.Bundle
import android.view.Menu import android.view.Menu
import android.view.MenuItem import android.view.MenuItem
import android.view.View import android.view.View
import androidx.appcompat.widget.SearchView
import androidx.lifecycle.LiveData
import io.legado.app.App import io.legado.app.App
import io.legado.app.R import io.legado.app.R
import io.legado.app.base.VMBaseFragment 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.FragmentRssBinding
import io.legado.app.databinding.ItemRssBinding import io.legado.app.databinding.ItemRssBinding
import io.legado.app.lib.theme.ATH 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.article.RssSortActivity
import io.legado.app.ui.rss.favorites.RssFavoritesActivity import io.legado.app.ui.rss.favorites.RssFavoritesActivity
import io.legado.app.ui.rss.read.ReadRssActivity import io.legado.app.ui.rss.read.ReadRssActivity
@ -30,11 +33,15 @@ class RssFragment : VMBaseFragment<RssSourceViewModel>(R.layout.fragment_rss),
RssAdapter.CallBack { RssAdapter.CallBack {
private val binding by viewBinding(FragmentRssBinding::bind) private val binding by viewBinding(FragmentRssBinding::bind)
private lateinit var adapter: RssAdapter private lateinit var adapter: RssAdapter
private lateinit var searchView: SearchView
override val viewModel: RssSourceViewModel override val viewModel: RssSourceViewModel
get() = getViewModel(RssSourceViewModel::class.java) get() = getViewModel(RssSourceViewModel::class.java)
private var liveRssData: LiveData<List<RssSource>>? = null
override fun onFragmentCreated(view: View, savedInstanceState: Bundle?) { override fun onFragmentCreated(view: View, savedInstanceState: Bundle?) {
searchView = binding.titleBar.findViewById(R.id.search_view)
setSupportToolbar(binding.titleBar.toolbar) setSupportToolbar(binding.titleBar.toolbar)
initSearchView()
initRecyclerView() initRecyclerView()
initData() initData()
} }
@ -51,6 +58,24 @@ class RssFragment : VMBaseFragment<RssSourceViewModel>(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() { private fun initRecyclerView() {
ATH.applyEdgeEffectColor(binding.recyclerView) ATH.applyEdgeEffectColor(binding.recyclerView)
adapter = RssAdapter(requireContext(), this) adapter = RssAdapter(requireContext(), this)
@ -66,11 +91,14 @@ class RssFragment : VMBaseFragment<RssSourceViewModel>(R.layout.fragment_rss),
} }
} }
private fun initData() { private fun initData(searchKey: String = "") {
App.db.rssSourceDao.liveEnabled().observe(viewLifecycleOwner, { liveRssData?.removeObservers(this)
liveRssData = App.db.rssSourceDao.liveEnabled(searchKey).apply {
observe(viewLifecycleOwner, {
adapter.setItems(it) adapter.setItems(it)
}) })
} }
}
override fun openRss(rssSource: RssSource) { override fun openRss(rssSource: RssSource) {
if (rssSource.singleUrl) { if (rssSource.singleUrl) {

@ -11,6 +11,7 @@
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
app:attachToActivity="false" app:attachToActivity="false"
app:contentLayout="@layout/view_search"
app:layout_constraintTop_toTopOf="parent" app:layout_constraintTop_toTopOf="parent"
app:title="@string/rss" /> app:title="@string/rss" />

Loading…
Cancel
Save