refactor currency usage

feature/top10
Razmig Sarkissian 7 years ago
parent bd187a49a4
commit 2782ad103b
  1. 12
      app/src/androidTest/java/net/pokeranalytics/android/unitTests/filter/BlindFilterInstrumentedTest.kt
  2. 8
      app/src/main/java/net/pokeranalytics/android/calculus/Stat.kt
  3. 2
      app/src/main/java/net/pokeranalytics/android/model/realm/FilterCondition.kt
  4. 67
      app/src/main/java/net/pokeranalytics/android/model/realm/Session.kt
  5. 4
      app/src/main/java/net/pokeranalytics/android/model/utils/Seed.kt
  6. 8
      app/src/main/java/net/pokeranalytics/android/ui/fragment/BankrollDataFragment.kt
  7. 5
      app/src/main/java/net/pokeranalytics/android/ui/fragment/SessionFragment.kt
  8. 12
      app/src/main/java/net/pokeranalytics/android/ui/fragment/SettingsFragment.kt
  9. 7
      app/src/main/java/net/pokeranalytics/android/ui/view/SessionRowView.kt
  10. 10
      app/src/main/java/net/pokeranalytics/android/ui/view/rowrepresentable/FilterElementRow.kt
  11. 4
      app/src/main/java/net/pokeranalytics/android/ui/view/rowrepresentable/FilterSectionRow.kt
  12. 64
      app/src/main/java/net/pokeranalytics/android/util/CurrencyUtils.kt
  13. 46
      app/src/main/java/net/pokeranalytics/android/util/Preferences.kt
  14. 11
      app/src/main/java/net/pokeranalytics/android/util/extensions/NumbersExtension.kt

