From 4c94570438a3e1369744cceb476a175c62930634 Mon Sep 17 00:00:00 2001 From: Laurent Date: Fri, 8 Mar 2019 14:59:34 +0100 Subject: [PATCH] cleanup code --- .../android/model/realm/Session.kt | 87 +++++++++---------- 1 file changed, 41 insertions(+), 46 deletions(-) diff --git a/app/src/main/java/net/pokeranalytics/android/model/realm/Session.kt b/app/src/main/java/net/pokeranalytics/android/model/realm/Session.kt index e663979e..639dd34e 100644 --- a/app/src/main/java/net/pokeranalytics/android/model/realm/Session.kt +++ b/app/src/main/java/net/pokeranalytics/android/model/realm/Session.kt @@ -65,16 +65,23 @@ open class Session : RealmObject(), SessionInterface, Savable, StaticRowRepresen // Timed interface + /** + * The start date of the session + */ override var startDate: Date = Date() set(value) { field = value this.computeNetDuration() + // nullifies endate when setting the start date after the end date if (this.endDate != null && this.startDate.after(this.endDate)) { this.endDate = null } this.dateChanged() } + /** + * the end date of the session + */ var endDate: Date? = null set(value) { field = value @@ -82,26 +89,23 @@ open class Session : RealmObject(), SessionInterface, Savable, StaticRowRepresen this.dateChanged() } - private fun dateChanged() { - if (this.endDate != null) { - SessionSetManager.updateTimeline(this) - } else if (this.sessionSet != null) { - SessionSetManager.removeFromTimeline(this) - } - } - - override fun endDate(): Date { - return this.endDate ?: Date() - } - + /** + * The break duration of the session + */ override var breakDuration: Long = 0L set(value) { field = value this.computeNetDuration() } + /** + * the net duration of the session, automatically calculated + */ override var netDuration: Long = 0L + /** + * The start date of the break + */ var pauseDate: Date? = null // The time frame of the Session, i.e. the start & end date @@ -169,6 +173,25 @@ open class Session : RealmObject(), SessionInterface, Savable, StaticRowRepresen // The features of the tournament, like Knockout, Shootout, Turbo... var tournamentFeatures: RealmList = RealmList() + /** + * Manages impacts on SessionSets + * Should be called when the start / end date are changed + */ + private fun dateChanged() { + if (this.endDate != null) { + SessionSetManager.updateTimeline(this) + } else if (this.sessionSet != null) { + SessionSetManager.removeFromTimeline(this) + } + } + + /** + * Returns a non-null date for the session + */ + override fun endDate(): Date { + return this.endDate ?: Date() + } + /** * Return if this session is a tournament */ @@ -183,7 +206,6 @@ open class Session : RealmObject(), SessionInterface, Savable, StaticRowRepresen return this.type == Type.CASH_GAME.ordinal } - // Stats @Ignore // SessionInterface value @@ -321,6 +343,9 @@ open class Session : RealmObject(), SessionInterface, Savable, StaticRowRepresen } } + /** + * Utility method to cleanly end a session + */ private fun end() { this.pauseDate = null if (this.endDate == null) { @@ -381,21 +406,13 @@ open class Session : RealmObject(), SessionInterface, Savable, StaticRowRepresen this.sessionSet?.let { set -> - // get all sessions part of the deleted session set - val sessionsFromSet = set.sessions - // cleanup unnecessary related objects set.deleteFromRealm() -// this.timeFrame?.deleteFromRealm() this.result?.deleteFromRealm() - // make sessions recreate/find their session set - sessionsFromSet?.let { sessions -> - sessions.forEach { session -> - // @todo -// session.timeFrame?.notifySessionDateChange(session) - } - } + // Updates the timeline + SessionSetManager.removeFromTimeline(this) + } } @@ -660,10 +677,7 @@ open class Session : RealmObject(), SessionInterface, Savable, StaticRowRepresen cgBigBlind = null } SessionRow.BREAK_TIME -> { -// val timeFrameToUpdate = -// if (timeFrame != null) timeFrame as TimeFrame else realm.createObject(TimeFrame::class.java) this.breakDuration = if (value != null) (value as String).toLong() * 60 * 1000 else 0 -// timeFrame = timeFrameToUpdate } SessionRow.BUY_IN -> { val localResult = if (result != null) result as Result else realm.createObject(Result::class.java) @@ -678,11 +692,6 @@ open class Session : RealmObject(), SessionInterface, Savable, StaticRowRepresen } else { localResult.cashout = (value as String).toDouble() this.end() - -// val timeFrameToUpdate = -// if (timeFrame != null) timeFrame as TimeFrame else realm.createObject(TimeFrame::class.java) -// timeFrameToUpdate.setEnd(Date()) -// timeFrame = timeFrameToUpdate } result = localResult @@ -692,11 +701,6 @@ open class Session : RealmObject(), SessionInterface, Savable, StaticRowRepresen SessionRow.END_DATE -> if (value is Date?) { this.endDate = value -// val timeFrameToUpdate = -// if (timeFrame != null) timeFrame as TimeFrame else realm.createObject(TimeFrame::class.java) -//// timeFrameToUpdate.setDate(null, value) -// timeFrameToUpdate.setEnd(value) -// timeFrame = timeFrameToUpdate } SessionRow.GAME -> { if (value is ArrayList<*>) { @@ -725,15 +729,6 @@ open class Session : RealmObject(), SessionInterface, Savable, StaticRowRepresen } SessionRow.START_DATE -> if (value is Date) { this.startDate = value -// if (value == null) { -// timeFrame = null -// } else { -// val timeFrameToUpdate = -// if (timeFrame != null) timeFrame as TimeFrame else realm.createObject(TimeFrame::class.java) -// timeFrameToUpdate.setStart(value) -//// timeFrameToUpdate.setDate(value, null) -// timeFrame = timeFrameToUpdate -// } } SessionRow.TABLE_SIZE -> tableSize = value as Int? SessionRow.TIPS -> {