From 51b2ee20e804fffb87ec9c5dfd3366e259b0b9a1 Mon Sep 17 00:00:00 2001 From: kunfei Date: Sun, 3 May 2020 12:32:29 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../ui/rss/article/RssArticlesViewModel.kt | 40 ++++++++++--------- 1 file changed, 22 insertions(+), 18 deletions(-) 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 4a7c66cf7..8d2d06df8 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 @@ -5,6 +5,7 @@ import android.os.Bundle import androidx.lifecycle.MutableLiveData import io.legado.app.App 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 @@ -58,24 +59,7 @@ class RssArticlesViewModel(application: Application) : BaseViewModel(application Rss.getArticles(sortName, pageUrl, rssSource, pageUrl) .onSuccess(Dispatchers.IO) { nextPageUrl = it.nextPageUrl - it.articles.let { list -> - if (list.isEmpty()) { - loadFinally.postValue(false) - return@let - } - val firstArticle = list.first() - if (App.db.rssArticleDao() - .get(firstArticle.origin, firstArticle.link) != null - ) { - loadFinally.postValue(false) - } else { - list.forEach { rssArticle -> - rssArticle.order = order-- - } - App.db.rssArticleDao().insert(*list.toTypedArray()) - } - } - isLoading = false + loadMoreSuccess(it.articles) } .onError { loadFinally.postValue(false) @@ -85,5 +69,25 @@ class RssArticlesViewModel(application: Application) : BaseViewModel(application } } + private fun loadMoreSuccess(articles: MutableList) { + articles.let { list -> + if (list.isEmpty()) { + loadFinally.postValue(false) + return@let + } + val firstArticle = list.first() + val dbArticle = App.db.rssArticleDao() + .get(firstArticle.origin, firstArticle.link) + if (dbArticle != null) { + loadFinally.postValue(false) + } else { + list.forEach { rssArticle -> + rssArticle.order = order-- + } + App.db.rssArticleDao().insert(*list.toTypedArray()) + } + } + isLoading = false + } } \ No newline at end of file