Timeframe date changes manages Session belonging to SessionGroups

dev_raz_wip
Laurent 7 years ago
parent e4c2b4edae
commit 073835b807
  1. 17
      app/src/main/java/net/pokeranalytics/android/model/realm/TimeFrame.kt

@ -56,7 +56,7 @@ open class TimeFrame : RealmObject() {
this.computeDuration() this.computeDuration()
if (this.sessions != null) { if (this.session != null) {
this.notifySessionDateChange() this.notifySessionDateChange()
} }
} }
@ -100,7 +100,7 @@ open class TimeFrame : RealmObject() {
when (sessionGroups.size) { when (sessionGroups.size) {
0 -> this.createSessionGroup() 0 -> this.createSessionGroup()
1 -> this.updateSingleSessionGroup(sessionGroups.first()?.timeFrame!!) 1 -> this.updateSingleSessionGroup(sessionGroups.first()!!)
else -> this.mergeSessionGroups(sessionGroups) else -> this.mergeSessionGroups(sessionGroups)
} }
@ -111,6 +111,9 @@ open class TimeFrame : RealmObject() {
*/ */
private fun createSessionGroup() { private fun createSessionGroup() {
val realm = Realm.getDefaultInstance()
realm.beginTransaction()
val group: SessionGroup = SessionGroup.newInstance() val group: SessionGroup = SessionGroup.newInstance()
group.timeFrame?.let { group.timeFrame?.let {
it.startDate = this.startDate it.startDate = this.startDate
@ -118,14 +121,17 @@ open class TimeFrame : RealmObject() {
} ?: run { } ?: run {
throw ModelException("TimeFrame should never be null here") throw ModelException("TimeFrame should never be null here")
} }
group.sessions.add(this.session)
realm.commitTransaction()
} }
/** /**
* Single session group update * Single session group update
* Changes the group timeframe using the current timeframe dates * Changes the group timeframe using the current timeframe dates
*/ */
private fun updateSingleSessionGroup(groupTimeFrame: TimeFrame) { private fun updateSingleSessionGroup(sessionGroup: SessionGroup) {
var groupTimeFrame: TimeFrame = sessionGroup.timeFrame!! // tested in the query
if (this.startDate.before(groupTimeFrame.startDate)) { if (this.startDate.before(groupTimeFrame.startDate)) {
groupTimeFrame.startDate = this.startDate groupTimeFrame.startDate = this.startDate
@ -140,6 +146,9 @@ open class TimeFrame : RealmObject() {
// Realm Update // Realm Update
val realm = Realm.getDefaultInstance() val realm = Realm.getDefaultInstance()
realm.beginTransaction() realm.beginTransaction()
if (!sessionGroup.sessions.contains(this.session)) {
sessionGroup.sessions.add(this.session)
}
realm.copyToRealmOrUpdate(groupTimeFrame) realm.copyToRealmOrUpdate(groupTimeFrame)
realm.commitTransaction() realm.commitTransaction()

Loading…
Cancel
Save