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,17 +16,27 @@ class BeersRepository {
}
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
val call = RetrofitProvider.getClient.getBeers(page, filter.brewedBefore, filter.brewedAfter)
call.enqueue(object: Callback<List<Beer>> {
override fun onResponse(call: Call<List<Beer>>, response: Response<List<Beer>>) {
if (response.body() != null)
listener?.onBeersObtained(response.body()!!)
else
if (response.body() != null) {
if (response.body()!!.isEmpty())
more = false
listener?.onBeersObtained(response.body()!!, more)
} else {
listener?.onFailure()
}
}
override fun onFailure(call: Call<List<Beer>>, t: Throwable) {
if (t.message != null)
@ -36,8 +46,16 @@ class BeersRepository {
})
}
/**
* Discard page and restarts from first
*/
fun reset() {
page = 1
more = true
}
interface Listener {
fun onBeersObtained(beers: List<Beer>)
fun onBeersObtained(beers: List<Beer>, more: Boolean)
fun onFailure()
}
}

View File

@ -15,7 +15,12 @@ import it.danieleverducci.subitobeers.entities.Beer
class BeerRecyclerAdapter : RecyclerView.Adapter<BeerRecyclerAdapter.ViewHolder>() {
// The click event listener
var listener: Listener? = null
// Whether there are more items in next page
private var more = true
private val items: ArrayList<Beer> = ArrayList()
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
@ -58,7 +63,7 @@ class BeerRecyclerAdapter : RecyclerView.Adapter<BeerRecyclerAdapter.ViewHolder>
.into(holder.pic)
// If we are drawing the last element, notify
if (position == items.size - 1) {
if (position == items.size - 1 && more) {
holder.progress.visibility = View.VISIBLE
listener?.OnLastItemScrolled()
} 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)
notifyDataSetChanged()
}
/**
* Clears the list and updates the view
*/
fun clear() {
items.clear()
more = true
notifyDataSetChanged()
}

View File

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