Improve date filters management

feature/top10
Aurelien Hubert 7 years ago
parent d1600dd055
commit 80cfa5605c
  1. 24
      app/src/main/java/net/pokeranalytics/android/model/realm/Filter.kt
  2. 20
      app/src/main/java/net/pokeranalytics/android/model/realm/FilterCondition.kt
  3. 19
      app/src/main/java/net/pokeranalytics/android/ui/fragment/FilterDetailsFragment.kt
  4. 8
      app/src/main/java/net/pokeranalytics/android/ui/view/rowrepresentable/FilterElementRow.kt

@ -82,7 +82,6 @@ open class Filter : RealmObject() {
casted.addAll(this) casted.addAll(this)
filterConditions.add(FilterCondition(casted)) filterConditions.add(FilterCondition(casted))
} }
} }
} }
} }
@ -104,13 +103,32 @@ open class Filter : RealmObject() {
return filterElementRow.contains(filtered) return filterElementRow.contains(filtered)
} }
fun getValueForElement(filterElementRow: FilterElementRow): Any? { /**
* Set the saved value in the filter for the given [filterElementRow]
*/
fun setSavedValueForElement(filterElementRow: FilterElementRow) {
when (filterElementRow) {
is FilterElementRow.PastDays -> {
val values = getSavedValueForElement(filterElementRow) as Array<*>
if (values.isNotEmpty() && values.first() is Int) {
filterElementRow.lastDays = values.first() as Int
}
}
is FilterElementRow.From -> filterElementRow.date = getSavedValueForElement(filterElementRow) as Date? ?: Date()
is FilterElementRow.To -> filterElementRow.date = getSavedValueForElement(filterElementRow) as Date? ?: Date()
}
}
/**
* Get the saved value for the given [filterElementRow]
*/
private fun getSavedValueForElement(filterElementRow: FilterElementRow): Any? {
val filtered = filterConditions.filter { val filtered = filterConditions.filter {
it.filterName == filterElementRow.filterName it.filterName == filterElementRow.filterName
} }
if (filtered.isNotEmpty()) { if (filtered.isNotEmpty()) {
return filtered.first().getValue(filterElementRow) return filtered.first().getFilterConditionValue(filterElementRow)
} }
return null return null

@ -16,6 +16,10 @@ open class FilterCondition() : RealmObject() {
} }
constructor(filterElementRows: ArrayList<FilterElementRow>) : this(filterElementRows.first().filterName, filterElementRows.first().filterSectionRow.name) { constructor(filterElementRows: ArrayList<FilterElementRow>) : this(filterElementRows.first().filterName, filterElementRows.first().filterSectionRow.name) {
val filterName : String = this.filterName ?: throw PokerAnalyticsException.FilterElementUnknownName val filterName : String = this.filterName ?: throw PokerAnalyticsException.FilterElementUnknownName
this.stringValues = when (QueryCondition.valueOf(filterName)) { this.stringValues = when (QueryCondition.valueOf(filterName)) {
QueryCondition.GAME, QueryCondition.BANKROLL, QueryCondition.TOURNAMENT_NAME, QueryCondition.ALL_TOURNAMENT_FEATURES, QueryCondition.ANY_TOURNAMENT_FEATURES, QueryCondition.LOCATION -> { QueryCondition.GAME, QueryCondition.BANKROLL, QueryCondition.TOURNAMENT_NAME, QueryCondition.ALL_TOURNAMENT_FEATURES, QueryCondition.ANY_TOURNAMENT_FEATURES, QueryCondition.LOCATION -> {
@ -72,6 +76,13 @@ open class FilterCondition() : RealmObject() {
}) })
} }
} }
QueryCondition.PAST_DAYS -> {
RealmList<Double>().apply {
this.addAll(filterElementRows.map {
(it as FilterElementRow.PastDays).lastDays.toDouble()
})
}
}
else -> null else -> null
} }
@ -159,12 +170,13 @@ open class FilterCondition() : RealmObject() {
/** /**
* Return the value associated with the given filter element row * Return the value associated with the given [filterElementRow]
*/ */
fun getValue(filterElementRow: FilterElementRow): Any? { fun getFilterConditionValue(filterElementRow: FilterElementRow): Any? {
return when (filterElementRow) { return when (filterElementRow) {
is From, is To -> dateValue is From, is To -> date
else -> null is PastDays -> values
else -> throw PokerAnalyticsException.FilterElementTypeMissing
} }
} }

@ -25,6 +25,7 @@ import net.pokeranalytics.android.ui.view.RowViewType
import net.pokeranalytics.android.ui.view.rowrepresentable.FilterCategoryRow import net.pokeranalytics.android.ui.view.rowrepresentable.FilterCategoryRow
import net.pokeranalytics.android.ui.view.rowrepresentable.FilterElementRow import net.pokeranalytics.android.ui.view.rowrepresentable.FilterElementRow
import net.pokeranalytics.android.ui.view.rowrepresentable.FilterSectionRow import net.pokeranalytics.android.ui.view.rowrepresentable.FilterSectionRow
import net.pokeranalytics.android.util.NULL_TEXT
import net.pokeranalytics.android.util.extensions.shortDate import net.pokeranalytics.android.util.extensions.shortDate
import net.pokeranalytics.android.util.extensions.toMinutes import net.pokeranalytics.android.util.extensions.toMinutes
import timber.log.Timber import timber.log.Timber
@ -70,7 +71,11 @@ open class FilterDetailsFragment : PokerAnalyticsFragment(), StaticRowRepresenta
when (row) { when (row) {
is FilterElementRow.From -> DateTimePickerManager.create(requireContext(), row, this, row.date, onlyDate = true) is FilterElementRow.From -> DateTimePickerManager.create(requireContext(), row, this, row.date, onlyDate = true)
is FilterElementRow.To -> DateTimePickerManager.create(requireContext(), row, this, row.date, onlyDate = true) is FilterElementRow.To -> DateTimePickerManager.create(requireContext(), row, this, row.date, onlyDate = true)
is FilterElementRow.PastDays -> {
val pastDays = if (row.lastDays > 0) row.lastDays.toString() else ""
val data = row.editingDescriptors(mapOf("pastDays" to pastDays))
BottomSheetFragment.create(fragmentManager, row, this, data, true)
}
is FilterElementRow.DurationMoreThan -> { is FilterElementRow.DurationMoreThan -> {
val hours = if (row.minutes / 60 > 0) (row.minutes / 60).toString() else "" val hours = if (row.minutes / 60 > 0) (row.minutes / 60).toString() else ""
val minutes = if (row.minutes % 60 > 0) (row.minutes % 60).toString() else "" val minutes = if (row.minutes % 60 > 0) (row.minutes % 60).toString() else ""
@ -121,6 +126,7 @@ open class FilterDetailsFragment : PokerAnalyticsFragment(), StaticRowRepresenta
override fun stringForRow(row: RowRepresentable): String { override fun stringForRow(row: RowRepresentable): String {
return when (row) { return when (row) {
is FilterElementRow.PastDays -> if (row.lastDays > 0) row.lastDays.toString() else NULL_TEXT
is FilterElementRow.From -> row.date.shortDate() is FilterElementRow.From -> row.date.shortDate()
is FilterElementRow.To -> row.date.shortDate() is FilterElementRow.To -> row.date.shortDate()
is FilterElementRow.DurationMoreThan -> row.minutes.toMinutes(requireContext()) is FilterElementRow.DurationMoreThan -> row.minutes.toMinutes(requireContext())
@ -139,6 +145,7 @@ open class FilterDetailsFragment : PokerAnalyticsFragment(), StaticRowRepresenta
when (row) { when (row) {
is FilterElementRow.From -> row.date = if (value != null && value is Date) value else Date() is FilterElementRow.From -> row.date = if (value != null && value is Date) value else Date()
is FilterElementRow.To -> row.date = if (value != null && value is Date) value else Date() is FilterElementRow.To -> row.date = if (value != null && value is Date) value else Date()
is FilterElementRow.PastDays -> row.lastDays = if (value != null && value is String) value.toInt() else 0
is FilterElementRow.DurationMoreThan -> { is FilterElementRow.DurationMoreThan -> {
if (value is ArrayList<*>) { if (value is ArrayList<*>) {
val hours = try { val hours = try {
@ -222,14 +229,7 @@ open class FilterDetailsFragment : PokerAnalyticsFragment(), StaticRowRepresenta
this.rows.forEach { element -> this.rows.forEach { element ->
if (element is FilterElementRow && currentFilter?.contains(element) == true) { if (element is FilterElementRow && currentFilter?.contains(element) == true) {
currentFilter?.setSavedValueForElement(element)
/*
when (element) {
is FilterElementRow.From -> element.date = currentFilter?.getValueForElement(element) as Date? ?: Date()
is FilterElementRow.To -> element.date = currentFilter?.getValueForElement(element) as Date? ?: Date()
}
*/
this.selectedRows.add(element) this.selectedRows.add(element)
} }
} }
@ -246,7 +246,6 @@ open class FilterDetailsFragment : PokerAnalyticsFragment(), StaticRowRepresenta
private fun saveData() { private fun saveData() {
//TODO: Save currentFilter details data //TODO: Save currentFilter details data
Timber.d("Save data for filter: ${currentFilter?.id}") Timber.d("Save data for filter: ${currentFilter?.id}")
selectedRows?.forEach { selectedRows?.forEach {
Timber.d("Selected rows: $it") Timber.d("Selected rows: $it")
} }

@ -159,6 +159,7 @@ sealed class FilterElementRow : RowRepresentable {
override val viewType: Int override val viewType: Int
get() { get() {
return when (this) { return when (this) {
is PastDays,
is From, is To, is From, is To,
is DurationMoreThan, is DurationLessThan -> RowViewType.TITLE_VALUE_CHECK.ordinal is DurationMoreThan, is DurationLessThan -> RowViewType.TITLE_VALUE_CHECK.ordinal
else -> RowViewType.TITLE_CHECK.ordinal else -> RowViewType.TITLE_CHECK.ordinal
@ -168,6 +169,7 @@ sealed class FilterElementRow : RowRepresentable {
override val bottomSheetType: BottomSheetType override val bottomSheetType: BottomSheetType
get() { get() {
return when (this) { return when (this) {
is PastDays -> BottomSheetType.EDIT_TEXT
is DurationMoreThan, is DurationLessThan -> BottomSheetType.DOUBLE_EDIT_TEXT is DurationMoreThan, is DurationLessThan -> BottomSheetType.DOUBLE_EDIT_TEXT
else -> BottomSheetType.NONE else -> BottomSheetType.NONE
} }
@ -175,6 +177,12 @@ sealed class FilterElementRow : RowRepresentable {
override fun editingDescriptors(map: Map<String, Any?>): ArrayList<RowRepresentableEditDescriptor>? { override fun editingDescriptors(map: Map<String, Any?>): ArrayList<RowRepresentableEditDescriptor>? {
return when (this) { return when (this) {
is PastDays -> {
val pastDays: String? by map
arrayListOf(
RowRepresentableEditDescriptor(pastDays, R.string.period_in_days, inputType = InputType.TYPE_CLASS_NUMBER)
)
}
is DurationMoreThan, is DurationLessThan -> { is DurationMoreThan, is DurationLessThan -> {
val hours: String? by map val hours: String? by map
val minutes: String? by map val minutes: String? by map

Loading…
Cancel
Save