Code improvement

bs
Laurent 5 years ago
parent 8a718de75d
commit 24cc9327e4
  1. 235
      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. 4
      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,138 +971,125 @@ 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) {
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) { cgBigBlind = try {
SessionRow.BANKROLL -> bankroll = value as Bankroll? (value[1] as String? ?: "0").toDouble()
SessionRow.BLINDS -> if (value is ArrayList<*>) { } catch (e: Exception) {
cgSmallBlind = try { null
(value[0] as String? ?: "0").toDouble() }
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) { } catch (e: Exception) {
null null
} }
game = try {
cgBigBlind = try { (value[1] as Game?)
(value[1] as String? ?: "0").toDouble()
} catch (e: Exception) { } catch (e: Exception) {
null null
} }
} else if (value is Game) {
cgBigBlind?.let { game = value
if (cgSmallBlind == null || cgSmallBlind == 0.0) {
cgSmallBlind = it / 2.0
}
}
} else if (value == null) { } else if (value == null) {
cgSmallBlind = null limit = null
cgBigBlind = 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 = SessionRow.POSITION -> {
if (this.result != null) this.result as Result else realm.createObject(Result::class.java) val localResult = if (result != null) result as Result else realm.createObject(Result::class.java)
localResult.buyin = value as Double? if (value is Double) {
this.result = localResult localResult.tournamentFinalPosition = value.toInt()
this.updateRowRepresentation() } else {
localResult.tournamentFinalPosition = null
} }
SessionRow.CASHED_OUT, SessionRow.PRIZE -> { result = localResult
val localResult = }
if (this.result != null) this.result as Result else realm.createObject(Result::class.java) SessionRow.START_DATE -> if (value is Date) {
this.startDate = value
localResult.cashout = value as Double? }
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 value?.let {
} tournamentFeatures = RealmList()
SessionRow.NET_RESULT -> { tournamentFeatures.addAll((it as ArrayList<TournamentFeature>))
this.result?.let { result -> } ?: run {
result.netResult = value as Double? tournamentFeatures.removeAll(this.tournamentFeatures)
}
} }
SessionRow.COMMENT -> comment = value as String? ?: "" }
is CustomField -> {
SessionRow.END_DATE -> if (value is Date?) { customFieldEntries.filter { it.customField?.id == row.id }.let {
this.endDate = value customFieldEntries.removeAll(it)
} }
SessionRow.GAME -> { when (row.type) {
if (value is ArrayList<*>) { CustomField.Type.AMOUNT.uniqueIdentifier,
limit = try { CustomField.Type.NUMBER.uniqueIdentifier -> {
(value[0] as Int?) if (value != null) {
} catch (e: Exception) { val customFieldEntry = CustomFieldEntry()
null customFieldEntry.numericValue = value as Double?
} customFieldEntries.add(customFieldEntry)
game = try { row.entries.add(customFieldEntry)
(value[1] as Game?)
} catch (e: Exception) {
null
} }
} else if (value is Game) {
game = value
} else if (value == null) {
limit = null
game = null
} }
} CustomField.Type.LIST.uniqueIdentifier -> {
SessionRow.INITIAL_BUY_IN -> { if (value != null && value is CustomFieldEntry) {
this.tournamentEntryFee = (value as Double?) customFieldEntries.add(value)
}
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<TournamentFeature>))
} ?: 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)
}
} }
} }
} }
@ -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 // Stat Entry
override fun entryTitle(context: Context): String { override fun entryTitle(context: Context): String {

@ -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 {
this.currentSession.updateValue(value, row) getRealm().executeTransaction {
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