订阅添加搜索

pull/754/head^2
gedoor 4 years ago
parent 629420e3d5
commit 7cfe9a1515
  1. 34
      app/src/main/java/io/legado/app/ui/main/rss/RssFragment.kt
  2. 10
      app/src/main/res/menu/main_rss.xml

@ -3,12 +3,14 @@ package io.legado.app.ui.main.rss
import android.os.Bundle
import android.view.Menu
import android.view.MenuItem
import android.view.SubMenu
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
import io.legado.app.constant.AppPattern
import io.legado.app.data.entities.RssSource
import io.legado.app.databinding.FragmentRssBinding
import io.legado.app.databinding.ItemRssBinding
@ -21,7 +23,9 @@ import io.legado.app.ui.rss.source.edit.RssSourceEditActivity
import io.legado.app.ui.rss.source.manage.RssSourceActivity
import io.legado.app.ui.rss.source.manage.RssSourceViewModel
import io.legado.app.ui.rss.subscription.RuleSubActivity
import io.legado.app.utils.cnCompare
import io.legado.app.utils.getViewModel
import io.legado.app.utils.splitNotBlank
import io.legado.app.utils.startActivity
import io.legado.app.utils.viewbindingdelegate.viewBinding
import org.jetbrains.anko.sdk27.listeners.onClick
@ -37,17 +41,23 @@ class RssFragment : VMBaseFragment<RssSourceViewModel>(R.layout.fragment_rss),
override val viewModel: RssSourceViewModel
get() = getViewModel(RssSourceViewModel::class.java)
private var liveRssData: LiveData<List<RssSource>>? = null
private val groups = linkedSetOf<String>()
private var liveGroup: LiveData<List<String>>? = null
private var groupsMenu: SubMenu? = null
override fun onFragmentCreated(view: View, savedInstanceState: Bundle?) {
searchView = binding.titleBar.findViewById(R.id.search_view)
setSupportToolbar(binding.titleBar.toolbar)
initSearchView()
initRecyclerView()
initGroupData()
initData()
}
override fun onCompatCreateOptionsMenu(menu: Menu) {
menuInflater.inflate(R.menu.main_rss, menu)
groupsMenu = menu.findItem(R.id.menu_group)?.subMenu
upGroupsMenu()
}
override fun onCompatOptionsItemSelected(item: MenuItem) {
@ -55,6 +65,18 @@ class RssFragment : VMBaseFragment<RssSourceViewModel>(R.layout.fragment_rss),
when (item.itemId) {
R.id.menu_rss_config -> startActivity<RssSourceActivity>()
R.id.menu_rss_star -> startActivity<RssFavoritesActivity>()
else -> if (item.groupId == R.id.menu_group_text) {
searchView.setQuery(item.title, true)
}
}
}
private fun upGroupsMenu() = groupsMenu?.let { subMenu ->
subMenu.removeGroup(R.id.menu_group_text)
groups.sortedWith { o1, o2 ->
o1.cnCompare(o2)
}.forEach {
subMenu.add(R.id.menu_group_text, Menu.NONE, Menu.NONE, it)
}
}
@ -100,6 +122,18 @@ class RssFragment : VMBaseFragment<RssSourceViewModel>(R.layout.fragment_rss),
}
}
private fun initGroupData() {
liveGroup?.removeObservers(viewLifecycleOwner)
liveGroup = App.db.rssSourceDao.liveGroup()
liveGroup?.observe(viewLifecycleOwner, {
groups.clear()
it.map { group ->
groups.addAll(group.splitNotBlank(AppPattern.splitGroupRegex))
}
upGroupsMenu()
})
}
override fun openRss(rssSource: RssSource) {
if (rssSource.singleUrl) {
startActivity<ReadRssActivity>(

@ -9,6 +9,16 @@
android:icon="@drawable/ic_star"
app:showAsAction="always" />
<item
android:id="@+id/menu_group"
android:title="@string/group"
android:icon="@drawable/ic_groups"
app:showAsAction="always">
<menu />
</item>
<item
android:id="@+id/menu_rss_config"
android:title="@string/setting"

Loading…
Cancel
Save