From c8df4032a0f630ed9cbe74591b20ea1d880c10f3 Mon Sep 17 00:00:00 2001 From: Laurent Date: Fri, 8 Mar 2019 15:53:27 +0100 Subject: [PATCH] Make the Session start date nullable + fixes --- .../android/PokerAnalyticsApplication.kt | 2 +- .../model/extensions/SessionExtensions.kt | 40 ++++++------------- .../android/model/interfaces/Timed.kt | 8 +++- .../android/model/realm/Session.kt | 21 +++++----- .../android/model/realm/SessionSet.kt | 6 ++- .../android/model/utils/SessionSetManager.kt | 12 +++--- .../android/ui/view/SessionRowView.kt | 6 +-- .../android/util/DateExtension.kt | 2 +- 8 files changed, 47 insertions(+), 50 deletions(-) diff --git a/app/src/main/java/net/pokeranalytics/android/PokerAnalyticsApplication.kt b/app/src/main/java/net/pokeranalytics/android/PokerAnalyticsApplication.kt index 97d17d46..fb3a36d0 100644 --- a/app/src/main/java/net/pokeranalytics/android/PokerAnalyticsApplication.kt +++ b/app/src/main/java/net/pokeranalytics/android/PokerAnalyticsApplication.kt @@ -63,7 +63,7 @@ class PokerAnalyticsApplication : Application() { this.createDefaultData() if (BuildConfig.DEBUG) { - this.createFakeSessions() // debug +// this.createFakeSessions() // debug } } diff --git a/app/src/main/java/net/pokeranalytics/android/model/extensions/SessionExtensions.kt b/app/src/main/java/net/pokeranalytics/android/model/extensions/SessionExtensions.kt index ee31132d..64967c57 100644 --- a/app/src/main/java/net/pokeranalytics/android/model/extensions/SessionExtensions.kt +++ b/app/src/main/java/net/pokeranalytics/android/model/extensions/SessionExtensions.kt @@ -8,8 +8,7 @@ enum class SessionState { PLANNED, STARTED, PAUSED, - FINISHED, - INVALID; + FINISHED } /** @@ -21,32 +20,19 @@ fun Session.getState(): SessionState { // if (timeFrame == null) { // return SessionState.PENDING // } - - if (startDate > Date()) { - return SessionState.PLANNED - } else if (endDate != null) { - return SessionState.FINISHED - } else if (this.pauseDate != null) { - return SessionState.PAUSED + val start = this.startDate + if (start == null) { + return SessionState.PENDING } else { - return SessionState.STARTED + if (start > Date()) { + return SessionState.PLANNED + } else if (this.endDate != null) { + return SessionState.FINISHED + } else if (this.pauseDate != null) { + return SessionState.PAUSED + } else { + return SessionState.STARTED + } } - -// val endDate = timeFrame?.endDate -// timeFrame?.let {sessionTimeFrame -> -// timeFrame?.startDate?.let {startDate -> -// if (startDate > Date()) { -// return SessionState.PLANNED -// } else if (endDate != null) { -// return SessionState.FINISHED -// } else if (sessionTimeFrame.paused) { -// return SessionState.PAUSED -// } else { -// return SessionState.STARTED -// } -// } -// } - -// return SessionState.INVALID } diff --git a/app/src/main/java/net/pokeranalytics/android/model/interfaces/Timed.kt b/app/src/main/java/net/pokeranalytics/android/model/interfaces/Timed.kt index a63d67c6..49256174 100644 --- a/app/src/main/java/net/pokeranalytics/android/model/interfaces/Timed.kt +++ b/app/src/main/java/net/pokeranalytics/android/model/interfaces/Timed.kt @@ -4,7 +4,7 @@ import java.util.* interface Timed { - var startDate: Date + fun startDate() : Date? fun endDate() : Date @@ -16,7 +16,11 @@ interface Timed { * Computes the net netDuration of the session */ fun computeNetDuration() { - this.netDuration = this.endDate().time - this.startDate.time - this.breakDuration + this.startDate()?.let { start -> + this.netDuration = this.endDate().time - start.time - this.breakDuration + } ?: run { + this.netDuration = 0L + } } var hourlyDuration: Double 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 1c026762..d69b3bb4 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 @@ -68,12 +68,12 @@ open class Session : RealmObject(), SessionInterface, Savable, StaticRowRepresen /** * The start date of the session */ - override var startDate: Date = Date() + var startDate: Date? = null 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)) { + if (value != null && this.endDate != null && value.after(this.endDate)) { this.endDate = null } this.dateChanged() @@ -192,6 +192,10 @@ open class Session : RealmObject(), SessionInterface, Savable, StaticRowRepresen return this.endDate ?: Date() } + override fun startDate(): Date? { + return this.startDate + } + /** * Return if this session is a tournament */ @@ -251,7 +255,7 @@ open class Session : RealmObject(), SessionInterface, Savable, StaticRowRepresen val hourlyRate: Double get() { this.result?.let { result -> - return result.net / this.netDuration.toDouble() + return result.net / this.hourlyDuration } throw ModelException("Session should have an existing Result relationship") } @@ -358,9 +362,8 @@ open class Session : RealmObject(), SessionInterface, Savable, StaticRowRepresen /** * Return the netDuration of the current session */ - fun getDuration(): String { - val enDate = this.endDate ?: Date() - return startDate.getDuration(enDate) + fun getFormattedDuration(): String { + return this.netDuration.toMinutes() } /** @@ -436,7 +439,7 @@ open class Session : RealmObject(), SessionInterface, Savable, StaticRowRepresen rows.add( HeaderRowRepresentable( RowViewType.HEADER_TITLE_AMOUNT_BIG, - title = getDuration(), + title = getFormattedDuration(), computedStat = ComputedStat(Stat.NETRESULT, result?.net ?: 0.0) ) ) @@ -456,7 +459,7 @@ open class Session : RealmObject(), SessionInterface, Savable, StaticRowRepresen rows.add( HeaderRowRepresentable( RowViewType.HEADER_TITLE_AMOUNT_BIG, - title = getDuration(), + title = getFormattedDuration(), computedStat = ComputedStat(Stat.NETRESULT, result?.net ?: 0.0) ) ) @@ -510,7 +513,7 @@ open class Session : RealmObject(), SessionInterface, Savable, StaticRowRepresen SessionRow.LOCATION -> location?.name ?: NULL_TEXT SessionRow.PLAYERS -> tournamentNumberOfPlayers?.toString() ?: NULL_TEXT SessionRow.POSITION -> result?.tournamentFinalPosition?.toString() ?: NULL_TEXT - SessionRow.START_DATE -> this.startDate.shortDateTime() + SessionRow.START_DATE -> this.startDate?.shortDateTime() ?: NULL_TEXT SessionRow.TABLE_SIZE -> this.tableSize?.let { TableSize(it).localizedTitle(context) } ?: NULL_TEXT SessionRow.TIPS -> result?.tips?.toCurrency() ?: NULL_TEXT SessionRow.TOURNAMENT_TYPE -> tournamentType?.name ?: NULL_TEXT diff --git a/app/src/main/java/net/pokeranalytics/android/model/realm/SessionSet.kt b/app/src/main/java/net/pokeranalytics/android/model/realm/SessionSet.kt index a9d26407..2ef8ca93 100644 --- a/app/src/main/java/net/pokeranalytics/android/model/realm/SessionSet.kt +++ b/app/src/main/java/net/pokeranalytics/android/model/realm/SessionSet.kt @@ -11,7 +11,7 @@ import java.util.* open class SessionSet : RealmObject(), Timed { - override var startDate: Date = Date() + var startDate: Date = Date() set(value) { field = value this.computeNetDuration() @@ -23,6 +23,10 @@ open class SessionSet : RealmObject(), Timed { this.computeNetDuration() } + override fun startDate(): Date? { + return this.startDate + } + override fun endDate(): Date { return this.endDate } diff --git a/app/src/main/java/net/pokeranalytics/android/model/utils/SessionSetManager.kt b/app/src/main/java/net/pokeranalytics/android/model/utils/SessionSetManager.kt index 720fdc01..c3faaaf1 100644 --- a/app/src/main/java/net/pokeranalytics/android/model/utils/SessionSetManager.kt +++ b/app/src/main/java/net/pokeranalytics/android/model/utils/SessionSetManager.kt @@ -15,11 +15,11 @@ class SessionSetManager { throw IllegalStateException("realm should be in transaction at this point") } - if (session.endDate == null) { - throw IllegalStateException("End date should never be null here") + if (session.startDate == null || session.endDate == null) { + throw IllegalStateException("Start or end date should never be null here") } val endDate = session.endDate!! // tested above - val startDate = session.startDate + val startDate = session.startDate!! val realm = session.realm @@ -57,7 +57,7 @@ class SessionSetManager { val set = session.sessionSet if (set != null) { - set.startDate = session.startDate + set.startDate = session.startDate!! // tested above set.endDate = session.endDate!! } else { this.createSessionSet(session) @@ -67,7 +67,7 @@ class SessionSetManager { private fun createSessionSet(session: Session) { val set: SessionSet = SessionSet.newInstance(session.realm) - set.startDate = session.startDate + set.startDate = session.startDate!! set.endDate = session.endDate!! session.sessionSet = set } @@ -78,7 +78,7 @@ class SessionSetManager { */ private fun mergeSessionGroups(session: Session, sessionSets: RealmResults) { - var startDate = session.startDate + var startDate = session.startDate!! var endDate = session.endDate!! // find earlier and later dates from all sets diff --git a/app/src/main/java/net/pokeranalytics/android/ui/view/SessionRowView.kt b/app/src/main/java/net/pokeranalytics/android/ui/view/SessionRowView.kt index ffc14407..ef1e82e8 100644 --- a/app/src/main/java/net/pokeranalytics/android/ui/view/SessionRowView.kt +++ b/app/src/main/java/net/pokeranalytics/android/ui/view/SessionRowView.kt @@ -98,10 +98,10 @@ class SessionRowView : FrameLayout { // rowHistorySession.sessionInfoDurationIcon.isVisible = session.timeFrame != null // rowHistorySession.sessionInfoDurationValue.isVisible = session.timeFrame != null // session.timeFrame?.let { -// rowHistorySession.sessionInfoDurationValue.text = session.getDuration() +// rowHistorySession.sessionInfoDurationValue.text = session.getFormattedDuration() // } - rowHistorySession.sessionInfoDurationValue.text = session.getDuration() + rowHistorySession.sessionInfoDurationValue.text = session.getFormattedDuration() // Location rowHistorySession.sessionInfoLocationIcon.isVisible = session.location != null @@ -129,7 +129,7 @@ class SessionRowView : FrameLayout { rowHistorySession.infoIcon.isVisible = true rowHistorySession.infoIcon.setImageResource(R.drawable.ic_planned) rowHistorySession.infoTitle.isVisible = true - rowHistorySession.infoTitle.text = session.startDate.shortTime() + rowHistorySession.infoTitle.text = session.startDate!!.shortTime() } else { rowHistorySession.gameResult.isVisible = true rowHistorySession.infoIcon.isVisible = false diff --git a/app/src/main/java/net/pokeranalytics/android/util/DateExtension.kt b/app/src/main/java/net/pokeranalytics/android/util/DateExtension.kt index 6029e96b..36c0a677 100644 --- a/app/src/main/java/net/pokeranalytics/android/util/DateExtension.kt +++ b/app/src/main/java/net/pokeranalytics/android/util/DateExtension.kt @@ -85,7 +85,7 @@ fun Date.getMonthAndYear(): String { } // Return the netDuration between two dates -fun Date.getDuration(toDate: Date) : String { +fun Date.getFormattedDuration(toDate: Date) : String { val difference = (toDate.time - this.time).toInt() val numOfDays = (difference / (1000 * 60 * 60 * 24)) val hours = (difference / (1000 * 60 * 60))