From 9bf14ed479ed49f63e4663415e9561b3567def69 Mon Sep 17 00:00:00 2001 From: kunfei Date: Wed, 20 Nov 2019 21:48:42 +0800 Subject: [PATCH] up --- .../io/legado/app/data/dao/RssArticleDao.kt | 3 ++ app/src/main/java/io/legado/app/model/Rss.kt | 4 +-- .../app/ui/rss/article/RssArticlesActivity.kt | 14 ++++++++ .../ui/rss/article/RssArticlesViewModel.kt | 33 ++++++++++--------- 4 files changed, 37 insertions(+), 17 deletions(-) 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 dff07d1ab..89c8af635 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 @@ -16,6 +16,9 @@ interface RssArticleDao { @Insert(onConflict = OnConflictStrategy.IGNORE) fun insert(vararg rssArticle: RssArticle) + @Query("delete from rssArticles where origin = :origin and `order` < :order") + fun clearOld(origin: String, order: Long) + @Update fun update(vararg rssArticle: RssArticle) diff --git a/app/src/main/java/io/legado/app/model/Rss.kt b/app/src/main/java/io/legado/app/model/Rss.kt index 5075e5b1e..4caca495d 100644 --- a/app/src/main/java/io/legado/app/model/Rss.kt +++ b/app/src/main/java/io/legado/app/model/Rss.kt @@ -15,12 +15,12 @@ object Rss { fun getArticles( rssSource: RssSource, - page: Int, + pageUrl: String? = null, scope: CoroutineScope = Coroutine.DEFAULT, context: CoroutineContext = Dispatchers.IO ): Coroutine> { return Coroutine.async(scope, context) { - val analyzeUrl = AnalyzeUrl(rssSource.sourceUrl, page = page) + val analyzeUrl = AnalyzeUrl(pageUrl ?: rssSource.sourceUrl) val body = analyzeUrl.getResponseAwait(rssSource.sourceUrl).body RssParserByRule.parseXML(body, rssSource) } diff --git a/app/src/main/java/io/legado/app/ui/rss/article/RssArticlesActivity.kt b/app/src/main/java/io/legado/app/ui/rss/article/RssArticlesActivity.kt index a9e4e9373..f0a8ebfa0 100644 --- a/app/src/main/java/io/legado/app/ui/rss/article/RssArticlesActivity.kt +++ b/app/src/main/java/io/legado/app/ui/rss/article/RssArticlesActivity.kt @@ -4,10 +4,12 @@ import android.os.Bundle import android.view.Menu import android.view.MenuItem import androidx.core.content.ContextCompat +import androidx.lifecycle.LiveData import androidx.lifecycle.Observer import androidx.recyclerview.widget.DividerItemDecoration import androidx.recyclerview.widget.LinearLayoutManager import androidx.recyclerview.widget.RecyclerView +import io.legado.app.App import io.legado.app.R import io.legado.app.base.VMBaseActivity import io.legado.app.data.entities.RssArticle @@ -32,6 +34,7 @@ class RssArticlesActivity : VMBaseActivity(R.layout.activi override lateinit var adapter: RssArticlesAdapter private val editSource = 12319 private lateinit var loadMoreView: LoadMoreView + private var rssArticlesData: LiveData>? = null override fun onActivityCreated(savedInstanceState: Bundle?) { viewModel.callBack = this @@ -40,6 +43,7 @@ class RssArticlesActivity : VMBaseActivity(R.layout.activi }) initView() viewModel.initData(intent) { + initData() refresh_recycler_view.startLoading() } } @@ -92,6 +96,16 @@ class RssArticlesActivity : VMBaseActivity(R.layout.activi }) } + private fun initData() { + viewModel.url?.let { + rssArticlesData?.removeObservers(this) + rssArticlesData = App.db.rssArticleDao().liveByOrigin(it) + rssArticlesData?.observe(this, Observer { list -> + adapter.setItems(list) + }) + } + } + private fun scrollToBottom() { if (viewModel.isLoading) return if (loadMoreView.hasMore && adapter.getActualItemCount() > 0) { diff --git a/app/src/main/java/io/legado/app/ui/rss/article/RssArticlesViewModel.kt b/app/src/main/java/io/legado/app/ui/rss/article/RssArticlesViewModel.kt index 3c18a5abc..5c5775bd1 100644 --- a/app/src/main/java/io/legado/app/ui/rss/article/RssArticlesViewModel.kt +++ b/app/src/main/java/io/legado/app/ui/rss/article/RssArticlesViewModel.kt @@ -8,6 +8,7 @@ import io.legado.app.base.BaseViewModel import io.legado.app.data.entities.RssArticle import io.legado.app.data.entities.RssSource import io.legado.app.model.Rss +import kotlinx.coroutines.Dispatchers.IO class RssArticlesViewModel(application: Application) : BaseViewModel(application) { @@ -15,10 +16,8 @@ class RssArticlesViewModel(application: Application) : BaseViewModel(application var url: String? = null var rssSource: RssSource? = null val titleLiveData = MutableLiveData() - private val articles = linkedSetOf() var isLoading = true - var page = 1 - var hasMore = true + var order = System.currentTimeMillis() fun initData(intent: Intent, finally: () -> Unit) { execute { @@ -39,17 +38,16 @@ class RssArticlesViewModel(application: Application) : BaseViewModel(application fun loadContent() { isLoading = true rssSource?.let { rssSource -> - Rss.getArticles(rssSource, page, this) - .onSuccess { + Rss.getArticles(rssSource, null) + .onSuccess(IO) { it?.let { - val oldSize = articles.size - articles.addAll(it) - if (articles.size == oldSize) { - hasMore = false - } else { - callBack?.adapter?.setItems(articles.toList()) + it.forEach { rssArticle -> + rssArticle.order = order-- + } + App.db.rssArticleDao().insert(*it.toTypedArray()) + if (!rssSource.ruleNextPage.isNullOrEmpty()) { + App.db.rssArticleDao().clearOld(url!!, order) } - page++ isLoading = false } }.onError { @@ -66,9 +64,14 @@ class RssArticlesViewModel(application: Application) : BaseViewModel(application } fun clear() { - page = 1 - articles.clear() - loadContent() + execute { + url?.let { + App.db.rssArticleDao().delete(it) + } + order = System.currentTimeMillis() + }.onSuccess { + loadContent() + } } interface CallBack {