pull/1155/head
gedoor 3 years ago
parent 91bdedf0c0
commit a2f2b8289f
  1. 7
      app/src/main/java/io/legado/app/data/dao/RssArticleDao.kt
  2. 4
      app/src/main/java/io/legado/app/data/dao/RssStarDao.kt
  3. 18
      app/src/main/java/io/legado/app/ui/rss/article/RssArticlesFragment.kt
  4. 12
      app/src/main/java/io/legado/app/ui/rss/favorites/RssFavoritesActivity.kt

@ -1,9 +1,9 @@
package io.legado.app.data.dao package io.legado.app.data.dao
import androidx.lifecycle.LiveData
import androidx.room.* import androidx.room.*
import io.legado.app.data.entities.RssArticle import io.legado.app.data.entities.RssArticle
import io.legado.app.data.entities.RssReadRecord import io.legado.app.data.entities.RssReadRecord
import kotlinx.coroutines.flow.Flow
@Dao @Dao
interface RssArticleDao { interface RssArticleDao {
@ -12,12 +12,13 @@ interface RssArticleDao {
fun get(origin: String, link: String): RssArticle? fun get(origin: String, link: String): RssArticle?
@Query( @Query(
"""select t1.link, t1.sort, t1.origin, t1.`order`, t1.title, t1.content, t1.description, t1.image, t1.pubDate, t1.variable, ifNull(t2.read, 0) as read """select t1.link, t1.sort, t1.origin, t1.`order`, t1.title, t1.content,
t1.description, t1.image, t1.pubDate, t1.variable, ifNull(t2.read, 0) as read
from rssArticles as t1 left join rssReadRecords as t2 from rssArticles as t1 left join rssReadRecords as t2
on t1.link = t2.record where origin = :origin and sort = :sort on t1.link = t2.record where origin = :origin and sort = :sort
order by `order` desc""" order by `order` desc"""
) )
fun liveByOriginSort(origin: String, sort: String): LiveData<List<RssArticle>> fun liveByOriginSort(origin: String, sort: String): Flow<List<RssArticle>>
@Insert(onConflict = OnConflictStrategy.REPLACE) @Insert(onConflict = OnConflictStrategy.REPLACE)
fun insert(vararg rssArticle: RssArticle) fun insert(vararg rssArticle: RssArticle)

@ -1,8 +1,8 @@
package io.legado.app.data.dao package io.legado.app.data.dao
import androidx.lifecycle.LiveData
import androidx.room.* import androidx.room.*
import io.legado.app.data.entities.RssStar import io.legado.app.data.entities.RssStar
import kotlinx.coroutines.flow.Flow
@Dao @Dao
interface RssStarDao { interface RssStarDao {
@ -14,7 +14,7 @@ interface RssStarDao {
fun get(origin: String, link: String): RssStar? fun get(origin: String, link: String): RssStar?
@Query("select * from rssStars order by starTime desc") @Query("select * from rssStars order by starTime desc")
fun liveAll(): LiveData<List<RssStar>> fun liveAll(): Flow<List<RssStar>>
@Insert(onConflict = OnConflictStrategy.REPLACE) @Insert(onConflict = OnConflictStrategy.REPLACE)
fun insert(vararg rssStar: RssStar) fun insert(vararg rssStar: RssStar)

@ -5,7 +5,6 @@ import android.os.Bundle
import android.view.View import android.view.View
import androidx.fragment.app.activityViewModels import androidx.fragment.app.activityViewModels
import androidx.fragment.app.viewModels import androidx.fragment.app.viewModels
import androidx.lifecycle.LiveData
import androidx.recyclerview.widget.GridLayoutManager import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.LinearLayoutManager import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView import androidx.recyclerview.widget.RecyclerView
@ -22,6 +21,9 @@ import io.legado.app.ui.widget.recycler.LoadMoreView
import io.legado.app.ui.widget.recycler.VerticalDivider import io.legado.app.ui.widget.recycler.VerticalDivider
import io.legado.app.utils.startActivity import io.legado.app.utils.startActivity
import io.legado.app.utils.viewbindingdelegate.viewBinding import io.legado.app.utils.viewbindingdelegate.viewBinding
import kotlinx.coroutines.Job
import kotlinx.coroutines.flow.collect
import kotlinx.coroutines.launch
class RssArticlesFragment : VMBaseFragment<RssArticlesViewModel>(R.layout.fragment_rss_articles), class RssArticlesFragment : VMBaseFragment<RssArticlesViewModel>(R.layout.fragment_rss_articles),
BaseRssArticlesAdapter.CallBack { BaseRssArticlesAdapter.CallBack {
@ -42,7 +44,7 @@ class RssArticlesFragment : VMBaseFragment<RssArticlesViewModel>(R.layout.fragme
override val viewModel by viewModels<RssArticlesViewModel>() override val viewModel by viewModels<RssArticlesViewModel>()
lateinit var adapter: BaseRssArticlesAdapter<*> lateinit var adapter: BaseRssArticlesAdapter<*>
private lateinit var loadMoreView: LoadMoreView private lateinit var loadMoreView: LoadMoreView
private var rssArticlesData: LiveData<List<RssArticle>>? = null private var articlesFlowJob: Job? = null
override val isGridLayout: Boolean override val isGridLayout: Boolean
get() = activityViewModel.isGridLayout get() = activityViewModel.isGridLayout
@ -92,12 +94,12 @@ class RssArticlesFragment : VMBaseFragment<RssArticlesViewModel>(R.layout.fragme
} }
private fun initData() { private fun initData() {
activityViewModel.url?.let { val rssUrl = activityViewModel.url ?: return
rssArticlesData?.removeObservers(this) articlesFlowJob?.cancel()
rssArticlesData = appDb.rssArticleDao.liveByOriginSort(it, viewModel.sortName) articlesFlowJob = launch {
rssArticlesData?.observe(viewLifecycleOwner, { list -> appDb.rssArticleDao.liveByOriginSort(rssUrl, viewModel.sortName).collect {
adapter.setItems(list) adapter.setItems(it)
}) }
} }
} }

@ -1,7 +1,6 @@
package io.legado.app.ui.rss.favorites package io.legado.app.ui.rss.favorites
import android.os.Bundle import android.os.Bundle
import androidx.lifecycle.LiveData
import androidx.recyclerview.widget.LinearLayoutManager import androidx.recyclerview.widget.LinearLayoutManager
import io.legado.app.base.BaseActivity import io.legado.app.base.BaseActivity
import io.legado.app.data.appDb import io.legado.app.data.appDb
@ -11,13 +10,14 @@ import io.legado.app.ui.rss.read.ReadRssActivity
import io.legado.app.ui.widget.recycler.VerticalDivider import io.legado.app.ui.widget.recycler.VerticalDivider
import io.legado.app.utils.startActivity import io.legado.app.utils.startActivity
import io.legado.app.utils.viewbindingdelegate.viewBinding import io.legado.app.utils.viewbindingdelegate.viewBinding
import kotlinx.coroutines.flow.collect
import kotlinx.coroutines.launch
class RssFavoritesActivity : BaseActivity<ActivityRssFavoritesBinding>(), class RssFavoritesActivity : BaseActivity<ActivityRssFavoritesBinding>(),
RssFavoritesAdapter.CallBack { RssFavoritesAdapter.CallBack {
override val binding by viewBinding(ActivityRssFavoritesBinding::inflate) override val binding by viewBinding(ActivityRssFavoritesBinding::inflate)
private var liveData: LiveData<List<RssStar>>? = null
private lateinit var adapter: RssFavoritesAdapter private lateinit var adapter: RssFavoritesAdapter
override fun onActivityCreated(savedInstanceState: Bundle?) { override fun onActivityCreated(savedInstanceState: Bundle?) {
@ -35,11 +35,11 @@ class RssFavoritesActivity : BaseActivity<ActivityRssFavoritesBinding>(),
} }
private fun initData() { private fun initData() {
liveData?.removeObservers(this) launch {
liveData = appDb.rssStarDao.liveAll() appDb.rssStarDao.liveAll().collect {
liveData?.observe(this, {
adapter.setItems(it) adapter.setItems(it)
}) }
}
} }
override fun readRss(rssStar: RssStar) { override fun readRss(rssStar: RssStar) {

Loading…
Cancel
Save