Refactors Double.formatted() into a val

hh
Laurent 6 years ago
parent d6ccea472e
commit 17bbb7765e
  1. 6
      app/src/main/java/net/pokeranalytics/android/calculus/Stat.kt
  2. 2
      app/src/main/java/net/pokeranalytics/android/model/realm/Session.kt
  3. 2
      app/src/main/java/net/pokeranalytics/android/model/realm/handhistory/Action.kt
  4. 6
      app/src/main/java/net/pokeranalytics/android/model/realm/handhistory/HandHistory.kt
  5. 2
      app/src/main/java/net/pokeranalytics/android/ui/modules/handhistory/HandHistoryAdapter.kt
  6. 6
      app/src/main/java/net/pokeranalytics/android/ui/modules/handhistory/model/HandHistoryViewModel.kt
  7. 2
      app/src/main/java/net/pokeranalytics/android/ui/modules/handhistory/views/RowHandHistoryViewHolder.kt
  8. 61
      app/src/main/java/net/pokeranalytics/android/ui/view/HandHistoryRowView.kt
  9. 14
      app/src/main/java/net/pokeranalytics/android/util/extensions/NumbersExtension.kt
  10. 11
      app/src/test/java/net/pokeranalytics/android/BasicUnitTest.kt

@ -150,7 +150,7 @@ enum class Stat(override var uniqueIdentifier: Int) : IntIdentifiable, RowRepres
// Red/green numericValues
HOURLY_RATE_BB, AVERAGE_NET_BB, NET_BB_PER_100_HANDS, BB_NET_RESULT -> {
val color = if (value >= this.threshold) R.color.green else R.color.red
return TextFormat(value.formatted(), color)
return TextFormat(value.formatted, color)
}
// white integers
NUMBER_OF_SETS, NUMBER_OF_GAMES, HANDS_PLAYED, LOCATIONS_PLAYED, DAYS_PLAYED -> {
@ -161,11 +161,11 @@ enum class Stat(override var uniqueIdentifier: Int) : IntIdentifiable, RowRepres
} // red/green percentages
WIN_RATIO, ROI -> {
val color = if (value * 100 >= this.threshold) R.color.green else R.color.red
return TextFormat("${(value * 100).formatted()}%", color)
return TextFormat("${(value * 100).formatted}%", color)
}
RISK_OF_RUIN -> {
val color = if (value * 100 <= this.threshold) R.color.green else R.color.red
return TextFormat("${(value * 100).formatted()}%", color)
return TextFormat("${(value * 100).formatted}%", color)
}
// white amountsr
AVERAGE_BUYIN, STANDARD_DEVIATION, STANDARD_DEVIATION_HOURLY,

