pull/49/head
kunfei 5 years ago
parent 91a56b2943
commit 2155812caf
  1. 3
      app/src/main/java/io/legado/app/ui/book/search/SearchActivity.kt
  2. 3
      app/src/main/java/io/legado/app/ui/explore/ExploreShowActivity.kt
  3. 63
      app/src/main/java/io/legado/app/ui/rss/article/RssArticlesActivity.kt
  4. 71
      app/src/main/java/io/legado/app/ui/rss/article/RssArticlesViewModel.kt

@ -171,7 +171,7 @@ class SearchActivity : VMBaseActivity<SearchViewModel>(R.layout.activity_book_se
} }
private fun scrollToBottom() { private fun scrollToBottom() {
if (!viewModel.isLoading && viewModel.searchKey.isNotEmpty()) { if (!viewModel.isLoading && viewModel.searchKey.isNotEmpty() && loadMoreView.hasMore) {
viewModel.search("") viewModel.search("")
} }
} }
@ -228,6 +228,7 @@ class SearchActivity : VMBaseActivity<SearchViewModel>(R.layout.activity_book_se
override fun searchFinally() { override fun searchFinally() {
refresh_progress_bar.isAutoLoading = false refresh_progress_bar.isAutoLoading = false
loadMoreView.startLoad()
fb_stop.invisible() fb_stop.invisible()
} }

@ -14,7 +14,6 @@ import io.legado.app.ui.book.info.BookInfoActivity
import io.legado.app.ui.widget.LoadMoreView import io.legado.app.ui.widget.LoadMoreView
import io.legado.app.utils.getViewModel import io.legado.app.utils.getViewModel
import kotlinx.android.synthetic.main.activity_explore_show.* import kotlinx.android.synthetic.main.activity_explore_show.*
import kotlinx.android.synthetic.main.view_load_more.view.*
import org.jetbrains.anko.startActivity import org.jetbrains.anko.startActivity
class ExploreShowActivity : VMBaseActivity<ExploreShowViewModel>(R.layout.activity_explore_show), class ExploreShowActivity : VMBaseActivity<ExploreShowViewModel>(R.layout.activity_explore_show),
@ -40,7 +39,7 @@ class ExploreShowActivity : VMBaseActivity<ExploreShowViewModel>(R.layout.activi
recycler_view.adapter = adapter recycler_view.adapter = adapter
loadMoreView = LoadMoreView(this) loadMoreView = LoadMoreView(this)
adapter.addFooterView(loadMoreView) adapter.addFooterView(loadMoreView)
loadMoreView.rotate_loading.show() loadMoreView.startLoad()
recycler_view.addOnScrollListener(object : RecyclerView.OnScrollListener() { recycler_view.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)

@ -1,23 +1,20 @@
package io.legado.app.ui.rss.article package io.legado.app.ui.rss.article
import android.os.Bundle import android.os.Bundle
import android.view.LayoutInflater
import android.view.Menu import android.view.Menu
import android.view.MenuItem import android.view.MenuItem
import android.view.View
import androidx.core.content.ContextCompat import androidx.core.content.ContextCompat
import androidx.lifecycle.LiveData
import androidx.lifecycle.Observer import androidx.lifecycle.Observer
import androidx.recyclerview.widget.DividerItemDecoration import androidx.recyclerview.widget.DividerItemDecoration
import androidx.recyclerview.widget.LinearLayoutManager import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView import androidx.recyclerview.widget.RecyclerView
import io.legado.app.App
import io.legado.app.R import io.legado.app.R
import io.legado.app.base.VMBaseActivity import io.legado.app.base.VMBaseActivity
import io.legado.app.data.entities.RssArticle import io.legado.app.data.entities.RssArticle
import io.legado.app.lib.theme.ATH import io.legado.app.lib.theme.ATH
import io.legado.app.ui.rss.read.ReadRssActivity import io.legado.app.ui.rss.read.ReadRssActivity
import io.legado.app.ui.rss.source.edit.RssSourceEditActivity import io.legado.app.ui.rss.source.edit.RssSourceEditActivity
import io.legado.app.ui.widget.LoadMoreView
import io.legado.app.utils.getViewModel import io.legado.app.utils.getViewModel
import kotlinx.android.synthetic.main.activity_rss_artivles.* import kotlinx.android.synthetic.main.activity_rss_artivles.*
import kotlinx.android.synthetic.main.view_load_more.view.* import kotlinx.android.synthetic.main.view_load_more.view.*
@ -26,26 +23,22 @@ import org.jetbrains.anko.startActivity
import org.jetbrains.anko.startActivityForResult import org.jetbrains.anko.startActivityForResult
class RssArticlesActivity : VMBaseActivity<RssArticlesViewModel>(R.layout.activity_rss_artivles), class RssArticlesActivity : VMBaseActivity<RssArticlesViewModel>(R.layout.activity_rss_artivles),
RssArticlesViewModel.CallBack,
RssArticlesAdapter.CallBack { RssArticlesAdapter.CallBack {
override val viewModel: RssArticlesViewModel override val viewModel: RssArticlesViewModel
get() = getViewModel(RssArticlesViewModel::class.java) get() = getViewModel(RssArticlesViewModel::class.java)
override lateinit var adapter: RssArticlesAdapter
private val editSource = 12319 private val editSource = 12319
private var adapter: RssArticlesAdapter? = null private lateinit var loadMoreView: LoadMoreView
private var rssArticlesData: LiveData<List<RssArticle>>? = null
private var url: String? = null
private lateinit var loadMoreView: View
override fun onActivityCreated(savedInstanceState: Bundle?) { override fun onActivityCreated(savedInstanceState: Bundle?) {
initView() viewModel.callBack = this
viewModel.titleLiveData.observe(this, Observer { viewModel.titleLiveData.observe(this, Observer {
title_bar.title = it title_bar.title = it
}) })
url = intent.getStringExtra("url") initView()
url?.let {
initData(it)
}
refresh_recycler_view.startLoading() refresh_recycler_view.startLoading()
} }
@ -60,11 +53,9 @@ class RssArticlesActivity : VMBaseActivity<RssArticlesViewModel>(R.layout.activi
startActivityForResult<RssSourceEditActivity>(editSource, Pair("data", it)) startActivityForResult<RssSourceEditActivity>(editSource, Pair("data", it))
} }
R.id.menu_clear -> { R.id.menu_clear -> {
intent.getStringExtra("url")?.let { viewModel.url?.let {
refresh_progress_bar.isAutoLoading = true refresh_progress_bar.isAutoLoading = true
viewModel.clear(it) { viewModel.clear()
refresh_progress_bar.isAutoLoading = false
}
} }
} }
} }
@ -82,13 +73,11 @@ class RssArticlesActivity : VMBaseActivity<RssArticlesViewModel>(R.layout.activi
}) })
adapter = RssArticlesAdapter(this, this) adapter = RssArticlesAdapter(this, this)
recycler_view.adapter = adapter recycler_view.adapter = adapter
loadMoreView = LoadMoreView(this)
adapter.addFooterView(loadMoreView)
refresh_recycler_view.onRefreshStart = { refresh_recycler_view.onRefreshStart = {
url?.let { viewModel.url?.let {
viewModel.loadContent(it, { viewModel.loadContent()
refresh_progress_bar.isAutoLoading = false
}, {
addLoadMoreView()
})
} }
} }
recycler_view.addOnScrollListener(object : RecyclerView.OnScrollListener() { recycler_view.addOnScrollListener(object : RecyclerView.OnScrollListener() {
@ -101,27 +90,19 @@ class RssArticlesActivity : VMBaseActivity<RssArticlesViewModel>(R.layout.activi
}) })
} }
private fun addLoadMoreView() { private fun scrollToBottom() {
if (adapter?.getFooterCount() == 0) { if (viewModel.isLoading) return
loadMoreView = if (loadMoreView.hasMore && adapter.getActualItemCount() > 0) {
LayoutInflater.from(this).inflate(R.layout.view_load_more, recycler_view, false) loadMoreView.rotate_loading.show()
adapter?.addFooterView(loadMoreView) viewModel.loadContent()
} }
} }
private fun initData(origin: String) { override fun loadFinally() {
rssArticlesData?.removeObservers(this) if (viewModel.hasMore) {
rssArticlesData = App.db.rssArticleDao().liveByOrigin(origin) loadMoreView.startLoad()
rssArticlesData?.observe(this, Observer { } else {
adapter?.setItems(it) loadMoreView.noMore()
})
}
private fun scrollToBottom() {
adapter?.let {
if (it.getActualItemCount() > 0) {
loadMoreView.rotate_loading.show()
}
} }
} }

