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 import it.danieleverducci.subitobeers.entities.Beer
/**
* Fragment navigation interface
*/
interface BeerNavigation { interface BeerNavigation {
fun showBeerDetail(beer: Beer) fun showBeerDetail(beer: Beer)
} }

View File

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

View File

@ -1,5 +1,8 @@
package it.danieleverducci.subitobeers package it.danieleverducci.subitobeers
/**
* Static configuration file
*/
object Config { object Config {
val API_BASEURL = "https://api.punkapi.com/" 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.entities.Beer
import it.danieleverducci.subitobeers.ui.BeerDetailFragment import it.danieleverducci.subitobeers.ui.BeerDetailFragment
/**
* App entrypoint
* An activity with two fragments (list + detail)
*/
class MainActivity : AppCompatActivity(), BeerNavigation { class MainActivity : AppCompatActivity(), BeerNavigation {
override fun onCreate(savedInstanceState: Bundle?) { override fun onCreate(savedInstanceState: Bundle?) {
@ -15,6 +19,10 @@ class MainActivity : AppCompatActivity(), BeerNavigation {
setContentView(R.layout.activity_main) setContentView(R.layout.activity_main)
} }
/**
* Show beer detail fragment
* @param beer to show in detail
*/
override fun showBeerDetail(beer: Beer) { override fun showBeerDetail(beer: Beer) {
val detailFragment = BeerDetailFragment(beer) val detailFragment = BeerDetailFragment(beer)
supportFragmentManager.beginTransaction() supportFragmentManager.beginTransaction()
@ -25,6 +33,7 @@ class MainActivity : AppCompatActivity(), BeerNavigation {
} }
override fun onBackPressed() { override fun onBackPressed() {
// Manage fragment navigation
val bsf = supportFragmentManager.backStackEntryCount val bsf = supportFragmentManager.backStackEntryCount
if(bsf == 0) { if(bsf == 0) {
super.onBackPressed() super.onBackPressed()

View File

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

View File

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

View File

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

View File

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

View File

@ -10,6 +10,9 @@ import it.danieleverducci.subitobeers.R
import it.danieleverducci.subitobeers.databinding.FragmentBeerDetailBinding import it.danieleverducci.subitobeers.databinding.FragmentBeerDetailBinding
import it.danieleverducci.subitobeers.entities.Beer import it.danieleverducci.subitobeers.entities.Beer
/**
* A simple beer detail fragment, with an image and some textual data
*/
class BeerDetailFragment(val beer: Beer) : Fragment() { class BeerDetailFragment(val beer: Beer) : Fragment() {
lateinit var binding: FragmentBeerDetailBinding 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.databinding.FragmentBeersListitemBinding
import it.danieleverducci.subitobeers.entities.Beer import it.danieleverducci.subitobeers.entities.Beer
/**
* RecyclerAdapter for beers list RecyclerView
*/
class BeerRecyclerAdapter : RecyclerView.Adapter<BeerRecyclerAdapter.ViewHolder>() { class BeerRecyclerAdapter : RecyclerView.Adapter<BeerRecyclerAdapter.ViewHolder>() {
// The click event listener // The click event listener
@ -102,6 +105,9 @@ class BeerRecyclerAdapter : RecyclerView.Adapter<BeerRecyclerAdapter.ViewHolder>
val progress: ProgressBar = binding.beerItemProgress val progress: ProgressBar = binding.beerItemProgress
} }
/**
* List event listener
*/
interface Listener { interface Listener {
fun OnItemClicked(item: Beer) fun OnItemClicked(item: Beer)
fun OnLastItemScrolled() fun OnLastItemScrolled()