|
|
@ -10,15 +10,14 @@ import io.legado.app.data.appDb |
|
|
|
import io.legado.app.data.entities.RssArticle |
|
|
|
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.Rss |
|
|
|
import io.legado.app.model.rss.Rss |
|
|
|
import io.legado.app.utils.printOnDebug |
|
|
|
import io.legado.app.utils.stackTraceStr |
|
|
|
|
|
|
|
|
|
|
|
import io.legado.app.utils.toastOnUi |
|
|
|
|
|
|
|
import kotlinx.coroutines.Dispatchers |
|
|
|
import kotlinx.coroutines.Dispatchers |
|
|
|
import kotlinx.coroutines.withContext |
|
|
|
import kotlinx.coroutines.withContext |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class RssArticlesViewModel(application: Application) : BaseViewModel(application) { |
|
|
|
class RssArticlesViewModel(application: Application) : BaseViewModel(application) { |
|
|
|
val loadFinally = MutableLiveData<Boolean>() |
|
|
|
val loadFinallyLiveData = MutableLiveData<Boolean>() |
|
|
|
|
|
|
|
val loadErrorLiveData = MutableLiveData<String>() |
|
|
|
var isLoading = true |
|
|
|
var isLoading = true |
|
|
|
var order = System.currentTimeMillis() |
|
|
|
var order = System.currentTimeMillis() |
|
|
|
private var nextPageUrl: String? = null |
|
|
|
private var nextPageUrl: String? = null |
|
|
@ -46,18 +45,18 @@ class RssArticlesViewModel(application: Application) : BaseViewModel(application |
|
|
|
appDb.rssArticleDao.insert(*list.toTypedArray()) |
|
|
|
appDb.rssArticleDao.insert(*list.toTypedArray()) |
|
|
|
if (!rssSource.ruleNextPage.isNullOrEmpty()) { |
|
|
|
if (!rssSource.ruleNextPage.isNullOrEmpty()) { |
|
|
|
appDb.rssArticleDao.clearOld(rssSource.sourceUrl, sortName, order) |
|
|
|
appDb.rssArticleDao.clearOld(rssSource.sourceUrl, sortName, order) |
|
|
|
loadFinally.postValue(true) |
|
|
|
loadFinallyLiveData.postValue(true) |
|
|
|
} else { |
|
|
|
} else { |
|
|
|
withContext(Dispatchers.Main) { |
|
|
|
withContext(Dispatchers.Main) { |
|
|
|
loadFinally.postValue(false) |
|
|
|
loadFinallyLiveData.postValue(false) |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
isLoading = false |
|
|
|
isLoading = false |
|
|
|
} |
|
|
|
} |
|
|
|
}.onError { |
|
|
|
}.onError { |
|
|
|
loadFinally.postValue(false) |
|
|
|
loadFinallyLiveData.postValue(false) |
|
|
|
AppLog.put("rss获取内容失败", it) |
|
|
|
AppLog.put("rss获取内容失败", it) |
|
|
|
context.toastOnUi(it.localizedMessage) |
|
|
|
loadErrorLiveData.postValue(it.stackTraceStr) |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
@ -72,25 +71,26 @@ class RssArticlesViewModel(application: Application) : BaseViewModel(application |
|
|
|
loadMoreSuccess(it.first) |
|
|
|
loadMoreSuccess(it.first) |
|
|
|
} |
|
|
|
} |
|
|
|
.onError { |
|
|
|
.onError { |
|
|
|
it.printOnDebug() |
|
|
|
loadFinallyLiveData.postValue(false) |
|
|
|
loadFinally.postValue(false) |
|
|
|
AppLog.put("rss获取内容失败", it) |
|
|
|
|
|
|
|
loadErrorLiveData.postValue(it.stackTraceStr) |
|
|
|
} |
|
|
|
} |
|
|
|
} else { |
|
|
|
} else { |
|
|
|
loadFinally.postValue(false) |
|
|
|
loadFinallyLiveData.postValue(false) |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
private fun loadMoreSuccess(articles: MutableList<RssArticle>) { |
|
|
|
private fun loadMoreSuccess(articles: MutableList<RssArticle>) { |
|
|
|
articles.let { list -> |
|
|
|
articles.let { list -> |
|
|
|
if (list.isEmpty()) { |
|
|
|
if (list.isEmpty()) { |
|
|
|
loadFinally.postValue(false) |
|
|
|
loadFinallyLiveData.postValue(false) |
|
|
|
return@let |
|
|
|
return@let |
|
|
|
} |
|
|
|
} |
|
|
|
val firstArticle = list.first() |
|
|
|
val firstArticle = list.first() |
|
|
|
val dbArticle = appDb.rssArticleDao |
|
|
|
val dbArticle = appDb.rssArticleDao |
|
|
|
.get(firstArticle.origin, firstArticle.link) |
|
|
|
.get(firstArticle.origin, firstArticle.link) |
|
|
|
if (dbArticle != null) { |
|
|
|
if (dbArticle != null) { |
|
|
|
loadFinally.postValue(false) |
|
|
|
loadFinallyLiveData.postValue(false) |
|
|
|
} else { |
|
|
|
} else { |
|
|
|
list.forEach { rssArticle -> |
|
|
|
list.forEach { rssArticle -> |
|
|
|
rssArticle.order = order-- |
|
|
|
rssArticle.order = order-- |
|
|
|