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. 14
      app/src/main/java/io/legado/app/ui/rss/favorites/RssFavoritesActivity.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<List<RssArticle>>
fun liveByOriginSort(origin: String, sort: String): Flow<List<RssArticle>>
@Insert(onConflict = OnConflictStrategy.REPLACE)
fun insert(vararg rssArticle: RssArticle)

@ -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<List<RssStar>>
fun liveAll(): Flow<List<RssStar>>
@Insert(onConflict = OnConflictStrategy.REPLACE)
fun insert(vararg rssStar: RssStar)

@ -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<RssArticlesViewModel>(R.layout.fragment_rss_articles),
BaseRssArticlesAdapter.CallBack {
@ -42,7 +44,7 @@ class RssArticlesFragment : VMBaseFragment<RssArticlesViewModel>(R.layout.fragme
override val viewModel by viewModels<RssArticlesViewModel>()
lateinit var adapter: BaseRssArticlesAdapter<*>
private lateinit var loadMoreView: LoadMoreView
private var rssArticlesData: LiveData<List<RssArticle>>? = null
private var articlesFlowJob: Job? = null
override val isGridLayout: Boolean
get() = activityViewModel.isGridLayout
@ -92,12 +94,12 @@ class RssArticlesFragment : VMBaseFragment<RssArticlesViewModel>(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)
}
}
}

@ -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<ActivityRssFavoritesBinding>(),
RssFavoritesAdapter.CallBack {
override val binding by viewBinding(ActivityRssFavoritesBinding::inflate)
private var liveData: LiveData<List<RssStar>>? = null
private lateinit var adapter: RssFavoritesAdapter
override fun onActivityCreated(savedInstanceState: Bundle?) {
@ -35,11 +35,11 @@ class RssFavoritesActivity : BaseActivity<ActivityRssFavoritesBinding>(),
}
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) {

Loading…
Cancel
Save