Code improvement

bs
Laurent 5 years ago
parent 8a718de75d
commit 24cc9327e4
  1. 37
      app/src/main/java/net/pokeranalytics/android/model/realm/Session.kt
  2. 2
      app/src/main/java/net/pokeranalytics/android/ui/modules/data/EditableDataFragment.kt
  3. 1
      app/src/main/java/net/pokeranalytics/android/ui/modules/data/PlayerDataFragment.kt
  4. 2
      app/src/main/java/net/pokeranalytics/android/ui/modules/session/SessionFragment.kt

@ -66,7 +66,6 @@ open class Session : RealmObject(), Savable, Editable, StaticRowRepresentableDat
} }
} }
} }
companion object { companion object {
@ -281,8 +280,6 @@ open class Session : RealmObject(), Savable, Editable, StaticRowRepresentableDat
// The number of tables played at the same time // The number of tables played at the same time
var numberOfTables: Int = 1 var numberOfTables: Int = 1
// var hands: RealmList<HandHistory> = RealmList()
// The hand histories of the session // The hand histories of the session
@LinkingObjects("session") @LinkingObjects("session")
val handHistories: RealmResults<HandHistory>? = null val handHistories: RealmResults<HandHistory>? = null
@ -464,7 +461,7 @@ open class Session : RealmObject(), Savable, Editable, StaticRowRepresentableDat
return playerHandsPerHour / tableSize.toDouble() return playerHandsPerHour / tableSize.toDouble()
} }
val hourlyRate: Double private val hourlyRate: Double
get() { get() {
this.result?.let { result -> this.result?.let { result ->
return result.net / this.hourlyDuration 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") throw ModelException("Session should have an existing Result relationship")
} }
val bbHourlyRate: BB private val bbHourlyRate: BB
get() { get() {
return this.bbNet / this.hourlyDuration return this.bbNet / this.hourlyDuration
} }
@ -974,8 +971,6 @@ open class Session : RealmObject(), Savable, Editable, StaticRowRepresentableDat
override fun updateValue(value: Any?, row: RowRepresentable) { override fun updateValue(value: Any?, row: RowRepresentable) {
realm.executeTransaction {
when (row) { when (row) {
SessionRow.BANKROLL -> bankroll = value as Bankroll? SessionRow.BANKROLL -> bankroll = value as Bankroll?
SessionRow.BLINDS -> if (value is ArrayList<*>) { SessionRow.BLINDS -> if (value is ArrayList<*>) {
@ -1004,27 +999,19 @@ open class Session : RealmObject(), Savable, Editable, StaticRowRepresentableDat
this.breakDuration = (value as Double? ?: 0.0).toLong() * 60 * 1000 this.breakDuration = (value as Double? ?: 0.0).toLong() * 60 * 1000
} }
SessionRow.BUY_IN -> { SessionRow.BUY_IN -> {
val localResult = val localResult = getOrCreateResult()
if (this.result != null) this.result as Result else realm.createObject(Result::class.java)
localResult.buyin = value as Double? localResult.buyin = value as Double?
this.result = localResult
this.updateRowRepresentation() this.updateRowRepresentation()
} }
SessionRow.CASHED_OUT, SessionRow.PRIZE -> { SessionRow.CASHED_OUT, SessionRow.PRIZE -> {
val localResult = val localResult = getOrCreateResult()
if (this.result != null) this.result as Result else realm.createObject(Result::class.java)
localResult.cashout = value as Double? localResult.cashout = value as Double?
this.result = localResult
} }
SessionRow.NET_RESULT -> { SessionRow.NET_RESULT -> {
this.result?.let { result -> val localResult = getOrCreateResult()
result.netResult = value as Double? localResult.netResult = value as Double?
}
} }
SessionRow.COMMENT -> comment = value as String? ?: "" SessionRow.COMMENT -> comment = value as String? ?: ""
SessionRow.END_DATE -> if (value is Date?) { SessionRow.END_DATE -> if (value is Date?) {
this.endDate = value this.endDate = value
} }
@ -1072,9 +1059,8 @@ open class Session : RealmObject(), Savable, Editable, StaticRowRepresentableDat
} }
SessionRow.TABLE_SIZE -> tableSize = value as Int? SessionRow.TABLE_SIZE -> tableSize = value as Int?
SessionRow.TIPS -> { SessionRow.TIPS -> {
val localResult = if (result != null) result as Result else realm.createObject(Result::class.java) val localResult = getOrCreateResult()
localResult.tips = value as Double? localResult.tips = value as Double?
result = localResult
} }
SessionRow.TOURNAMENT_NAME -> tournamentName = value as TournamentName? SessionRow.TOURNAMENT_NAME -> tournamentName = value as TournamentName?
SessionRow.TOURNAMENT_TYPE -> tournamentType = (value as TournamentType?)?.ordinal SessionRow.TOURNAMENT_TYPE -> tournamentType = (value as TournamentType?)?.ordinal
@ -1087,7 +1073,6 @@ open class Session : RealmObject(), Savable, Editable, StaticRowRepresentableDat
tournamentFeatures.removeAll(this.tournamentFeatures) tournamentFeatures.removeAll(this.tournamentFeatures)
} }
} }
is CustomField -> { is CustomField -> {
customFieldEntries.filter { it.customField?.id == row.id }.let { customFieldEntries.filter { it.customField?.id == row.id }.let {
customFieldEntries.removeAll(it) customFieldEntries.removeAll(it)
@ -1110,8 +1095,16 @@ 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 // Stat Entry

@ -67,7 +67,7 @@ open class EditableDataFragment : DataManagerFragment(), RowRepresentableDelegat
} }
override fun onRowValueChanged(value: Any?, row: RowRepresentable) { override fun onRowValueChanged(value: Any?, row: RowRepresentable) {
this.getRealm().executeTransaction { getRealm().executeTransaction {
try { try {
(this.model.item as Editable).updateValue(value, row) (this.model.item as Editable).updateValue(value, row)
} catch (e: Exception) { } catch (e: Exception) {

@ -206,7 +206,6 @@ class PlayerDataFragment : EditableDataFragment(), StaticRowRepresentableDataSou
builder.show() builder.show()
} }
override fun onDataSaved() { override fun onDataSaved() {
super.onDataSaved() super.onDataSaved()
player.cleanupComments() player.cleanupComments()

@ -252,7 +252,9 @@ class SessionFragment : RealmFragment(), RowRepresentableDelegate {
override fun onRowValueChanged(value: Any?, row: RowRepresentable) { override fun onRowValueChanged(value: Any?, row: RowRepresentable) {
this.sessionHasBeenUserCustomized = true this.sessionHasBeenUserCustomized = true
try { try {
getRealm().executeTransaction {
this.currentSession.updateValue(value, row) this.currentSession.updateValue(value, row)
}
} catch (e: PAIllegalStateException) { } catch (e: PAIllegalStateException) {
Toast.makeText(context, e.message, Toast.LENGTH_LONG).show() Toast.makeText(context, e.message, Toast.LENGTH_LONG).show()
return return

Loading…
Cancel
Save