订阅添加搜索

pull/754/head^2
gedoor 4 years ago
parent ccd06501d9
commit 629420e3d5
  1. 6
      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

@ -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<RssSource>
fun getRssSources(vararg sourceUrls: String): List<RssSource>
@get:Query("SELECT * FROM rssSources")
val all: List<RssSource>
@ -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<List<RssSource>>
@Query("SELECT * FROM rssSources where enabled = 1 order by customOrder")
fun liveEnabled(): LiveData<List<RssSource>>
@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<List<RssSource>>
@Query("select sourceGroup from rssSources where trim(sourceGroup) <> ''")
fun liveGroup(): LiveData<List<String>>

@ -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<RssSourceViewModel>(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<List<RssSource>>? = 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<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() {
ATH.applyEdgeEffectColor(binding.recyclerView)
adapter = RssAdapter(requireContext(), this)
@ -66,11 +91,14 @@ class RssFragment : VMBaseFragment<RssSourceViewModel>(R.layout.fragment_rss),
}
}
private fun initData() {
App.db.rssSourceDao.liveEnabled().observe(viewLifecycleOwner, {
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) {
if (rssSource.singleUrl) {

@ -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" />

Loading…
Cancel
Save