This commit is contained in:
Daniele 2021-09-05 21:02:51 +02:00
parent ef9944e9ca
commit 0ac7627b0c
10 changed files with 53 additions and 0 deletions

View File

@ -2,6 +2,9 @@ package it.danieleverducci.subitobeers
import it.danieleverducci.subitobeers.entities.Beer
/**
* Fragment navigation interface
*/
interface BeerNavigation {
fun showBeerDetail(beer: Beer)
}

View File

@ -9,6 +9,10 @@ import retrofit2.Call
import retrofit2.Callback
import retrofit2.Response
/**
* Beers repository
* Allows to retrieve beers from Punk API via its Retrofit implementation
*/
class BeersRepository {
companion object {
@ -23,6 +27,7 @@ class BeersRepository {
/**
* Obtain a paginated list of beers from the server
* Use page property to set the page
* Async: will call listener's onResponse() or onFailure()
*/
fun getBeers() {
// Obtain beers
@ -54,6 +59,9 @@ class BeersRepository {
more = true
}
/**
* Listener for api callbacks
*/
interface Listener {
fun onBeersObtained(beers: List<Beer>, more: Boolean)
fun onFailure()

View File

@ -1,5 +1,8 @@
package it.danieleverducci.subitobeers
/**
* Static configuration file
*/
object Config {
val API_BASEURL = "https://api.punkapi.com/"
}

View File

@ -8,6 +8,10 @@ import androidx.fragment.app.FragmentManager
import it.danieleverducci.subitobeers.entities.Beer
import it.danieleverducci.subitobeers.ui.BeerDetailFragment
/**
* App entrypoint
* An activity with two fragments (list + detail)
*/
class MainActivity : AppCompatActivity(), BeerNavigation {
override fun onCreate(savedInstanceState: Bundle?) {
@ -15,6 +19,10 @@ class MainActivity : AppCompatActivity(), BeerNavigation {
setContentView(R.layout.activity_main)
}
/**
* Show beer detail fragment
* @param beer to show in detail
*/
override fun showBeerDetail(beer: Beer) {
val detailFragment = BeerDetailFragment(beer)
supportFragmentManager.beginTransaction()
@ -25,6 +33,7 @@ class MainActivity : AppCompatActivity(), BeerNavigation {
}
override fun onBackPressed() {
// Manage fragment navigation
val bsf = supportFragmentManager.backStackEntryCount
if(bsf == 0) {
super.onBackPressed()

View File

@ -2,6 +2,9 @@ package it.danieleverducci.subitobeers.entities
import com.google.gson.annotations.SerializedName
/**
* Represents a Punk API's (https://punkapi.com/documentation/v2) beer item
*/
class Beer {
val id: Int = 0
val name: String = ""

View File

@ -3,6 +3,9 @@ package it.danieleverducci.subitobeers.networking
import android.util.Log
import java.util.*
/**
* Class to represent a beer filter. Filters by brewing date.
*/
class BeersFilter {
// The date format is like "09/2007" (MM/YYYY). Month start from 1.
var brewedBefore: String? = null
@ -17,6 +20,9 @@ class BeersFilter {
val brewedAfterMonth: Int
get() = getMonth(brewedAfter)
/**
* Clears filter
*/
fun clear() {
brewedBefore = null
brewedAfter = null

View File

@ -5,11 +5,16 @@ import retrofit2.Call
import retrofit2.http.GET
import retrofit2.http.Query
/**
* Punk API description for Retrofit
*/
interface PunkAPI {
@GET("/v2/beers?per_page=10")
fun getBeers(
@Query("page") page: Int,
@Query("brewed_before") brewedBefore: String?,
@Query("brewed_after") brewedAfter: String?
): Call<List<Beer>>
}

View File

@ -7,7 +7,14 @@ import okhttp3.OkHttpClient
import retrofit2.Retrofit
import retrofit2.converter.gson.GsonConverterFactory
/**
* Provider for Punk API Retrofit implementation
*/
object RetrofitProvider {
/**
* Punk API Retrofit client
*/
val getClient: PunkAPI
get() {

View File

@ -10,6 +10,9 @@ import it.danieleverducci.subitobeers.R
import it.danieleverducci.subitobeers.databinding.FragmentBeerDetailBinding
import it.danieleverducci.subitobeers.entities.Beer
/**
* A simple beer detail fragment, with an image and some textual data
*/
class BeerDetailFragment(val beer: Beer) : Fragment() {
lateinit var binding: FragmentBeerDetailBinding

View File

@ -13,6 +13,9 @@ import it.danieleverducci.subitobeers.R
import it.danieleverducci.subitobeers.databinding.FragmentBeersListitemBinding
import it.danieleverducci.subitobeers.entities.Beer
/**
* RecyclerAdapter for beers list RecyclerView
*/
class BeerRecyclerAdapter : RecyclerView.Adapter<BeerRecyclerAdapter.ViewHolder>() {
// The click event listener
@ -102,6 +105,9 @@ class BeerRecyclerAdapter : RecyclerView.Adapter<BeerRecyclerAdapter.ViewHolder>
val progress: ProgressBar = binding.beerItemProgress
}
/**
* List event listener
*/
interface Listener {
fun OnItemClicked(item: Beer)
fun OnLastItemScrolled()