Improve filter management (Limit, table size)

feature/top10
Aurelien Hubert 7 years ago
parent f40bc3325c
commit 8f05a9ca9e
  1. 181
      app/src/main/java/net/pokeranalytics/android/model/realm/Filter.kt
  2. 284
      app/src/main/java/net/pokeranalytics/android/model/realm/FilterElement.kt
  3. 18
      app/src/main/java/net/pokeranalytics/android/ui/view/rowrepresentable/FilterElementRow.kt

@ -19,101 +19,102 @@ import java.util.*
*/ */
open class Filter : RealmObject() { open class Filter : RealmObject() {
private var entityType : Int? = Entity.SESSION.ordinal private var entityType: Int? = Entity.SESSION.ordinal
private enum class Entity { private enum class Entity {
SESSION, SESSION,
; ;
} }
companion object { companion object {
// Create a new instance // Create a new instance
fun newInstance(realm: Realm) : Filter { fun newInstance(realm: Realm): Filter {
val filter = Filter() val filter = Filter()
return realm.copyToRealm(filter) return realm.copyToRealm(filter)
} }
// Get a filter by its id // Get a filter by its id
fun getFilterBydId(realm: Realm, filterId: String) : Filter? { fun getFilterBydId(realm: Realm, filterId: String): Filter? {
return realm.where<Filter>().equalTo("id", filterId).findFirst() return realm.where<Filter>().equalTo("id", filterId).findFirst()
} }
@TestOnly @TestOnly
inline fun <reified T : Filterable> queryOn(realm: Realm, queries: List<QueryType>): RealmResults<T> { inline fun <reified T : Filterable> queryOn(realm: Realm, queries: List<QueryType>): RealmResults<T> {
var realmQuery = realm.where<T>() var realmQuery = realm.where<T>()
queries.forEach { queries.forEach {
realmQuery = it.filter<T>(realmQuery) realmQuery = it.filter<T>(realmQuery)
} }
return realmQuery.findAll() return realmQuery.findAll()
} }
} }
@PrimaryKey @PrimaryKey
var id = UUID.randomUUID().toString() var id = UUID.randomUUID().toString()
// the filter name // the filter name
var name: String = "" var name: String = ""
// the number of use of the filter, // the number of use of the filter,
// for MutableRealmInteger, see https://realm.io/docs/java/latest/#counters // for MutableRealmInteger, see https://realm.io/docs/java/latest/#counters
val usageCount: MutableRealmInteger = MutableRealmInteger.valueOf(0) val usageCount: MutableRealmInteger = MutableRealmInteger.valueOf(0)
var filterElements: RealmList<FilterElement> = RealmList() var filterElements: RealmList<FilterElement> = RealmList()
private set private set
fun createOrUpdateFilterElements(filterElementRows: ArrayList<FilterElementRow>) { fun createOrUpdateFilterElements(filterElementRows: ArrayList<FilterElementRow>) {
filterElements.clear() filterElements.clear()
filterElementRows filterElementRows
.map { .map {
it.filterSectionRow it.filterSectionRow
} }
.distinct() .distinct()
.forEach { section -> .forEach { section ->
filterElementRows filterElementRows
.filter { .filter {
it.filterSectionRow == section it.filterSectionRow == section
} }
.apply { .apply {
if (this.size == 1) { if (this.size == 1) {
filterElements.add(FilterElement(this.first())) filterElements.add(FilterElement(this.first()))
} else { } else {
val casted = arrayListOf<FilterElementRow>() val casted = arrayListOf<FilterElementRow>()
casted.addAll(this) casted.addAll(this)
filterElements.add(FilterElement(casted)) filterElements.add(FilterElement(casted))
} }
} }
} }
} }
fun countBy(filterCategoryRow: FilterCategoryRow) : Int { fun countBy(filterCategoryRow: FilterCategoryRow): Int {
val sections = filterCategoryRow.filterSectionRows val sections = filterCategoryRow.filterSectionRows
return filterElements.count { return filterElements.count {
sections.contains(FilterSectionRow.valueOf(it.sectionName ?: throw PokerAnalyticsException.FilterElementUnknownSectionName)) sections.contains(FilterSectionRow.valueOf(it.sectionName ?: throw PokerAnalyticsException.FilterElementUnknownSectionName))
} }
} }
fun contains(filterElementRow:FilterElementRow) : Boolean { fun contains(filterElementRow: FilterElementRow): Boolean {
val filtered = filterElements.filter { val filtered = filterElements.filter {
it.filterName == filterElementRow.filterName it.filterName == filterElementRow.filterName
} }
if (filtered.isEmpty()) {
return false if (filtered.isEmpty()) {
} return false
return filterElementRow.contains(filtered) }
} return filterElementRow.contains(filtered)
}
inline fun <reified T : Filterable> results(): RealmResults<T> { inline fun <reified T : Filterable> results(): RealmResults<T> {
var realmQuery = realm.where<T>() var realmQuery = realm.where<T>()
this.filterElements.map { this.filterElements.map {
it.queryType it.queryType
}.forEach { }.forEach {
realmQuery = it.filter(realmQuery) realmQuery = it.filter(realmQuery)
} }
return realmQuery.findAll() return realmQuery.findAll()
} }
} }

