diff --git a/app/src/main/java/net/pokeranalytics/android/model/realm/TimeFrame.kt b/app/src/main/java/net/pokeranalytics/android/model/realm/TimeFrame.kt index 784bdb00..e515da67 100644 --- a/app/src/main/java/net/pokeranalytics/android/model/realm/TimeFrame.kt +++ b/app/src/main/java/net/pokeranalytics/android/model/realm/TimeFrame.kt @@ -4,6 +4,7 @@ import io.realm.Realm import io.realm.RealmObject import io.realm.RealmQuery import io.realm.RealmResults +import io.realm.annotations.Ignore import io.realm.annotations.LinkingObjects import net.pokeranalytics.android.exceptions.ModelException import java.util.* @@ -13,23 +14,11 @@ open class TimeFrame : RealmObject() { // A start date var startDate: Date = Date() - set(value) { - field = value - this.computeDuration() - if (this.session != null) { - this.notifySessionDateChange() - } - } + private set // An end date var endDate: Date? = null - set(value) { - field = value - this.computeDuration() - if (this.session != null) { - this.notifySessionDateChange() - } - } + private set // The break duration var breakDuration: Long = 0L @@ -46,10 +35,29 @@ open class TimeFrame : RealmObject() { var paused: Boolean = false @LinkingObjects("timeFrame") - private val session: RealmResults? = null + private val sessions: RealmResults? = null @LinkingObjects("timeFrame") - private val group: RealmResults? = null + private val groups: RealmResults? = null + + @Ignore + var session: Session? = null + get() = this.sessions?.first() + + @Ignore + var group: SessionGroup? = null + get() = this.groups?.first() + + fun setDate(startDate: Date, endDate: Date?) { + this.startDate = startDate + this.endDate = endDate + + this.computeDuration() + + if (this.sessions != null) { + this.notifySessionDateChange() + } + } private fun computeDuration() { @@ -96,6 +104,7 @@ open class TimeFrame : RealmObject() { // 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)) { @@ -108,6 +117,12 @@ open class TimeFrame : RealmObject() { groupTimeFrame.endDate = null } + // Realm Update + val realm = Realm.getDefaultInstance() + realm.beginTransaction() + realm.copyToRealmOrUpdate(groupTimeFrame) + realm.commitTransaction() + } else if (sessionGroups.size > 1) { var startDate: Date = this.startDate @@ -132,14 +147,16 @@ open class TimeFrame : RealmObject() { } // get all sessions from groups - val sessions = sessionGroups.flatMap { it.sessions } + var sessions = sessionGroups.flatMap { it.sessions } - // delete all groups + // Start Realm updates val realm = Realm.getDefaultInstance() - realm.executeTransaction { - sessionGroups.deleteAllFromRealm() - } + realm.beginTransaction() + + // delete all groups + sessionGroups.deleteAllFromRealm() + // Create a new groups val group: SessionGroup = SessionGroup.newInstance() group.timeFrame?.let { it.startDate = startDate @@ -148,17 +165,17 @@ open class TimeFrame : RealmObject() { throw ModelException("TimeFrame should never be null here") } - this.session?.first()?.let { + // Add the session linked to this timeframe to the new group + this.sessions?.first()?.let { group.sessions.add(it) } ?: run { throw ModelException("TimeFrame should never be null here") } + // Add all orphan sessions + group.sessions.addAll(sessions) - - //@todo delete all timeframes, create a new one - - + realm.commitTransaction() }