|
|
|
|
@ -1,14 +1,17 @@ |
|
|
|
|
package net.pokeranalytics.android.model.utils |
|
|
|
|
|
|
|
|
|
import android.content.Context |
|
|
|
|
import io.realm.Realm |
|
|
|
|
import io.realm.RealmQuery |
|
|
|
|
import io.realm.RealmResults |
|
|
|
|
import net.pokeranalytics.android.calculus.ReportWhistleBlower |
|
|
|
|
import net.pokeranalytics.android.exceptions.ModelException |
|
|
|
|
import net.pokeranalytics.android.exceptions.PAIllegalStateException |
|
|
|
|
import net.pokeranalytics.android.model.realm.Session |
|
|
|
|
import net.pokeranalytics.android.model.realm.SessionSet |
|
|
|
|
import net.pokeranalytics.android.util.extensions.findById |
|
|
|
|
import net.pokeranalytics.android.util.extensions.writeAsync |
|
|
|
|
import timber.log.Timber |
|
|
|
|
import kotlin.math.max |
|
|
|
|
|
|
|
|
|
class CorruptSessionSetException(message: String) : Exception(message) |
|
|
|
|
@ -26,16 +29,35 @@ object SessionManager { |
|
|
|
|
// private var netModifiedSessionIds: MutableSet<String> = mutableSetOf() |
|
|
|
|
private var statsToComputeSessionIds: MutableSet<String> = mutableSetOf() |
|
|
|
|
|
|
|
|
|
var context: Context? = null |
|
|
|
|
private set |
|
|
|
|
|
|
|
|
|
var reportWhistleBlower: ReportWhistleBlower? = null |
|
|
|
|
|
|
|
|
|
init { |
|
|
|
|
|
|
|
|
|
val realm = Realm.getDefaultInstance() |
|
|
|
|
|
|
|
|
|
this.sessions = realm.where(Session::class.java).findAllAsync() |
|
|
|
|
this.sessions?.addChangeListener { results, changeSet -> |
|
|
|
|
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() |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
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) |
|
|
|
|
@ -48,6 +70,8 @@ object SessionManager { |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
this.reportWhistleBlower?.requestReportLaunch() |
|
|
|
|
|
|
|
|
|
// if (this.netModifiedSessionIds.isNotEmpty()) { |
|
|
|
|
// results.realm.writeAsync { asyncRealm -> |
|
|
|
|
// for (sessionId in netModifiedSessionIds) { |
|
|
|
|
@ -57,22 +81,14 @@ object SessionManager { |
|
|
|
|
// } |
|
|
|
|
// } |
|
|
|
|
|
|
|
|
|
if (this.statsToComputeSessionIds.isNotEmpty()) { |
|
|
|
|
results.realm.writeAsync { asyncRealm -> |
|
|
|
|
for (sessionId in statsToComputeSessionIds) { |
|
|
|
|
asyncRealm.findById<Session>(sessionId)?.computeStats() |
|
|
|
|
} |
|
|
|
|
this.statsToComputeSessionIds.clear() |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
realm.close() |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
fun create() { |
|
|
|
|
// empty, juste called to make sure the singleton is initialized |
|
|
|
|
fun configure(context: Context) { |
|
|
|
|
this.context = context |
|
|
|
|
this.reportWhistleBlower = ReportWhistleBlower(context) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
fun sessionDateChanged(session: Session) { |
|
|
|
|
@ -90,6 +106,8 @@ object SessionManager { |
|
|
|
|
*/ |
|
|
|
|
fun updateTimeline(session: Session) { |
|
|
|
|
|
|
|
|
|
Timber.d("updateTimeline...") |
|
|
|
|
|
|
|
|
|
if (!session.realm.isInTransaction) { |
|
|
|
|
throw PAIllegalStateException("realm should be in transaction at this point") |
|
|
|
|
} |
|
|
|
|
@ -132,6 +150,7 @@ object SessionManager { |
|
|
|
|
* Does that by deleting then recreating |
|
|
|
|
*/ |
|
|
|
|
private fun cleanupSessionSets(session: Session, sessionSets: RealmResults<SessionSet>) { |
|
|
|
|
Timber.d("cleanupSessionSets...") |
|
|
|
|
|
|
|
|
|
// get all endedSessions from sets |
|
|
|
|
val allImpactedSessions = mutableSetOf<Session>() |
|
|
|
|
@ -152,11 +171,11 @@ object SessionManager { |
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* Update the global timeline using the impacted [sessionSets] and the updated [session] |
|
|
|
|
*/ |
|
|
|
|
private fun updateTimeFrames(sessionSets: RealmResults<SessionSet>, session: Session) { |
|
|
|
|
Timber.d("updateTimeFrames...") |
|
|
|
|
|
|
|
|
|
when (sessionSets.size) { |
|
|
|
|
0 -> this.createOrUpdateSessionSet(session) |
|
|
|
|
@ -169,6 +188,7 @@ object SessionManager { |
|
|
|
|
* Creates or update the session set for the [session] |
|
|
|
|
*/ |
|
|
|
|
private fun createOrUpdateSessionSet(session: Session) { |
|
|
|
|
Timber.d("createOrUpdateSessionSet...") |
|
|
|
|
|
|
|
|
|
val set = session.sessionSet |
|
|
|
|
if (set != null) { |
|
|
|
|
@ -184,6 +204,8 @@ object SessionManager { |
|
|
|
|
* Create a set and affect it to the [session] |
|
|
|
|
*/ |
|
|
|
|
private fun createSessionSet(session: Session) { |
|
|
|
|
Timber.d("createSessionSet...") |
|
|
|
|
|
|
|
|
|
val set: SessionSet = SessionSet.newInstance(session.realm) |
|
|
|
|
set.startDate = session.startDate!! |
|
|
|
|
set.endDate = session.endDate!! |
|
|
|
|
@ -198,6 +220,8 @@ object SessionManager { |
|
|
|
|
*/ |
|
|
|
|
private fun mergeSessionGroups(session: Session, sessionSets: RealmResults<SessionSet>) { |
|
|
|
|
|
|
|
|
|
Timber.d("mergeSessionGroups") |
|
|
|
|
|
|
|
|
|
var startDate = session.startDate!! |
|
|
|
|
var endDate = session.endDate!! |
|
|
|
|
|
|
|
|
|
@ -251,6 +275,8 @@ object SessionManager { |
|
|
|
|
*/ |
|
|
|
|
fun removeFromTimeline(session: Session) { |
|
|
|
|
|
|
|
|
|
Timber.d("removeFromTimeline") |
|
|
|
|
|
|
|
|
|
if (!session.realm.isInTransaction) { |
|
|
|
|
throw PAIllegalStateException("realm should be in transaction at this point") |
|
|
|
|
} |
|
|
|
|
|