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. 45
      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() {
if (!viewModel.isLoading && viewModel.searchKey.isNotEmpty()) {
if (!viewModel.isLoading && viewModel.searchKey.isNotEmpty() && loadMoreView.hasMore) {
viewModel.search("")
}
}
@ -228,6 +228,7 @@ class SearchActivity : VMBaseActivity<SearchViewModel>(R.layout.activity_book_se
override fun searchFinally() {
refresh_progress_bar.isAutoLoading = false
loadMoreView.startLoad()
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.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<ExploreShowViewModel>(R.layout.activity_explore_show),
@ -40,7 +39,7 @@ class ExploreShowActivity : VMBaseActivity<ExploreShowViewModel>(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)

@ -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<RssArticlesViewModel>(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<List<RssArticle>>? = 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<RssArticlesViewModel>(R.layout.activi
startActivityForResult<RssSourceEditActivity>(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<RssArticlesViewModel>(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<RssArticlesViewModel>(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)
rssArticlesData?.observe(this, Observer {
adapter?.setItems(it)
})
}
private fun scrollToBottom() {
adapter?.let {
if (it.getActualItemCount() > 0) {
if (viewModel.isLoading) return
if (loadMoreView.hasMore && adapter.getActualItemCount() > 0) {
loadMoreView.rotate_loading.show()
viewModel.loadContent()
}
}
override fun loadFinally() {
if (viewModel.hasMore) {
loadMoreView.startLoad()
} else {
loadMoreView.noMore()
}
}

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