From 59ae98bac5a166b60cd0a501d6b723d86619706c Mon Sep 17 00:00:00 2001 From: kunfei Date: Mon, 9 Sep 2019 11:07:00 +0800 Subject: [PATCH] up --- .../io/legado/app/data/dao/RssSourceDao.kt | 6 +-- .../io/legado/app/ui/main/rss/RssAdapter.kt | 45 ++++--------------- .../io/legado/app/ui/main/rss/RssFragment.kt | 20 ++++----- 3 files changed, 21 insertions(+), 50 deletions(-) diff --git a/app/src/main/java/io/legado/app/data/dao/RssSourceDao.kt b/app/src/main/java/io/legado/app/data/dao/RssSourceDao.kt index 49532a846..cb4d695ab 100644 --- a/app/src/main/java/io/legado/app/data/dao/RssSourceDao.kt +++ b/app/src/main/java/io/legado/app/data/dao/RssSourceDao.kt @@ -1,6 +1,6 @@ package io.legado.app.data.dao -import androidx.paging.DataSource +import androidx.lifecycle.LiveData import androidx.room.Dao import androidx.room.Query import io.legado.app.data.entities.RssSource @@ -9,8 +9,8 @@ import io.legado.app.data.entities.RssSource interface RssSourceDao { @Query("SELECT * FROM rssSources") - fun observeAll(): DataSource.Factory + fun liveAll(): LiveData> @Query("SELECT * FROM rssSources where enabled = 1") - fun observeEnabled(): DataSource.Factory + fun liveEnabled(): LiveData> } \ No newline at end of file diff --git a/app/src/main/java/io/legado/app/ui/main/rss/RssAdapter.kt b/app/src/main/java/io/legado/app/ui/main/rss/RssAdapter.kt index 882e4f738..6bef664c6 100644 --- a/app/src/main/java/io/legado/app/ui/main/rss/RssAdapter.kt +++ b/app/src/main/java/io/legado/app/ui/main/rss/RssAdapter.kt @@ -1,48 +1,19 @@ package io.legado.app.ui.main.rss -import android.view.LayoutInflater -import android.view.View -import android.view.ViewGroup -import androidx.paging.PagedListAdapter -import androidx.recyclerview.widget.DiffUtil -import androidx.recyclerview.widget.RecyclerView +import android.content.Context 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 -class RssAdapter : PagedListAdapter(DIFF_CALLBACK) { +class RssAdapter(context: Context, val callBack: CallBack) : + SimpleRecyclerAdapter(context, R.layout.item_rss) { - companion object { - @JvmField - val DIFF_CALLBACK = object : DiffUtil.ItemCallback() { - override fun areItemsTheSame(oldItem: RssSource, newItem: RssSource): Boolean = - oldItem.sourceUrl == newItem.sourceUrl + override fun convert(holder: ItemViewHolder, item: RssSource, payloads: MutableList) { - 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() } } \ No newline at end of file diff --git a/app/src/main/java/io/legado/app/ui/main/rss/RssFragment.kt b/app/src/main/java/io/legado/app/ui/main/rss/RssFragment.kt index fba701142..70df16148 100644 --- a/app/src/main/java/io/legado/app/ui/main/rss/RssFragment.kt +++ b/app/src/main/java/io/legado/app/ui/main/rss/RssFragment.kt @@ -2,22 +2,18 @@ package io.legado.app.ui.main.rss import android.os.Bundle import android.view.View -import androidx.lifecycle.LiveData import androidx.lifecycle.Observer -import androidx.paging.LivePagedListBuilder -import androidx.paging.PagedList import androidx.recyclerview.widget.GridLayoutManager import io.legado.app.App import io.legado.app.R import io.legado.app.base.BaseFragment -import io.legado.app.data.entities.RssSource import io.legado.app.lib.theme.ATH 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 var rssLiveData: LiveData>? = null override fun onViewCreated(view: View, savedInstanceState: Bundle?) { initRecyclerView() @@ -26,14 +22,18 @@ class RssFragment : BaseFragment(R.layout.fragment_rss) { private fun initRecyclerView() { ATH.applyEdgeEffectColor(recycler_view) - adapter = RssAdapter() + adapter = RssAdapter(requireContext(), this) recycler_view.layoutManager = GridLayoutManager(requireContext(), 4) recycler_view.adapter = adapter } private fun initData() { - rssLiveData?.removeObservers(viewLifecycleOwner) - rssLiveData = LivePagedListBuilder(App.db.rssSourceDao().observeEnabled(), 50).build() - rssLiveData?.observe(viewLifecycleOwner, Observer { adapter.submitList(it) }) + App.db.rssSourceDao().liveEnabled().observe(viewLifecycleOwner, Observer { + adapter.setItems(it) + }) + } + + override fun openRss() { + } } \ No newline at end of file