diff --git a/.idea/misc.xml b/.idea/misc.xml index 7657d6c..9442b32 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -5,6 +5,7 @@ + diff --git a/.idea/runConfigurations.xml b/.idea/runConfigurations.xml deleted file mode 100644 index 797acea..0000000 --- a/.idea/runConfigurations.xml +++ /dev/null @@ -1,10 +0,0 @@ - - - - - - \ 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 92d394d..910ca25 100644 --- a/app/src/main/java/it/danieleverducci/subitobeers/BeersRepository.kt +++ b/app/src/main/java/it/danieleverducci/subitobeers/BeersRepository.kt @@ -17,7 +17,7 @@ class BeersRepository { var listener: Listener? = null var page = 1 - var filter = BeersFilter + var filter = BeersFilter() private var more = true /** 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 e7959a2..20f17cb 100644 --- a/app/src/main/java/it/danieleverducci/subitobeers/entities/Beer.kt +++ b/app/src/main/java/it/danieleverducci/subitobeers/entities/Beer.kt @@ -1,14 +1,8 @@ package it.danieleverducci.subitobeers.entities import com.google.gson.annotations.SerializedName -import org.json.JSONException -import org.json.JSONObject -import java.time.LocalDate -import java.time.format.DateTimeFormatter -import java.util.* -class Beer// Parse date -@Throws(JSONException::class) constructor() { +class Beer { val id: Int = 0 val name: String = "" val tagline: String = "" @@ -17,5 +11,4 @@ class Beer// Parse date val imageUrl: String = "" @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 index acb38eb..12d2c99 100644 --- a/app/src/main/java/it/danieleverducci/subitobeers/networking/BeersFilter.kt +++ b/app/src/main/java/it/danieleverducci/subitobeers/networking/BeersFilter.kt @@ -1,6 +1,65 @@ package it.danieleverducci.subitobeers.networking -object BeersFilter { +import android.util.Log +import java.util.* + +class BeersFilter { + // The date format is like "09/2007" (MM/YYYY). Month start from 1. var brewedBefore: String? = null var brewedAfter: String? = null + + val brewedBeforeYear: Int + get() = getYear(brewedBefore) + val brewedAfterYear: Int + get() = getYear(brewedAfter) + val brewedBeforeMonth: Int + get() = getMonth(brewedBefore) + val brewedAfterMonth: Int + get() = getMonth(brewedAfter) + + fun clear() { + brewedBefore = null + brewedAfter = null + } + + /** + * Sets the end filter date. + * Uses Calendar month format (January is 0, December is 11). + * @param month Month starting from 0 + * @param year Year as 4-digit + */ + fun setBrewedBefore(month: Int, year: Int) { + brewedBefore = "${String.format("%02d", month + 1)}-${String.format("%04d", year)}" + } + + /** + * Sets the start filter date. + * Uses Calendar month format (January is 0, December is 11). + * @param month Month starting from 0 + * @param year Year as 4-digit + */ + fun setBrewedAfter(month: Int, year: Int) { + brewedAfter = "${String.format("%02d", month + 1)}-${String.format("%04d", year)}" + } + + /** + * Returns the selected year, if any, otherwise returns current year + * @param filter value String? + * @return the selected year + */ + private fun getYear(value: String?): Int { + if (value == null) return Calendar.getInstance().get(Calendar.YEAR) + return value.substring(3).toInt() + } + + /** + * Returns the selected month, if any, otherwise returns current month + * Uses Calendar month format (January is 0, December is 11) + * @param filter value String? + * @return the selected month + */ + private fun getMonth(value: String?): Int { + if (value == null) return Calendar.getInstance().get(Calendar.MONTH) + return value.substring(0,2).toInt() + } } \ No newline at end of file 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 5300217..b3b1c60 100644 --- a/app/src/main/java/it/danieleverducci/subitobeers/ui/BeersFragment.kt +++ b/app/src/main/java/it/danieleverducci/subitobeers/ui/BeersFragment.kt @@ -3,6 +3,7 @@ package it.danieleverducci.subitobeers.ui import android.app.DatePickerDialog import android.content.res.Resources import android.os.Bundle +import android.util.Log import android.view.* import androidx.fragment.app.Fragment import androidx.recyclerview.widget.LinearLayoutManager @@ -21,7 +22,7 @@ import java.util.* */ class BeersFragment : Fragment(), BeersRepository.Listener, BeerRecyclerAdapter.Listener { - enum class FILTERTYPE {SINCE, TO} + enum class FILTERTYPE {SINCE, TO, CLEAR} private val rvAdapter = BeerRecyclerAdapter() private val repo = BeersRepository() @@ -43,6 +44,7 @@ class BeersFragment : Fragment(), BeersRepository.Listener, BeerRecyclerAdapter. // Register filter buttons listeners binding.listFilterSinceBt.setOnClickListener { onFilterButtonClicked(FILTERTYPE.SINCE) } binding.listFilterToBt.setOnClickListener { onFilterButtonClicked(FILTERTYPE.TO) } + binding.listFilterClear.setOnClickListener { onFilterButtonClicked(FILTERTYPE.CLEAR) } // Set the adapter with(binding.list) { @@ -93,24 +95,34 @@ class BeersFragment : Fragment(), BeersRepository.Listener, BeerRecyclerAdapter. /** * Called when a filter button is clicked - * @param type whether is start or end date + * @param type whether is start or end date, or the filter has been cleared */ 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) - repo.filter.brewedAfter = formattedDate - else - repo.filter.brewedBefore = formattedDate + if (type == FILTERTYPE.CLEAR) { + repo.filter.clear() + loadBeers(true) + binding.listFilterSinceBt.setText(R.string.list_filter_brewed_after) + binding.listFilterToBt.setText(R.string.list_filter_brewed_before) + return + } + + val listener = DatePickerDialog.OnDateSetListener { view, selYear, selMonth, dayOfMonth -> + // On date chosen (month starts from 0) + + if (type == FILTERTYPE.SINCE) { + repo.filter.setBrewedAfter(selMonth, selYear) + binding.listFilterSinceBt.setText(repo.filter.brewedAfter) + } else { + repo.filter.setBrewedBefore(selMonth, selYear) + binding.listFilterToBt.setText(repo.filter.brewedBefore) + } + // Update list loadBeers(true) - }) + } + val year = if (type == FILTERTYPE.SINCE) repo.filter.brewedAfterYear else repo.filter.brewedBeforeYear + val month = if (type == FILTERTYPE.SINCE) repo.filter.brewedAfterMonth else repo.filter.brewedBeforeMonth + val dpd = DatePickerDialog(requireContext(), listener, year, month - 1,1) dpd.show() } diff --git a/app/src/main/res/drawable/ic_clear.xml b/app/src/main/res/drawable/ic_clear.xml new file mode 100644 index 0000000..16d6d37 --- /dev/null +++ b/app/src/main/res/drawable/ic_clear.xml @@ -0,0 +1,10 @@ + + + diff --git a/app/src/main/res/layout/fragment_beers_list.xml b/app/src/main/res/layout/fragment_beers_list.xml index ac1ace4..29c29b5 100644 --- a/app/src/main/res/layout/fragment_beers_list.xml +++ b/app/src/main/res/layout/fragment_beers_list.xml @@ -11,13 +11,15 @@ android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@drawable/filter_dropdown_rounded_background" - android:padding="10dp" + android:paddingLeft="10dp" + android:paddingTop="10dp" + android:paddingBottom="10dp" android:visibility="gone">