@ -637,7 +637,7 @@ open class Session : RealmObject(), Savable, Editable, StaticRowRepresentableDat
if (cgBigBlind == null) return
cgBigBlind?.let { bb ->
val sb = cgSmallBlind ?: bb / 2.0
val preFormattedBlinds = "${sb.formatted()}/${bb.round()}"
val preFormattedBlinds = "${sb.formatted}/${bb.round()}"
// println("<<<<<< bb.toCurrency(currency) : ${bb.toCurrency(currency)}")
// println("<<<<<< preFormattedBlinds : $preFormattedBlinds")
val regex = Regex("-?\\d+(\\.\\d+)?")

@ -202,7 +202,7 @@ open class Action : RealmObject() {
Type.CALL, Type.CALL_ALLIN -> this.effectiveAmount
else -> this.amount
}
return amount?.formatted()
return amount?.formatted
}
fun toggleType(remainingStack: Double) {

@ -308,7 +308,7 @@ open class HandHistory : RealmObject(), Deletable, RowRepresentable, Filterable,
this.smallBlind?.let { sb ->
this.bigBlind?.let { bb ->
firstLineComponents.add("${sb.formatted()}/${bb.formatted()}")
firstLineComponents.add("${sb.formatted}/${bb.formatted}")
}
}
if (this.ante > 0.0) {
@ -343,7 +343,7 @@ open class HandHistory : RealmObject(), Deletable, RowRepresentable, Filterable,
val potSize = this.potSizeForStreet(street)
if (potSize > 0) {
streetItems.add(context.getString(R.string.pot_size))
streetItems.add(potSize.formatted())
streetItems.add(potSize.formatted)
}
if (this.cards.isNotEmpty()) {
@ -404,7 +404,7 @@ open class HandHistory : RealmObject(), Deletable, RowRepresentable, Filterable,
actionItems.add(context.getString(type.resId))
}
actionReadRow.amount?.let { amount ->
actionItems.add(amount.formatted())
actionItems.add(amount.formatted)
}
return actionItems.joinToString(" ")
}

@ -439,7 +439,7 @@ class HandHistoryAdapter(
// itemView.actionText.setTextColor(itemView.context.getColor(type.color))
// }
itemView.amountText.text = actionReadRow.amount?.formatted()
itemView.amountText.text = actionReadRow.amount?.formatted
}

@ -386,7 +386,7 @@ class HandHistoryViewModel : ViewModel(), RowRepresentableDataSource, CardCentra
val firstIndexOfStreet = this.sortedActions.firstOrNull { it.street == street }?.action?.index
?: this.sortedActions.size
val potSize = this.sortedActions.totalPotSize(firstIndexOfStreet)
return if (potSize > 0) potSize.formatted() else "" // "" required otherwise random values come up
return if (potSize > 0) potSize.formatted else "" // "" required otherwise random values come up
}
/***
@ -735,7 +735,7 @@ class HandHistoryViewModel : ViewModel(), RowRepresentableDataSource, CardCentra
val string = when (row) {
HandRowType.PLAYER_NUMBER -> this.handHistory.numberOfPlayers.toString()
HandRowType.COMMENT -> this.handHistory.comment ?: context.getString(R.string.comment)
HandRowType.ANTE -> this.handHistory.ante.formatted()
HandRowType.ANTE -> this.handHistory.ante.formatted
HandRowType.BIG_BLIND_ANTE_READ -> context.getString(R.string.yes)
// HandRowType.HERO_POSITION -> context.getString(R.string.set_hero_position)
// HandRowType.PLAYER_POSITION -> context.getString(R.string.set_position_details)
@ -745,7 +745,7 @@ class HandHistoryViewModel : ViewModel(), RowRepresentableDataSource, CardCentra
when (tag) {
PlayerSetupRow.Tag.POSITION.ordinal -> row.position.value
PlayerSetupRow.Tag.HAND.ordinal -> row.cardHolder?.cards?.formatted(context)
PlayerSetupRow.Tag.STACK.ordinal -> row.playerSetup?.stack?.formatted()
PlayerSetupRow.Tag.STACK.ordinal -> row.playerSetup?.stack?.formatted
else -> throw PAIllegalStateException("Unmanaged case with $row, tag = $tag")
}
}

@ -29,7 +29,7 @@ class RowHandHistoryViewHolder(itemView: View) : RecyclerView.ViewHolder(itemVie
itemView.cardsLayout.addView(view)
}
itemView.amount.text = handHistory.potSizeForStreet(Street.SUMMARY).formatted()
itemView.amount.text = handHistory.potSizeForStreet(Street.SUMMARY).formatted
val colorId = when(handHistory.heroWins) {
true -> R.color.green

@ -1,61 +0,0 @@
package net.pokeranalytics.android.ui.view
import android.content.Context
import android.util.AttributeSet
import android.view.LayoutInflater
import android.widget.FrameLayout
import androidx.constraintlayout.widget.ConstraintLayout
import kotlinx.android.synthetic.main.row_hand_history_view.view.*
import net.pokeranalytics.android.R
import net.pokeranalytics.android.model.handhistory.Street
import net.pokeranalytics.android.model.realm.handhistory.HandHistory
import net.pokeranalytics.android.util.extensions.formatted
/**
* Display a transaction row
*/
class HandHistoryRowView : FrameLayout {
private lateinit var rowHandHistory: ConstraintLayout
/**
* Constructors
*/
constructor(context: Context) : super(context) {
init()
}
constructor(context: Context, attrs: AttributeSet?) : super(context, attrs) {
init()
}
constructor(context: Context, attrs: AttributeSet?, defStyleAttr: Int) : super(context, attrs, defStyleAttr) {
init()
}
/**
* Init
*/
private fun init() {
val layoutInflater = LayoutInflater.from(context)
rowHandHistory = layoutInflater.inflate(R.layout.row_hand_history_view, this, false) as ConstraintLayout
val layoutParams = LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT)
addView(rowHandHistory, layoutParams)
}
/**
* Set the session data to the view
*/
fun setData(handHistory: HandHistory) {
rowHandHistory.cardsLayout.removeAllViews()
handHistory.cardViews(context, rowHandHistory.cardsLayout).forEach { view ->
rowHandHistory.cardsLayout.addView(view)
}
rowHandHistory.amount.text = handHistory.potSizeForStreet(Street.SUMMARY).formatted()
}
}

@ -9,6 +9,14 @@ import java.text.DecimalFormat
import java.text.NumberFormat
import java.util.*
val Double.formatted: String
get() {
val format = NumberFormat.getNumberInstance()
format.maximumFractionDigits = 2
format.minimumFractionDigits = 0
return format.format(this)
}
fun Number.kmbFormatted(threshold: Double = 10000.0): String {
var thousandsExponent = 0
var v = this.toDouble()
@ -42,12 +50,6 @@ fun Double.roundOffDecimal(): Double {
return this.toBigDecimal().setScale(2, RoundingMode.CEILING).toDouble()
}
fun Double.formatted(): String {
val format = NumberFormat.getNumberInstance()
format.maximumFractionDigits = 2
format.minimumFractionDigits = 0
return format.format(this)
}
val Double.noGroupingFormatted: String
get() {

@ -1,10 +1,10 @@
package net.pokeranalytics.android
import net.pokeranalytics.android.util.Parser
import net.pokeranalytics.android.util.extensions.formatted
import net.pokeranalytics.android.util.extensions.kmbFormatted
import org.junit.Assert
import org.junit.Test
import java.text.NumberFormat
class BasicUnitTest : RealmUnitTest() {
@ -13,6 +13,13 @@ class BasicUnitTest : RealmUnitTest() {
Assert.assertEquals(0, 0)
}
@Test
fun testStandardFormatting() {
val n = 2222.0
val f = n.formatted
Assert.assertEquals("2 222", f)
}
@Test
fun testFormatting() {
@ -22,7 +29,7 @@ class BasicUnitTest : RealmUnitTest() {
val n4 = n3 * n2 // 1B
val s1 = n1.kmbFormatted()
val s2 = n2.kmbFormatted()
// val s2 = n2.kmbFormatted()
val s2b = n2.kmbFormatted(1000.0)
val s3 = n3.kmbFormatted()
val s4 = n4.kmbFormatted()

Loading…
Cancel
Save