pull/2385/head
kunfei 2 years ago
parent 0ec3d76d5a
commit 3d79faceda
  1. 2
      app/src/main/java/io/legado/app/ui/book/explore/ExploreShowActivity.kt
  2. 10
      app/src/main/java/io/legado/app/ui/rss/article/RssArticlesFragment.kt
  3. 26
      app/src/main/java/io/legado/app/ui/rss/article/RssArticlesViewModel.kt
  4. 16
      app/src/main/java/io/legado/app/ui/widget/recycler/LoadMoreView.kt

@ -42,12 +42,10 @@ class ExploreShowActivity : VMBaseActivity<ActivityExploreShowBinding, ExploreSh
loadMoreView.startLoad() loadMoreView.startLoad()
loadMoreView.setOnClickListener { loadMoreView.setOnClickListener {
if (!loadMoreView.isLoading) { if (!loadMoreView.isLoading) {
if (!loadMoreView.showErrorDialog()) {
loadMoreView.hasMore() loadMoreView.hasMore()
scrollToBottom() scrollToBottom()
} }
} }
}
binding.recyclerView.addOnScrollListener(object : RecyclerView.OnScrollListener() { binding.recyclerView.addOnScrollListener(object : RecyclerView.OnScrollListener() {
override fun onScrolled(recyclerView: RecyclerView, dx: Int, dy: Int) { override fun onScrolled(recyclerView: RecyclerView, dx: Int, dy: Int) {
super.onScrolled(recyclerView, dx, dy) super.onScrolled(recyclerView, dx, dy)

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

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

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

Loading…
Cancel
Save