package net.pokeranalytics.android.calculus import io.realm.Realm import io.realm.RealmResults import net.pokeranalytics.android.model.filter.Query import net.pokeranalytics.android.model.filter.QueryCondition import net.pokeranalytics.android.model.realm.ComputableResult import net.pokeranalytics.android.model.realm.Filter import net.pokeranalytics.android.model.realm.SessionSet /** * A sessionGroup of computable items identified by a name */ class ComputableGroup(var query: Query, var displayedStats: List? = null) { /** * A subgroup used to compute stat variation */ var comparedGroup: ComputableGroup? = null /** * The computed statIds of the comparable sessionGroup */ var comparedComputedResults: ComputedResults? = null /** * A list of _conditions to get */ val conditions: List get() { return this.query.conditions } /** * The list of endedSessions to compute */ private var _computables: RealmResults? = null /** * Retrieves the computables on the relative [realm] filtered with the provided [conditions] */ fun computables(realm: Realm, sorted: Boolean = false): RealmResults { // if computables exists and is valid (previous realm not closed) this._computables?.let { if (it.isValid) { return it } } val sortedField = if (sorted) "session.startDate" else null val computables = Filter.queryOn(realm, this.query, sortedField) this._computables = computables return computables } /** * The list of sets to compute */ private var _sessionSets: RealmResults? = null /** * Retrieves the session sets on the relative [realm] filtered with the provided [conditions] */ fun sessionSets(realm: Realm, sorted: Boolean = false): RealmResults { // if computables exists and is valid (previous realm not closed) this._sessionSets?.let { if (it.isValid) { return it } } val sortedField = if (sorted) SessionSet.Field.START_DATE.identifier else null val sets = Filter.queryOn(realm, this.query, sortedField) this._sessionSets = sets return sets } fun cleanup() { this._computables = null this._sessionSets = null } val isEmpty: Boolean get() { return this._computables?.isEmpty() ?: true } }