|
|
|
@ -83,18 +83,26 @@ open class TimeFrame : RealmObject() { |
|
|
|
|
|
|
|
|
|
|
|
this.computeDuration() |
|
|
|
this.computeDuration() |
|
|
|
|
|
|
|
|
|
|
|
if (this.session != null) { |
|
|
|
this.session?.let { |
|
|
|
this.notifySessionDateChange() |
|
|
|
this.notifySessionDateChange(it) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
|
|
* Computes the net duration 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 |
|
|
|
val netDuration = endDate.time - this.startDate.time - this.breakDuration |
|
|
|
this.duration = netDuration |
|
|
|
this.duration = netDuration |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
fun notifySessionDateChange() { |
|
|
|
/** |
|
|
|
|
|
|
|
* Queries all time frames that might be impacted by the date change |
|
|
|
|
|
|
|
* Makes all necessary changes to keep sequential time frames |
|
|
|
|
|
|
|
*/ |
|
|
|
|
|
|
|
fun notifySessionDateChange(owner: Session) { |
|
|
|
val realm = Realm.getDefaultInstance() |
|
|
|
val realm = Realm.getDefaultInstance() |
|
|
|
var query: RealmQuery<SessionSet> = realm.where(SessionSet::class.java) |
|
|
|
var query: RealmQuery<SessionSet> = realm.where(SessionSet::class.java) |
|
|
|
query.isNotNull("timeFrame") |
|
|
|
query.isNotNull("timeFrame") |
|
|
|
@ -118,20 +126,19 @@ open class TimeFrame : RealmObject() { |
|
|
|
|
|
|
|
|
|
|
|
val sessionGroups = query.findAll() |
|
|
|
val sessionGroups = query.findAll() |
|
|
|
|
|
|
|
|
|
|
|
this.updateTimeFrames(sessionGroups) |
|
|
|
this.updateTimeFrames(sessionGroups, owner) |
|
|
|
|
|
|
|
|
|
|
|
// realm.close() |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* Update Time frames from sets |
|
|
|
* Update Time frames from sets |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
private fun updateTimeFrames(sessionSets: RealmResults<SessionSet>) { |
|
|
|
private fun updateTimeFrames(sessionSets: RealmResults<SessionSet>, owner: Session) { |
|
|
|
|
|
|
|
|
|
|
|
when (sessionSets.size) { |
|
|
|
when (sessionSets.size) { |
|
|
|
0 -> this.createSessionGroup() |
|
|
|
0 -> this.createSessionGroup(owner) |
|
|
|
1 -> this.updateSingleSessionGroup(sessionSets.first()!!) |
|
|
|
1 -> this.updateSingleSessionGroup(owner, sessionSets.first()!!) |
|
|
|
else -> this.mergeSessionGroups(sessionSets) |
|
|
|
else -> this.mergeSessionGroups(owner, sessionSets) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
@ -139,7 +146,7 @@ open class TimeFrame : RealmObject() { |
|
|
|
/** |
|
|
|
/** |
|
|
|
* Creates the session sessionGroup when the session has none |
|
|
|
* Creates the session sessionGroup when the session has none |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
private fun createSessionGroup() { |
|
|
|
private fun createSessionGroup(owner: Session) { |
|
|
|
|
|
|
|
|
|
|
|
val realm = Realm.getDefaultInstance() |
|
|
|
val realm = Realm.getDefaultInstance() |
|
|
|
|
|
|
|
|
|
|
|
@ -151,11 +158,7 @@ open class TimeFrame : RealmObject() { |
|
|
|
throw ModelException("TimeFrame should never be null here") |
|
|
|
throw ModelException("TimeFrame should never be null here") |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
this.session?.let { |
|
|
|
owner.sessionSet = set |
|
|
|
it.sessionSet = set |
|
|
|
|
|
|
|
} ?: run { |
|
|
|
|
|
|
|
throw ModelException("Session should never be null here") |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Timber.d("sd = : ${set.timeFrame?.startDate}, ed = ${set.timeFrame?.endDate}") |
|
|
|
Timber.d("sd = : ${set.timeFrame?.startDate}, ed = ${set.timeFrame?.endDate}") |
|
|
|
|
|
|
|
|
|
|
|
@ -165,7 +168,7 @@ open class TimeFrame : RealmObject() { |
|
|
|
* Single session sessionGroup update |
|
|
|
* Single session sessionGroup update |
|
|
|
* Changes the sessionGroup timeframe using the current timeframe dates |
|
|
|
* Changes the sessionGroup timeframe using the current timeframe dates |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
private fun updateSingleSessionGroup(sessionSet: SessionSet) { |
|
|
|
private fun updateSingleSessionGroup(owner: Session, sessionSet: SessionSet) { |
|
|
|
|
|
|
|
|
|
|
|
var groupTimeFrame: TimeFrame = sessionSet.timeFrame!! // tested in the query |
|
|
|
var groupTimeFrame: TimeFrame = sessionSet.timeFrame!! // tested in the query |
|
|
|
|
|
|
|
|
|
|
|
@ -179,7 +182,7 @@ open class TimeFrame : RealmObject() { |
|
|
|
groupTimeFrame.endDate = null |
|
|
|
groupTimeFrame.endDate = null |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
this.session?.sessionSet = sessionSet |
|
|
|
owner.sessionSet = sessionSet |
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@ -187,7 +190,7 @@ open class TimeFrame : RealmObject() { |
|
|
|
* Multiple session sets update: |
|
|
|
* Multiple session sets update: |
|
|
|
* Merges all sets into one (delete all then create a new one) |
|
|
|
* Merges all sets into one (delete all then create a new one) |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
private fun mergeSessionGroups(sessionSets: RealmResults<SessionSet>) { |
|
|
|
private fun mergeSessionGroups(owner: Session, sessionSets: RealmResults<SessionSet>) { |
|
|
|
|
|
|
|
|
|
|
|
var startDate: Date = this.startDate |
|
|
|
var startDate: Date = this.startDate |
|
|
|
var endDate: Date? = this.endDate |
|
|
|
var endDate: Date? = this.endDate |
|
|
|
@ -228,11 +231,7 @@ open class TimeFrame : RealmObject() { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// Add the session linked to this timeframe to the new sessionGroup |
|
|
|
// Add the session linked to this timeframe to the new sessionGroup |
|
|
|
this.sessions?.first()?.let { |
|
|
|
owner.sessionSet = set |
|
|
|
it.sessionSet = set |
|
|
|
|
|
|
|
} ?: run { |
|
|
|
|
|
|
|
throw ModelException("TimeFrame should never be null here") |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Add all orphan sessions |
|
|
|
// Add all orphan sessions |
|
|
|
sessions.forEach { it.sessionSet = set } |
|
|
|
sessions.forEach { it.sessionSet = set } |
|
|
|
|