Manages pauses and break durations

feature/top10
Laurent 7 years ago
parent c2bede9988
commit 17b234a428
  1. 3
      app/src/main/java/net/pokeranalytics/android/model/interfaces/Timed.kt
  2. 21
      app/src/main/java/net/pokeranalytics/android/model/realm/Session.kt
  3. 8
      app/src/main/java/net/pokeranalytics/android/model/realm/SessionSet.kt
  4. 25
      app/src/main/java/net/pokeranalytics/android/model/utils/SessionSetManager.kt

@ -10,8 +10,11 @@ interface Timed {
var breakDuration: Long var breakDuration: Long
var pauseDate: Date?
var netDuration: Long var netDuration: Long
/** /**
* Computes the net netDuration of the session * Computes the net netDuration of the session
*/ */

@ -106,7 +106,7 @@ open class Session : RealmObject(), SessionInterface, Savable, StaticRowRepresen
/** /**
* The start date of the break * The start date of the break
*/ */
var pauseDate: Date? = null override var pauseDate: Date? = null
// The time frame of the Session, i.e. the start & end date // The time frame of the Session, i.e. the start & end date
// var timeFrame: TimeFrame? = null // var timeFrame: TimeFrame? = null
@ -288,14 +288,14 @@ open class Session : RealmObject(), SessionInterface, Savable, StaticRowRepresen
if (this.tournamentEntryFee != null) { if (this.tournamentEntryFee != null) {
this.result?.buyin = this.tournamentEntryFee this.result?.buyin = this.tournamentEntryFee
} }
// val sessionTimeFrame = this.timeFrame ?: realm.createObject(TimeFrame::class.java)
// sessionTimeFrame.setStart(Date())
// sessionTimeFrame.setDate(Date(), null)
// this.timeFrame = sessionTimeFrame
} }
SessionState.PAUSED -> { SessionState.PAUSED -> {
// this.timeFrame?.paused = false val pauseDate = this.pauseDate
if (pauseDate != null) {
this.breakDuration += Date().time - pauseDate.time
} else {
throw IllegalStateException("When resuming, the pause date must be set")
}
this.pauseDate = null this.pauseDate = null
} }
else -> { else -> {
@ -312,10 +312,9 @@ open class Session : RealmObject(), SessionInterface, Savable, StaticRowRepresen
realm.executeTransaction { realm.executeTransaction {
when (getState()) { when (getState()) {
SessionState.STARTED -> { SessionState.STARTED -> {
// this.?.paused = true
this.pauseDate = Date() this.pauseDate = Date()
} }
else -> throw IllegalStateException("unmanaged state") else -> throw IllegalStateException("Pausing a session in an unmanaged state")
} }
} }
} }
@ -328,10 +327,6 @@ open class Session : RealmObject(), SessionInterface, Savable, StaticRowRepresen
when (getState()) { when (getState()) {
SessionState.STARTED, SessionState.PAUSED -> { SessionState.STARTED, SessionState.PAUSED -> {
this.end() this.end()
// this.timeFrame?.paused = false
// this.pauseDate = null
// this.endDate = Date()
// this.timeFrame?.setDate(null, Date())
} }
else -> throw Exception("Stopping session in unmanaged state") else -> throw Exception("Stopping session in unmanaged state")
} }

@ -37,6 +37,14 @@ open class SessionSet : RealmObject(), Timed {
this.computeNetDuration() this.computeNetDuration()
} }
/**
* The start date of the break
*/
override var pauseDate: Date? = null
/**
* the net duration of the set
*/
override var netDuration: Long = 0L override var netDuration: Long = 0L
companion object { companion object {

@ -4,11 +4,19 @@ import io.realm.RealmQuery
import io.realm.RealmResults import io.realm.RealmResults
import net.pokeranalytics.android.model.realm.Session import net.pokeranalytics.android.model.realm.Session
import net.pokeranalytics.android.model.realm.SessionSet import net.pokeranalytics.android.model.realm.SessionSet
import kotlin.math.max
/**
* The manager is in charge of updating the abstract concept of timeline,
* representing the sequenced time frames where the user plays.
*/
class SessionSetManager { class SessionSetManager {
companion object { companion object {
/**
* Updates the global timeline using the updated [session]
*/
fun updateTimeline(session: Session) { fun updateTimeline(session: Session) {
if (!session.realm.isInTransaction) { if (!session.realm.isInTransaction) {
@ -42,7 +50,7 @@ class SessionSetManager {
} }
/** /**
* Update Time frames from sets * Update the global timeline using the impacted [sessionSets] and the updated [session]
*/ */
private fun updateTimeFrames(sessionSets: RealmResults<SessionSet>, session: Session) { private fun updateTimeFrames(sessionSets: RealmResults<SessionSet>, session: Session) {
@ -53,6 +61,9 @@ class SessionSetManager {
} }
/**
* Creates or update the session set for the [session]
*/
private fun createOrUpdateSessionSet(session: Session) { private fun createOrUpdateSessionSet(session: Session) {
val set = session.sessionSet val set = session.sessionSet
@ -65,10 +76,14 @@ class SessionSetManager {
} }
/**
* Create a set and affect it to the [session]
*/
private fun createSessionSet(session: Session) { private fun createSessionSet(session: Session) {
val set: SessionSet = SessionSet.newInstance(session.realm) val set: SessionSet = SessionSet.newInstance(session.realm)
set.startDate = session.startDate!! set.startDate = session.startDate!!
set.endDate = session.endDate!! set.endDate = session.endDate!!
set.breakDuration = session.breakDuration
session.sessionSet = set session.sessionSet = set
} }
@ -109,11 +124,17 @@ class SessionSetManager {
session.sessionSet = set session.sessionSet = set
// Add all orphan endedSessions // Add all orphan endedSessions
sessions.forEach { it.sessionSet = set } sessions.forEach { session ->
session.sessionSet = set
set.breakDuration = max(set.breakDuration, session.breakDuration)
}
// Timber.d("netDuration 3 = : ${set.timeFrame?.netDuration}") // Timber.d("netDuration 3 = : ${set.timeFrame?.netDuration}")
} }
/**
* Removes the [session] from the timeline
*/
fun removeFromTimeline(session: Session) { fun removeFromTimeline(session: Session) {
if (!session.realm.isInTransaction) { if (!session.realm.isInTransaction) {

Loading…
Cancel
Save