|
|
|
|
@ -6,6 +6,8 @@ import io.realm.RealmResults |
|
|
|
|
import net.pokeranalytics.android.exceptions.ModelException |
|
|
|
|
import net.pokeranalytics.android.model.realm.Session |
|
|
|
|
import net.pokeranalytics.android.model.realm.SessionSet |
|
|
|
|
import net.pokeranalytics.android.util.extensions.findById |
|
|
|
|
import timber.log.Timber |
|
|
|
|
import kotlin.math.max |
|
|
|
|
|
|
|
|
|
class CorruptSessionSetException(message: String) : Exception(message) |
|
|
|
|
@ -24,7 +26,7 @@ object SessionSetManager { |
|
|
|
|
|
|
|
|
|
val realm = Realm.getDefaultInstance() |
|
|
|
|
|
|
|
|
|
this.sessions = realm.where(Session::class.java).findAll() |
|
|
|
|
this.sessions = realm.where(Session::class.java).findAllAsync() |
|
|
|
|
this.sessions.addChangeListener { sessions, changeSet -> |
|
|
|
|
changeSet.insertions.forEach { index -> |
|
|
|
|
sessions[index]?.let { this.sessionsToProcess.add(it) } |
|
|
|
|
@ -33,7 +35,11 @@ object SessionSetManager { |
|
|
|
|
sessions[index]?.let { this.sessionsToProcess.add(it) } |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
processSessions() |
|
|
|
|
val sessionIds = this.sessionsToProcess.map { it.id } |
|
|
|
|
realm.executeTransactionAsync { asyncRealm -> |
|
|
|
|
processSessions(asyncRealm, sessionIds) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
realm.close() |
|
|
|
|
@ -43,14 +49,25 @@ object SessionSetManager { |
|
|
|
|
// this.sessionsToProcess.add(session) |
|
|
|
|
// } |
|
|
|
|
|
|
|
|
|
private fun processSessions() { |
|
|
|
|
for (session in this.sessionsToProcess) { |
|
|
|
|
if (session.startDate != null && session.endDate != null) { |
|
|
|
|
this.updateTimeline(session) |
|
|
|
|
} else if (session.sessionSet != null) { |
|
|
|
|
removeFromTimeline(session) |
|
|
|
|
private fun processSessions(realm: Realm, sessionIds: List<String>) { |
|
|
|
|
|
|
|
|
|
for (sessionId in sessionIds) { |
|
|
|
|
realm.findById<Session>(sessionId)?.let { session -> |
|
|
|
|
if (session.startDate != null && session.endDate != null) { |
|
|
|
|
updateTimeline(session) |
|
|
|
|
} else if (session.sessionSet != null) { |
|
|
|
|
removeFromTimeline(session) |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// for (session in this.sessionsToProcess) { |
|
|
|
|
// if (session.startDate != null && session.endDate != null) { |
|
|
|
|
// this.updateTimeline(realm, session) |
|
|
|
|
// } else if (session.sessionSet != null) { |
|
|
|
|
// removeFromTimeline(realm, session) |
|
|
|
|
// } |
|
|
|
|
// } |
|
|
|
|
this.sessionsToProcess.clear() |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@ -59,6 +76,8 @@ object SessionSetManager { |
|
|
|
|
*/ |
|
|
|
|
fun updateTimeline(session: Session) { |
|
|
|
|
|
|
|
|
|
Timber.d("updateTimeline") |
|
|
|
|
|
|
|
|
|
// if (!session.realm.isInTransaction) { |
|
|
|
|
// throw PAIllegalStateException("realm should be in transaction at this point") |
|
|
|
|
// } |
|
|
|
|
@ -73,6 +92,14 @@ object SessionSetManager { |
|
|
|
|
val sessionSets = this.matchingSets(session) |
|
|
|
|
cleanupSessionSets(session, sessionSets) |
|
|
|
|
|
|
|
|
|
// val sessionId = session.id |
|
|
|
|
// realm.executeTransactionAsync { asyncRealm -> |
|
|
|
|
// asyncRealm.findById<Session>(sessionId)?.let { s -> |
|
|
|
|
// val sessionSets = this.matchingSets(session) |
|
|
|
|
// cleanupSessionSets(session, sessionSets) |
|
|
|
|
// } |
|
|
|
|
// } |
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
private fun matchingSets(session: Session): RealmResults<SessionSet> { |
|
|
|
|
@ -125,12 +152,10 @@ object SessionSetManager { |
|
|
|
|
* Update the global timeline using the impacted [sessionSets] and the updated [session] |
|
|
|
|
*/ |
|
|
|
|
private fun updateTimeFrames(sessionSets: RealmResults<SessionSet>, session: Session) { |
|
|
|
|
|
|
|
|
|
when (sessionSets.size) { |
|
|
|
|
0 -> this.createOrUpdateSessionSet(session) |
|
|
|
|
else -> this.mergeSessionGroups(session, sessionSets) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
@ -152,7 +177,7 @@ object SessionSetManager { |
|
|
|
|
* Create a set and affect it to the [session] |
|
|
|
|
*/ |
|
|
|
|
private fun createSessionSet(session: Session) { |
|
|
|
|
val set = SessionSet() //.newInstance(session.realm) |
|
|
|
|
val set = SessionSet.newInstance(session.realm) |
|
|
|
|
set.startDate = session.startDate!! |
|
|
|
|
set.endDate = session.endDate!! |
|
|
|
|
set.breakDuration = session.breakDuration |
|
|
|
|
@ -196,7 +221,7 @@ object SessionSetManager { |
|
|
|
|
sessionSets.deleteAllFromRealm() |
|
|
|
|
|
|
|
|
|
// Create a new set |
|
|
|
|
val set = SessionSet() //.newInstance(session.realm) |
|
|
|
|
val set = SessionSet.newInstance(session.realm) |
|
|
|
|
set.startDate = startDate |
|
|
|
|
set.endDate = endDate |
|
|
|
|
|
|
|
|
|
|