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() {
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<Filter>().equalTo("id", filterId).findFirst()
}
@TestOnly
inline fun <reified T : Filterable> queryOn(realm: Realm, queries: List<QueryType>): RealmResults<T> {
var realmQuery = realm.where<T>()
queries.forEach {
realmQuery = it.filter<T>(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<FilterElement> = RealmList()
private set
fun createOrUpdateFilterElements(filterElementRows: ArrayList<FilterElementRow>) {
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<FilterElementRow>()
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<Filter>().equalTo("id", filterId).findFirst()
}
@TestOnly
inline fun <reified T : Filterable> queryOn(realm: Realm, queries: List<QueryType>): RealmResults<T> {
var realmQuery = realm.where<T>()
queries.forEach {
realmQuery = it.filter<T>(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<FilterElement> = RealmList()
private set
fun createOrUpdateFilterElements(filterElementRows: ArrayList<FilterElementRow>) {
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<FilterElementRow>()
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 <reified T : Filterable> results(): RealmResults<T> {
var realmQuery = realm.where<T>()
this.filterElements.map {
it.queryType
}.forEach {
realmQuery = it.filter(realmQuery)
}
return realmQuery.findAll()
}
var realmQuery = realm.where<T>()
this.filterElements.map {
it.queryType
}.forEach {
realmQuery = it.filter(realmQuery)
}
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 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<FilterElementRow>) : 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<String>().apply {
this.addAll(filterElementRows.map {
(it as DataFilterElementRow).id
})
}
}
else -> null
}
this.numericValues = when (QueryType.valueOf(filterName)) {
QueryType.LIMIT -> {
RealmList<Double>().apply {
this.addAll(filterElementRows.map {
(it as FilterElementRow.Limit).limit.ordinal.toDouble()
})
}
}
QueryType.TABLE_SIZE -> {
RealmList<Double>().apply {
this.addAll(filterElementRows.map {
(it as FilterElementRow.TableSize).tableSize.numberOfPlayer.toDouble()
})
}
}
QueryType.YEAR, QueryType.MONTH, QueryType.DAY_OF_WEEK -> {
RealmList<Double>().apply {
this.addAll(filterElementRows.map {
(it as SingleValueFilterElementRow).value.toDouble()
})
}
}
QueryType.LESS_THAN_NET_RESULT -> {
RealmList<Double>().apply {
this.addAll(filterElementRows.map {
(it as ResultLessThan).value
})
}
}
QueryType.MORE_THAN_NET_RESULT -> {
RealmList<Double>().apply {
this.addAll(filterElementRows.map {
(it as ResultMoreThan).value
})
}
}
else -> null
}
this.blindValues = when (QueryType.valueOf(filterName)) {
QueryType.BLINDS -> {
RealmList<FilterElementBlind>().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<Double>? = null
private var dateValue : Date? = null
private var stringValues : RealmList<String>? = null
private var blindValues : RealmList<FilterElementBlind>? = null
val ids : Array<String>
get() = stringValues?.toTypedArray()?: throw PokerAnalyticsException.FilterElementExpectedValueMissing
val blinds : RealmList<FilterElementBlind>
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<Int>
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<FilterElementRow>) : 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<String>().apply {
this.addAll(filterElementRows.map {
(it as DataFilterElementRow).id
})
}
}
QueryType.LIMIT, QueryType.TABLE_SIZE -> {
RealmList<String>().apply {
this.addAll(filterElementRows.map {
(it as StaticDataFilterElementRow).id
})
}
}
else -> null
}
this.numericValues = when (QueryType.valueOf(filterName)) {
QueryType.LIMIT -> {
RealmList<Double>().apply {
this.addAll(filterElementRows.map {
(it as FilterElementRow.Limit).limit.ordinal.toDouble()
})
}
}
QueryType.TABLE_SIZE -> {
RealmList<Double>().apply {
this.addAll(filterElementRows.map {
(it as FilterElementRow.TableSize).tableSize.numberOfPlayer.toDouble()
})
}
}
QueryType.YEAR, QueryType.MONTH, QueryType.DAY_OF_WEEK -> {
RealmList<Double>().apply {
this.addAll(filterElementRows.map {
(it as SingleValueFilterElementRow).value.toDouble()
})
}
}
QueryType.LESS_THAN_NET_RESULT -> {
RealmList<Double>().apply {
this.addAll(filterElementRows.map {
(it as ResultLessThan).value
})
}
}
QueryType.MORE_THAN_NET_RESULT -> {
RealmList<Double>().apply {
this.addAll(filterElementRows.map {
(it as ResultMoreThan).value
})
}
}
else -> null
}
this.blindValues = when (QueryType.valueOf(filterName)) {
QueryType.BLINDS -> {
RealmList<FilterElementBlind>().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<Double>? = null
private var dateValue: Date? = null
private var stringValues: RealmList<String>? = null
private var blindValues: RealmList<FilterElementBlind>? = null
val ids: Array<String>
get() = stringValues?.toTypedArray() ?: throw PokerAnalyticsException.FilterElementExpectedValueMissing
val blinds: RealmList<FilterElementBlind>
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<Int>
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
}

@ -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()
}
}

Loading…
Cancel
Save