From 2155812cafbf627208684a81779d40c8c6c8ca4e Mon Sep 17 00:00:00 2001 From: kunfei Date: Tue, 19 Nov 2019 22:19:46 +0800 Subject: [PATCH] up --- .../app/ui/book/search/SearchActivity.kt | 3 +- .../app/ui/explore/ExploreShowActivity.kt | 3 +- .../app/ui/rss/article/RssArticlesActivity.kt | 63 ++++++---------- .../ui/rss/article/RssArticlesViewModel.kt | 71 +++++++++++-------- 4 files changed, 67 insertions(+), 73 deletions(-) diff --git a/app/src/main/java/io/legado/app/ui/book/search/SearchActivity.kt b/app/src/main/java/io/legado/app/ui/book/search/SearchActivity.kt index 43dbeca82..36d0fefe6 100644 --- a/app/src/main/java/io/legado/app/ui/book/search/SearchActivity.kt +++ b/app/src/main/java/io/legado/app/ui/book/search/SearchActivity.kt @@ -171,7 +171,7 @@ class SearchActivity : VMBaseActivity(R.layout.activity_book_se } private fun scrollToBottom() { - if (!viewModel.isLoading && viewModel.searchKey.isNotEmpty()) { + if (!viewModel.isLoading && viewModel.searchKey.isNotEmpty() && loadMoreView.hasMore) { viewModel.search("") } } @@ -228,6 +228,7 @@ class SearchActivity : VMBaseActivity(R.layout.activity_book_se override fun searchFinally() { refresh_progress_bar.isAutoLoading = false + loadMoreView.startLoad() fb_stop.invisible() } diff --git a/app/src/main/java/io/legado/app/ui/explore/ExploreShowActivity.kt b/app/src/main/java/io/legado/app/ui/explore/ExploreShowActivity.kt index e9d7ef69d..90333bd5f 100644 --- a/app/src/main/java/io/legado/app/ui/explore/ExploreShowActivity.kt +++ b/app/src/main/java/io/legado/app/ui/explore/ExploreShowActivity.kt @@ -14,7 +14,6 @@ import io.legado.app.ui.book.info.BookInfoActivity import io.legado.app.ui.widget.LoadMoreView import io.legado.app.utils.getViewModel import kotlinx.android.synthetic.main.activity_explore_show.* -import kotlinx.android.synthetic.main.view_load_more.view.* import org.jetbrains.anko.startActivity class ExploreShowActivity : VMBaseActivity(R.layout.activity_explore_show), @@ -40,7 +39,7 @@ class ExploreShowActivity : VMBaseActivity(R.layout.activi recycler_view.adapter = adapter loadMoreView = LoadMoreView(this) adapter.addFooterView(loadMoreView) - loadMoreView.rotate_loading.show() + loadMoreView.startLoad() recycler_view.addOnScrollListener(object : RecyclerView.OnScrollListener() { override fun onScrolled(recyclerView: RecyclerView, dx: Int, dy: Int) { super.onScrolled(recyclerView, dx, dy) 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 8cb633963..d28050509 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 @@ -1,23 +1,20 @@ package io.legado.app.ui.rss.article import android.os.Bundle -import android.view.LayoutInflater import android.view.Menu import android.view.MenuItem -import android.view.View 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 import io.legado.app.lib.theme.ATH import io.legado.app.ui.rss.read.ReadRssActivity import io.legado.app.ui.rss.source.edit.RssSourceEditActivity +import io.legado.app.ui.widget.LoadMoreView import io.legado.app.utils.getViewModel import kotlinx.android.synthetic.main.activity_rss_artivles.* import kotlinx.android.synthetic.main.view_load_more.view.* @@ -26,26 +23,22 @@ import org.jetbrains.anko.startActivity import org.jetbrains.anko.startActivityForResult class RssArticlesActivity : VMBaseActivity(R.layout.activity_rss_artivles), + RssArticlesViewModel.CallBack, RssArticlesAdapter.CallBack { override val viewModel: RssArticlesViewModel get() = getViewModel(RssArticlesViewModel::class.java) + override lateinit var adapter: RssArticlesAdapter private val editSource = 12319 - private var adapter: RssArticlesAdapter? = null - private var rssArticlesData: LiveData>? = null - private var url: String? = null - private lateinit var loadMoreView: View + private lateinit var loadMoreView: LoadMoreView override fun onActivityCreated(savedInstanceState: Bundle?) { - initView() + viewModel.callBack = this viewModel.titleLiveData.observe(this, Observer { title_bar.title = it }) - url = intent.getStringExtra("url") - url?.let { - initData(it) - } + initView() refresh_recycler_view.startLoading() } @@ -60,11 +53,9 @@ class RssArticlesActivity : VMBaseActivity(R.layout.activi startActivityForResult(editSource, Pair("data", it)) } R.id.menu_clear -> { - intent.getStringExtra("url")?.let { + viewModel.url?.let { refresh_progress_bar.isAutoLoading = true - viewModel.clear(it) { - refresh_progress_bar.isAutoLoading = false - } + viewModel.clear() } } } @@ -82,13 +73,11 @@ class RssArticlesActivity : VMBaseActivity(R.layout.activi }) adapter = RssArticlesAdapter(this, this) recycler_view.adapter = adapter + loadMoreView = LoadMoreView(this) + adapter.addFooterView(loadMoreView) refresh_recycler_view.onRefreshStart = { - url?.let { - viewModel.loadContent(it, { - refresh_progress_bar.isAutoLoading = false - }, { - addLoadMoreView() - }) + viewModel.url?.let { + viewModel.loadContent() } } recycler_view.addOnScrollListener(object : RecyclerView.OnScrollListener() { @@ -101,27 +90,19 @@ 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 scrollToBottom() { + if (viewModel.isLoading) return + if (loadMoreView.hasMore && adapter.getActualItemCount() > 0) { + loadMoreView.rotate_loading.show() + viewModel.loadContent() } } - private fun initData(origin: String) { - rssArticlesData?.removeObservers(this) - rssArticlesData = App.db.rssArticleDao().liveByOrigin(origin) - rssArticlesData?.observe(this, Observer { - adapter?.setItems(it) - }) - } - - private fun scrollToBottom() { - adapter?.let { - if (it.getActualItemCount() > 0) { - loadMoreView.rotate_loading.show() - } + override fun loadFinally() { + if (viewModel.hasMore) { + loadMoreView.startLoad() + } else { + loadMoreView.noMore() } } 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 4640909e1..5ad2a3ef2 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 @@ -1,44 +1,58 @@ package io.legado.app.ui.rss.article import android.app.Application +import android.content.Intent import androidx.lifecycle.MutableLiveData import io.legado.app.App 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 -import kotlinx.coroutines.withContext class RssArticlesViewModel(application: Application) : BaseViewModel(application) { + var callBack: CallBack? = null + var url: String? = null var rssSource: RssSource? = null val titleLiveData = MutableLiveData() + private val articles = linkedSetOf() + var isLoading = true var page = 1 + var hasMore = true - fun loadContent(url: String, onFinally: () -> Unit, loadMore: (() -> Unit)? = null) { + fun initData(intent: Intent) { execute { - rssSource = App.db.rssSourceDao().getByKey(url) - rssSource?.let { - titleLiveData.postValue(it.sourceName) - } ?: let { - rssSource = RssSource(sourceUrl = url) + url = intent.getStringExtra("url") + url?.let { url -> + rssSource = App.db.rssSourceDao().getByKey(url) + rssSource?.let { + titleLiveData.postValue(it.sourceName) + } ?: let { + rssSource = RssSource(sourceUrl = url) + } } - rssSource?.let { rssSource -> - Rss.getArticles(rssSource, page, this) - .onSuccess { - it?.let { - withContext(IO) { - App.db.rssArticleDao().insert(*it.toTypedArray()) - } - loadMore?.invoke() + } + } + + fun loadContent() { + isLoading = true + rssSource?.let { rssSource -> + Rss.getArticles(rssSource, page, this) + .onSuccess { + it?.let { + val oldSize = articles.size + articles.addAll(it) + if (articles.size == oldSize) { + hasMore = false + } else { + callBack?.adapter?.setItems(articles.toList()) } - }.onError { - toast(it.localizedMessage) - }.onFinally { - onFinally() + page++ + isLoading = false } - } + }.onError { + toast(it.localizedMessage) + } } } @@ -49,15 +63,14 @@ class RssArticlesViewModel(application: Application) : BaseViewModel(application } } - fun clear(url: String, onFinally: () -> Unit) { - execute { - App.db.rssArticleDao().delete(url) - page = 1 - loadContent(url, onFinally) - } + fun clear() { + page = 1 + articles.clear() + loadContent() } - fun loadMore() { - + interface CallBack { + var adapter: RssArticlesAdapter + fun loadFinally() } } \ No newline at end of file