|
|
|
|
@ -1,12 +1,14 @@ |
|
|
|
|
package net.pokeranalytics.android.model.realm |
|
|
|
|
|
|
|
|
|
import io.realm.Realm |
|
|
|
|
import io.realm.RealmObject |
|
|
|
|
import io.realm.RealmQuery |
|
|
|
|
import io.realm.RealmResults |
|
|
|
|
import io.realm.annotations.LinkingObjects |
|
|
|
|
import net.pokeranalytics.android.exceptions.ModelException |
|
|
|
|
import java.util.* |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
open class TimeFrame : RealmObject() { |
|
|
|
|
|
|
|
|
|
// A start date |
|
|
|
|
@ -15,7 +17,7 @@ open class TimeFrame : RealmObject() { |
|
|
|
|
field = value |
|
|
|
|
this.computeDuration() |
|
|
|
|
if (this.session != null) { |
|
|
|
|
this.notifyDateChange() |
|
|
|
|
this.notifySessionDateChange() |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@ -25,7 +27,7 @@ open class TimeFrame : RealmObject() { |
|
|
|
|
field = value |
|
|
|
|
this.computeDuration() |
|
|
|
|
if (this.session != null) { |
|
|
|
|
this.notifyDateChange() |
|
|
|
|
this.notifySessionDateChange() |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@ -47,7 +49,7 @@ open class TimeFrame : RealmObject() { |
|
|
|
|
private val session: RealmResults<Session>? = null |
|
|
|
|
|
|
|
|
|
@LinkingObjects("timeFrame") |
|
|
|
|
private val group: RealmResults<TimeFrameGroup>? = null |
|
|
|
|
private val group: RealmResults<SessionGroup>? = null |
|
|
|
|
|
|
|
|
|
private fun computeDuration() { |
|
|
|
|
|
|
|
|
|
@ -60,20 +62,106 @@ open class TimeFrame : RealmObject() { |
|
|
|
|
this.duration = endDate.time - startDate.time - this.breakDuration |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
private fun notifyDateChange() { |
|
|
|
|
// val realm = Realm.getDefaultInstance() |
|
|
|
|
// |
|
|
|
|
// var query: RealmQuery<TimeFrame> = realm.where(TimeFrame::class.java) |
|
|
|
|
// query.greaterThan("startDate", this.startDate.time) |
|
|
|
|
// |
|
|
|
|
// this.endDate?.let { endDate -> |
|
|
|
|
// query.or() |
|
|
|
|
// .greaterThan("startDate", endDate.time) |
|
|
|
|
// .lessThan("endDate", endDate.time) |
|
|
|
|
// } |
|
|
|
|
// |
|
|
|
|
// |
|
|
|
|
// realm.close() |
|
|
|
|
private fun notifySessionDateChange() { |
|
|
|
|
val realm = Realm.getDefaultInstance() |
|
|
|
|
var query: RealmQuery<SessionGroup> = realm.where(SessionGroup::class.java) |
|
|
|
|
query.isNotNull("timeFrame") |
|
|
|
|
|
|
|
|
|
if (this.endDate == null) { |
|
|
|
|
query.greaterThan("timeFrame.startDate", this.startDate.time).or().greaterThan("timeFrame.endDate", this.startDate.time) |
|
|
|
|
} else { |
|
|
|
|
val endDate = this.endDate!! |
|
|
|
|
query |
|
|
|
|
.greaterThan("timeFrame.startDate", this.startDate.time) |
|
|
|
|
.lessThan("timeFrame.endDate", this.startDate.time) |
|
|
|
|
.or() |
|
|
|
|
.greaterThan("timeFrame.startDate", endDate) |
|
|
|
|
.lessThan("timeFrame.endDate", endDate) |
|
|
|
|
.or() |
|
|
|
|
.lessThan("timeFrame.startDate", this.startDate.time) |
|
|
|
|
.greaterThan("timeFrame.endDate", endDate) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
val sessionGroups = query.findAll() |
|
|
|
|
|
|
|
|
|
this.updateTimeFrames(sessionGroups) |
|
|
|
|
|
|
|
|
|
realm.close() |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* Update Time frames from groups |
|
|
|
|
*/ |
|
|
|
|
private fun updateTimeFrames(sessionGroups: RealmResults<SessionGroup>) { |
|
|
|
|
|
|
|
|
|
// update |
|
|
|
|
if (sessionGroups.size == 1) { |
|
|
|
|
// we are sure that the timeframe is not null from the realm query |
|
|
|
|
val groupTimeFrame: TimeFrame = sessionGroups.first()?.timeFrame!! |
|
|
|
|
if (this.startDate.before(groupTimeFrame.startDate)) { |
|
|
|
|
groupTimeFrame.startDate = this.startDate |
|
|
|
|
} |
|
|
|
|
val endDate = this.endDate |
|
|
|
|
if (endDate != null && groupTimeFrame.endDate != null && endDate.after(groupTimeFrame.endDate)) { |
|
|
|
|
groupTimeFrame.endDate = endDate |
|
|
|
|
} else if (endDate == null) { |
|
|
|
|
groupTimeFrame.endDate = null |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
} else if (sessionGroups.size > 1) { |
|
|
|
|
|
|
|
|
|
var startDate: Date = this.startDate |
|
|
|
|
var endDate: Date? = this.endDate |
|
|
|
|
|
|
|
|
|
val timeFrames = sessionGroups.mapNotNull { it.timeFrame } |
|
|
|
|
timeFrames.forEach { tf -> |
|
|
|
|
if (tf.startDate.before(startDate)) { |
|
|
|
|
startDate = tf.startDate |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
endDate?.let { ed -> |
|
|
|
|
tf.endDate?.let { tfed -> |
|
|
|
|
if (tfed.after(ed)) { |
|
|
|
|
endDate = tfed |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} ?: run { |
|
|
|
|
endDate = tf.endDate |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// get all sessions from groups |
|
|
|
|
val sessions = sessionGroups.flatMap { it.sessions } |
|
|
|
|
|
|
|
|
|
// delete all groups |
|
|
|
|
val realm = Realm.getDefaultInstance() |
|
|
|
|
realm.executeTransaction { |
|
|
|
|
sessionGroups.deleteAllFromRealm() |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
val group: SessionGroup = SessionGroup.newInstance() |
|
|
|
|
group.timeFrame?.let { |
|
|
|
|
it.startDate = startDate |
|
|
|
|
it.endDate = endDate |
|
|
|
|
} ?: run { |
|
|
|
|
throw ModelException("TimeFrame should never be null here") |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
this.session?.first()?.let { |
|
|
|
|
group.sessions.add(it) |
|
|
|
|
} ?: run { |
|
|
|
|
throw ModelException("TimeFrame should never be null here") |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//@todo delete all timeframes, create a new one |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|