@ -43,7 +43,7 @@ class BlindFilterInstrumentedTest : BaseFilterInstrumentedUnitTest() {
val filter = QueryCondition.BLIND val filter = QueryCondition.BLIND
val blind = FilterElementRow.Blind(s1.stake!!) val blind = FilterElementRow.Blind(s1.blinds!!, true)
blind.filterSectionRow = FilterSectionRow.BLIND blind.filterSectionRow = FilterSectionRow.BLIND
val filterElement = FilterCondition(filterElementRows = arrayListOf(blind)) val filterElement = FilterCondition(filterElementRows = arrayListOf(blind))
filter.updateValueMap(filterElement) filter.updateValueMap(filterElement)
@ -87,8 +87,8 @@ class BlindFilterInstrumentedTest : BaseFilterInstrumentedUnitTest() {
val filter = QueryCondition.BLIND val filter = QueryCondition.BLIND
val blind1 = FilterElementRow.Blind(s1.stake!!) val blind1 = FilterElementRow.Blind(s1.blinds!!, true)
val blind2 = FilterElementRow.Blind(s2.stake!!) val blind2 = FilterElementRow.Blind(s2.blinds!!, true)
blind1.filterSectionRow = FilterSectionRow.BLIND blind1.filterSectionRow = FilterSectionRow.BLIND
blind2.filterSectionRow = FilterSectionRow.BLIND blind2.filterSectionRow = FilterSectionRow.BLIND
@ -133,7 +133,7 @@ class BlindFilterInstrumentedTest : BaseFilterInstrumentedUnitTest() {
val filter = QueryCondition.BLIND val filter = QueryCondition.BLIND
val blind = FilterElementRow.Blind(s3.stake!!, "$") val blind = FilterElementRow.Blind(s3.blinds!!, false)
blind.filterSectionRow = FilterSectionRow.BLIND blind.filterSectionRow = FilterSectionRow.BLIND
val filterElement = FilterCondition(filterElementRows = arrayListOf(blind)) val filterElement = FilterCondition(filterElementRows = arrayListOf(blind))
@ -176,10 +176,10 @@ class BlindFilterInstrumentedTest : BaseFilterInstrumentedUnitTest() {
val filter = QueryCondition.BLIND val filter = QueryCondition.BLIND
val blind1 = FilterElementRow.Blind(s1.stake!!) val blind1 = FilterElementRow.Blind(s1.blinds!!, false)
blind1.filterSectionRow = FilterSectionRow.BLIND blind1.filterSectionRow = FilterSectionRow.BLIND
val blind2 = FilterElementRow.Blind(s2.stake!!) val blind2 = FilterElementRow.Blind(s2.blinds!!, false)
blind2.filterSectionRow = FilterSectionRow.BLIND blind2.filterSectionRow = FilterSectionRow.BLIND
val filterElement = FilterCondition(filterElementRows = arrayListOf(blind1, blind2)) val filterElement = FilterCondition(filterElementRows = arrayListOf(blind1, blind2))
filter.updateValueMap(filterElement) filter.updateValueMap(filterElement)

@ -7,10 +7,10 @@ import net.pokeranalytics.android.exceptions.FormattingException
import net.pokeranalytics.android.model.interfaces.Timed import net.pokeranalytics.android.model.interfaces.Timed
import net.pokeranalytics.android.ui.view.RowRepresentable import net.pokeranalytics.android.ui.view.RowRepresentable
import net.pokeranalytics.android.ui.view.RowViewType import net.pokeranalytics.android.ui.view.RowViewType
import net.pokeranalytics.android.util.CurrencyUtils
import net.pokeranalytics.android.util.NULL_TEXT import net.pokeranalytics.android.util.NULL_TEXT
import net.pokeranalytics.android.util.extensions.formatted import net.pokeranalytics.android.util.extensions.formatted
import net.pokeranalytics.android.util.extensions.formattedHourlyDuration import net.pokeranalytics.android.util.extensions.formattedHourlyDuration
import net.pokeranalytics.android.util.extensions.toCurrency
import java.util.* import java.util.*
class StatFormattingException(message: String) : Exception(message) { class StatFormattingException(message: String) : Exception(message) {
@ -132,9 +132,8 @@ enum class Stat(var underlyingClass: Class<out Timed>? = null) : RowRepresentabl
when (this) { when (this) {
// Amounts + red/green // Amounts + red/green
Stat.NETRESULT, Stat.HOURLY_RATE, Stat.AVERAGE -> { Stat.NETRESULT, Stat.HOURLY_RATE, Stat.AVERAGE -> {
val numberFormat = CurrencyUtils.getCurrencyFormatter(context, currency)
val color = if (value >= this.threshold) R.color.green else R.color.red val color = if (value >= this.threshold) R.color.green else R.color.red
return TextFormat(numberFormat.format(value), color) return TextFormat(value.toCurrency(currency), color)
} }
// Red/green numericValues // Red/green numericValues
Stat.HOURLY_RATE_BB, Stat.AVERAGE_NET_BB, Stat.NET_BB_PER_100_HANDS -> { Stat.HOURLY_RATE_BB, Stat.AVERAGE_NET_BB, Stat.NET_BB_PER_100_HANDS -> {
@ -154,8 +153,7 @@ enum class Stat(var underlyingClass: Class<out Timed>? = null) : RowRepresentabl
} // white amountsr } // white amountsr
Stat.AVERAGE_BUYIN, Stat.STANDARD_DEVIATION, Stat.STANDARD_DEVIATION_HOURLY, Stat.AVERAGE_BUYIN, Stat.STANDARD_DEVIATION, Stat.STANDARD_DEVIATION_HOURLY,
Stat.STANDARD_DEVIATION_BB_PER_100_HANDS -> { Stat.STANDARD_DEVIATION_BB_PER_100_HANDS -> {
val numberFormat = CurrencyUtils.getCurrencyFormatter(context, currency) return TextFormat(value.toCurrency(currency))
return TextFormat(numberFormat.format(value))
} }
else -> throw FormattingException("Stat formatting of ${this.name} not handled") else -> throw FormattingException("Stat formatting of ${this.name} not handled")
} }

@ -32,7 +32,7 @@ open class FilterCondition() : RealmObject() {
} }
this.booleanValues = RealmList<Boolean>().apply { this.booleanValues = RealmList<Boolean>().apply {
this.addAll(filterElementRows.map { this.addAll(filterElementRows.map {
((it as Blind).currencySymbol == null) (it as Blind).hasDefaultCurrency
}) })
} }
} }

@ -35,9 +35,8 @@ import net.pokeranalytics.android.ui.view.RowViewType
import net.pokeranalytics.android.ui.view.rowrepresentable.CustomizableRowRepresentable import net.pokeranalytics.android.ui.view.rowrepresentable.CustomizableRowRepresentable
import net.pokeranalytics.android.ui.view.rowrepresentable.SeparatorRowRepresentable import net.pokeranalytics.android.ui.view.rowrepresentable.SeparatorRowRepresentable
import net.pokeranalytics.android.ui.view.rowrepresentable.SessionRow import net.pokeranalytics.android.ui.view.rowrepresentable.SessionRow
import net.pokeranalytics.android.util.CurrencyUtils
import net.pokeranalytics.android.util.NULL_TEXT import net.pokeranalytics.android.util.NULL_TEXT
import net.pokeranalytics.android.util.Preferences import net.pokeranalytics.android.util.UserDefaults
import net.pokeranalytics.android.util.extensions.* import net.pokeranalytics.android.util.extensions.*
import java.util.* import java.util.*
import java.util.Currency import java.util.Currency
@ -220,7 +219,7 @@ open class Session : RealmObject(), Savable, Editable, StaticRowRepresentableDat
var cgSmallBlind: Double? = null var cgSmallBlind: Double? = null
set(value) { set(value) {
field = value field = value
computeStakeAsString() formatBlinds()
} }
// The big blind value // The big blind value
@ -228,10 +227,10 @@ open class Session : RealmObject(), Savable, Editable, StaticRowRepresentableDat
set(value) { set(value) {
field = value field = value
this.computeStats() this.computeStats()
computeStakeAsString() formatBlinds()
} }
var stake: String? = null var blinds: String? = null
private set private set
// Tournament // Tournament
@ -252,7 +251,7 @@ open class Session : RealmObject(), Savable, Editable, StaticRowRepresentableDat
var tournamentFeatures: RealmList<TournamentFeature> = RealmList() var tournamentFeatures: RealmList<TournamentFeature> = RealmList()
fun bankrollHasBeenUpdated() { fun bankrollHasBeenUpdated() {
computeStakeAsString() formatBlinds()
} }
/** /**
@ -492,13 +491,19 @@ open class Session : RealmObject(), Savable, Editable, StaticRowRepresentableDat
return NULL_TEXT return NULL_TEXT
} }
/** val hasDefaultCurrency: Boolean
* Return the formatted blinds get() {
*/ return bankroll?.currency?.code == null
fun getCurrencyFormattedStake(context:Context) : String { }
val currencySymbol = currencySymbol ?: Preferences.getDefaultCurrency(context).symbol
return "$currencySymbol $stake" val currency : Currency
} get() {
return bankroll?.currency?.code?.let {
Currency.getInstance(it)
} ?: run {
UserDefaults.currency
}
}
/** /**
* Return the game title * Return the game title
@ -517,20 +522,16 @@ open class Session : RealmObject(), Savable, Editable, StaticRowRepresentableDat
return if (gameTitle.isNotBlank()) gameTitle else NULL_TEXT return if (gameTitle.isNotBlank()) gameTitle else NULL_TEXT
} }
val currencySymbol : String? fun getFormattedBlinds(): String {
get() { return blinds ?: NULL_TEXT
bankroll?.currency?.code?.let { }
return Currency.getInstance(it).symbol
}
return null
}
private fun computeStakeAsString() { private fun formatBlinds() {
stake = null blinds = null
if (cgBigBlind == null) return if (cgBigBlind == null) return
cgBigBlind?.let { bb -> cgBigBlind?.let { bb ->
val sb = cgSmallBlind ?: bb / 2.0 val sb = cgSmallBlind ?: bb / 2.0
stake = "${sb.formatted()}/${bb.round()}" blinds = "${currency.symbol} ${sb.formatted()}/${bb.round()}"
} }
} }
@ -584,7 +585,7 @@ open class Session : RealmObject(), Savable, Editable, StaticRowRepresentableDat
CustomizableRowRepresentable( CustomizableRowRepresentable(
RowViewType.HEADER_TITLE_AMOUNT_BIG, RowViewType.HEADER_TITLE_AMOUNT_BIG,
title = getFormattedDuration(), title = getFormattedDuration(),
computedStat = ComputedStat(Stat.NETRESULT, result?.net ?: 0.0, CurrencyUtils.getCurrency(bankroll)) computedStat = ComputedStat(Stat.NETRESULT, result?.net ?: 0.0, currency)
) )
) )
rows.add(SeparatorRowRepresentable()) rows.add(SeparatorRowRepresentable())
@ -594,7 +595,7 @@ open class Session : RealmObject(), Savable, Editable, StaticRowRepresentableDat
CustomizableRowRepresentable( CustomizableRowRepresentable(
RowViewType.HEADER_TITLE_AMOUNT_BIG, RowViewType.HEADER_TITLE_AMOUNT_BIG,
resId = R.string.pause, resId = R.string.pause,
computedStat = ComputedStat(Stat.NETRESULT, result?.net ?: 0.0, CurrencyUtils.getCurrency(bankroll)) computedStat = ComputedStat(Stat.NETRESULT, result?.net ?: 0.0, currency)
) )
) )
rows.add(SeparatorRowRepresentable()) rows.add(SeparatorRowRepresentable())
@ -604,14 +605,14 @@ open class Session : RealmObject(), Savable, Editable, StaticRowRepresentableDat
CustomizableRowRepresentable( CustomizableRowRepresentable(
RowViewType.HEADER_TITLE_AMOUNT_BIG, RowViewType.HEADER_TITLE_AMOUNT_BIG,
title = getFormattedDuration(), title = getFormattedDuration(),
computedStat = ComputedStat(Stat.NETRESULT, result?.net ?: 0.0, CurrencyUtils.getCurrency(bankroll)) computedStat = ComputedStat(Stat.NETRESULT, result?.net ?: 0.0, currency)
) )
) )
rows.add( rows.add(
CustomizableRowRepresentable( CustomizableRowRepresentable(
RowViewType.HEADER_TITLE_AMOUNT, RowViewType.HEADER_TITLE_AMOUNT,
resId = R.string.hour_rate_without_pauses, resId = R.string.hour_rate_without_pauses,
computedStat = ComputedStat(Stat.HOURLY_RATE, this.hourlyRate, CurrencyUtils.getCurrency(bankroll)) computedStat = ComputedStat(Stat.HOURLY_RATE, this.hourlyRate, currency)
) )
) )
@ -650,20 +651,20 @@ open class Session : RealmObject(), Savable, Editable, StaticRowRepresentableDat
override fun stringForRow(row: RowRepresentable, context: Context): String { override fun stringForRow(row: RowRepresentable, context: Context): String {
return when (row) { return when (row) {
SessionRow.BANKROLL -> bankroll?.name ?: NULL_TEXT SessionRow.BANKROLL -> bankroll?.name ?: NULL_TEXT
SessionRow.BLINDS -> getCurrencyFormattedStake(context) SessionRow.BLINDS -> getFormattedBlinds()
SessionRow.BREAK_TIME -> if (this.breakDuration > 0.0) this.breakDuration.toMinutes() else NULL_TEXT SessionRow.BREAK_TIME -> if (this.breakDuration > 0.0) this.breakDuration.toMinutes() else NULL_TEXT
SessionRow.BUY_IN -> this.result?.buyin?.toCurrency(CurrencyUtils.getCurrency(bankroll)) ?: NULL_TEXT SessionRow.BUY_IN -> this.result?.buyin?.toCurrency(currency) ?: NULL_TEXT
SessionRow.CASHED_OUT, SessionRow.PRIZE, SessionRow.NET_RESULT -> this.result?.cashout?.toCurrency(CurrencyUtils.getCurrency(bankroll)) ?: NULL_TEXT SessionRow.CASHED_OUT, SessionRow.PRIZE, SessionRow.NET_RESULT -> this.result?.cashout?.toCurrency(currency) ?: NULL_TEXT
SessionRow.COMMENT -> if (this.comment.isNotEmpty()) this.comment else NULL_TEXT SessionRow.COMMENT -> if (this.comment.isNotEmpty()) this.comment else NULL_TEXT
SessionRow.END_DATE -> this.endDate?.shortDateTime() ?: NULL_TEXT SessionRow.END_DATE -> this.endDate?.shortDateTime() ?: NULL_TEXT
SessionRow.GAME -> getFormattedGame() SessionRow.GAME -> getFormattedGame()
SessionRow.INITIAL_BUY_IN -> tournamentEntryFee?.toCurrency(CurrencyUtils.getCurrency(bankroll)) ?: NULL_TEXT SessionRow.INITIAL_BUY_IN -> tournamentEntryFee?.toCurrency(currency) ?: NULL_TEXT
SessionRow.LOCATION -> location?.name ?: NULL_TEXT SessionRow.LOCATION -> location?.name ?: NULL_TEXT
SessionRow.PLAYERS -> tournamentNumberOfPlayers?.toString() ?: NULL_TEXT SessionRow.PLAYERS -> tournamentNumberOfPlayers?.toString() ?: NULL_TEXT
SessionRow.POSITION -> result?.tournamentFinalPosition?.toString() ?: NULL_TEXT SessionRow.POSITION -> result?.tournamentFinalPosition?.toString() ?: NULL_TEXT
SessionRow.START_DATE -> this.startDate?.shortDateTime() ?: NULL_TEXT SessionRow.START_DATE -> this.startDate?.shortDateTime() ?: NULL_TEXT
SessionRow.TABLE_SIZE -> this.tableSize?.let { TableSize(it).localizedTitle(context) } ?: NULL_TEXT SessionRow.TABLE_SIZE -> this.tableSize?.let { TableSize(it).localizedTitle(context) } ?: NULL_TEXT
SessionRow.TIPS -> result?.tips?.toCurrency(CurrencyUtils.getCurrency(bankroll)) ?: NULL_TEXT SessionRow.TIPS -> result?.tips?.toCurrency(currency) ?: NULL_TEXT
SessionRow.TOURNAMENT_TYPE -> this.tournamentType?.let { SessionRow.TOURNAMENT_TYPE -> this.tournamentType?.let {
TournamentType.values()[it].localizedTitle(context) TournamentType.values()[it].localizedTitle(context)
} ?: run { } ?: run {
@ -886,7 +887,7 @@ open class Session : RealmObject(), Savable, Editable, StaticRowRepresentableDat
} }
value?.let { value?.let {
return stat.format(it, CurrencyUtils.getCurrency(this.bankroll), context) return stat.format(it, currency, context)
} ?: run { } ?: run {
return TextFormat(NULL_TEXT) return TextFormat(NULL_TEXT)
} }

@ -8,7 +8,7 @@ import net.pokeranalytics.android.model.realm.Currency
import net.pokeranalytics.android.model.realm.TournamentFeature import net.pokeranalytics.android.model.realm.TournamentFeature
import java.util.* import java.util.*
import net.pokeranalytics.android.model.realm.Game import net.pokeranalytics.android.model.realm.Game
import net.pokeranalytics.android.util.CurrencyUtils import net.pokeranalytics.android.util.UserDefaults
class Seed(var context:Context) : Realm.Transaction { class Seed(var context:Context) : Realm.Transaction {
@ -31,7 +31,7 @@ class Seed(var context:Context) : Realm.Transaction {
private fun createDefaultCurrencyAndBankroll(realm: Realm) { private fun createDefaultCurrencyAndBankroll(realm: Realm) {
// Currency // Currency
val localeCurrency = CurrencyUtils.getLocaleCurrency() val localeCurrency = UserDefaults.getLocaleCurrency()
val defaultCurrency = Currency() val defaultCurrency = Currency()
defaultCurrency.code = localeCurrency.currencyCode defaultCurrency.code = localeCurrency.currencyCode
realm.insertOrUpdate(defaultCurrency) realm.insertOrUpdate(defaultCurrency)

@ -17,9 +17,9 @@ import net.pokeranalytics.android.ui.view.RowViewType
import net.pokeranalytics.android.ui.view.rowrepresentable.BankrollRow import net.pokeranalytics.android.ui.view.rowrepresentable.BankrollRow
import net.pokeranalytics.android.ui.view.rowrepresentable.CustomizableRowRepresentable import net.pokeranalytics.android.ui.view.rowrepresentable.CustomizableRowRepresentable
import net.pokeranalytics.android.ui.view.rowrepresentable.SimpleRow import net.pokeranalytics.android.ui.view.rowrepresentable.SimpleRow
import net.pokeranalytics.android.util.CurrencyUtils
import net.pokeranalytics.android.util.NULL_TEXT import net.pokeranalytics.android.util.NULL_TEXT
import net.pokeranalytics.android.util.Preferences import net.pokeranalytics.android.util.Preferences
import net.pokeranalytics.android.util.extensions.toRate
import retrofit2.Call import retrofit2.Call
import retrofit2.Response import retrofit2.Response
import java.util.* import java.util.*
@ -96,9 +96,9 @@ class BankrollDataFragment : EditableDataFragment(), StaticRowRepresentableDataS
} }
BankrollRow.RATE -> { BankrollRow.RATE -> {
this.bankroll.currency?.rate?.let { rate -> this.bankroll.currency?.rate?.let { rate ->
CurrencyUtils.getCurrencyRateFormatter().format(rate) rate.toRate()
} ?: run { } ?: run {
CurrencyUtils.getCurrencyRateFormatter().format(1.0) 1.0.toRate()
} }
} }
else -> super.stringForRow(row) else -> super.stringForRow(row)
@ -119,7 +119,7 @@ class BankrollDataFragment : EditableDataFragment(), StaticRowRepresentableDataS
BankrollRow.RATE -> { BankrollRow.RATE -> {
this.bankroll.currency?.rate?.let { rate -> this.bankroll.currency?.rate?.let { rate ->
row.editingDescriptors(mapOf("defaultValue" to CurrencyUtils.getCurrencyRateFormatter().format(rate))) row.editingDescriptors(mapOf("defaultValue" to rate.toRate()))
} ?: run { } ?: run {
row.editingDescriptors(mapOf()) row.editingDescriptors(mapOf())
} }

@ -28,7 +28,6 @@ import net.pokeranalytics.android.ui.view.RowRepresentable
import net.pokeranalytics.android.ui.view.RowRepresentableDiffCallback import net.pokeranalytics.android.ui.view.RowRepresentableDiffCallback
import net.pokeranalytics.android.ui.view.SmoothScrollLinearLayoutManager import net.pokeranalytics.android.ui.view.SmoothScrollLinearLayoutManager
import net.pokeranalytics.android.ui.view.rowrepresentable.SessionRow import net.pokeranalytics.android.ui.view.rowrepresentable.SessionRow
import net.pokeranalytics.android.util.CurrencyUtils
import java.util.* import java.util.*
@ -105,9 +104,9 @@ class SessionFragment : PokerAnalyticsFragment(), RowRepresentableDelegate {
} }
SessionRow.BANKROLL -> { SessionRow.BANKROLL -> {
BottomSheetFragment.create(fragmentManager, row, this, data, false, CurrencyUtils.getCurrency(currentSession.bankroll)) BottomSheetFragment.create(fragmentManager, row, this, data, false, currentSession.currency)
} }
else -> BottomSheetFragment.create(fragmentManager, row, this, data, currentCurrency = CurrencyUtils.getCurrency(currentSession.bankroll)) else -> BottomSheetFragment.create(fragmentManager, row, this, data, currentCurrency = currentSession.currency)
} }
} }

@ -7,9 +7,11 @@ import android.view.LayoutInflater
import android.view.View import android.view.View
import android.view.ViewGroup import android.view.ViewGroup
import androidx.recyclerview.widget.LinearLayoutManager import androidx.recyclerview.widget.LinearLayoutManager
import io.realm.Realm
import kotlinx.android.synthetic.main.fragment_settings.* import kotlinx.android.synthetic.main.fragment_settings.*
import net.pokeranalytics.android.BuildConfig import net.pokeranalytics.android.BuildConfig
import net.pokeranalytics.android.R import net.pokeranalytics.android.R
import net.pokeranalytics.android.model.realm.Bankroll
import net.pokeranalytics.android.ui.activity.CurrenciesActivity import net.pokeranalytics.android.ui.activity.CurrenciesActivity
import net.pokeranalytics.android.ui.activity.DataListActivity import net.pokeranalytics.android.ui.activity.DataListActivity
import net.pokeranalytics.android.ui.activity.GDPRActivity import net.pokeranalytics.android.ui.activity.GDPRActivity
@ -22,6 +24,7 @@ import net.pokeranalytics.android.ui.view.RowRepresentable
import net.pokeranalytics.android.ui.view.rowrepresentable.SettingRow import net.pokeranalytics.android.ui.view.rowrepresentable.SettingRow
import net.pokeranalytics.android.util.Preferences import net.pokeranalytics.android.util.Preferences
import net.pokeranalytics.android.util.URL import net.pokeranalytics.android.util.URL
import net.pokeranalytics.android.util.UserDefaults
import net.pokeranalytics.android.util.extensions.openContactMail import net.pokeranalytics.android.util.extensions.openContactMail
import net.pokeranalytics.android.util.extensions.openPlayStorePage import net.pokeranalytics.android.util.extensions.openPlayStorePage
import net.pokeranalytics.android.util.extensions.openUrl import net.pokeranalytics.android.util.extensions.openUrl
@ -56,7 +59,7 @@ class SettingsFragment : PokerAnalyticsFragment(), RowRepresentableDelegate, Sta
override fun stringForRow(row: RowRepresentable): String { override fun stringForRow(row: RowRepresentable): String {
return when (row) { return when (row) {
SettingRow.VERSION -> BuildConfig.VERSION_NAME + if (BuildConfig.DEBUG) " (${BuildConfig.VERSION_CODE}) DEBUG" else "" SettingRow.VERSION -> BuildConfig.VERSION_NAME + if (BuildConfig.DEBUG) " (${BuildConfig.VERSION_CODE}) DEBUG" else ""
SettingRow.CURRENCY -> Currency.getInstance(Preferences.getCurrencyLocale(this.parentActivity)).symbol SettingRow.CURRENCY -> UserDefaults.currency.symbol
else -> "" else -> ""
} }
} }
@ -77,6 +80,13 @@ class SettingsFragment : PokerAnalyticsFragment(), RowRepresentableDelegate, Sta
if (requestCode == SettingsFragment.REQUEST_CODE_CURRENCY && resultCode == Activity.RESULT_OK) { if (requestCode == SettingsFragment.REQUEST_CODE_CURRENCY && resultCode == Activity.RESULT_OK) {
data?.let { data?.let {
Preferences.setCurrencyCode(data.getStringExtra(CurrenciesFragment.INTENT_CURRENCY_CODE), requireContext()) Preferences.setCurrencyCode(data.getStringExtra(CurrenciesFragment.INTENT_CURRENCY_CODE), requireContext())
val realm = Realm.getDefaultInstance()
realm.executeTransaction {
it.where(Bankroll::class.java).isNull("currency.code").findAll().forEach { bankroll ->
bankroll.currencyCodeHasBeenUpdated()
}
}
realm.close()
settingsAdapterRow.refreshRow(SettingRow.CURRENCY) settingsAdapterRow.refreshRow(SettingRow.CURRENCY)
} }
} }

@ -15,7 +15,6 @@ import net.pokeranalytics.android.model.TournamentType
import net.pokeranalytics.android.model.extensions.SessionState import net.pokeranalytics.android.model.extensions.SessionState
import net.pokeranalytics.android.model.extensions.getState import net.pokeranalytics.android.model.extensions.getState
import net.pokeranalytics.android.model.realm.Session import net.pokeranalytics.android.model.realm.Session
import net.pokeranalytics.android.util.CurrencyUtils
import net.pokeranalytics.android.util.extensions.getDayNumber import net.pokeranalytics.android.util.extensions.getDayNumber
import net.pokeranalytics.android.util.extensions.getShortDayName import net.pokeranalytics.android.util.extensions.getShortDayName
import net.pokeranalytics.android.util.extensions.shortTime import net.pokeranalytics.android.util.extensions.shortTime
@ -68,7 +67,7 @@ class SessionRowView : FrameLayout {
if (session.isTournament()) { if (session.isTournament()) {
session.tournamentEntryFee?.let { session.tournamentEntryFee?.let {
parameters.add(it.toCurrency(CurrencyUtils.getCurrency(session.bankroll))) parameters.add(it.toCurrency(session.currency))
} }
session.tournamentName?.let { session.tournamentName?.let {
@ -85,7 +84,7 @@ class SessionRowView : FrameLayout {
} }
} else { } else {
if (session.cgSmallBlind != null && session.cgBigBlind != null) { if (session.cgSmallBlind != null && session.cgBigBlind != null) {
parameters.add(session.getCurrencyFormattedStake(this.context)) parameters.add(session.getFormattedBlinds())
} }
session.game?.let { session.game?.let {
parameters.add(session.getFormattedGame()) parameters.add(session.getFormattedGame())
@ -149,7 +148,7 @@ class SessionRowView : FrameLayout {
rowHistorySession.infoTitle.isVisible = false rowHistorySession.infoTitle.isVisible = false
val result = session.result?.net ?: 0.0 val result = session.result?.net ?: 0.0
val formattedStat = ComputedStat(Stat.NETRESULT, result, currency = CurrencyUtils.getCurrency(session.bankroll)).format(context) val formattedStat = ComputedStat(Stat.NETRESULT, result, currency = session.currency).format(context)
rowHistorySession.gameResult.setTextColor(formattedStat.getColor(context)) rowHistorySession.gameResult.setTextColor(formattedStat.getColor(context))
rowHistorySession.gameResult.text = formattedStat.text rowHistorySession.gameResult.text = formattedStat.text
} }

@ -12,6 +12,7 @@ import net.pokeranalytics.android.ui.view.RowRepresentable
import net.pokeranalytics.android.ui.view.RowRepresentableEditDescriptor import net.pokeranalytics.android.ui.view.RowRepresentableEditDescriptor
import net.pokeranalytics.android.ui.view.RowViewType import net.pokeranalytics.android.ui.view.RowViewType
import net.pokeranalytics.android.util.Preferences import net.pokeranalytics.android.util.Preferences
import net.pokeranalytics.android.util.UserDefaults
import java.text.DateFormatSymbols import java.text.DateFormatSymbols
import java.util.* import java.util.*
@ -102,7 +103,7 @@ sealed class FilterElementRow : RowRepresentable {
data class Month(val month: Int) : SingleValueFilterElementRow(month) data class Month(val month: Int) : SingleValueFilterElementRow(month)
data class Day(val day: Int) : SingleValueFilterElementRow(day) data class Day(val day: Int) : SingleValueFilterElementRow(day)
data class Blind(val blind : String, val currencySymbol:String? = null) : StringFilterElementRow(blind) data class Blind(val blind: String, val hasDefaultCurrency: Boolean) : StringFilterElementRow(blind)
//TODO: Refactor? //TODO: Refactor?
data class PastDays(var lastDays: Int = 0) : SingleValueFilterElementRow(lastDays) { data class PastDays(var lastDays: Int = 0) : SingleValueFilterElementRow(lastDays) {
@ -298,16 +299,13 @@ sealed class FilterElementRow : RowRepresentable {
} }
is DataFilterElementRow -> this.name is DataFilterElementRow -> this.name
is StaticDataFilterElementRow -> this.name is StaticDataFilterElementRow -> this.name
else -> super.getDisplayName() is Blind -> this.blind
else -> super.getDisplayName()
} }
} }
override fun localizedTitle(context: Context): String { override fun localizedTitle(context: Context): String {
return when (this) { return when (this) {
is Blind -> {
val currencySymbol = currencySymbol ?: Preferences.getDefaultCurrency(context).symbol
"$currencySymbol $blind"
}
is StaticDataFilterElementRow -> this.getDataLocalizedTitle(context) is StaticDataFilterElementRow -> this.getDataLocalizedTitle(context)
else -> super.localizedTitle(context) else -> super.localizedTitle(context)
} }

@ -164,8 +164,8 @@ enum class FilterSectionRow(override val resId: Int?) : RowRepresentable {
val blinds = arrayListOf<Blind>() val blinds = arrayListOf<Blind>()
val realm = Realm.getDefaultInstance() val realm = Realm.getDefaultInstance()
realm.where<Session>().distinct("blind", "bankroll.currency.code").findAll().sort("cgSmallBlind", Sort.ASCENDING).map { realm.where<Session>().distinct("blind", "bankroll.currency.code").findAll().sort("cgSmallBlind", Sort.ASCENDING).map {
it.stake?.let { stake -> it.blinds?.let { stake ->
blinds.add(Blind(stake, it.currencySymbol)) blinds.add(Blind(stake, it.hasDefaultCurrency))
} }
} }
realm.close() realm.close()

@ -1,64 +0,0 @@
package net.pokeranalytics.android.util
import android.content.Context
import net.pokeranalytics.android.model.realm.Bankroll
import java.text.NumberFormat
import java.util.*
class CurrencyUtils {
companion object {
/**
* return the currency associated with this bankroll
*/
fun getCurrency(bankroll: Bankroll? = null) : Currency {
val currencyCode = bankroll?.currency?.code ?: CurrencyUtils.getLocaleCurrency().currencyCode
return Currency.getInstance(currencyCode)
}
/**
* Get a currency formatter
*/
fun getCurrencyFormatter(context: Context, currency: Currency? = null) : NumberFormat {
val currencyFormatter = NumberFormat.getCurrencyInstance(Preferences.getCurrencyLocale(context))
currency?.let {
currencyFormatter.currency = it
}
currencyFormatter.minimumFractionDigits = 0
currencyFormatter.maximumFractionDigits = 2
return currencyFormatter
}
/**
* Get a currency rate formatter
*/
fun getCurrencyRateFormatter() : NumberFormat {
val currencyFormatter = NumberFormat.getInstance()
currencyFormatter.minimumFractionDigits = 0
currencyFormatter.maximumFractionDigits = 6
return currencyFormatter
}
/**
* Return the locale currency, or en_US if there
*/
fun getLocaleCurrency() : Currency {
return try {
Currency.getInstance(Locale.getDefault())
} catch (ex: Exception) {
when (Locale.getDefault().language) {
"en" -> Currency.getInstance(Locale("en", "US"))
"fr" -> Currency.getInstance(Locale("fr", "FR"))
"es" -> Currency.getInstance(Locale("es", "ES"))
"de" -> Currency.getInstance(Locale("de", "DE"))
"ja" -> Currency.getInstance(Locale("ja", "JP"))
"zh" -> Currency.getInstance(Locale("zh", "CN"))
else -> Currency.getInstance(Locale("en", "US"))
}
}
}
}
}

@ -16,8 +16,6 @@ class Preferences {
companion object { companion object {
private var currencyLocale : Locale? = null
fun setString(key: Keys, value: String, context: Context) { fun setString(key: Keys, value: String, context: Context) {
val preferences = PreferenceManager.getDefaultSharedPreferences(context) val preferences = PreferenceManager.getDefaultSharedPreferences(context)
val editor = preferences.edit() val editor = preferences.edit()
@ -44,8 +42,7 @@ class Preferences {
fun setCurrencyCode(currencyCode: String, context: Context) { fun setCurrencyCode(currencyCode: String, context: Context) {
setString(Keys.CURRENCY_CODE, currencyCode, context) setString(Keys.CURRENCY_CODE, currencyCode, context)
currencyLocale = null UserDefaults.setCurrencyValues(context)
UserDefaults.currency = getDefaultCurrency(context)
} }
private fun getCurrencyCode(context: Context) : String? { private fun getCurrencyCode(context: Context) : String? {
@ -53,10 +50,6 @@ class Preferences {
} }
fun getCurrencyLocale(context : Context) : Locale? { fun getCurrencyLocale(context : Context) : Locale? {
currencyLocale?. let {
return it
}
getCurrencyCode(context)?.let { currencyCode -> getCurrencyCode(context)?.let { currencyCode ->
Locale.getAvailableLocales().filter{ Locale.getAvailableLocales().filter{
try { try {
@ -65,19 +58,17 @@ class Preferences {
false false
} }
}.first().let { }.first().let {
currencyLocale = it
return it return it
} }
} }
return null return null
} }
fun getDefaultCurrency(context: Context) : Currency { fun getDefaultCurrency(context: Context) : Currency? {
return getCurrencyLocale(context)?.let { getCurrencyLocale(context)?.let {
Currency.getInstance(it) return Currency.getInstance(it)
} ?: run {
CurrencyUtils.getLocaleCurrency()
} }
return null
} }
fun setStopShowingDisclaimer(context: Context) { fun setStopShowingDisclaimer(context: Context) {
@ -94,11 +85,36 @@ class Preferences {
class UserDefaults private constructor(context: Context) { class UserDefaults private constructor(context: Context) {
init { init {
currency = Preferences.getDefaultCurrency(context) setCurrencyValues(context)
} }
companion object : SingletonHolder<UserDefaults, Context>(::UserDefaults) { companion object : SingletonHolder<UserDefaults, Context>(::UserDefaults) {
lateinit var currency : Currency lateinit var currency : Currency
lateinit var currencyLocale : Locale
fun setCurrencyValues(context: Context) {
currency = Preferences.getDefaultCurrency(context) ?: getLocaleCurrency()
currencyLocale = Preferences.getCurrencyLocale(context) ?: Locale.getDefault()
}
/**
* Return the locale currency, or en_US if there
*/
fun getLocaleCurrency() : Currency {
return try {
Currency.getInstance(Locale.getDefault())
} catch (ex: Exception) {
when (Locale.getDefault().language) {
"en" -> Currency.getInstance(Locale("en", "US"))
"fr" -> Currency.getInstance(Locale("fr", "FR"))
"es" -> Currency.getInstance(Locale("es", "ES"))
"de" -> Currency.getInstance(Locale("de", "DE"))
"ja" -> Currency.getInstance(Locale("ja", "JP"))
"zh" -> Currency.getInstance(Locale("zh", "CN"))
else -> Currency.getInstance(Locale("en", "US"))
}
}
}
} }
} }

@ -2,6 +2,7 @@ package net.pokeranalytics.android.util.extensions
import android.content.Context import android.content.Context
import net.pokeranalytics.android.R import net.pokeranalytics.android.R
import net.pokeranalytics.android.util.UserDefaults
import java.text.DecimalFormat import java.text.DecimalFormat
import java.text.NumberFormat import java.text.NumberFormat
import java.util.* import java.util.*
@ -23,7 +24,7 @@ fun Double.formatted(): String {
fun Double.toCurrency(currency: Currency? = null): String { fun Double.toCurrency(currency: Currency? = null): String {
val currencyFormatter = NumberFormat.getCurrencyInstance() val currencyFormatter = NumberFormat.getCurrencyInstance(UserDefaults.currencyLocale)
currency?.let { currency?.let {
currencyFormatter.currency = currency currencyFormatter.currency = currency
} }
@ -33,6 +34,14 @@ fun Double.toCurrency(currency: Currency? = null): String {
return currencyFormatter.format(this) return currencyFormatter.format(this)
} }
fun Double.toRate(): String {
val currencyFormatter = NumberFormat.getInstance()
currencyFormatter.minimumFractionDigits = 0
currencyFormatter.maximumFractionDigits = 6
return currencyFormatter.format(this)
}
fun Double.formattedHourlyDuration() : String { fun Double.formattedHourlyDuration() : String {
return (this * 1000 * 3600).toLong().toMinutes() return (this * 1000 * 3600).toLong().toMinutes()
} }

Loading…
Cancel
Save