diff --git a/app/src/main/java/io/legado/app/data/dao/RssArticleDao.kt b/app/src/main/java/io/legado/app/data/dao/RssArticleDao.kt index c05dcb880..e75a8eba8 100644 --- a/app/src/main/java/io/legado/app/data/dao/RssArticleDao.kt +++ b/app/src/main/java/io/legado/app/data/dao/RssArticleDao.kt @@ -1,9 +1,9 @@ package io.legado.app.data.dao -import androidx.lifecycle.LiveData import androidx.room.* import io.legado.app.data.entities.RssArticle import io.legado.app.data.entities.RssReadRecord +import kotlinx.coroutines.flow.Flow @Dao interface RssArticleDao { @@ -12,12 +12,13 @@ interface RssArticleDao { fun get(origin: String, link: String): RssArticle? @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 on t1.link = t2.record where origin = :origin and sort = :sort order by `order` desc""" ) - fun liveByOriginSort(origin: String, sort: String): LiveData> + fun liveByOriginSort(origin: String, sort: String): Flow> @Insert(onConflict = OnConflictStrategy.REPLACE) fun insert(vararg rssArticle: RssArticle) diff --git a/app/src/main/java/io/legado/app/data/dao/RssStarDao.kt b/app/src/main/java/io/legado/app/data/dao/RssStarDao.kt index faaf85b9f..2c40349c0 100644 --- a/app/src/main/java/io/legado/app/data/dao/RssStarDao.kt +++ b/app/src/main/java/io/legado/app/data/dao/RssStarDao.kt @@ -1,8 +1,8 @@ package io.legado.app.data.dao -import androidx.lifecycle.LiveData import androidx.room.* import io.legado.app.data.entities.RssStar +import kotlinx.coroutines.flow.Flow @Dao interface RssStarDao { @@ -14,7 +14,7 @@ interface RssStarDao { fun get(origin: String, link: String): RssStar? @Query("select * from rssStars order by starTime desc") - fun liveAll(): LiveData> + fun liveAll(): Flow> @Insert(onConflict = OnConflictStrategy.REPLACE) fun insert(vararg rssStar: RssStar) diff --git a/app/src/main/java/io/legado/app/ui/rss/article/RssArticlesFragment.kt b/app/src/main/java/io/legado/app/ui/rss/article/RssArticlesFragment.kt index da784aa0c..228ac9df9 100644 --- a/app/src/main/java/io/legado/app/ui/rss/article/RssArticlesFragment.kt +++ b/app/src/main/java/io/legado/app/ui/rss/article/RssArticlesFragment.kt @@ -5,7 +5,6 @@ import android.os.Bundle import android.view.View import androidx.fragment.app.activityViewModels import androidx.fragment.app.viewModels -import androidx.lifecycle.LiveData import androidx.recyclerview.widget.GridLayoutManager import androidx.recyclerview.widget.LinearLayoutManager 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.utils.startActivity import io.legado.app.utils.viewbindingdelegate.viewBinding +import kotlinx.coroutines.Job +import kotlinx.coroutines.flow.collect +import kotlinx.coroutines.launch class RssArticlesFragment : VMBaseFragment(R.layout.fragment_rss_articles), BaseRssArticlesAdapter.CallBack { @@ -42,7 +44,7 @@ class RssArticlesFragment : VMBaseFragment(R.layout.fragme override val viewModel by viewModels() lateinit var adapter: BaseRssArticlesAdapter<*> private lateinit var loadMoreView: LoadMoreView - private var rssArticlesData: LiveData>? = null + private var articlesFlowJob: Job? = null override val isGridLayout: Boolean get() = activityViewModel.isGridLayout @@ -92,12 +94,12 @@ class RssArticlesFragment : VMBaseFragment(R.layout.fragme } private fun initData() { - activityViewModel.url?.let { - rssArticlesData?.removeObservers(this) - rssArticlesData = appDb.rssArticleDao.liveByOriginSort(it, viewModel.sortName) - rssArticlesData?.observe(viewLifecycleOwner, { list -> - adapter.setItems(list) - }) + val rssUrl = activityViewModel.url ?: return + articlesFlowJob?.cancel() + articlesFlowJob = launch { + appDb.rssArticleDao.liveByOriginSort(rssUrl, viewModel.sortName).collect { + adapter.setItems(it) + } } } diff --git a/app/src/main/java/io/legado/app/ui/rss/favorites/RssFavoritesActivity.kt b/app/src/main/java/io/legado/app/ui/rss/favorites/RssFavoritesActivity.kt index 3f90252b0..74710a7f2 100644 --- a/app/src/main/java/io/legado/app/ui/rss/favorites/RssFavoritesActivity.kt +++ b/app/src/main/java/io/legado/app/ui/rss/favorites/RssFavoritesActivity.kt @@ -1,7 +1,6 @@ package io.legado.app.ui.rss.favorites import android.os.Bundle -import androidx.lifecycle.LiveData import androidx.recyclerview.widget.LinearLayoutManager import io.legado.app.base.BaseActivity 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.utils.startActivity import io.legado.app.utils.viewbindingdelegate.viewBinding +import kotlinx.coroutines.flow.collect +import kotlinx.coroutines.launch class RssFavoritesActivity : BaseActivity(), RssFavoritesAdapter.CallBack { override val binding by viewBinding(ActivityRssFavoritesBinding::inflate) - private var liveData: LiveData>? = null private lateinit var adapter: RssFavoritesAdapter override fun onActivityCreated(savedInstanceState: Bundle?) { @@ -35,11 +35,11 @@ class RssFavoritesActivity : BaseActivity(), } private fun initData() { - liveData?.removeObservers(this) - liveData = appDb.rssStarDao.liveAll() - liveData?.observe(this, { - adapter.setItems(it) - }) + launch { + appDb.rssStarDao.liveAll().collect { + adapter.setItems(it) + } + } } override fun readRss(rssStar: RssStar) {