feat: 优化

pull/209/head
kunfei 5 years ago
parent bf9f979106
commit 51b2ee20e8
  1. 28
      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,15 +59,26 @@ class RssArticlesViewModel(application: Application) : BaseViewModel(application
Rss.getArticles(sortName, pageUrl, rssSource, pageUrl)
.onSuccess(Dispatchers.IO) {
nextPageUrl = it.nextPageUrl
it.articles.let { list ->
loadMoreSuccess(it.articles)
}
.onError {
loadFinally.postValue(false)
}
} else {
loadFinally.postValue(false)
}
}
private fun loadMoreSuccess(articles: MutableList<RssArticle>) {
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
) {
val dbArticle = App.db.rssArticleDao()
.get(firstArticle.origin, firstArticle.link)
if (dbArticle != null) {
loadFinally.postValue(false)
} else {
list.forEach { rssArticle ->
@ -77,13 +89,5 @@ class RssArticlesViewModel(application: Application) : BaseViewModel(application
}
isLoading = false
}
.onError {
loadFinally.postValue(false)
}
} else {
loadFinally.postValue(false)
}
}
}
Loading…
Cancel
Save