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 {
@ -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<HandHistory> = RealmList()
// The hand histories of the session
@LinkingObjects("session")
val handHistories: RealmResults<HandHistory>? = 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<TournamentFeature>))
} ?: 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<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)
}
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 {

@ -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) {

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

@ -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

Loading…
Cancel
Save