From 3d934da0062280643a44516bc46962685d007569 Mon Sep 17 00:00:00 2001 From: Daniele Date: Sun, 5 Sep 2021 09:15:40 +0200 Subject: [PATCH] Do not make request if there are no more items --- .../subitobeers/BeersRepository.kt | 28 +++++++++++++++---- .../subitobeers/ui/BeerRecyclerAdapter.kt | 19 +++++++++++-- .../subitobeers/ui/BeersFragment.kt | 16 +++++------ 3 files changed, 47 insertions(+), 16 deletions(-) diff --git a/app/src/main/java/it/danieleverducci/subitobeers/BeersRepository.kt b/app/src/main/java/it/danieleverducci/subitobeers/BeersRepository.kt index 65adb72..92d394d 100644 --- a/app/src/main/java/it/danieleverducci/subitobeers/BeersRepository.kt +++ b/app/src/main/java/it/danieleverducci/subitobeers/BeersRepository.kt @@ -16,16 +16,26 @@ 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> { override fun onResponse(call: Call>, response: Response>) { - 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>, t: Throwable) { @@ -36,8 +46,16 @@ class BeersRepository { }) } + /** + * Discard page and restarts from first + */ + fun reset() { + page = 1 + more = true + } + interface Listener { - fun onBeersObtained(beers: List) + fun onBeersObtained(beers: List, more: Boolean) fun onFailure() } } \ No newline at end of file diff --git a/app/src/main/java/it/danieleverducci/subitobeers/ui/BeerRecyclerAdapter.kt b/app/src/main/java/it/danieleverducci/subitobeers/ui/BeerRecyclerAdapter.kt index eb4b2dc..4919d62 100644 --- a/app/src/main/java/it/danieleverducci/subitobeers/ui/BeerRecyclerAdapter.kt +++ b/app/src/main/java/it/danieleverducci/subitobeers/ui/BeerRecyclerAdapter.kt @@ -15,7 +15,12 @@ import it.danieleverducci.subitobeers.entities.Beer class BeerRecyclerAdapter : RecyclerView.Adapter() { + // The click event listener var listener: Listener? = null + + // Whether there are more items in next page + private var more = true + private val items: ArrayList = ArrayList() override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder { @@ -58,7 +63,7 @@ class BeerRecyclerAdapter : RecyclerView.Adapter .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 } } - fun addItems(ni: List) { + /** + * 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, more: Boolean) { + this.more = more items.addAll(ni) notifyDataSetChanged() } + /** + * Clears the list and updates the view + */ fun clear() { items.clear() + more = true notifyDataSetChanged() } diff --git a/app/src/main/java/it/danieleverducci/subitobeers/ui/BeersFragment.kt b/app/src/main/java/it/danieleverducci/subitobeers/ui/BeersFragment.kt index cae03e4..5300217 100644 --- a/app/src/main/java/it/danieleverducci/subitobeers/ui/BeersFragment.kt +++ b/app/src/main/java/it/danieleverducci/subitobeers/ui/BeersFragment.kt @@ -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) { - rvAdapter.addItems(beers) + override fun onBeersObtained(beers: List, 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() } } \ No newline at end of file