Skip to content

Commit

Permalink
WIP: Localize amount formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
Iliyan Germanov committed Nov 15, 2021
1 parent e21711b commit 70bffc3
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 133 deletions.
9 changes: 1 addition & 8 deletions app/src/main/java/com/ivy/wallet/base/UtilExt.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import android.content.Context
import android.icu.util.Currency
import com.ivy.wallet.model.IvyCurrency
import java.text.DecimalFormat
import java.text.DecimalFormatSymbols
import java.util.*
import kotlin.math.abs
import kotlin.math.log10
Expand Down Expand Up @@ -183,13 +182,7 @@ fun shouldShortAmount(amount: Double): Boolean {
}

fun formatInt(number: Int): String {
return DecimalFormat(
"#,###,###,###",
DecimalFormatSymbols().apply {
this.groupingSeparator = ','
//TODO: Support groupingSeparator localization, example CZK doesn't like ","
}
).format(number)
return DecimalFormat("#,###,###,###").format(number)
}

fun hasLockScreen(context: Context): Boolean {
Expand Down

This file was deleted.

62 changes: 45 additions & 17 deletions app/src/main/java/com/ivy/wallet/ui/theme/modal/edit/AmountModal.kt
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import com.ivy.wallet.ui.theme.components.IvyIcon
import com.ivy.wallet.ui.theme.modal.IvyModal
import com.ivy.wallet.ui.theme.modal.ModalPositiveButton
import com.ivy.wallet.ui.theme.modal.modalPreviewActionRowHeight
import java.text.DecimalFormatSymbols
import java.util.*
import kotlin.math.truncate

Expand Down Expand Up @@ -77,7 +78,7 @@ fun BoxWithConstraintsScope.AmountModal(
if (amount.isEmpty()) {
onAmountChanged(0.0)
} else {
onAmountChanged(amount.replace(",", "").toDouble())
onAmountChanged(amount.amountToDouble())
}
dismiss()
} catch (e: Exception) {
Expand Down Expand Up @@ -169,21 +170,21 @@ fun AmountInput(
setAmount(it)
firstInput = false
} else {
val newlyEnteredNumberString = amount.replace(",", "") + it
val newlyEnteredNumberString = amount + it

val decimalPartString = newlyEnteredNumberString
.split(".")
.split(localDecimalSeparator())
.getOrNull(1)
val decimalCount = decimalPartString?.length ?: 0

val amountDouble = newlyEnteredNumberString.toDoubleOrNull()
val amountDouble = newlyEnteredNumberString.amountToDoubleOrNull()

val decimalCountOkay = IvyCurrency.fromCode(currency)?.isCrypto == true
|| decimalCount <= 2
|| decimalCount <= 2
if (amountDouble != null && decimalCountOkay) {
val intPart = truncate(amountDouble).toInt()
val decimalPartFormatted = if (decimalPartString != null) {
".${decimalPartString}"
"${localDecimalSeparator()}${decimalPartString}"
} else ""

val finalAmount = formatInt(intPart) + decimalPartFormatted
Expand All @@ -194,11 +195,12 @@ fun AmountInput(
},
onDecimalPoint = {
if (firstInput) {
setAmount("0.")
setAmount("0${localDecimalSeparator()}")
firstInput = false
} else {
val newlyEnteredString = if (amount.isEmpty()) "0." else "$amount."
if (newlyEnteredString.replace(",", "").toDoubleOrNull() != null) {
val newlyEnteredString = if (amount.isEmpty())
"0${localDecimalSeparator()}" else "$amount${localDecimalSeparator()}"
if (newlyEnteredString.amountToDoubleOrNull() != null) {
setAmount(newlyEnteredString)
}
}
Expand All @@ -219,19 +221,17 @@ fun AmountInput(
}

private fun formatNumber(number: String): String? {
val newAmountString = number.replace(",", "")

val decimalPartString = newAmountString
.split(".")
val decimalPartString = number
.split(localDecimalSeparator())
.getOrNull(1)
val newDecimalCount = decimalPartString?.length ?: 0

val amountDouble = newAmountString.toDoubleOrNull()
val amountDouble = number.amountToDoubleOrNull()

if (newDecimalCount <= 2 && amountDouble != null) {
val intPart = truncate(amountDouble).toInt()
val decimalFormatted = if (decimalPartString != null) {
".${decimalPartString}"
"${localDecimalSeparator()}${decimalPartString}"
} else ""

return formatInt(intPart) + decimalFormatted
Expand Down Expand Up @@ -368,7 +368,7 @@ fun AmountKeyboard(
verticalAlignment = Alignment.CenterVertically,
horizontalArrangement = Arrangement.Center
) {
KeypadCircleButton(text = ".") {
KeypadCircleButton(text = localDecimalSeparator()) {
onDecimalPoint()
}

Expand Down Expand Up @@ -449,6 +449,35 @@ private fun circleButtonModifier(
.border(2.dp, IvyTheme.colors.medium, Shapes.roundedFull)
}

fun String.amountToDoubleOrNull(): Double? {
return this.normalizeAmount().toDoubleOrNull()
}

fun String.amountToDouble(): Double {
return this.normalizeAmount().toDouble()
}

fun String.normalizeAmount(): String {
return this.removeGroupingSeparator()
.normalizeDecimalSeparator()
}

fun String.removeGroupingSeparator(): String {
return replace(localGroupingSeparator(), "")
}

fun String.normalizeDecimalSeparator(): String {
return replace(localDecimalSeparator(), ".")
}

fun localDecimalSeparator(): String {
return DecimalFormatSymbols.getInstance().decimalSeparator.toString()
}

fun localGroupingSeparator(): String {
return DecimalFormatSymbols.getInstance().groupingSeparator.toString()
}

@Preview
@Composable
private fun Preview() {
Expand All @@ -465,6 +494,5 @@ private fun Preview() {

}
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ fun BoxWithConstraintsScope.CalculatorModal(
modifier = Modifier
.fillMaxWidth()
.padding(horizontal = 24.dp),
text = if (isEmpty) "Expression (+-/*=)" else expression,
text = if (isEmpty) "Calculation (+-/*=)" else expression,
style = Typo.numberH2.style(
fontWeight = FontWeight.Bold,
textAlign = TextAlign.Center,
Expand Down Expand Up @@ -117,7 +117,7 @@ fun BoxWithConstraintsScope.CalculatorModal(
KeypadCircleButton(text = "=") {
val result = calculate(expression)
if (result != null) {
expression = result.format(currency).replace(",", "")
expression = result.format(currency).removeGroupingSeparator()
}
}
},
Expand All @@ -126,7 +126,7 @@ fun BoxWithConstraintsScope.CalculatorModal(
expression += it
},
onDecimalPoint = {
expression += "."
expression += localDecimalSeparator()
},
onBackspace = {
if (expression.isNotEmpty()) {
Expand All @@ -141,12 +141,17 @@ fun BoxWithConstraintsScope.CalculatorModal(

private fun calculate(expression: String): Double? {
return try {
Keval.eval(expression)
Keval.eval(expression.normalizeExpression())
} catch (e: Exception) {
null
}
}

private fun String.normalizeExpression(): String {
return this.replace(localGroupingSeparator(), "")
.replace(localDecimalSeparator(), ".")
}

@Preview
@Composable
private fun Preview() {
Expand Down

0 comments on commit 70bffc3

Please sign in to comment.