|
|
|
|
@ -1,12 +1,13 @@ |
|
|
|
|
package net.pokeranalytics.android.model.utils |
|
|
|
|
package net.pokeranalytics.android.calculus |
|
|
|
|
|
|
|
|
|
import android.content.Context |
|
|
|
|
import io.realm.Realm |
|
|
|
|
import io.realm.RealmQuery |
|
|
|
|
import io.realm.RealmResults |
|
|
|
|
import net.pokeranalytics.android.calculus.ReportWhistleBlower |
|
|
|
|
import io.realm.kotlin.where |
|
|
|
|
import net.pokeranalytics.android.exceptions.ModelException |
|
|
|
|
import net.pokeranalytics.android.exceptions.PAIllegalStateException |
|
|
|
|
import net.pokeranalytics.android.model.realm.Currency |
|
|
|
|
import net.pokeranalytics.android.model.realm.Session |
|
|
|
|
import net.pokeranalytics.android.model.realm.SessionSet |
|
|
|
|
import net.pokeranalytics.android.util.extensions.findById |
|
|
|
|
@ -20,17 +21,14 @@ class CorruptSessionSetException(message: String) : Exception(message) |
|
|
|
|
* The manager is in charge of updating the abstract concept of timeline, |
|
|
|
|
* representing the sequenced time frames where the user plays. |
|
|
|
|
*/ |
|
|
|
|
object SessionManager { |
|
|
|
|
object DataManager { |
|
|
|
|
|
|
|
|
|
private var sessions: RealmResults<Session>? = null |
|
|
|
|
// private var results: RealmResults<Result>? = null |
|
|
|
|
private var currencies: RealmResults<Currency>? = null |
|
|
|
|
|
|
|
|
|
private var dateModifiedSessionIds: MutableSet<String> = mutableSetOf() |
|
|
|
|
// private var netModifiedSessionIds: MutableSet<String> = mutableSetOf() |
|
|
|
|
private var statsToComputeSessionIds: MutableSet<String> = mutableSetOf() |
|
|
|
|
|
|
|
|
|
var context: Context? = null |
|
|
|
|
private set |
|
|
|
|
private var changedCurrencies: MutableSet<String> = mutableSetOf() |
|
|
|
|
|
|
|
|
|
var reportWhistleBlower: ReportWhistleBlower? = null |
|
|
|
|
|
|
|
|
|
@ -38,67 +36,79 @@ object SessionManager { |
|
|
|
|
|
|
|
|
|
val realm = Realm.getDefaultInstance() |
|
|
|
|
|
|
|
|
|
this.sessions = realm.where(Session::class.java).findAllAsync() |
|
|
|
|
this.sessions?.addChangeListener { results -> |
|
|
|
|
|
|
|
|
|
if (this.statsToComputeSessionIds.isNotEmpty()) { |
|
|
|
|
results.realm.writeAsync { asyncRealm -> |
|
|
|
|
for (sessionId in statsToComputeSessionIds) { |
|
|
|
|
Timber.d("Session Manager > compute stats") |
|
|
|
|
asyncRealm.findById<Session>(sessionId)?.let { session -> |
|
|
|
|
session.computeStats() |
|
|
|
|
session.sessionSet?.computeStats() |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
this.statsToComputeSessionIds.clear() |
|
|
|
|
} |
|
|
|
|
sessions = realm.where(Session::class.java).findAllAsync() |
|
|
|
|
sessions?.addChangeListener { results -> |
|
|
|
|
results.realm.writeAsync { asyncRealm -> |
|
|
|
|
computeStatsIfNecessary(asyncRealm) |
|
|
|
|
computeDatesIfNecessary(asyncRealm) |
|
|
|
|
reportWhistleBlower?.requestReportLaunch() |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
if (this.dateModifiedSessionIds.isNotEmpty()) { |
|
|
|
|
results.realm.writeAsync { asyncRealm -> |
|
|
|
|
for (sessionId in dateModifiedSessionIds) { |
|
|
|
|
Timber.d("Session Manager > manage dates") |
|
|
|
|
asyncRealm.findById<Session>(sessionId)?.let { session -> |
|
|
|
|
if (session.endDate != null) { |
|
|
|
|
this.updateTimeline(session) |
|
|
|
|
} else if (session.sessionSet != null) { |
|
|
|
|
this.removeFromTimeline(session) |
|
|
|
|
} |
|
|
|
|
this.currencies = realm.where(Currency::class.java).findAll() |
|
|
|
|
this.currencies?.addChangeListener { _, _ -> |
|
|
|
|
if (changedCurrencies.isNotEmpty()) { |
|
|
|
|
realm.writeAsync { asyncRealm -> |
|
|
|
|
for (currencyId in this.changedCurrencies) { |
|
|
|
|
asyncRealm.findById<Currency>(currencyId)?.let { currency -> |
|
|
|
|
Timber.d("Compute currency ${currency.code} ") |
|
|
|
|
currency.refreshRelatedRatedValues() |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
this.dateModifiedSessionIds.clear() |
|
|
|
|
changedCurrencies.clear() |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
this.reportWhistleBlower?.requestReportLaunch() |
|
|
|
|
|
|
|
|
|
// if (this.netModifiedSessionIds.isNotEmpty()) { |
|
|
|
|
// results.realm.writeAsync { asyncRealm -> |
|
|
|
|
// for (sessionId in netModifiedSessionIds) { |
|
|
|
|
// asyncRealm.findById<Session>(sessionId)?.computeNet(false) |
|
|
|
|
// } |
|
|
|
|
// this.dateModifiedSessionIds.clear() |
|
|
|
|
// } |
|
|
|
|
// } |
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
realm.close() |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
fun configure(context: Context) { |
|
|
|
|
this.context = context |
|
|
|
|
this.reportWhistleBlower = ReportWhistleBlower(context) |
|
|
|
|
reportWhistleBlower = ReportWhistleBlower(context) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
fun sessionToCompute(session: Session) { |
|
|
|
|
Timber.d("sessionToCompute") |
|
|
|
|
statsToComputeSessionIds.add(session.id) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
fun sessionDateChanged(session: Session) { |
|
|
|
|
this.dateModifiedSessionIds.add(session.id) |
|
|
|
|
Timber.d("sessionDateChanged") |
|
|
|
|
dateModifiedSessionIds.add(session.id) |
|
|
|
|
} |
|
|
|
|
// fun sessionNetChanged(session: Session) { |
|
|
|
|
// this.netModifiedSessionIds.add(session.id) |
|
|
|
|
// } |
|
|
|
|
fun sessionToCompute(session: Session) { |
|
|
|
|
this.statsToComputeSessionIds.add(session.id) |
|
|
|
|
|
|
|
|
|
fun currencyToCompute(currency: Currency) { |
|
|
|
|
Timber.d("sessionToCompute") |
|
|
|
|
changedCurrencies.add(currency.id) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
private fun computeStatsIfNecessary(realm: Realm) { |
|
|
|
|
if (statsToComputeSessionIds.isNotEmpty()) { |
|
|
|
|
for (sessionId in statsToComputeSessionIds) { |
|
|
|
|
Timber.d("Session Manager > compute stats") |
|
|
|
|
realm.findById<Session>(sessionId)?.let { session -> |
|
|
|
|
session.computeStats() |
|
|
|
|
session.sessionSet?.computeStats() |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
statsToComputeSessionIds.clear() |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
private fun computeDatesIfNecessary(realm: Realm) { |
|
|
|
|
if (dateModifiedSessionIds.isNotEmpty()) { |
|
|
|
|
for (sessionId in dateModifiedSessionIds) { |
|
|
|
|
Timber.d("Session Manager > manage dates") |
|
|
|
|
realm.findById<Session>(sessionId)?.let { session -> |
|
|
|
|
if (session.endDate != null) { |
|
|
|
|
updateTimeline(session) |
|
|
|
|
} else if (session.sessionSet != null) { |
|
|
|
|
removeFromTimeline(session) |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
dateModifiedSessionIds.clear() |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
@ -119,7 +129,7 @@ object SessionManager { |
|
|
|
|
throw ModelException("End date should never be null here") |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
val sessionSets = this.matchingSets(session) |
|
|
|
|
val sessionSets = matchingSets(session) |
|
|
|
|
cleanupSessionSets(session, sessionSets) |
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
@ -164,11 +174,9 @@ object SessionManager { |
|
|
|
|
|
|
|
|
|
allImpactedSessions.forEach { impactedSession -> |
|
|
|
|
val sets = matchingSets(impactedSession) |
|
|
|
|
this.updateTimeFrames(sets, impactedSession) |
|
|
|
|
updateTimeFrames(sets, impactedSession) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// Timber.d("netDuration 3 = : ${set.timeFrame?.netDuration}") |
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
@ -178,8 +186,8 @@ object SessionManager { |
|
|
|
|
Timber.d("updateTimeFrames...") |
|
|
|
|
|
|
|
|
|
when (sessionSets.size) { |
|
|
|
|
0 -> this.createOrUpdateSessionSet(session) |
|
|
|
|
else -> this.mergeSessionGroups(session, sessionSets) |
|
|
|
|
0 -> createOrUpdateSessionSet(session) |
|
|
|
|
else -> mergeSessionGroups(session, sessionSets) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
@ -195,7 +203,7 @@ object SessionManager { |
|
|
|
|
set.startDate = session.startDate!! // tested above |
|
|
|
|
set.endDate = session.endDate!! |
|
|
|
|
} else { |
|
|
|
|
this.createSessionSet(session) |
|
|
|
|
createSessionSet(session) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
@ -206,12 +214,20 @@ object SessionManager { |
|
|
|
|
private fun createSessionSet(session: Session) { |
|
|
|
|
Timber.d("createSessionSet...") |
|
|
|
|
|
|
|
|
|
val set: SessionSet = SessionSet.newInstance(session.realm) |
|
|
|
|
val realm = session.realm |
|
|
|
|
val set = SessionSet.newInstance(realm) |
|
|
|
|
set.startDate = session.startDate!! |
|
|
|
|
set.endDate = session.endDate!! |
|
|
|
|
set.breakDuration = session.breakDuration |
|
|
|
|
session.sessionSet = set |
|
|
|
|
set.computeStats() |
|
|
|
|
|
|
|
|
|
Timber.d("SET SESSION count = ${set.sessions?.size}") |
|
|
|
|
|
|
|
|
|
val t = 0 |
|
|
|
|
val f = realm.where<SessionSet>().equalTo("sessions.type", t).findAll() |
|
|
|
|
Timber.d("CASH SET COUNT = ${f.size}") |
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
@ -288,6 +304,7 @@ object SessionManager { |
|
|
|
|
sessionSet.sessions?.asIterable()?.let { sessions.addAll(it) } |
|
|
|
|
sessions.remove(session) |
|
|
|
|
|
|
|
|
|
Timber.d(">>> sessionSet.deleteFromRealm") |
|
|
|
|
sessionSet.deleteFromRealm() |
|
|
|
|
|
|
|
|
|
sessions.forEach { |