|
|
|
@ -1,6 +1,5 @@ |
|
|
|
package net.pokeranalytics.android.model.realm |
|
|
|
package net.pokeranalytics.android.model.realm |
|
|
|
|
|
|
|
|
|
|
|
import io.realm.Realm |
|
|
|
|
|
|
|
import io.realm.RealmObject |
|
|
|
import io.realm.RealmObject |
|
|
|
import io.realm.RealmQuery |
|
|
|
import io.realm.RealmQuery |
|
|
|
import io.realm.RealmResults |
|
|
|
import io.realm.RealmResults |
|
|
|
@ -38,20 +37,20 @@ open class TimeFrame : RealmObject() { |
|
|
|
this.computeDuration() |
|
|
|
this.computeDuration() |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// The break duration |
|
|
|
// The break netDuration |
|
|
|
var breakDuration: Long = 0L |
|
|
|
var breakDuration: Long = 0L |
|
|
|
set(value) { |
|
|
|
set(value) { |
|
|
|
field = value |
|
|
|
field = value |
|
|
|
this.computeDuration() |
|
|
|
this.computeDuration() |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// the total duration |
|
|
|
// the total netDuration |
|
|
|
var duration: Long = 0L |
|
|
|
var netDuration: Long = 0L |
|
|
|
private set |
|
|
|
private set |
|
|
|
|
|
|
|
|
|
|
|
var hourlyDuration: Double = 0.0 |
|
|
|
var hourlyDuration: Double = 0.0 |
|
|
|
get() { |
|
|
|
get() { |
|
|
|
return this.duration / 3600000.0 // 3.6 millions of milliseconds |
|
|
|
return this.netDuration / 3600000.0 // 3.6 millions of milliseconds |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// indicates a state of pause |
|
|
|
// indicates a state of pause |
|
|
|
@ -73,29 +72,35 @@ open class TimeFrame : RealmObject() { |
|
|
|
var set: SessionSet? = null |
|
|
|
var set: SessionSet? = null |
|
|
|
get() = this.sets?.first() |
|
|
|
get() = this.sets?.first() |
|
|
|
|
|
|
|
|
|
|
|
fun setDate(startDate: Date?, endDate: Date?) { |
|
|
|
fun setStart(startDate: Date) { |
|
|
|
|
|
|
|
|
|
|
|
startDate?.let { |
|
|
|
|
|
|
|
this.startDate = startDate |
|
|
|
this.startDate = startDate |
|
|
|
|
|
|
|
this.session?.let { |
|
|
|
|
|
|
|
this.notifySessionDateChange(it) |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
fun setEnd(endDate: Date?) { |
|
|
|
this.endDate = endDate |
|
|
|
this.endDate = endDate |
|
|
|
|
|
|
|
this.session?.let { |
|
|
|
|
|
|
|
this.notifySessionDateChange(it) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
this.computeDuration() |
|
|
|
fun setDate(startDate: Date, endDate: Date?) { |
|
|
|
|
|
|
|
this.startDate = startDate |
|
|
|
|
|
|
|
this.endDate = endDate |
|
|
|
|
|
|
|
|
|
|
|
this.session?.let { |
|
|
|
this.session?.let { |
|
|
|
this.notifySessionDateChange(it) |
|
|
|
this.notifySessionDateChange(it) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* Computes the net duration of the session |
|
|
|
* Computes the net netDuration of the session |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
private fun computeDuration() { |
|
|
|
private fun computeDuration() { |
|
|
|
var endDate: Date = this.endDate ?: Date() |
|
|
|
var endDate: Date = this.endDate ?: Date() |
|
|
|
val netDuration = endDate.time - this.startDate.time - this.breakDuration |
|
|
|
this.netDuration = endDate.time - this.startDate.time - this.breakDuration |
|
|
|
this.duration = netDuration |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
@ -103,25 +108,35 @@ open class TimeFrame : RealmObject() { |
|
|
|
* Makes all necessary changes to keep sequential time frames |
|
|
|
* Makes all necessary changes to keep sequential time frames |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
fun notifySessionDateChange(owner: Session) { |
|
|
|
fun notifySessionDateChange(owner: Session) { |
|
|
|
val realm = Realm.getDefaultInstance() |
|
|
|
|
|
|
|
var query: RealmQuery<SessionSet> = realm.where(SessionSet::class.java) |
|
|
|
var query: RealmQuery<SessionSet> = this.realm.where(SessionSet::class.java) |
|
|
|
query.isNotNull("timeFrame") |
|
|
|
query.isNotNull("timeFrame") |
|
|
|
|
|
|
|
|
|
|
|
// Timber.d("this> sd = : ${this.startDate}, ed = ${this.endDate}") |
|
|
|
// Timber.d("this> sd = : ${this.startDate}, ed = ${this.endDate}") |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
val sets = realm.where(SessionSet::class.java).findAll() |
|
|
|
|
|
|
|
// Timber.d("set count = ${sets.size}") |
|
|
|
|
|
|
|
|
|
|
|
if (this.endDate == null) { |
|
|
|
if (this.endDate == null) { |
|
|
|
query.greaterThan("timeFrame.startDate", this.startDate).or().greaterThan("timeFrame.endDate", this.startDate) |
|
|
|
query.greaterThanOrEqualTo("timeFrame.startDate", this.startDate) |
|
|
|
|
|
|
|
.or() |
|
|
|
|
|
|
|
.greaterThanOrEqualTo("timeFrame.endDate", this.startDate) |
|
|
|
|
|
|
|
.or() |
|
|
|
|
|
|
|
.isNull("timeFrame.endDate") |
|
|
|
} else { |
|
|
|
} else { |
|
|
|
val endDate = this.endDate!! |
|
|
|
val endDate = this.endDate!! |
|
|
|
query |
|
|
|
query |
|
|
|
.lessThan("timeFrame.startDate", this.startDate) |
|
|
|
.lessThanOrEqualTo("timeFrame.startDate", this.startDate) |
|
|
|
.greaterThan("timeFrame.endDate", this.startDate) |
|
|
|
.greaterThanOrEqualTo("timeFrame.endDate", this.startDate) |
|
|
|
|
|
|
|
.or() |
|
|
|
|
|
|
|
.lessThanOrEqualTo("timeFrame.startDate", endDate) |
|
|
|
|
|
|
|
.greaterThanOrEqualTo("timeFrame.endDate", endDate) |
|
|
|
.or() |
|
|
|
.or() |
|
|
|
.lessThan("timeFrame.startDate", endDate) |
|
|
|
.greaterThanOrEqualTo("timeFrame.startDate", this.startDate) |
|
|
|
.greaterThan("timeFrame.endDate", endDate) |
|
|
|
.lessThanOrEqualTo("timeFrame.endDate", endDate) |
|
|
|
.or() |
|
|
|
.or() |
|
|
|
.greaterThan("timeFrame.startDate", this.startDate) |
|
|
|
.isNull("timeFrame.endDate") |
|
|
|
.lessThan("timeFrame.endDate", endDate) |
|
|
|
.lessThanOrEqualTo("timeFrame.startDate", endDate) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
val sessionGroups = query.findAll() |
|
|
|
val sessionGroups = query.findAll() |
|
|
|
@ -137,7 +152,7 @@ open class TimeFrame : RealmObject() { |
|
|
|
|
|
|
|
|
|
|
|
when (sessionSets.size) { |
|
|
|
when (sessionSets.size) { |
|
|
|
0 -> this.createSessionGroup(owner) |
|
|
|
0 -> this.createSessionGroup(owner) |
|
|
|
1 -> this.updateSingleSessionGroup(owner, sessionSets.first()!!) |
|
|
|
1 -> this.updateSessionGroup(owner, sessionSets.first()!!) |
|
|
|
else -> this.mergeSessionGroups(owner, sessionSets) |
|
|
|
else -> this.mergeSessionGroups(owner, sessionSets) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@ -148,9 +163,7 @@ open class TimeFrame : RealmObject() { |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
private fun createSessionGroup(owner: Session) { |
|
|
|
private fun createSessionGroup(owner: Session) { |
|
|
|
|
|
|
|
|
|
|
|
val realm = Realm.getDefaultInstance() |
|
|
|
val set: SessionSet = SessionSet.newInstance(this.realm) |
|
|
|
|
|
|
|
|
|
|
|
val set: SessionSet = SessionSet.newInstance(realm) |
|
|
|
|
|
|
|
set.timeFrame?.let { |
|
|
|
set.timeFrame?.let { |
|
|
|
it.startDate = this.startDate |
|
|
|
it.startDate = this.startDate |
|
|
|
it.endDate = this.endDate |
|
|
|
it.endDate = this.endDate |
|
|
|
@ -160,30 +173,54 @@ open class TimeFrame : RealmObject() { |
|
|
|
|
|
|
|
|
|
|
|
owner.sessionSet = set |
|
|
|
owner.sessionSet = set |
|
|
|
|
|
|
|
|
|
|
|
Timber.d("sd = : ${set.timeFrame?.startDate}, ed = ${set.timeFrame?.endDate}") |
|
|
|
// Timber.d("sd = : ${set.timeFrame?.startDate}, ed = ${set.timeFrame?.endDate}") |
|
|
|
|
|
|
|
Timber.d("netDuration 1 = : ${set.timeFrame?.netDuration}") |
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* Single session sessionGroup update |
|
|
|
* Single SessionSet update, the session might be the owner |
|
|
|
* Changes the sessionGroup timeframe using the current timeframe dates |
|
|
|
* Changes the sessionGroup timeframe using the current timeframe dates |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
private fun updateSingleSessionGroup(owner: Session, sessionSet: SessionSet) { |
|
|
|
private fun updateSessionGroup(owner: Session, sessionSet: SessionSet) { |
|
|
|
|
|
|
|
|
|
|
|
var groupTimeFrame: TimeFrame = sessionSet.timeFrame!! // tested in the query |
|
|
|
var timeFrame: TimeFrame = sessionSet.timeFrame!! // tested in the query |
|
|
|
|
|
|
|
// timeFrame.setDate(this.startDate, this.endDate) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
val sisterSessions = sessionSet.sessions!! // shouldn't crash ever |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// if we have only one session in the set and that it corresponds to the set |
|
|
|
|
|
|
|
if (sessionSet.sessions?.size == 1 && sessionSet.sessions?.first() == owner) { |
|
|
|
|
|
|
|
timeFrame.setDate(this.startDate, this.endDate) |
|
|
|
|
|
|
|
} else { // there are 2+ sessions to manage and possible splits |
|
|
|
|
|
|
|
|
|
|
|
if (this.startDate.before(groupTimeFrame.startDate)) { |
|
|
|
|
|
|
|
groupTimeFrame.startDate = this.startDate |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
val endDate = this.endDate |
|
|
|
val endDate = this.endDate |
|
|
|
if (endDate != null && groupTimeFrame.endDate != null && endDate.after(groupTimeFrame.endDate)) { |
|
|
|
|
|
|
|
groupTimeFrame.endDate = endDate |
|
|
|
// case where all sessions are over but the set is not, we might have a split, so we delete the set and save everything again |
|
|
|
|
|
|
|
if (endDate != null && sisterSessions.all { it.timeFrame?.endDate != null } && timeFrame.endDate == null) { |
|
|
|
|
|
|
|
var sessions = mutableListOf<Session>(owner) |
|
|
|
|
|
|
|
sessionSet.sessions?.forEach { sessions.add(it) } |
|
|
|
|
|
|
|
sessionSet.deleteFromRealm() |
|
|
|
|
|
|
|
sessions.forEach { it.timeFrame?.notifySessionDateChange(it) } |
|
|
|
|
|
|
|
} else { |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (this.startDate.before(timeFrame.startDate)) { |
|
|
|
|
|
|
|
timeFrame.startDate = this.startDate |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
if (endDate != null && timeFrame.endDate != null && endDate.after(timeFrame.endDate)) { |
|
|
|
|
|
|
|
timeFrame.endDate = endDate |
|
|
|
} else if (endDate == null) { |
|
|
|
} else if (endDate == null) { |
|
|
|
groupTimeFrame.endDate = null |
|
|
|
timeFrame.endDate = null |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
owner.sessionSet = sessionSet |
|
|
|
owner.sessionSet = sessionSet |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Timber.d("sd = : ${sessionSet.timeFrame?.startDate}, ed = ${sessionSet.timeFrame?.endDate}") |
|
|
|
|
|
|
|
Timber.d("netDuration 2 = : ${sessionSet.timeFrame?.netDuration}") |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
@ -216,16 +253,17 @@ open class TimeFrame : RealmObject() { |
|
|
|
|
|
|
|
|
|
|
|
// get all sessions from sets |
|
|
|
// get all sessions from sets |
|
|
|
var sessions = mutableSetOf<Session>() |
|
|
|
var sessions = mutableSetOf<Session>() |
|
|
|
sessionSets.forEach { it.sessions?.asIterable()?.let { it1 -> sessions.addAll(it1) } } |
|
|
|
sessionSets.forEach { set -> |
|
|
|
|
|
|
|
set.sessions?.asIterable()?.let { sessions.addAll(it) } |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// delete all sets |
|
|
|
// delete all sets |
|
|
|
sessionSets.deleteAllFromRealm() |
|
|
|
sessionSets.deleteAllFromRealm() |
|
|
|
|
|
|
|
|
|
|
|
// Create a new sets |
|
|
|
// Create a new sets |
|
|
|
val set: SessionSet = SessionSet.newInstance(realm) |
|
|
|
val set: SessionSet = SessionSet.newInstance(this.realm) |
|
|
|
set.timeFrame?.let { |
|
|
|
set.timeFrame?.let { |
|
|
|
it.startDate = startDate |
|
|
|
it.setDate(startDate, endDate) |
|
|
|
it.endDate = endDate |
|
|
|
|
|
|
|
} ?: run { |
|
|
|
} ?: run { |
|
|
|
throw ModelException("TimeFrame should never be null here") |
|
|
|
throw ModelException("TimeFrame should never be null here") |
|
|
|
} |
|
|
|
} |
|
|
|
@ -235,6 +273,7 @@ open class TimeFrame : RealmObject() { |
|
|
|
|
|
|
|
|
|
|
|
// Add all orphan sessions |
|
|
|
// Add all orphan sessions |
|
|
|
sessions.forEach { it.sessionSet = set } |
|
|
|
sessions.forEach { it.sessionSet = set } |
|
|
|
|
|
|
|
Timber.d("netDuration 3 = : ${set.timeFrame?.netDuration}") |
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|