Do not make request if there are no more items

This commit is contained in:
Daniele 2021-09-05 09:15:40 +02:00
parent 91c4485265
commit 3d934da006
3 changed files with 47 additions and 16 deletions

View File

@ -16,16 +16,26 @@ class BeersRepository {
} }
var listener: Listener? = null var listener: Listener? = null
var page = 1
var filter = BeersFilter
private var more = true
fun getBeers(page: Int, filter: BeersFilter) { /**
* Obtain a paginated list of beers from the server
* Use page property to set the page
*/
fun getBeers() {
// Obtain beers // Obtain beers
val call = RetrofitProvider.getClient.getBeers(page, filter.brewedBefore, filter.brewedAfter) val call = RetrofitProvider.getClient.getBeers(page, filter.brewedBefore, filter.brewedAfter)
call.enqueue(object: Callback<List<Beer>> { call.enqueue(object: Callback<List<Beer>> {
override fun onResponse(call: Call<List<Beer>>, response: Response<List<Beer>>) { override fun onResponse(call: Call<List<Beer>>, response: Response<List<Beer>>) {
if (response.body() != null) if (response.body() != null) {
listener?.onBeersObtained(response.body()!!) if (response.body()!!.isEmpty())
else more = false
listener?.onBeersObtained(response.body()!!, more)
} else {
listener?.onFailure() listener?.onFailure()
}
} }
override fun onFailure(call: Call<List<Beer>>, t: Throwable) { override fun onFailure(call: Call<List<Beer>>, t: Throwable) {
@ -36,8 +46,16 @@ class BeersRepository {
}) })
} }
/**
* Discard page and restarts from first
*/
fun reset() {
page = 1
more = true
}
interface Listener { interface Listener {
fun onBeersObtained(beers: List<Beer>) fun onBeersObtained(beers: List<Beer>, more: Boolean)
fun onFailure() fun onFailure()
} }
} }

View File

@ -15,7 +15,12 @@ import it.danieleverducci.subitobeers.entities.Beer
class BeerRecyclerAdapter : RecyclerView.Adapter<BeerRecyclerAdapter.ViewHolder>() { class BeerRecyclerAdapter : RecyclerView.Adapter<BeerRecyclerAdapter.ViewHolder>() {
// The click event listener
var listener: Listener? = null var listener: Listener? = null
// Whether there are more items in next page
private var more = true
private val items: ArrayList<Beer> = ArrayList() private val items: ArrayList<Beer> = ArrayList()
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder { override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
@ -58,7 +63,7 @@ class BeerRecyclerAdapter : RecyclerView.Adapter<BeerRecyclerAdapter.ViewHolder>
.into(holder.pic) .into(holder.pic)
// If we are drawing the last element, notify // If we are drawing the last element, notify
if (position == items.size - 1) { if (position == items.size - 1 && more) {
holder.progress.visibility = View.VISIBLE holder.progress.visibility = View.VISIBLE
listener?.OnLastItemScrolled() listener?.OnLastItemScrolled()
} else { } else {
@ -66,13 +71,23 @@ class BeerRecyclerAdapter : RecyclerView.Adapter<BeerRecyclerAdapter.ViewHolder>
} }
} }
fun addItems(ni: List<Beer>) { /**
* Add items to the list and updates the view
* @param ni List of beers to be added
* @param more Whether there are more beers
*/
fun addItems(ni: List<Beer>, more: Boolean) {
this.more = more
items.addAll(ni) items.addAll(ni)
notifyDataSetChanged() notifyDataSetChanged()
} }
/**
* Clears the list and updates the view
*/
fun clear() { fun clear() {
items.clear() items.clear()
more = true
notifyDataSetChanged() notifyDataSetChanged()
} }

View File

@ -26,8 +26,6 @@ class BeersFragment : Fragment(), BeersRepository.Listener, BeerRecyclerAdapter.
private val rvAdapter = BeerRecyclerAdapter() private val rvAdapter = BeerRecyclerAdapter()
private val repo = BeersRepository() private val repo = BeersRepository()
lateinit var binding: FragmentBeersListBinding lateinit var binding: FragmentBeersListBinding
private var page = 1
private var filter = BeersFilter
override fun onCreateView( override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?, inflater: LayoutInflater, container: ViewGroup?,
@ -75,8 +73,8 @@ class BeersFragment : Fragment(), BeersRepository.Listener, BeerRecyclerAdapter.
return super.onOptionsItemSelected(item) return super.onOptionsItemSelected(item)
} }
override fun onBeersObtained(beers: List<Beer>) { override fun onBeersObtained(beers: List<Beer>, more: Boolean) {
rvAdapter.addItems(beers) rvAdapter.addItems(beers, more)
} }
override fun onFailure() { override fun onFailure() {
@ -107,9 +105,9 @@ class BeersFragment : Fragment(), BeersRepository.Listener, BeerRecyclerAdapter.
// On date chosen // On date chosen
val formattedDate = "${String.format("%02d", month)}-${String.format("%04d", year)}" val formattedDate = "${String.format("%02d", month)}-${String.format("%04d", year)}"
if (type == FILTERTYPE.SINCE) if (type == FILTERTYPE.SINCE)
filter.brewedAfter = formattedDate repo.filter.brewedAfter = formattedDate
else else
filter.brewedBefore = formattedDate repo.filter.brewedBefore = formattedDate
// Update list // Update list
loadBeers(true) loadBeers(true)
}) })
@ -121,7 +119,7 @@ class BeersFragment : Fragment(), BeersRepository.Listener, BeerRecyclerAdapter.
* Used to fetch more elements * Used to fetch more elements
*/ */
override fun OnLastItemScrolled() { override fun OnLastItemScrolled() {
page++ repo.page++
loadBeers(false) loadBeers(false)
} }
@ -140,11 +138,11 @@ class BeersFragment : Fragment(), BeersRepository.Listener, BeerRecyclerAdapter.
private fun loadBeers(clear: Boolean) { private fun loadBeers(clear: Boolean) {
if (clear) { if (clear) {
rvAdapter.clear() rvAdapter.clear()
page = 1 repo.reset()
} }
// Load with filter // Load with filter
repo.getBeers(page, filter) repo.getBeers()
} }
} }