This commit is contained in:
kunfei
2022-10-06 22:54:14 +08:00
parent 0ec3d76d5a
commit 3d79faceda
4 changed files with 39 additions and 19 deletions

View File

@@ -42,10 +42,8 @@ class ExploreShowActivity : VMBaseActivity<ActivityExploreShowBinding, ExploreSh
loadMoreView.startLoad()
loadMoreView.setOnClickListener {
if (!loadMoreView.isLoading) {
if (!loadMoreView.showErrorDialog()) {
loadMoreView.hasMore()
scrollToBottom()
}
loadMoreView.hasMore()
scrollToBottom()
}
}
binding.recyclerView.addOnScrollListener(object : RecyclerView.OnScrollListener() {

View File

@@ -62,6 +62,11 @@ class RssArticlesFragment() : VMBaseFragment<RssArticlesViewModel>(R.layout.frag
private fun initView() = binding.run {
refreshLayout.setColorSchemeColors(accentColor)
recyclerView.setEdgeEffectColor(primaryColor)
loadMoreView.setOnClickListener {
if (!loadMoreView.isLoading) {
scrollToBottom()
}
}
recyclerView.layoutManager = if (activityViewModel.isGridLayout) {
recyclerView.setPadding(8, 0, 8, 0)
GridLayoutManager(requireContext(), 2)
@@ -117,7 +122,10 @@ class RssArticlesFragment() : VMBaseFragment<RssArticlesViewModel>(R.layout.frag
}
override fun observeLiveBus() {
viewModel.loadFinally.observe(viewLifecycleOwner) {
viewModel.loadErrorLiveData.observe(viewLifecycleOwner) {
loadMoreView.error(it)
}
viewModel.loadFinallyLiveData.observe(viewLifecycleOwner) {
binding.refreshLayout.isRefreshing = false
if (it) {
loadMoreView.startLoad()

View File

@@ -10,15 +10,14 @@ import io.legado.app.data.appDb
import io.legado.app.data.entities.RssArticle
import io.legado.app.data.entities.RssSource
import io.legado.app.model.rss.Rss
import io.legado.app.utils.printOnDebug
import io.legado.app.utils.toastOnUi
import io.legado.app.utils.stackTraceStr
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
class RssArticlesViewModel(application: Application) : BaseViewModel(application) {
val loadFinally = MutableLiveData<Boolean>()
val loadFinallyLiveData = MutableLiveData<Boolean>()
val loadErrorLiveData = MutableLiveData<String>()
var isLoading = true
var order = System.currentTimeMillis()
private var nextPageUrl: String? = null
@@ -46,18 +45,18 @@ class RssArticlesViewModel(application: Application) : BaseViewModel(application
appDb.rssArticleDao.insert(*list.toTypedArray())
if (!rssSource.ruleNextPage.isNullOrEmpty()) {
appDb.rssArticleDao.clearOld(rssSource.sourceUrl, sortName, order)
loadFinally.postValue(true)
loadFinallyLiveData.postValue(true)
} else {
withContext(Dispatchers.Main) {
loadFinally.postValue(false)
loadFinallyLiveData.postValue(false)
}
}
isLoading = false
}
}.onError {
loadFinally.postValue(false)
loadFinallyLiveData.postValue(false)
AppLog.put("rss获取内容失败", it)
context.toastOnUi(it.localizedMessage)
loadErrorLiveData.postValue(it.stackTraceStr)
}
}
@@ -72,25 +71,26 @@ class RssArticlesViewModel(application: Application) : BaseViewModel(application
loadMoreSuccess(it.first)
}
.onError {
it.printOnDebug()
loadFinally.postValue(false)
loadFinallyLiveData.postValue(false)
AppLog.put("rss获取内容失败", it)
loadErrorLiveData.postValue(it.stackTraceStr)
}
} else {
loadFinally.postValue(false)
loadFinallyLiveData.postValue(false)
}
}
private fun loadMoreSuccess(articles: MutableList<RssArticle>) {
articles.let { list ->
if (list.isEmpty()) {
loadFinally.postValue(false)
loadFinallyLiveData.postValue(false)
return@let
}
val firstArticle = list.first()
val dbArticle = appDb.rssArticleDao
.get(firstArticle.origin, firstArticle.link)
if (dbArticle != null) {
loadFinally.postValue(false)
loadFinallyLiveData.postValue(false)
} else {
list.forEach { rssArticle ->
rssArticle.order = order--

View File

@@ -16,12 +16,26 @@ class LoadMoreView(context: Context, attrs: AttributeSet? = null) : FrameLayout(
private val binding = ViewLoadMoreBinding.inflate(LayoutInflater.from(context), this)
private var errorMsg = ""
private var onClickListener: OnClickListener? = null
var isLoading = false
private set
var hasMore = true
private set
init {
super.setOnClickListener {
if (!showErrorDialog()) {
onClickListener?.onClick(it)
}
}
}
override fun setOnClickListener(l: OnClickListener?) {
this.onClickListener = l
}
override fun onAttachedToWindow() {
super.onAttachedToWindow()
layoutParams.width = ViewGroup.LayoutParams.MATCH_PARENT
@@ -64,7 +78,7 @@ class LoadMoreView(context: Context, attrs: AttributeSet? = null) : FrameLayout(
binding.tvText.visible()
}
fun showErrorDialog(): Boolean {
private fun showErrorDialog(): Boolean {
if (errorMsg.isBlank()) {
return false
}