@ -1,44 +1,58 @@
package io.legado.app.ui.rss.article package io.legado.app.ui.rss.article
import android.app.Application import android.app.Application
import android.content.Intent
import androidx.lifecycle.MutableLiveData import androidx.lifecycle.MutableLiveData
import io.legado.app.App import io.legado.app.App
import io.legado.app.base.BaseViewModel import io.legado.app.base.BaseViewModel
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 import io.legado.app.model.Rss
import kotlinx.coroutines.Dispatchers.IO
import kotlinx.coroutines.withContext
class RssArticlesViewModel(application: Application) : BaseViewModel(application) { class RssArticlesViewModel(application: Application) : BaseViewModel(application) {
var callBack: CallBack? = null
var url: String? = null
var rssSource: RssSource? = null var rssSource: RssSource? = null
val titleLiveData = MutableLiveData<String>() val titleLiveData = MutableLiveData<String>()
private val articles = linkedSetOf<RssArticle>()
var isLoading = true
var page = 1 var page = 1
var hasMore = true
fun loadContent(url: String, onFinally: () -> Unit, loadMore: (() -> Unit)? = null) { fun initData(intent: Intent) {
execute { execute {
rssSource = App.db.rssSourceDao().getByKey(url) url = intent.getStringExtra("url")
rssSource?.let { url?.let { url ->
titleLiveData.postValue(it.sourceName) rssSource = App.db.rssSourceDao().getByKey(url)
} ?: let { rssSource?.let {
rssSource = RssSource(sourceUrl = url) titleLiveData.postValue(it.sourceName)
} ?: let {
rssSource = RssSource(sourceUrl = url)
}
} }
rssSource?.let { rssSource -> }
Rss.getArticles(rssSource, page, this) }
.onSuccess {
it?.let { fun loadContent() {
withContext(IO) { isLoading = true
App.db.rssArticleDao().insert(*it.toTypedArray()) rssSource?.let { rssSource ->
} Rss.getArticles(rssSource, page, this)
loadMore?.invoke() .onSuccess {
it?.let {
val oldSize = articles.size
articles.addAll(it)
if (articles.size == oldSize) {
hasMore = false
} else {
callBack?.adapter?.setItems(articles.toList())
} }
}.onError { page++
toast(it.localizedMessage) isLoading = false
}.onFinally {
onFinally()
} }
} }.onError {
toast(it.localizedMessage)
}
} }
} }
@ -49,15 +63,14 @@ class RssArticlesViewModel(application: Application) : BaseViewModel(application
} }
} }
fun clear(url: String, onFinally: () -> Unit) { fun clear() {
execute { page = 1
App.db.rssArticleDao().delete(url) articles.clear()
page = 1 loadContent()
loadContent(url, onFinally)
}
} }
fun loadMore() { interface CallBack {
var adapter: RssArticlesAdapter
fun loadFinally()
} }
} }
Loading…
Cancel
Save