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) @Insert(onConflict = OnConflictStrategy.IGNORE)
fun insert(vararg rssArticle: RssArticle) fun insert(vararg rssArticle: RssArticle)
@Query("delete from rssArticles where origin = :origin and `order` < :order")
fun clearOld(origin: String, order: Long)
@Update @Update
fun update(vararg rssArticle: RssArticle) fun update(vararg rssArticle: RssArticle)

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

@ -4,10 +4,12 @@ import android.os.Bundle
import android.view.Menu import android.view.Menu
import android.view.MenuItem import android.view.MenuItem
import androidx.core.content.ContextCompat import androidx.core.content.ContextCompat
import androidx.lifecycle.LiveData
import androidx.lifecycle.Observer import androidx.lifecycle.Observer
import androidx.recyclerview.widget.DividerItemDecoration import androidx.recyclerview.widget.DividerItemDecoration
import androidx.recyclerview.widget.LinearLayoutManager import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView import androidx.recyclerview.widget.RecyclerView
import io.legado.app.App
import io.legado.app.R import io.legado.app.R
import io.legado.app.base.VMBaseActivity import io.legado.app.base.VMBaseActivity
import io.legado.app.data.entities.RssArticle import io.legado.app.data.entities.RssArticle
@ -32,6 +34,7 @@ class RssArticlesActivity : VMBaseActivity<RssArticlesViewModel>(R.layout.activi
override lateinit var adapter: RssArticlesAdapter override lateinit var adapter: RssArticlesAdapter
private val editSource = 12319 private val editSource = 12319
private lateinit var loadMoreView: LoadMoreView private lateinit var loadMoreView: LoadMoreView
private var rssArticlesData: LiveData<List<RssArticle>>? = null
override fun onActivityCreated(savedInstanceState: Bundle?) { override fun onActivityCreated(savedInstanceState: Bundle?) {
viewModel.callBack = this viewModel.callBack = this
@ -40,6 +43,7 @@ class RssArticlesActivity : VMBaseActivity<RssArticlesViewModel>(R.layout.activi
}) })
initView() initView()
viewModel.initData(intent) { viewModel.initData(intent) {
initData()
refresh_recycler_view.startLoading() 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() { private fun scrollToBottom() {
if (viewModel.isLoading) return if (viewModel.isLoading) return
if (loadMoreView.hasMore && adapter.getActualItemCount() > 0) { 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.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.IO
class RssArticlesViewModel(application: Application) : BaseViewModel(application) { class RssArticlesViewModel(application: Application) : BaseViewModel(application) {
@ -15,10 +16,8 @@ class RssArticlesViewModel(application: Application) : BaseViewModel(application
var url: String? = null var url: String? = null
var rssSource: RssSource? = null var rssSource: RssSource? = null
val titleLiveData = MutableLiveData<String>() val titleLiveData = MutableLiveData<String>()
private val articles = linkedSetOf<RssArticle>()
var isLoading = true var isLoading = true
var page = 1 var order = System.currentTimeMillis()
var hasMore = true
fun initData(intent: Intent, finally: () -> Unit) { fun initData(intent: Intent, finally: () -> Unit) {
execute { execute {
@ -39,17 +38,16 @@ class RssArticlesViewModel(application: Application) : BaseViewModel(application
fun loadContent() { fun loadContent() {
isLoading = true isLoading = true
rssSource?.let { rssSource -> rssSource?.let { rssSource ->
Rss.getArticles(rssSource, page, this) Rss.getArticles(rssSource, null)
.onSuccess { .onSuccess(IO) {
it?.let { it?.let {
val oldSize = articles.size it.forEach { rssArticle ->
articles.addAll(it) rssArticle.order = order--
if (articles.size == oldSize) { }
hasMore = false App.db.rssArticleDao().insert(*it.toTypedArray())
} else { if (!rssSource.ruleNextPage.isNullOrEmpty()) {
callBack?.adapter?.setItems(articles.toList()) App.db.rssArticleDao().clearOld(url!!, order)
} }
page++
isLoading = false isLoading = false
} }
}.onError { }.onError {
@ -66,10 +64,15 @@ class RssArticlesViewModel(application: Application) : BaseViewModel(application
} }
fun clear() { fun clear() {
page = 1 execute {
articles.clear() url?.let {
App.db.rssArticleDao().delete(it)
}
order = System.currentTimeMillis()
}.onSuccess {
loadContent() loadContent()
} }
}
interface CallBack { interface CallBack {
var adapter: RssArticlesAdapter var adapter: RssArticlesAdapter

Loading…
Cancel
Save