fix issue with filterableClass enum

feature/top10
Razmig Sarkissian 7 years ago
parent ef9b00e149
commit 0c3cc898da
  1. 27
      app/src/main/java/net/pokeranalytics/android/model/realm/Filter.kt

@ -11,19 +11,18 @@ 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 org.jetbrains.annotations.TestOnly import org.jetbrains.annotations.TestOnly
import java.util.* import java.util.*
import kotlin.reflect.KClass
/** /**
* A [Filter] is the top level representation of the filtering system * A [Filter] is the top level representation of the filtering system
* It contains a list of [FilterElement] describing the complete query to launch * It contains a list of [FilterElement] describing the complete query to launch
* The [Filter] is working closely with a [Filterable] interface providing the entity we want the query being launched on * The [Filter] is working closely with a [Filterable] interface providing the entity we want the query being launched on
*/ */
open class Filter() : RealmObject() { open class Filter(entity:Filterable) : RealmObject() {
constructor(entity:Filterable) : this() { constructor() : this(entity = Session)
this.entityType = FilterableClass.filterableClass(entity = entity).ordinal
}
private var entityType : Int? = null private var entityType : Int? = FilterableClass.filterableClass(entity = entity).ordinal
private val filterableClass : FilterableClass? private val filterableClass : FilterableClass?
get() { get() {
entityType?.let { entityType?.let {
@ -49,17 +48,26 @@ open class Filter() : RealmObject() {
get() { get() {
return when (this) { return when (this) {
SESSION -> Session::class.java SESSION -> Session::class.java
else -> throw FilterUnhandledEntityException("this entity is not filterable") // else -> throw FilterUnhandledEntityException("this entity is not filterable")
} }
} }
val filterable : Filterable
get() {
return when (this) {
SESSION -> Session.Companion
// else -> throw FilterUnhandledEntityException("this entity is not filterable")
}
}
} }
companion object { companion object {
@TestOnly @TestOnly
fun queryOn(realm: Realm, entity: Filterable, queries:List<QueryType>): RealmResults<*> { fun queryOn(realm: Realm, entity: Filterable, queries:List<QueryType>): RealmResults<*> {
var realmQuery : RealmQuery<out RealmObject> = realm.where(FilterableClass.filterableClass(entity).relatedEntity) val realmEntity : Class < out RealmObject > = FilterableClass.filterableClass(entity).relatedEntity
var realmQuery : RealmQuery<out RealmObject> = realm.where(realmEntity)
queries.forEach { queries.forEach {
realmQuery = (it.filter(realmQuery, entity)) realmQuery = (it.filter(realmQuery, entity))
} }
@ -124,12 +132,13 @@ open class Filter() : RealmObject() {
} }
fun queryOn(entity: Filterable) : RealmResults<*> { fun queryOn(entity: Filterable) : RealmResults<*> {
val realmEntity : Class < out RealmObject > = filterableClass?.relatedEntity ?: throw FilterMissingEntityException("this filter has no entity initialized") val filterableClass : FilterableClass = this.filterableClass ?: throw FilterMissingEntityException("this filter has no entity initialized")
val realmEntity : Class < out RealmObject > = filterableClass.relatedEntity
var realmQuery : RealmQuery<out RealmObject> = realm.where(realmEntity) var realmQuery : RealmQuery<out RealmObject> = realm.where(realmEntity)
this.filterElements.map { this.filterElements.map {
it.queryType it.queryType
}.forEach { }.forEach {
realmQuery = (it.filter(realmQuery, entity)) realmQuery = (it.filter(realmQuery, filterableClass.filterable))
} }
return realmQuery.findAll() return realmQuery.findAll()

Loading…
Cancel
Save