diff --git a/.idea/runConfigurations.xml b/.idea/runConfigurations.xml
new file mode 100644
index 0000000..797acea
--- /dev/null
+++ b/.idea/runConfigurations.xml
@@ -0,0 +1,10 @@
+
+
+
+
+
+
\ No newline at end of file
diff --git a/app/src/main/java/it/danieleverducci/subitobeers/BeersRepository.kt b/app/src/main/java/it/danieleverducci/subitobeers/BeersRepository.kt
index 187ca8b..65adb72 100644
--- a/app/src/main/java/it/danieleverducci/subitobeers/BeersRepository.kt
+++ b/app/src/main/java/it/danieleverducci/subitobeers/BeersRepository.kt
@@ -3,6 +3,7 @@ package it.danieleverducci.subitobeers
import android.util.Log
import android.widget.Toast
import it.danieleverducci.subitobeers.entities.Beer
+import it.danieleverducci.subitobeers.networking.BeersFilter
import it.danieleverducci.subitobeers.networking.RetrofitProvider
import retrofit2.Call
import retrofit2.Callback
@@ -16,9 +17,9 @@ class BeersRepository {
var listener: Listener? = null
- fun getBeers(page: Int) {
+ fun getBeers(page: Int, filter: BeersFilter) {
// Obtain beers
- val call = RetrofitProvider.getClient.getBeers(page)
+ 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)
diff --git a/app/src/main/java/it/danieleverducci/subitobeers/entities/Beer.kt b/app/src/main/java/it/danieleverducci/subitobeers/entities/Beer.kt
index 2bf3e4a..e7959a2 100644
--- a/app/src/main/java/it/danieleverducci/subitobeers/entities/Beer.kt
+++ b/app/src/main/java/it/danieleverducci/subitobeers/entities/Beer.kt
@@ -15,7 +15,7 @@ class Beer// Parse date
val description: String = ""
@SerializedName("image_url")
val imageUrl: String = ""
-// @SerializedName("first_brewed")
-// val firstBrewed: Date? = null
+ @SerializedName("first_brewed")
+ val firstBrewed: String = ""
}
\ No newline at end of file
diff --git a/app/src/main/java/it/danieleverducci/subitobeers/networking/BeersFilter.kt b/app/src/main/java/it/danieleverducci/subitobeers/networking/BeersFilter.kt
new file mode 100644
index 0000000..acb38eb
--- /dev/null
+++ b/app/src/main/java/it/danieleverducci/subitobeers/networking/BeersFilter.kt
@@ -0,0 +1,6 @@
+package it.danieleverducci.subitobeers.networking
+
+object BeersFilter {
+ var brewedBefore: String? = null
+ var brewedAfter: String? = null
+}
\ No newline at end of file
diff --git a/app/src/main/java/it/danieleverducci/subitobeers/networking/PunkAPI.kt b/app/src/main/java/it/danieleverducci/subitobeers/networking/PunkAPI.kt
index c26232d..a0596e7 100644
--- a/app/src/main/java/it/danieleverducci/subitobeers/networking/PunkAPI.kt
+++ b/app/src/main/java/it/danieleverducci/subitobeers/networking/PunkAPI.kt
@@ -8,6 +8,8 @@ import retrofit2.http.Query
interface PunkAPI {
@GET("/v2/beers?per_page=10")
fun getBeers(
- @Query("page") page: Int
+ @Query("page") page: Int,
+ @Query("brewed_before") brewedBefore: String?,
+ @Query("brewed_after") brewedAfter: String?
): Call>
}
\ No newline at end of file
diff --git a/app/src/main/java/it/danieleverducci/subitobeers/ui/BeerDetailFragment.kt b/app/src/main/java/it/danieleverducci/subitobeers/ui/BeerDetailFragment.kt
index be47e7f..a3d0715 100644
--- a/app/src/main/java/it/danieleverducci/subitobeers/ui/BeerDetailFragment.kt
+++ b/app/src/main/java/it/danieleverducci/subitobeers/ui/BeerDetailFragment.kt
@@ -34,7 +34,7 @@ class BeerDetailFragment(val beer: Beer) : Fragment() {
binding.beerDetailTitle.text = beer.name
binding.beerDetailShortdesc.text = beer.tagline
binding.beerDetailDesc.text = beer.description
-// binding.beerDetailFirstbrewed.text = beer.
+ binding.beerDetailFirstbrewed.text = beer.firstBrewed
return binding.root
}
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 574e3a2..eb4b2dc 100644
--- a/app/src/main/java/it/danieleverducci/subitobeers/ui/BeerRecyclerAdapter.kt
+++ b/app/src/main/java/it/danieleverducci/subitobeers/ui/BeerRecyclerAdapter.kt
@@ -50,6 +50,7 @@ class BeerRecyclerAdapter : RecyclerView.Adapter
// Fill layout with data
holder.name.text = item.name
holder.descr.text = item.tagline
+ holder.firstbrewed.text = item.firstBrewed
Picasso.get()
.load(item.imageUrl)
.placeholder(R.drawable.ic_launcher_foreground)
@@ -66,8 +67,13 @@ class BeerRecyclerAdapter : RecyclerView.Adapter
}
fun addItems(ni: List) {
- items.addAll(ni);
- notifyDataSetChanged();
+ items.addAll(ni)
+ notifyDataSetChanged()
+ }
+
+ fun clear() {
+ items.clear()
+ notifyDataSetChanged()
}
override fun getItemCount(): Int = items.size
@@ -76,6 +82,7 @@ class BeerRecyclerAdapter : RecyclerView.Adapter
val root: View = binding.root
val name: TextView = binding.beerItemName
val descr: TextView = binding.beerItemDescr
+ val firstbrewed: TextView = binding.beerItemFirstbrewed
val pic: ImageView = binding.beerItemPic
val progress: ProgressBar = binding.beerItemProgress
}
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 4eccec9..cae03e4 100644
--- a/app/src/main/java/it/danieleverducci/subitobeers/ui/BeersFragment.kt
+++ b/app/src/main/java/it/danieleverducci/subitobeers/ui/BeersFragment.kt
@@ -1,5 +1,7 @@
package it.danieleverducci.subitobeers.ui
+import android.app.DatePickerDialog
+import android.content.res.Resources
import android.os.Bundle
import android.view.*
import androidx.fragment.app.Fragment
@@ -11,16 +13,21 @@ import it.danieleverducci.subitobeers.BeersRepository
import it.danieleverducci.subitobeers.R
import it.danieleverducci.subitobeers.databinding.FragmentBeersListBinding
import it.danieleverducci.subitobeers.entities.Beer
+import it.danieleverducci.subitobeers.networking.BeersFilter
+import java.util.*
/**
* A fragment representing a list of Items.
*/
class BeersFragment : Fragment(), BeersRepository.Listener, BeerRecyclerAdapter.Listener {
+ enum class FILTERTYPE {SINCE, TO}
+
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?,
@@ -28,12 +35,17 @@ class BeersFragment : Fragment(), BeersRepository.Listener, BeerRecyclerAdapter.
): View? {
setHasOptionsMenu(true);
+ // Inflate layout
binding = FragmentBeersListBinding.inflate(
LayoutInflater.from(container!!.context),
container,
false
)
+ // Register filter buttons listeners
+ binding.listFilterSinceBt.setOnClickListener { onFilterButtonClicked(FILTERTYPE.SINCE) }
+ binding.listFilterToBt.setOnClickListener { onFilterButtonClicked(FILTERTYPE.TO) }
+
// Set the adapter
with(binding.list) {
layoutManager = LinearLayoutManager(context)
@@ -43,18 +55,13 @@ class BeersFragment : Fragment(), BeersRepository.Listener, BeerRecyclerAdapter.
// Register for recyclerview adapter events
rvAdapter.listener = this
- return binding.root
- }
-
- override fun onStart() {
- super.onStart()
-
- // Load beers from network
+ // Register for network completed events
repo.listener = this
- // TODO: evitare che ogni volta ricarichi gli stessi elementi
- page = 1
- repo.getBeers(page)
+ // Load first page
+ loadBeers(true)
+
+ return binding.root
}
override fun onCreateOptionsMenu(menu: Menu, inflater: MenuInflater) {
@@ -86,18 +93,58 @@ class BeersFragment : Fragment(), BeersRepository.Listener, BeerRecyclerAdapter.
throw IllegalStateException("Activity must implement BeerNavigation")
}
+ /**
+ * Called when a filter button is clicked
+ * @param type whether is start or end date
+ */
+ fun onFilterButtonClicked(type: FILTERTYPE) {
+ val dpd = DatePickerDialog(requireContext())
+ // Hide day selector
+// dpd.datePicker.findViewById(
+// Resources.getSystem().getIdentifier("day", "id", "android")
+// ).visibility = View.GONE
+ dpd.setOnDateSetListener(DatePickerDialog.OnDateSetListener { view, year, month, dayOfMonth ->
+ // On date chosen
+ val formattedDate = "${String.format("%02d", month)}-${String.format("%04d", year)}"
+ if (type == FILTERTYPE.SINCE)
+ filter.brewedAfter = formattedDate
+ else
+ filter.brewedBefore = formattedDate
+ // Update list
+ loadBeers(true)
+ })
+ dpd.show()
+ }
+
/**
* Called when last list item is displayed.
* Used to fetch more elements
*/
override fun OnLastItemScrolled() {
- page++;
- repo.getBeers(page)
+ page++
+ loadBeers(false)
}
+ /**
+ * Toggles beer filter visibility
+ */
private fun toggleFilter() {
binding.listFilterDropdown.visibility =
if (binding.listFilterDropdown.visibility == View.VISIBLE) View.GONE else View.VISIBLE
}
+ /**
+ * Loads beers from network from first page
+ * @param clear: clears the list and fetch from first element
+ */
+ private fun loadBeers(clear: Boolean) {
+ if (clear) {
+ rvAdapter.clear()
+ page = 1
+ }
+
+ // Load with filter
+ repo.getBeers(page, filter)
+ }
+
}
\ No newline at end of file
diff --git a/app/src/main/java/it/danieleverducci/subitobeers/ui/views/CustomMonthPickerDialog.kt b/app/src/main/java/it/danieleverducci/subitobeers/ui/views/CustomMonthPickerDialog.kt
deleted file mode 100644
index 393fa14..0000000
--- a/app/src/main/java/it/danieleverducci/subitobeers/ui/views/CustomMonthPickerDialog.kt
+++ /dev/null
@@ -1,11 +0,0 @@
-package it.danieleverducci.subitobeers.ui.views
-
-import android.app.DatePickerDialog
-import android.content.Context
-
-/**
- * Here I extend DatePickerDialog to hide the Day picker
- */
-class CustomMonthPickerDialog(context: Context) : DatePickerDialog(context) {
-
-}
\ No newline at end of file
diff --git a/app/src/main/res/drawable/ic_firstbrewed_list.xml b/app/src/main/res/drawable/ic_firstbrewed_list.xml
new file mode 100644
index 0000000..8facac5
--- /dev/null
+++ b/app/src/main/res/drawable/ic_firstbrewed_list.xml
@@ -0,0 +1,5 @@
+
+
+
diff --git a/app/src/main/res/layout/fragment_beers_list.xml b/app/src/main/res/layout/fragment_beers_list.xml
index fe78feb..ac1ace4 100644
--- a/app/src/main/res/layout/fragment_beers_list.xml
+++ b/app/src/main/res/layout/fragment_beers_list.xml
@@ -19,17 +19,19 @@
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
- android:layout_marginRight="10dp"
- android:text="00/0000"
- android:drawableLeft="@drawable/ic_firstbrewed"/>
+ android:layout_marginEnd="10dp"
+ android:text="@string/list_filter_brewed_after"
+ app:drawableLeftCompat="@drawable/ic_firstbrewed"
+ android:drawableTint="@color/white"/>
+ android:text="@string/list_filter_brewed_before"
+ app:drawableLeftCompat="@drawable/ic_firstbrewed"
+ android:drawableTint="@color/white"/>
diff --git a/app/src/main/res/layout/fragment_beers_listitem.xml b/app/src/main/res/layout/fragment_beers_listitem.xml
index b4b7bb0..86f5af3 100644
--- a/app/src/main/res/layout/fragment_beers_listitem.xml
+++ b/app/src/main/res/layout/fragment_beers_listitem.xml
@@ -42,15 +42,30 @@
android:layout_height="wrap_content"
android:layout_marginStart="20dp"
android:layout_marginEnd="10dp"
- android:layout_marginBottom="16dp"
android:ellipsize="end"
android:maxLines="3"
android:text="@string/placeholder_tagline"
- app:layout_constraintBottom_toBottomOf="@+id/beer_item_pic"
+ app:layout_constraintBottom_toTopOf="@+id/beer_item_firstbrewed"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@+id/beer_item_pic"
app:layout_constraintTop_toBottomOf="@+id/beer_item_name" />
+
+
-
\ No newline at end of file
diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml
index 68b316a..b94144c 100644
--- a/app/src/main/res/values/strings.xml
+++ b/app/src/main/res/values/strings.xml
@@ -3,6 +3,8 @@
Unable to retrieve beers from network
+ Brewed after
+ Brewed before