cleaner implementation of equals check on FilterValues

This commit is contained in:
johan12345
2021-07-14 22:22:48 +02:00
parent 9a0a7b4e5f
commit fa53a9fc5a
3 changed files with 17 additions and 17 deletions

View File

@@ -48,6 +48,8 @@ sealed class FilterValue : BaseObservable(), Equatable {
abstract val key: String
var dataSource: String = ""
var profile: Long = FILTERS_CUSTOM
abstract fun hasSameValueAs(other: FilterValue): Boolean
}
@Entity(
@@ -62,7 +64,11 @@ sealed class FilterValue : BaseObservable(), Equatable {
data class BooleanFilterValue(
override val key: String,
var value: Boolean
) : FilterValue()
) : FilterValue() {
override fun hasSameValueAs(other: FilterValue): Boolean {
return other is BooleanFilterValue && other.value == this.value
}
}
@Entity(
foreignKeys = [ForeignKey(
@@ -78,23 +84,14 @@ data class MultipleChoiceFilterValue(
var values: MutableSet<String>,
var all: Boolean
) : FilterValue() {
override fun equals(other: Any?): Boolean {
if (other == null || other !is MultipleChoiceFilterValue) return false
if (key != other.key) return false
return if (all) {
other.all
override fun hasSameValueAs(other: FilterValue): Boolean {
return other is MultipleChoiceFilterValue && if (other.all) {
this.all
} else {
!other.all && values == other.values
!this.all && other.values == this.values
}
}
override fun hashCode(): Int {
var result = key.hashCode()
result = 31 * result + all.hashCode()
result = 31 * result + if (all) 0 else values.hashCode()
return result
}
}
@Entity(
@@ -109,7 +106,11 @@ data class MultipleChoiceFilterValue(
data class SliderFilterValue(
override val key: String,
var value: Int
) : FilterValue()
) : FilterValue() {
override fun hasSameValueAs(other: FilterValue): Boolean {
return other is SliderFilterValue && other.value == this.value
}
}
data class FilterWithValue<T : FilterValue>(val filter: Filter<T>, val value: T) : Equatable

View File

@@ -97,7 +97,6 @@ class FilterViewModel(application: Application) : AndroidViewModel(application)
val value = it.value
value.profile = FILTERS_CUSTOM
value.dataSource = prefs.dataSource
db.filterValueDao().insert(value)
value
}?.let {
db.filterValueDao().insert(*it.toTypedArray())

View File

@@ -124,7 +124,7 @@ class MapViewModel(application: Application) : AndroidViewModel(application) {
value = 0
addSource(filtersWithValue) { filtersWithValue ->
value = filtersWithValue.count {
it.filter.defaultValue() != it.value
!it.value.hasSameValueAs(it.filter.defaultValue())
}
}
}