feat: 优化

pull/209/head
kunfei 5 years ago
parent bf9f979106
commit 51b2ee20e8
  1. 40
      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 androidx.lifecycle.MutableLiveData
import io.legado.app.App import io.legado.app.App
import io.legado.app.base.BaseViewModel import io.legado.app.base.BaseViewModel
import io.legado.app.data.entities.RssArticle
import io.legado.app.data.entities.RssSource import io.legado.app.data.entities.RssSource
import io.legado.app.model.Rss import io.legado.app.model.Rss
import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.Dispatchers
@ -58,24 +59,7 @@ class RssArticlesViewModel(application: Application) : BaseViewModel(application
Rss.getArticles(sortName, pageUrl, rssSource, pageUrl) Rss.getArticles(sortName, pageUrl, rssSource, pageUrl)
.onSuccess(Dispatchers.IO) { .onSuccess(Dispatchers.IO) {
nextPageUrl = it.nextPageUrl nextPageUrl = it.nextPageUrl
it.articles.let { list -> loadMoreSuccess(it.articles)
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
} }
.onError { .onError {
loadFinally.postValue(false) loadFinally.postValue(false)
@ -85,5 +69,25 @@ class RssArticlesViewModel(application: Application) : BaseViewModel(application
} }
} }
private fun loadMoreSuccess(articles: MutableList<RssArticle>) {
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
}
} }
Loading…
Cancel
Save