@ -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 net.pokeranalytics.android.ui.view.rowrepresentable.FilterElementRow.* import net.pokeranalytics.android.ui.view.rowrepresentable.FilterElementRow.*
import java.util.* import java.util.*
import kotlin.collections.ArrayList
open class FilterElement() : RealmObject() { open class FilterElement() : RealmObject() {
private constructor(filterName:String, sectionName:String) : this() { private constructor(filterName: String, sectionName: String) : this() {
this.filterName = filterName this.filterName = filterName
this.sectionName = sectionName this.sectionName = sectionName
} }
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 (QueryType.valueOf(filterName)) { this.stringValues = when (QueryType.valueOf(filterName)) {
QueryType.GAME, QueryType.BANKROLL, QueryType.TOURNAMENT_NAME, QueryType.ALL_TOURNAMENT_FEATURES, QueryType.ANY_TOURNAMENT_FEATURES, QueryType.LOCATION -> { QueryType.GAME, QueryType.BANKROLL, QueryType.TOURNAMENT_NAME, QueryType.ALL_TOURNAMENT_FEATURES, QueryType.ANY_TOURNAMENT_FEATURES, QueryType.LOCATION -> {
RealmList<String>().apply { RealmList<String>().apply {
this.addAll(filterElementRows.map { this.addAll(filterElementRows.map {
(it as DataFilterElementRow).id (it as DataFilterElementRow).id
}) })
} }
} }
else -> null QueryType.LIMIT, QueryType.TABLE_SIZE -> {
} RealmList<String>().apply {
this.addAll(filterElementRows.map {
this.numericValues = when (QueryType.valueOf(filterName)) { (it as StaticDataFilterElementRow).id
QueryType.LIMIT -> { })
RealmList<Double>().apply { }
this.addAll(filterElementRows.map { }
(it as FilterElementRow.Limit).limit.ordinal.toDouble() else -> null
}) }
}
} this.numericValues = when (QueryType.valueOf(filterName)) {
QueryType.TABLE_SIZE -> { QueryType.LIMIT -> {
RealmList<Double>().apply { RealmList<Double>().apply {
this.addAll(filterElementRows.map { this.addAll(filterElementRows.map {
(it as FilterElementRow.TableSize).tableSize.numberOfPlayer.toDouble() (it as FilterElementRow.Limit).limit.ordinal.toDouble()
}) })
} }
} }
QueryType.YEAR, QueryType.MONTH, QueryType.DAY_OF_WEEK -> { QueryType.TABLE_SIZE -> {
RealmList<Double>().apply { RealmList<Double>().apply {
this.addAll(filterElementRows.map { this.addAll(filterElementRows.map {
(it as SingleValueFilterElementRow).value.toDouble() (it as FilterElementRow.TableSize).tableSize.numberOfPlayer.toDouble()
}) })
} }
} }
QueryType.LESS_THAN_NET_RESULT -> { QueryType.YEAR, QueryType.MONTH, QueryType.DAY_OF_WEEK -> {
RealmList<Double>().apply { RealmList<Double>().apply {
this.addAll(filterElementRows.map { this.addAll(filterElementRows.map {
(it as ResultLessThan).value (it as SingleValueFilterElementRow).value.toDouble()
}) })
} }
} }
QueryType.MORE_THAN_NET_RESULT -> { QueryType.LESS_THAN_NET_RESULT -> {
RealmList<Double>().apply { RealmList<Double>().apply {
this.addAll(filterElementRows.map { this.addAll(filterElementRows.map {
(it as ResultMoreThan).value (it as ResultLessThan).value
}) })
} }
} }
else -> null QueryType.MORE_THAN_NET_RESULT -> {
} RealmList<Double>().apply {
this.addAll(filterElementRows.map {
this.blindValues = when (QueryType.valueOf(filterName)) { (it as ResultMoreThan).value
QueryType.BLINDS -> { })
RealmList<FilterElementBlind>().apply { }
this.addAll(filterElementRows.map { }
FilterElementBlind((it as FilterElementRow.Blind).sb, it.bb, it.code) else -> null
}) }
}
} this.blindValues = when (QueryType.valueOf(filterName)) {
else -> null QueryType.BLINDS -> {
} RealmList<FilterElementBlind>().apply {
} this.addAll(filterElementRows.map {
FilterElementBlind((it as FilterElementRow.Blind).sb, it.bb, it.code)
constructor(filterElementRow:FilterElementRow) : this(arrayListOf(filterElementRow)) { })
when (filterElementRow) { }
is From -> dateValue = filterElementRow.date }
is To -> dateValue= filterElementRow.date else -> null
} }
} }
var filterName : String? = null constructor(filterElementRow: FilterElementRow) : this(arrayListOf(filterElementRow)) {
var sectionName : String? = null when (filterElementRow) {
is From -> dateValue = filterElementRow.date
val queryType : QueryType is To -> dateValue = filterElementRow.date
get() = QueryType.valueOf(this.filterName ?: throw PokerAnalyticsException.FilterElementUnknownName) }
.apply { }
this.updateValueMap(this@FilterElement)
} var filterName: String? = null
var sectionName: String? = null
private var numericValues: RealmList<Double>? = null
private var dateValue : Date? = null val queryType: QueryType
private var stringValues : RealmList<String>? = null get() = QueryType.valueOf(this.filterName ?: throw PokerAnalyticsException.FilterElementUnknownName)
private var blindValues : RealmList<FilterElementBlind>? = null .apply {
this.updateValueMap(this@FilterElement)
val ids : Array<String> }
get() = stringValues?.toTypedArray()?: throw PokerAnalyticsException.FilterElementExpectedValueMissing
private var numericValues: RealmList<Double>? = null
val blinds : RealmList<FilterElementBlind> private var dateValue: Date? = null
get() { private var stringValues: RealmList<String>? = null
blindValues?.let { private var blindValues: RealmList<FilterElementBlind>? = null
if (it.isNotEmpty()) {
return it val ids: Array<String>
} else { get() = stringValues?.toTypedArray() ?: throw PokerAnalyticsException.FilterElementExpectedValueMissing
throw PokerAnalyticsException.FilterElementExpectedValueMissing
} val blinds: RealmList<FilterElementBlind>
} get() {
throw PokerAnalyticsException.FilterElementExpectedValueMissing blindValues?.let {
} if (it.isNotEmpty()) {
return it
} else {
val date : Date throw PokerAnalyticsException.FilterElementExpectedValueMissing
get() = dateValue?: throw PokerAnalyticsException.FilterElementExpectedValueMissing }
}
throw PokerAnalyticsException.FilterElementExpectedValueMissing
val values : Array<Int> }
get() = numericValues?.map {
it.toInt()
}?.toTypedArray()?: throw PokerAnalyticsException.FilterElementExpectedValueMissing val date: Date
get() = dateValue ?: throw PokerAnalyticsException.FilterElementExpectedValueMissing
val value : Double
get() = numericValues?.first()?: throw PokerAnalyticsException.FilterElementExpectedValueMissing val values: Array<Int>
get() = numericValues?.map {
it.toInt()
val leftValue : Double }?.toTypedArray() ?: throw PokerAnalyticsException.FilterElementExpectedValueMissing
get() = numericValues?.first()?: throw PokerAnalyticsException.FilterElementExpectedValueMissing
val value: Double
val rightValue : Double get() = numericValues?.first() ?: throw PokerAnalyticsException.FilterElementExpectedValueMissing
get() = numericValues?.last()?: throw PokerAnalyticsException.FilterElementExpectedValueMissing
val leftValue: Double
val dayOfWeek : Int get() = numericValues?.first() ?: throw PokerAnalyticsException.FilterElementExpectedValueMissing
get() = numericValues?.first()?.toInt()?: throw PokerAnalyticsException.FilterElementExpectedValueMissing
val rightValue: Double
val month : Int get() = numericValues?.last() ?: throw PokerAnalyticsException.FilterElementExpectedValueMissing
get() = numericValues?.first()?.toInt()?: throw PokerAnalyticsException.FilterElementExpectedValueMissing
val dayOfWeek: Int
val year : Int get() = numericValues?.first()?.toInt() ?: throw PokerAnalyticsException.FilterElementExpectedValueMissing
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
} }

