@ -3,14 +3,19 @@ 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
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
@ -18,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
@ -30,17 +37,27 @@ 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
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 ) {
@ -48,9 +65,39 @@ 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 )
}
}
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,9 +113,24 @@ class RssFragment : VMBaseFragment<RssSourceViewModel>(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 )
} )
}
}
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 ( )
} )
}