From 8f05a9ca9edb84e4967d2d5e791be58f0da2fb19 Mon Sep 17 00:00:00 2001 From: Aurelien Hubert Date: Tue, 9 Apr 2019 10:46:39 +0200 Subject: [PATCH] Improve filter management (Limit, table size) --- .../android/model/realm/Filter.kt | 181 +++++------ .../android/model/realm/FilterElement.kt | 284 +++++++++--------- .../view/rowrepresentable/FilterElementRow.kt | 18 +- 3 files changed, 245 insertions(+), 238 deletions(-) diff --git a/app/src/main/java/net/pokeranalytics/android/model/realm/Filter.kt b/app/src/main/java/net/pokeranalytics/android/model/realm/Filter.kt index a1dc9cef..f94cdc99 100644 --- a/app/src/main/java/net/pokeranalytics/android/model/realm/Filter.kt +++ b/app/src/main/java/net/pokeranalytics/android/model/realm/Filter.kt @@ -19,101 +19,102 @@ import java.util.* */ open class Filter : RealmObject() { - private var entityType : Int? = Entity.SESSION.ordinal + private var entityType: Int? = Entity.SESSION.ordinal - private enum class Entity { - SESSION, - ; - } + private enum class Entity { + SESSION, + ; + } - companion object { + companion object { - // Create a new instance - fun newInstance(realm: Realm) : Filter { + // Create a new instance + fun newInstance(realm: Realm): Filter { val filter = Filter() - return realm.copyToRealm(filter) - } - - // Get a filter by its id - fun getFilterBydId(realm: Realm, filterId: String) : Filter? { - return realm.where().equalTo("id", filterId).findFirst() - } - - @TestOnly - inline fun queryOn(realm: Realm, queries: List): RealmResults { - var realmQuery = realm.where() - queries.forEach { - realmQuery = it.filter(realmQuery) - } - return realmQuery.findAll() - } - } - - @PrimaryKey - var id = UUID.randomUUID().toString() - - // the filter name - var name: String = "" - - // the number of use of the filter, - // for MutableRealmInteger, see https://realm.io/docs/java/latest/#counters - val usageCount: MutableRealmInteger = MutableRealmInteger.valueOf(0) - - var filterElements: RealmList = RealmList() - private set - - fun createOrUpdateFilterElements(filterElementRows: ArrayList) { - filterElements.clear() - filterElementRows - .map { - it.filterSectionRow - } - .distinct() - .forEach { section -> - filterElementRows - .filter { - it.filterSectionRow == section - } - .apply { - - if (this.size == 1) { - filterElements.add(FilterElement(this.first())) - } else { - val casted = arrayListOf() - casted.addAll(this) - filterElements.add(FilterElement(casted)) - } - - } - } - } - - fun countBy(filterCategoryRow: FilterCategoryRow) : Int { - val sections = filterCategoryRow.filterSectionRows - return filterElements.count { - sections.contains(FilterSectionRow.valueOf(it.sectionName ?: throw PokerAnalyticsException.FilterElementUnknownSectionName)) - } - } - - fun contains(filterElementRow:FilterElementRow) : Boolean { - val filtered = filterElements.filter { - it.filterName == filterElementRow.filterName - } - if (filtered.isEmpty()) { - return false - } - return filterElementRow.contains(filtered) - } + return realm.copyToRealm(filter) + } + + // Get a filter by its id + fun getFilterBydId(realm: Realm, filterId: String): Filter? { + return realm.where().equalTo("id", filterId).findFirst() + } + + @TestOnly + inline fun queryOn(realm: Realm, queries: List): RealmResults { + var realmQuery = realm.where() + queries.forEach { + realmQuery = it.filter(realmQuery) + } + return realmQuery.findAll() + } + } + + @PrimaryKey + var id = UUID.randomUUID().toString() + + // the filter name + var name: String = "" + + // the number of use of the filter, + // for MutableRealmInteger, see https://realm.io/docs/java/latest/#counters + val usageCount: MutableRealmInteger = MutableRealmInteger.valueOf(0) + + var filterElements: RealmList = RealmList() + private set + + fun createOrUpdateFilterElements(filterElementRows: ArrayList) { + filterElements.clear() + filterElementRows + .map { + it.filterSectionRow + } + .distinct() + .forEach { section -> + filterElementRows + .filter { + it.filterSectionRow == section + } + .apply { + + if (this.size == 1) { + filterElements.add(FilterElement(this.first())) + } else { + val casted = arrayListOf() + casted.addAll(this) + filterElements.add(FilterElement(casted)) + } + + } + } + } + + fun countBy(filterCategoryRow: FilterCategoryRow): Int { + val sections = filterCategoryRow.filterSectionRows + return filterElements.count { + sections.contains(FilterSectionRow.valueOf(it.sectionName ?: throw PokerAnalyticsException.FilterElementUnknownSectionName)) + } + } + + fun contains(filterElementRow: FilterElementRow): Boolean { + val filtered = filterElements.filter { + it.filterName == filterElementRow.filterName + } + + if (filtered.isEmpty()) { + return false + } + return filterElementRow.contains(filtered) + } inline fun results(): RealmResults { - var realmQuery = realm.where() - this.filterElements.map { - it.queryType - }.forEach { - realmQuery = it.filter(realmQuery) - } - - return realmQuery.findAll() - } + var realmQuery = realm.where() + this.filterElements.map { + it.queryType + }.forEach { + realmQuery = it.filter(realmQuery) + } + + return realmQuery.findAll() + } } diff --git a/app/src/main/java/net/pokeranalytics/android/model/realm/FilterElement.kt b/app/src/main/java/net/pokeranalytics/android/model/realm/FilterElement.kt index c0d35070..fe7dbaa6 100644 --- a/app/src/main/java/net/pokeranalytics/android/model/realm/FilterElement.kt +++ b/app/src/main/java/net/pokeranalytics/android/model/realm/FilterElement.kt @@ -7,147 +7,153 @@ import net.pokeranalytics.android.model.filter.QueryType import net.pokeranalytics.android.ui.view.rowrepresentable.FilterElementRow import net.pokeranalytics.android.ui.view.rowrepresentable.FilterElementRow.* import java.util.* -import kotlin.collections.ArrayList open class FilterElement() : RealmObject() { - private constructor(filterName:String, sectionName:String) : this() { - this.filterName = filterName - this.sectionName = sectionName - } - - constructor(filterElementRows: ArrayList) : this(filterElementRows.first().filterName, filterElementRows.first().filterSectionRow.name) { - val filterName : String = this.filterName ?: throw PokerAnalyticsException.FilterElementUnknownName - this.stringValues = when (QueryType.valueOf(filterName)) { - QueryType.GAME, QueryType.BANKROLL, QueryType.TOURNAMENT_NAME, QueryType.ALL_TOURNAMENT_FEATURES, QueryType.ANY_TOURNAMENT_FEATURES, QueryType.LOCATION -> { - RealmList().apply { - this.addAll(filterElementRows.map { - (it as DataFilterElementRow).id - }) - } - } - else -> null - } - - this.numericValues = when (QueryType.valueOf(filterName)) { - QueryType.LIMIT -> { - RealmList().apply { - this.addAll(filterElementRows.map { - (it as FilterElementRow.Limit).limit.ordinal.toDouble() - }) - } - } - QueryType.TABLE_SIZE -> { - RealmList().apply { - this.addAll(filterElementRows.map { - (it as FilterElementRow.TableSize).tableSize.numberOfPlayer.toDouble() - }) - } - } - QueryType.YEAR, QueryType.MONTH, QueryType.DAY_OF_WEEK -> { - RealmList().apply { - this.addAll(filterElementRows.map { - (it as SingleValueFilterElementRow).value.toDouble() - }) - } - } - QueryType.LESS_THAN_NET_RESULT -> { - RealmList().apply { - this.addAll(filterElementRows.map { - (it as ResultLessThan).value - }) - } - } - QueryType.MORE_THAN_NET_RESULT -> { - RealmList().apply { - this.addAll(filterElementRows.map { - (it as ResultMoreThan).value - }) - } - } - else -> null - } - - this.blindValues = when (QueryType.valueOf(filterName)) { - QueryType.BLINDS -> { - RealmList().apply { - this.addAll(filterElementRows.map { - FilterElementBlind((it as FilterElementRow.Blind).sb, it.bb, it.code) - }) - } - } - else -> null - } - } - - constructor(filterElementRow:FilterElementRow) : this(arrayListOf(filterElementRow)) { - when (filterElementRow) { - is From -> dateValue = filterElementRow.date - is To -> dateValue= filterElementRow.date - } - } - - var filterName : String? = null - var sectionName : String? = null - - val queryType : QueryType - get() = QueryType.valueOf(this.filterName ?: throw PokerAnalyticsException.FilterElementUnknownName) - .apply { - this.updateValueMap(this@FilterElement) - } - - private var numericValues: RealmList? = null - private var dateValue : Date? = null - private var stringValues : RealmList? = null - private var blindValues : RealmList? = null - - val ids : Array - get() = stringValues?.toTypedArray()?: throw PokerAnalyticsException.FilterElementExpectedValueMissing - - val blinds : RealmList - get() { - blindValues?.let { - if (it.isNotEmpty()) { - return it - } else { - throw PokerAnalyticsException.FilterElementExpectedValueMissing - } - } - throw PokerAnalyticsException.FilterElementExpectedValueMissing - } - - - val date : Date - get() = dateValue?: throw PokerAnalyticsException.FilterElementExpectedValueMissing - - - val values : Array - get() = numericValues?.map { - it.toInt() - }?.toTypedArray()?: throw PokerAnalyticsException.FilterElementExpectedValueMissing - - - val value : Double - get() = numericValues?.first()?: throw PokerAnalyticsException.FilterElementExpectedValueMissing - - - val leftValue : Double - get() = numericValues?.first()?: throw PokerAnalyticsException.FilterElementExpectedValueMissing - - - val rightValue : Double - get() = numericValues?.last()?: throw PokerAnalyticsException.FilterElementExpectedValueMissing - - - val dayOfWeek : Int - get() = numericValues?.first()?.toInt()?: throw PokerAnalyticsException.FilterElementExpectedValueMissing - - - val month : Int - get() = numericValues?.first()?.toInt()?: throw PokerAnalyticsException.FilterElementExpectedValueMissing - - - val year : Int - get() = numericValues?.first()?.toInt()?: throw PokerAnalyticsException.FilterElementExpectedValueMissing + private constructor(filterName: String, sectionName: String) : this() { + this.filterName = filterName + this.sectionName = sectionName + } + + constructor(filterElementRows: ArrayList) : this(filterElementRows.first().filterName, filterElementRows.first().filterSectionRow.name) { + val filterName: String = this.filterName ?: throw PokerAnalyticsException.FilterElementUnknownName + this.stringValues = when (QueryType.valueOf(filterName)) { + QueryType.GAME, QueryType.BANKROLL, QueryType.TOURNAMENT_NAME, QueryType.ALL_TOURNAMENT_FEATURES, QueryType.ANY_TOURNAMENT_FEATURES, QueryType.LOCATION -> { + RealmList().apply { + this.addAll(filterElementRows.map { + (it as DataFilterElementRow).id + }) + } + } + QueryType.LIMIT, QueryType.TABLE_SIZE -> { + RealmList().apply { + this.addAll(filterElementRows.map { + (it as StaticDataFilterElementRow).id + }) + } + } + else -> null + } + + this.numericValues = when (QueryType.valueOf(filterName)) { + QueryType.LIMIT -> { + RealmList().apply { + this.addAll(filterElementRows.map { + (it as FilterElementRow.Limit).limit.ordinal.toDouble() + }) + } + } + QueryType.TABLE_SIZE -> { + RealmList().apply { + this.addAll(filterElementRows.map { + (it as FilterElementRow.TableSize).tableSize.numberOfPlayer.toDouble() + }) + } + } + QueryType.YEAR, QueryType.MONTH, QueryType.DAY_OF_WEEK -> { + RealmList().apply { + this.addAll(filterElementRows.map { + (it as SingleValueFilterElementRow).value.toDouble() + }) + } + } + QueryType.LESS_THAN_NET_RESULT -> { + RealmList().apply { + this.addAll(filterElementRows.map { + (it as ResultLessThan).value + }) + } + } + QueryType.MORE_THAN_NET_RESULT -> { + RealmList().apply { + this.addAll(filterElementRows.map { + (it as ResultMoreThan).value + }) + } + } + else -> null + } + + this.blindValues = when (QueryType.valueOf(filterName)) { + QueryType.BLINDS -> { + RealmList().apply { + this.addAll(filterElementRows.map { + FilterElementBlind((it as FilterElementRow.Blind).sb, it.bb, it.code) + }) + } + } + else -> null + } + } + + constructor(filterElementRow: FilterElementRow) : this(arrayListOf(filterElementRow)) { + when (filterElementRow) { + is From -> dateValue = filterElementRow.date + is To -> dateValue = filterElementRow.date + } + } + + var filterName: String? = null + var sectionName: String? = null + + val queryType: QueryType + get() = QueryType.valueOf(this.filterName ?: throw PokerAnalyticsException.FilterElementUnknownName) + .apply { + this.updateValueMap(this@FilterElement) + } + + private var numericValues: RealmList? = null + private var dateValue: Date? = null + private var stringValues: RealmList? = null + private var blindValues: RealmList? = null + + val ids: Array + get() = stringValues?.toTypedArray() ?: throw PokerAnalyticsException.FilterElementExpectedValueMissing + + val blinds: RealmList + get() { + blindValues?.let { + if (it.isNotEmpty()) { + return it + } else { + throw PokerAnalyticsException.FilterElementExpectedValueMissing + } + } + throw PokerAnalyticsException.FilterElementExpectedValueMissing + } + + + val date: Date + get() = dateValue ?: throw PokerAnalyticsException.FilterElementExpectedValueMissing + + + val values: Array + get() = numericValues?.map { + it.toInt() + }?.toTypedArray() ?: throw PokerAnalyticsException.FilterElementExpectedValueMissing + + + val value: Double + get() = numericValues?.first() ?: throw PokerAnalyticsException.FilterElementExpectedValueMissing + + + val leftValue: Double + get() = numericValues?.first() ?: throw PokerAnalyticsException.FilterElementExpectedValueMissing + + + val rightValue: Double + get() = numericValues?.last() ?: throw PokerAnalyticsException.FilterElementExpectedValueMissing + + + val dayOfWeek: Int + get() = numericValues?.first()?.toInt() ?: throw PokerAnalyticsException.FilterElementExpectedValueMissing + + + val month: Int + get() = numericValues?.first()?.toInt() ?: throw PokerAnalyticsException.FilterElementExpectedValueMissing + + + val year: Int + get() = numericValues?.first()?.toInt() ?: throw PokerAnalyticsException.FilterElementExpectedValueMissing } diff --git a/app/src/main/java/net/pokeranalytics/android/ui/view/rowrepresentable/FilterElementRow.kt b/app/src/main/java/net/pokeranalytics/android/ui/view/rowrepresentable/FilterElementRow.kt index 316ff259..d72eefb3 100644 --- a/app/src/main/java/net/pokeranalytics/android/ui/view/rowrepresentable/FilterElementRow.kt +++ b/app/src/main/java/net/pokeranalytics/android/ui/view/rowrepresentable/FilterElementRow.kt @@ -30,13 +30,10 @@ sealed class FilterElementRow : RowRepresentable { val name: String = (data as RowRepresentable).getDisplayName() } - open class StaticDataFilterElementRow(var row: RowRepresentable) : FilterElementRow() { + open class StaticDataFilterElementRow(var row: RowRepresentable, var id: String) : FilterElementRow() { override val resId: Int? = row.resId - - fun getDataDisplayName() : String { - return row.getDisplayName() - } + val name: String = row.getDisplayName() fun getDataLocalizedTitle(context: Context): String { return row.localizedTitle(context) @@ -52,8 +49,8 @@ sealed class FilterElementRow : RowRepresentable { data class Month(val month: Int) : SingleValueFilterElementRow(month) data class Day(val day: Int) : SingleValueFilterElementRow(day) data class PastDays(var lastDays: Int = 0) : FilterElementRow() - data class Limit(val limit: net.pokeranalytics.android.model.Limit) : StaticDataFilterElementRow(limit) - data class TableSize(val tableSize: net.pokeranalytics.android.model.TableSize) : StaticDataFilterElementRow(tableSize) + data class Limit(val limit: net.pokeranalytics.android.model.Limit) : StaticDataFilterElementRow(limit, limit.longName) + data class TableSize(val tableSize: net.pokeranalytics.android.model.TableSize) : StaticDataFilterElementRow(tableSize, tableSize.numberOfPlayer.toString()) data class Bankroll(val bankroll: Manageable) : DataFilterElementRow(bankroll) data class Game(val game: Manageable) : DataFilterElementRow(game) data class Location(val location: Manageable) : DataFilterElementRow(location) @@ -112,7 +109,10 @@ sealed class FilterElementRow : RowRepresentable { is DataFilterElementRow -> filterElements.any { it.ids.contains(this.id) } - else -> return true + is StaticDataFilterElementRow ->filterElements.any { + it.ids.contains(this.id) + } + else -> true } } @@ -148,7 +148,7 @@ sealed class FilterElementRow : RowRepresentable { override fun getDisplayName(): String { return when (this) { is DataFilterElementRow -> this.name - is StaticDataFilterElementRow -> this.getDataDisplayName() + is StaticDataFilterElementRow -> this.name else -> super.getDisplayName() } }