pull/49/head
kunfei 5 years ago
parent e7a8385118
commit 9bf14ed479
  1. 3
      app/src/main/java/io/legado/app/data/dao/RssArticleDao.kt
  2. 4
      app/src/main/java/io/legado/app/model/Rss.kt
  3. 14
      app/src/main/java/io/legado/app/ui/rss/article/RssArticlesActivity.kt
  4. 33
      app/src/main/java/io/legado/app/ui/rss/article/RssArticlesViewModel.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)

@ -15,12 +15,12 @@ object Rss {
fun getArticles(
rssSource: RssSource,
page: Int,
pageUrl: String? = null,
scope: CoroutineScope = Coroutine.DEFAULT,
context: CoroutineContext = Dispatchers.IO
): Coroutine<MutableList<RssArticle>> {
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)
}

@ -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<RssArticlesViewModel>(R.layout.activi
override lateinit var adapter: RssArticlesAdapter
private val editSource = 12319
private lateinit var loadMoreView: LoadMoreView
private var rssArticlesData: LiveData<List<RssArticle>>? = null
override fun onActivityCreated(savedInstanceState: Bundle?) {
viewModel.callBack = this
@ -40,6 +43,7 @@ class RssArticlesActivity : VMBaseActivity<RssArticlesViewModel>(R.layout.activi
})
initView()
viewModel.initData(intent) {
initData()
refresh_recycler_view.startLoading()
}
}
@ -92,6 +96,16 @@ class RssArticlesActivity : VMBaseActivity<RssArticlesViewModel>(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) {

@ -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<String>()
private val articles = linkedSetOf<RssArticle>()
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 {

Loading…
Cancel
Save