Daniele Verducci su MatissePenguin d470f7e968 WIP implementing infinte scroll
2021-08-31 21:20:58 +02:00

77 lines
1.9 KiB
Kotlin

package it.danieleverducci.subitobeers
import android.os.Bundle
import androidx.fragment.app.Fragment
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Toast
import it.danieleverducci.subitobeers.entities.Beer
/**
* A fragment representing a list of Items.
*/
class BeersFragment : Fragment(), BeersRepository.Listener, BeerRecyclerAdapter.Listener {
private val rvAdapter = BeerRecyclerAdapter()
private val repo = BeersRepository()
private var page = 1
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
val view = inflater.inflate(R.layout.fragment_beers_list, container, false)
// Set the adapter
if (view is RecyclerView) {
with(view) {
layoutManager = LinearLayoutManager(context)
adapter = rvAdapter
}
}
// Register for recyclerview adapter events
rvAdapter.listener = this
return view
}
override fun onStart() {
super.onStart()
// Load beers from network
repo.listener = this
// TODO: evitare che ogni volta ricarichi gli stessi elementi
page = 1
repo.getBeers(page)
}
override fun onBeersObtained(beers: List<Beer>) {
rvAdapter.addItems(beers)
}
override fun onFailure() {
Toast.makeText(context, R.string.network_error, Toast.LENGTH_SHORT).show()
}
/**
* Called when an item is clicked in the list
*/
override fun OnItemClicked(item: Beer) {
TODO("Not yet implemented")
}
/**
* Called when last list item is displayed.
* Used to fetch more elements
*/
override fun OnLastItemScrolled() {
page++;
repo.getBeers(page)
}
}