Refinements in filter

This commit is contained in:
Daniele 2021-09-05 10:57:16 +02:00
parent 3d934da006
commit 7114c6199a
11 changed files with 119 additions and 40 deletions

View File

@ -5,6 +5,7 @@
<map> <map>
<entry key="app/src/main/res/layout/activity_main.xml" value="0.5307291666666667" /> <entry key="app/src/main/res/layout/activity_main.xml" value="0.5307291666666667" />
<entry key="app/src/main/res/layout/fragment_beer_detail.xml" value="0.536" /> <entry key="app/src/main/res/layout/fragment_beer_detail.xml" value="0.536" />
<entry key="app/src/main/res/layout/fragment_beers_list.xml" value="0.634963768115942" />
<entry key="app/src/main/res/layout/fragment_beers_listitem.xml" value="0.5307291666666667" /> <entry key="app/src/main/res/layout/fragment_beers_listitem.xml" value="0.5307291666666667" />
</map> </map>
</option> </option>

View File

@ -1,10 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="RunConfigurationProducerService">
<option name="ignoredProducers">
<set>
<option value="com.android.tools.idea.compose.preview.runconfiguration.ComposePreviewRunConfigurationProducer" />
</set>
</option>
</component>
</project>

View File

@ -17,7 +17,7 @@ class BeersRepository {
var listener: Listener? = null var listener: Listener? = null
var page = 1 var page = 1
var filter = BeersFilter var filter = BeersFilter()
private var more = true private var more = true
/** /**

View File

@ -1,14 +1,8 @@
package it.danieleverducci.subitobeers.entities package it.danieleverducci.subitobeers.entities
import com.google.gson.annotations.SerializedName 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 class Beer {
@Throws(JSONException::class) constructor() {
val id: Int = 0 val id: Int = 0
val name: String = "" val name: String = ""
val tagline: String = "" val tagline: String = ""
@ -17,5 +11,4 @@ class Beer// Parse date
val imageUrl: String = "" val imageUrl: String = ""
@SerializedName("first_brewed") @SerializedName("first_brewed")
val firstBrewed: String = "" val firstBrewed: String = ""
} }

View File

@ -1,6 +1,65 @@
package it.danieleverducci.subitobeers.networking 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 brewedBefore: String? = null
var brewedAfter: 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()
}
} }

View File

@ -3,6 +3,7 @@ package it.danieleverducci.subitobeers.ui
import android.app.DatePickerDialog import android.app.DatePickerDialog
import android.content.res.Resources import android.content.res.Resources
import android.os.Bundle import android.os.Bundle
import android.util.Log
import android.view.* import android.view.*
import androidx.fragment.app.Fragment import androidx.fragment.app.Fragment
import androidx.recyclerview.widget.LinearLayoutManager import androidx.recyclerview.widget.LinearLayoutManager
@ -21,7 +22,7 @@ import java.util.*
*/ */
class BeersFragment : Fragment(), BeersRepository.Listener, BeerRecyclerAdapter.Listener { class BeersFragment : Fragment(), BeersRepository.Listener, BeerRecyclerAdapter.Listener {
enum class FILTERTYPE {SINCE, TO} enum class FILTERTYPE {SINCE, TO, CLEAR}
private val rvAdapter = BeerRecyclerAdapter() private val rvAdapter = BeerRecyclerAdapter()
private val repo = BeersRepository() private val repo = BeersRepository()
@ -43,6 +44,7 @@ class BeersFragment : Fragment(), BeersRepository.Listener, BeerRecyclerAdapter.
// Register filter buttons listeners // Register filter buttons listeners
binding.listFilterSinceBt.setOnClickListener { onFilterButtonClicked(FILTERTYPE.SINCE) } binding.listFilterSinceBt.setOnClickListener { onFilterButtonClicked(FILTERTYPE.SINCE) }
binding.listFilterToBt.setOnClickListener { onFilterButtonClicked(FILTERTYPE.TO) } binding.listFilterToBt.setOnClickListener { onFilterButtonClicked(FILTERTYPE.TO) }
binding.listFilterClear.setOnClickListener { onFilterButtonClicked(FILTERTYPE.CLEAR) }
// Set the adapter // Set the adapter
with(binding.list) { with(binding.list) {
@ -93,24 +95,34 @@ class BeersFragment : Fragment(), BeersRepository.Listener, BeerRecyclerAdapter.
/** /**
* Called when a filter button is clicked * 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) { fun onFilterButtonClicked(type: FILTERTYPE) {
val dpd = DatePickerDialog(requireContext()) if (type == FILTERTYPE.CLEAR) {
// Hide day selector repo.filter.clear()
// dpd.datePicker.findViewById<View>( loadBeers(true)
// Resources.getSystem().getIdentifier("day", "id", "android") binding.listFilterSinceBt.setText(R.string.list_filter_brewed_after)
// ).visibility = View.GONE binding.listFilterToBt.setText(R.string.list_filter_brewed_before)
dpd.setOnDateSetListener(DatePickerDialog.OnDateSetListener { view, year, month, dayOfMonth -> return
// On date chosen }
val formattedDate = "${String.format("%02d", month)}-${String.format("%04d", year)}"
if (type == FILTERTYPE.SINCE) val listener = DatePickerDialog.OnDateSetListener { view, selYear, selMonth, dayOfMonth ->
repo.filter.brewedAfter = formattedDate // On date chosen (month starts from 0)
else
repo.filter.brewedBefore = formattedDate 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 // Update list
loadBeers(true) 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() dpd.show()
} }

View File

@ -0,0 +1,10 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="24"
android:viewportHeight="24"
android:tint="?attr/colorControlNormal">
<path
android:fillColor="@android:color/white"
android:pathData="M19,6.41L17.59,5 12,10.59 6.41,5 5,6.41 10.59,12 5,17.59 6.41,19 12,13.41 17.59,19 19,17.59 13.41,12z"/>
</vector>

View File

@ -11,13 +11,15 @@
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:background="@drawable/filter_dropdown_rounded_background" android:background="@drawable/filter_dropdown_rounded_background"
android:padding="10dp" android:paddingLeft="10dp"
android:paddingTop="10dp"
android:paddingBottom="10dp"
android:visibility="gone"> android:visibility="gone">
<Button <Button
android:id="@+id/list_filter_since_bt" android:id="@+id/list_filter_since_bt"
android:layout_width="0dp" android:layout_width="0dp"
android:layout_height="wrap_content" android:layout_height="match_parent"
android:layout_weight="1" android:layout_weight="1"
android:layout_marginEnd="10dp" android:layout_marginEnd="10dp"
android:text="@string/list_filter_brewed_after" android:text="@string/list_filter_brewed_after"
@ -27,12 +29,23 @@
<Button <Button
android:id="@+id/list_filter_to_bt" android:id="@+id/list_filter_to_bt"
android:layout_width="0dp" android:layout_width="0dp"
android:layout_height="wrap_content" android:layout_height="match_parent"
android:layout_weight="1" android:layout_weight="1"
android:text="@string/list_filter_brewed_before" android:text="@string/list_filter_brewed_before"
app:drawableLeftCompat="@drawable/ic_firstbrewed" app:drawableLeftCompat="@drawable/ic_firstbrewed"
android:drawableTint="@color/white"/> android:drawableTint="@color/white"/>
<ImageButton
android:id="@+id/list_filter_clear"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="0"
android:paddingLeft="15dp"
android:paddingRight="15dp"
android:background="@color/transparent"
android:src="@drawable/ic_clear"
app:tint="@color/white"/>
</LinearLayout> </LinearLayout>
<androidx.recyclerview.widget.RecyclerView <androidx.recyclerview.widget.RecyclerView

View File

@ -7,4 +7,5 @@
<color name="secondary_700">#D54300</color> <color name="secondary_700">#D54300</color>
<color name="black">#FF000000</color> <color name="black">#FF000000</color>
<color name="white">#FFFFFFFF</color> <color name="white">#FFFFFFFF</color>
<color name="transparent">#0fff</color>
</resources> </resources>

View File

@ -6,7 +6,7 @@ buildscript {
mavenCentral() mavenCentral()
} }
dependencies { dependencies {
classpath "com.android.tools.build:gradle:4.2.2" classpath 'com.android.tools.build:gradle:7.0.2'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
// NOTE: Do not place your application dependencies here; they belong // NOTE: Do not place your application dependencies here; they belong

View File

@ -1,6 +1,6 @@
#Tue Aug 31 07:45:08 CEST 2021 #Tue Aug 31 07:45:08 CEST 2021
distributionBase=GRADLE_USER_HOME distributionBase=GRADLE_USER_HOME
distributionUrl=https\://services.gradle.org/distributions/gradle-6.7.1-bin.zip distributionUrl=https\://services.gradle.org/distributions/gradle-7.0.2-bin.zip
distributionPath=wrapper/dists distributionPath=wrapper/dists
zipStorePath=wrapper/dists zipStorePath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME zipStoreBase=GRADLE_USER_HOME