From 1567131df0b37592e725f3a65fb0d8c1e3211ea4 Mon Sep 17 00:00:00 2001 From: kunfei Date: Tue, 19 Nov 2019 16:03:39 +0800 Subject: [PATCH] up --- .../app/ui/rss/article/RssArticlesActivity.kt | 17 ++++++++++++----- .../app/ui/rss/article/RssArticlesViewModel.kt | 10 +++++++--- 2 files changed, 19 insertions(+), 8 deletions(-) diff --git a/app/src/main/java/io/legado/app/ui/rss/article/RssArticlesActivity.kt b/app/src/main/java/io/legado/app/ui/rss/article/RssArticlesActivity.kt index 8e32ad867..8cb633963 100644 --- a/app/src/main/java/io/legado/app/ui/rss/article/RssArticlesActivity.kt +++ b/app/src/main/java/io/legado/app/ui/rss/article/RssArticlesActivity.kt @@ -82,14 +82,13 @@ class RssArticlesActivity : VMBaseActivity(R.layout.activi }) adapter = RssArticlesAdapter(this, this) recycler_view.adapter = adapter - loadMoreView = - LayoutInflater.from(this).inflate(R.layout.view_load_more, recycler_view, false) - adapter?.addFooterView(loadMoreView) refresh_recycler_view.onRefreshStart = { url?.let { - viewModel.loadContent(it) { + viewModel.loadContent(it, { refresh_progress_bar.isAutoLoading = false - } + }, { + addLoadMoreView() + }) } } recycler_view.addOnScrollListener(object : RecyclerView.OnScrollListener() { @@ -102,6 +101,14 @@ class RssArticlesActivity : VMBaseActivity(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) { rssArticlesData?.removeObservers(this) rssArticlesData = App.db.rssArticleDao().liveByOrigin(origin) diff --git a/app/src/main/java/io/legado/app/ui/rss/article/RssArticlesViewModel.kt b/app/src/main/java/io/legado/app/ui/rss/article/RssArticlesViewModel.kt index 603a24148..74fc81bd1 100644 --- a/app/src/main/java/io/legado/app/ui/rss/article/RssArticlesViewModel.kt +++ b/app/src/main/java/io/legado/app/ui/rss/article/RssArticlesViewModel.kt @@ -8,13 +8,14 @@ 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 +import kotlinx.coroutines.withContext class RssArticlesViewModel(application: Application) : BaseViewModel(application) { var rssSource: RssSource? = null val titleLiveData = MutableLiveData() - fun loadContent(url: String, onFinally: () -> Unit) { + fun loadContent(url: String, onFinally: () -> Unit, loadMore: (() -> Unit)? = null) { execute { rssSource = App.db.rssSourceDao().getByKey(url) rssSource?.let { @@ -24,9 +25,12 @@ class RssArticlesViewModel(application: Application) : BaseViewModel(application } rssSource?.let { rssSource -> Rss.getArticles(rssSource, this) - .onSuccess(IO) { + .onSuccess { it?.let { - App.db.rssArticleDao().insert(*it.toTypedArray()) + withContext(IO) { + App.db.rssArticleDao().insert(*it.toTypedArray()) + } + loadMore?.invoke() } }.onError { toast(it.localizedMessage)