@ -30,13 +30,10 @@ sealed class FilterElementRow : RowRepresentable {
val name: String = (data as RowRepresentable).getDisplayName() 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 override val resId: Int? = row.resId
val name: String = row.getDisplayName()
fun getDataDisplayName() : String {
return row.getDisplayName()
}
fun getDataLocalizedTitle(context: Context): String { fun getDataLocalizedTitle(context: Context): String {
return row.localizedTitle(context) return row.localizedTitle(context)
@ -52,8 +49,8 @@ sealed class FilterElementRow : RowRepresentable {
data class Month(val month: Int) : SingleValueFilterElementRow(month) data class Month(val month: Int) : SingleValueFilterElementRow(month)
data class Day(val day: Int) : SingleValueFilterElementRow(day) data class Day(val day: Int) : SingleValueFilterElementRow(day)
data class PastDays(var lastDays: Int = 0) : FilterElementRow() data class PastDays(var lastDays: Int = 0) : FilterElementRow()
data class Limit(val limit: net.pokeranalytics.android.model.Limit) : StaticDataFilterElementRow(limit) 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) 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 Bankroll(val bankroll: Manageable) : DataFilterElementRow(bankroll)
data class Game(val game: Manageable) : DataFilterElementRow(game) data class Game(val game: Manageable) : DataFilterElementRow(game)
data class Location(val location: Manageable) : DataFilterElementRow(location) data class Location(val location: Manageable) : DataFilterElementRow(location)
@ -112,7 +109,10 @@ sealed class FilterElementRow : RowRepresentable {
is DataFilterElementRow -> filterElements.any { is DataFilterElementRow -> filterElements.any {
it.ids.contains(this.id) 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 { override fun getDisplayName(): String {
return when (this) { return when (this) {
is DataFilterElementRow -> this.name is DataFilterElementRow -> this.name
is StaticDataFilterElementRow -> this.getDataDisplayName() is StaticDataFilterElementRow -> this.name
else -> super.getDisplayName() else -> super.getDisplayName()
} }
} }

Loading…
Cancel
Save