diff --git a/app/src/main/java/net/pokeranalytics/android/model/realm/Session.kt b/app/src/main/java/net/pokeranalytics/android/model/realm/Session.kt index 24c0e502..75c5cf66 100644 --- a/app/src/main/java/net/pokeranalytics/android/model/realm/Session.kt +++ b/app/src/main/java/net/pokeranalytics/android/model/realm/Session.kt @@ -66,7 +66,6 @@ open class Session : RealmObject(), Savable, Editable, StaticRowRepresentableDat } } - } companion object { @@ -281,8 +280,6 @@ open class Session : RealmObject(), Savable, Editable, StaticRowRepresentableDat // The number of tables played at the same time var numberOfTables: Int = 1 -// var hands: RealmList = RealmList() - // The hand histories of the session @LinkingObjects("session") val handHistories: RealmResults? = null @@ -464,7 +461,7 @@ open class Session : RealmObject(), Savable, Editable, StaticRowRepresentableDat return playerHandsPerHour / tableSize.toDouble() } - val hourlyRate: Double + private val hourlyRate: Double get() { this.result?.let { result -> return result.net / this.hourlyDuration @@ -472,7 +469,7 @@ open class Session : RealmObject(), Savable, Editable, StaticRowRepresentableDat throw ModelException("Session should have an existing Result relationship") } - val bbHourlyRate: BB + private val bbHourlyRate: BB get() { return this.bbNet / this.hourlyDuration } @@ -974,138 +971,125 @@ open class Session : RealmObject(), Savable, Editable, StaticRowRepresentableDat override fun updateValue(value: Any?, row: RowRepresentable) { - realm.executeTransaction { + when (row) { + SessionRow.BANKROLL -> bankroll = value as Bankroll? + SessionRow.BLINDS -> if (value is ArrayList<*>) { + cgSmallBlind = try { + (value[0] as String? ?: "0").toDouble() + } catch (e: Exception) { + null + } - when (row) { - SessionRow.BANKROLL -> bankroll = value as Bankroll? - SessionRow.BLINDS -> if (value is ArrayList<*>) { - cgSmallBlind = try { - (value[0] as String? ?: "0").toDouble() + cgBigBlind = try { + (value[1] as String? ?: "0").toDouble() + } catch (e: Exception) { + null + } + + cgBigBlind?.let { + if (cgSmallBlind == null || cgSmallBlind == 0.0) { + cgSmallBlind = it / 2.0 + } + } + } else if (value == null) { + cgSmallBlind = null + cgBigBlind = null + } + SessionRow.BREAK_TIME -> { + this.breakDuration = (value as Double? ?: 0.0).toLong() * 60 * 1000 + } + SessionRow.BUY_IN -> { + val localResult = getOrCreateResult() + localResult.buyin = value as Double? + this.updateRowRepresentation() + } + SessionRow.CASHED_OUT, SessionRow.PRIZE -> { + val localResult = getOrCreateResult() + localResult.cashout = value as Double? + } + SessionRow.NET_RESULT -> { + val localResult = getOrCreateResult() + localResult.netResult = value as Double? + } + SessionRow.COMMENT -> comment = value as String? ?: "" + SessionRow.END_DATE -> if (value is Date?) { + this.endDate = value + } + SessionRow.GAME -> { + if (value is ArrayList<*>) { + limit = try { + (value[0] as Int?) } catch (e: Exception) { null } - - cgBigBlind = try { - (value[1] as String? ?: "0").toDouble() + game = try { + (value[1] as Game?) } catch (e: Exception) { null } - - cgBigBlind?.let { - if (cgSmallBlind == null || cgSmallBlind == 0.0) { - cgSmallBlind = it / 2.0 - } - } + } else if (value is Game) { + game = value } else if (value == null) { - cgSmallBlind = null - cgBigBlind = null + limit = null + game = null } - SessionRow.BREAK_TIME -> { - this.breakDuration = (value as Double? ?: 0.0).toLong() * 60 * 1000 + } + SessionRow.INITIAL_BUY_IN -> { + this.tournamentEntryFee = (value as Double?) + } + SessionRow.LOCATION -> location = value as Location? + SessionRow.PLAYERS -> { + if (value is Double) { + this.tournamentNumberOfPlayers = value.toInt() + } else { + this.tournamentNumberOfPlayers = null } - SessionRow.BUY_IN -> { - val localResult = - if (this.result != null) this.result as Result else realm.createObject(Result::class.java) - localResult.buyin = value as Double? - this.result = localResult - this.updateRowRepresentation() + } + SessionRow.POSITION -> { + val localResult = if (result != null) result as Result else realm.createObject(Result::class.java) + if (value is Double) { + localResult.tournamentFinalPosition = value.toInt() + } else { + localResult.tournamentFinalPosition = null } - SessionRow.CASHED_OUT, SessionRow.PRIZE -> { - val localResult = - if (this.result != null) this.result as Result else realm.createObject(Result::class.java) - - localResult.cashout = value as Double? + result = localResult + } + SessionRow.START_DATE -> if (value is Date) { + this.startDate = value + } + SessionRow.TABLE_SIZE -> tableSize = value as Int? + SessionRow.TIPS -> { + val localResult = getOrCreateResult() + localResult.tips = value as Double? + } + SessionRow.TOURNAMENT_NAME -> tournamentName = value as TournamentName? + SessionRow.TOURNAMENT_TYPE -> tournamentType = (value as TournamentType?)?.ordinal + SessionRow.TOURNAMENT_FEATURE -> { - this.result = localResult - } - SessionRow.NET_RESULT -> { - this.result?.let { result -> - result.netResult = value as Double? - } + value?.let { + tournamentFeatures = RealmList() + tournamentFeatures.addAll((it as ArrayList)) + } ?: run { + tournamentFeatures.removeAll(this.tournamentFeatures) } - SessionRow.COMMENT -> comment = value as String? ?: "" - - SessionRow.END_DATE -> if (value is Date?) { - this.endDate = value + } + is CustomField -> { + customFieldEntries.filter { it.customField?.id == row.id }.let { + customFieldEntries.removeAll(it) } - SessionRow.GAME -> { - if (value is ArrayList<*>) { - limit = try { - (value[0] as Int?) - } catch (e: Exception) { - null - } - game = try { - (value[1] as Game?) - } catch (e: Exception) { - null + when (row.type) { + CustomField.Type.AMOUNT.uniqueIdentifier, + CustomField.Type.NUMBER.uniqueIdentifier -> { + if (value != null) { + val customFieldEntry = CustomFieldEntry() + customFieldEntry.numericValue = value as Double? + customFieldEntries.add(customFieldEntry) + row.entries.add(customFieldEntry) } - } else if (value is Game) { - game = value - } else if (value == null) { - limit = null - game = null } - } - SessionRow.INITIAL_BUY_IN -> { - this.tournamentEntryFee = (value as Double?) - } - SessionRow.LOCATION -> location = value as Location? - SessionRow.PLAYERS -> { - if (value is Double) { - this.tournamentNumberOfPlayers = value.toInt() - } else { - this.tournamentNumberOfPlayers = null - } - } - SessionRow.POSITION -> { - val localResult = if (result != null) result as Result else realm.createObject(Result::class.java) - if (value is Double) { - localResult.tournamentFinalPosition = value.toInt() - } else { - localResult.tournamentFinalPosition = null - } - result = localResult - } - SessionRow.START_DATE -> if (value is Date) { - this.startDate = value - } - SessionRow.TABLE_SIZE -> tableSize = value as Int? - SessionRow.TIPS -> { - val localResult = if (result != null) result as Result else realm.createObject(Result::class.java) - localResult.tips = value as Double? - result = localResult - } - SessionRow.TOURNAMENT_NAME -> tournamentName = value as TournamentName? - SessionRow.TOURNAMENT_TYPE -> tournamentType = (value as TournamentType?)?.ordinal - SessionRow.TOURNAMENT_FEATURE -> { - - value?.let { - tournamentFeatures = RealmList() - tournamentFeatures.addAll((it as ArrayList)) - } ?: run { - tournamentFeatures.removeAll(this.tournamentFeatures) - } - } - - is CustomField -> { - customFieldEntries.filter { it.customField?.id == row.id }.let { - customFieldEntries.removeAll(it) - } - when (row.type) { - CustomField.Type.AMOUNT.uniqueIdentifier, - CustomField.Type.NUMBER.uniqueIdentifier -> { - if (value != null) { - val customFieldEntry = CustomFieldEntry() - customFieldEntry.numericValue = value as Double? - customFieldEntries.add(customFieldEntry) - row.entries.add(customFieldEntry) - } - } - CustomField.Type.LIST.uniqueIdentifier -> { - if (value != null && value is CustomFieldEntry) { - customFieldEntries.add(value) - } + CustomField.Type.LIST.uniqueIdentifier -> { + if (value != null && value is CustomFieldEntry) { + customFieldEntries.add(value) } } } @@ -1114,6 +1098,15 @@ open class Session : RealmObject(), Savable, Editable, StaticRowRepresentableDat } + private fun getOrCreateResult(): Result { + return this.result + ?: run { + val result = realm.createObject(Result::class.java) + this.result = result + result + } + } + // Stat Entry override fun entryTitle(context: Context): String { diff --git a/app/src/main/java/net/pokeranalytics/android/ui/modules/data/EditableDataFragment.kt b/app/src/main/java/net/pokeranalytics/android/ui/modules/data/EditableDataFragment.kt index 4129c9e2..cadc3026 100644 --- a/app/src/main/java/net/pokeranalytics/android/ui/modules/data/EditableDataFragment.kt +++ b/app/src/main/java/net/pokeranalytics/android/ui/modules/data/EditableDataFragment.kt @@ -67,7 +67,7 @@ open class EditableDataFragment : DataManagerFragment(), RowRepresentableDelegat } override fun onRowValueChanged(value: Any?, row: RowRepresentable) { - this.getRealm().executeTransaction { + getRealm().executeTransaction { try { (this.model.item as Editable).updateValue(value, row) } catch (e: Exception) { diff --git a/app/src/main/java/net/pokeranalytics/android/ui/modules/data/PlayerDataFragment.kt b/app/src/main/java/net/pokeranalytics/android/ui/modules/data/PlayerDataFragment.kt index 8a8d082d..bae2219b 100644 --- a/app/src/main/java/net/pokeranalytics/android/ui/modules/data/PlayerDataFragment.kt +++ b/app/src/main/java/net/pokeranalytics/android/ui/modules/data/PlayerDataFragment.kt @@ -206,7 +206,6 @@ class PlayerDataFragment : EditableDataFragment(), StaticRowRepresentableDataSou builder.show() } - override fun onDataSaved() { super.onDataSaved() player.cleanupComments() diff --git a/app/src/main/java/net/pokeranalytics/android/ui/modules/session/SessionFragment.kt b/app/src/main/java/net/pokeranalytics/android/ui/modules/session/SessionFragment.kt index 3d226d70..7a236f76 100644 --- a/app/src/main/java/net/pokeranalytics/android/ui/modules/session/SessionFragment.kt +++ b/app/src/main/java/net/pokeranalytics/android/ui/modules/session/SessionFragment.kt @@ -252,7 +252,9 @@ class SessionFragment : RealmFragment(), RowRepresentableDelegate { override fun onRowValueChanged(value: Any?, row: RowRepresentable) { this.sessionHasBeenUserCustomized = true try { - this.currentSession.updateValue(value, row) + getRealm().executeTransaction { + this.currentSession.updateValue(value, row) + } } catch (e: PAIllegalStateException) { Toast.makeText(context, e.message, Toast.LENGTH_LONG).show() return