pull/49/head
kunfei 5 years ago
parent 946cf45393
commit 1567131df0
  1. 17
      app/src/main/java/io/legado/app/ui/rss/article/RssArticlesActivity.kt
  2. 8
      app/src/main/java/io/legado/app/ui/rss/article/RssArticlesViewModel.kt

@ -82,14 +82,13 @@ class RssArticlesActivity : VMBaseActivity<RssArticlesViewModel>(R.layout.activi
}) })
adapter = RssArticlesAdapter(this, this) adapter = RssArticlesAdapter(this, this)
recycler_view.adapter = adapter recycler_view.adapter = adapter
loadMoreView =
LayoutInflater.from(this).inflate(R.layout.view_load_more, recycler_view, false)
adapter?.addFooterView(loadMoreView)
refresh_recycler_view.onRefreshStart = { refresh_recycler_view.onRefreshStart = {
url?.let { url?.let {
viewModel.loadContent(it) { viewModel.loadContent(it, {
refresh_progress_bar.isAutoLoading = false refresh_progress_bar.isAutoLoading = false
} }, {
addLoadMoreView()
})
} }
} }
recycler_view.addOnScrollListener(object : RecyclerView.OnScrollListener() { recycler_view.addOnScrollListener(object : RecyclerView.OnScrollListener() {
@ -102,6 +101,14 @@ class RssArticlesActivity : VMBaseActivity<RssArticlesViewModel>(R.layout.activi
}) })
} }
private fun addLoadMoreView() {
if (adapter?.getFooterCount() == 0) {
loadMoreView =
LayoutInflater.from(this).inflate(R.layout.view_load_more, recycler_view, false)
adapter?.addFooterView(loadMoreView)
}
}
private fun initData(origin: String) { private fun initData(origin: String) {
rssArticlesData?.removeObservers(this) rssArticlesData?.removeObservers(this)
rssArticlesData = App.db.rssArticleDao().liveByOrigin(origin) rssArticlesData = App.db.rssArticleDao().liveByOrigin(origin)

@ -8,13 +8,14 @@ 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 import kotlinx.coroutines.Dispatchers.IO
import kotlinx.coroutines.withContext
class RssArticlesViewModel(application: Application) : BaseViewModel(application) { class RssArticlesViewModel(application: Application) : BaseViewModel(application) {
var rssSource: RssSource? = null var rssSource: RssSource? = null
val titleLiveData = MutableLiveData<String>() val titleLiveData = MutableLiveData<String>()
fun loadContent(url: String, onFinally: () -> Unit) { fun loadContent(url: String, onFinally: () -> Unit, loadMore: (() -> Unit)? = null) {
execute { execute {
rssSource = App.db.rssSourceDao().getByKey(url) rssSource = App.db.rssSourceDao().getByKey(url)
rssSource?.let { rssSource?.let {
@ -24,10 +25,13 @@ class RssArticlesViewModel(application: Application) : BaseViewModel(application
} }
rssSource?.let { rssSource -> rssSource?.let { rssSource ->
Rss.getArticles(rssSource, this) Rss.getArticles(rssSource, this)
.onSuccess(IO) { .onSuccess {
it?.let { it?.let {
withContext(IO) {
App.db.rssArticleDao().insert(*it.toTypedArray()) App.db.rssArticleDao().insert(*it.toTypedArray())
} }
loadMore?.invoke()
}
}.onError { }.onError {
toast(it.localizedMessage) toast(it.localizedMessage)
}.onFinally { }.onFinally {

Loading…
Cancel
Save