pull/32/head
kunfei 5 years ago
parent 0f0405480f
commit 59ae98bac5
  1. 6
      app/src/main/java/io/legado/app/data/dao/RssSourceDao.kt
  2. 45
      app/src/main/java/io/legado/app/ui/main/rss/RssAdapter.kt
  3. 20
      app/src/main/java/io/legado/app/ui/main/rss/RssFragment.kt

@ -1,6 +1,6 @@
package io.legado.app.data.dao package io.legado.app.data.dao
import androidx.paging.DataSource import androidx.lifecycle.LiveData
import androidx.room.Dao import androidx.room.Dao
import androidx.room.Query import androidx.room.Query
import io.legado.app.data.entities.RssSource import io.legado.app.data.entities.RssSource
@ -9,8 +9,8 @@ import io.legado.app.data.entities.RssSource
interface RssSourceDao { interface RssSourceDao {
@Query("SELECT * FROM rssSources") @Query("SELECT * FROM rssSources")
fun observeAll(): DataSource.Factory<Int, RssSource> fun liveAll(): LiveData<List<RssSource>>
@Query("SELECT * FROM rssSources where enabled = 1") @Query("SELECT * FROM rssSources where enabled = 1")
fun observeEnabled(): DataSource.Factory<Int, RssSource> fun liveEnabled(): LiveData<List<RssSource>>
} }

@ -1,48 +1,19 @@
package io.legado.app.ui.main.rss package io.legado.app.ui.main.rss
import android.view.LayoutInflater import android.content.Context
import android.view.View
import android.view.ViewGroup
import androidx.paging.PagedListAdapter
import androidx.recyclerview.widget.DiffUtil
import androidx.recyclerview.widget.RecyclerView
import io.legado.app.R import io.legado.app.R
import io.legado.app.base.adapter.ItemViewHolder
import io.legado.app.base.adapter.SimpleRecyclerAdapter
import io.legado.app.data.entities.RssSource import io.legado.app.data.entities.RssSource
class RssAdapter : PagedListAdapter<RssSource, RssAdapter.MyViewHolder>(DIFF_CALLBACK) { class RssAdapter(context: Context, val callBack: CallBack) :
SimpleRecyclerAdapter<RssSource>(context, R.layout.item_rss) {
companion object { override fun convert(holder: ItemViewHolder, item: RssSource, payloads: MutableList<Any>) {
@JvmField
val DIFF_CALLBACK = object : DiffUtil.ItemCallback<RssSource>() {
override fun areItemsTheSame(oldItem: RssSource, newItem: RssSource): Boolean =
oldItem.sourceUrl == newItem.sourceUrl
override fun areContentsTheSame(oldItem: RssSource, newItem: RssSource): Boolean =
oldItem.sourceName == newItem.sourceName
}
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MyViewHolder {
return MyViewHolder(
LayoutInflater.from(parent.context).inflate(
R.layout.item_rss,
parent,
false
)
)
}
override fun onBindViewHolder(holder: MyViewHolder, position: Int) {
getItem(position)?.let {
holder.onBind(it)
}
}
class MyViewHolder(view: View) : RecyclerView.ViewHolder(view) {
fun onBind(rssSource: RssSource) {
} }
interface CallBack {
fun openRss()
} }
} }

@ -2,22 +2,18 @@ package io.legado.app.ui.main.rss
import android.os.Bundle import android.os.Bundle
import android.view.View import android.view.View
import androidx.lifecycle.LiveData
import androidx.lifecycle.Observer import androidx.lifecycle.Observer
import androidx.paging.LivePagedListBuilder
import androidx.paging.PagedList
import androidx.recyclerview.widget.GridLayoutManager import androidx.recyclerview.widget.GridLayoutManager
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.BaseFragment import io.legado.app.base.BaseFragment
import io.legado.app.data.entities.RssSource
import io.legado.app.lib.theme.ATH import io.legado.app.lib.theme.ATH
import kotlinx.android.synthetic.main.fragment_rss.* import kotlinx.android.synthetic.main.fragment_rss.*
class RssFragment : BaseFragment(R.layout.fragment_rss) { class RssFragment : BaseFragment(R.layout.fragment_rss),
RssAdapter.CallBack {
private lateinit var adapter: RssAdapter private lateinit var adapter: RssAdapter
private var rssLiveData: LiveData<PagedList<RssSource>>? = null
override fun onViewCreated(view: View, savedInstanceState: Bundle?) { override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
initRecyclerView() initRecyclerView()
@ -26,14 +22,18 @@ class RssFragment : BaseFragment(R.layout.fragment_rss) {
private fun initRecyclerView() { private fun initRecyclerView() {
ATH.applyEdgeEffectColor(recycler_view) ATH.applyEdgeEffectColor(recycler_view)
adapter = RssAdapter() adapter = RssAdapter(requireContext(), this)
recycler_view.layoutManager = GridLayoutManager(requireContext(), 4) recycler_view.layoutManager = GridLayoutManager(requireContext(), 4)
recycler_view.adapter = adapter recycler_view.adapter = adapter
} }
private fun initData() { private fun initData() {
rssLiveData?.removeObservers(viewLifecycleOwner) App.db.rssSourceDao().liveEnabled().observe(viewLifecycleOwner, Observer {
rssLiveData = LivePagedListBuilder(App.db.rssSourceDao().observeEnabled(), 50).build() adapter.setItems(it)
rssLiveData?.observe(viewLifecycleOwner, Observer { adapter.submitList(it) }) })
}
override fun openRss() {
} }
} }
Loading…
Cancel
Save