-
Notifications
You must be signed in to change notification settings - Fork 674
Updated FormatMoneyUseCase #3657
Changes from 2 commits
0b8d70d
6900c68
1db738f
9cce740
bd9f88a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,6 +13,15 @@ const val THOUSAND = 1_000 | |
const val MILLION = 1_000_000 | ||
const val BILLION = 1_000_000_000 | ||
|
||
/** | ||
* A use case class responsible for formatting currency and cryptocurrency values based on user preferences. | ||
* It supports regular currency formatting with or without decimal places, as well as shortened formats | ||
* (e.g., "1k", "1m"). For cryptocurrency, it formats up to 9 decimal places and removes unnecessary trailing zeros. | ||
* | ||
* @property features Provides feature toggles to customize app behavior. | ||
* @property devicePreferences Manages user-specific preferences for locale and other device settings. | ||
* @property context Application context, used for feature check and resource access. | ||
*/ | ||
class FormatMoneyUseCase @Inject constructor( | ||
private val features: Features, | ||
private val devicePreferences: DevicePreferences, | ||
|
@@ -23,25 +32,69 @@ class FormatMoneyUseCase @Inject constructor( | |
private val withoutDecimalFormatter = DecimalFormat("###,###", DecimalFormatSymbols(locale)) | ||
private val withDecimalFormatter = DecimalFormat("###,###.00", DecimalFormatSymbols(locale)) | ||
private val shortenAmountFormatter = DecimalFormat("###,###.##", DecimalFormatSymbols(locale)) | ||
private val cryptoFormatter = | ||
DecimalFormat("###,###,##0.${"0".repeat(9)}", DecimalFormatSymbols(locale)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you try fixing the formatter by There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done |
||
|
||
suspend fun format(value: Double, shortenAmount: Boolean): String { | ||
if (abs(value) >= THOUSAND && shortenAmount) { | ||
val result = if (abs(value) >= BILLION) { | ||
/** | ||
* Formats a currency or cryptocurrency amount based on the input parameters. | ||
* | ||
* @param value The numeric value to format. | ||
* @param shortenAmount Flag to indicate if the amount should be shortened (e.g., "1k" for 1,000). | ||
* @param isCrypto Flag to indicate if the value is a cryptocurrency, enabling up to 9 decimal places. | ||
* @return The formatted string representation of the value. | ||
*/ | ||
suspend fun format(value: Double, shortenAmount: Boolean, isCrypto: Boolean = false): String { | ||
val result = if (isCrypto) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You can directly There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done |
||
formatCrypto(value) | ||
} else if (abs(value) >= THOUSAND && shortenAmount) { | ||
if (abs(value) >= BILLION) { | ||
"${shortenAmountFormatter.format(value / BILLION)}b" | ||
} else if (abs(value) >= MILLION) { | ||
"${shortenAmountFormatter.format(value / MILLION)}m" | ||
} else { | ||
"${shortenAmountFormatter.format(value / THOUSAND)}k" | ||
} | ||
return result | ||
} else { | ||
val showDecimalPoint = features.showDecimalNumber.isEnabled(context) | ||
|
||
val formatter = when (showDecimalPoint) { | ||
true -> withDecimalFormatter | ||
false -> withoutDecimalFormatter | ||
} | ||
return formatter.format(value) | ||
formatter.format(value) | ||
} | ||
|
||
return result | ||
} | ||
|
||
/** | ||
* Formats a cryptocurrency value with up to 9 decimal places, removing unnecessary trailing zeros. | ||
* | ||
* @param value The cryptocurrency value to format. | ||
* @return The formatted cryptocurrency value as a string. | ||
*/ | ||
private fun formatCrypto(value: Double): String { | ||
val result = cryptoFormatter.format(value) | ||
return when { | ||
result.lastOrNull() == localDecimalSeparator().firstOrNull() -> { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't like this logic - can we just fix the formatter? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Removed the function, just relying on the suggested formatter now |
||
val newResult = result.dropLast(1) | ||
newResult.ifEmpty { "0" } | ||
} | ||
|
||
result.isEmpty() -> { | ||
"0" | ||
} | ||
|
||
else -> result | ||
} | ||
} | ||
|
||
/** | ||
* Retrieves the local decimal separator based on the user's locale. | ||
* | ||
* @return The decimal separator as a string. | ||
*/ | ||
private fun localDecimalSeparator(): String { | ||
return DecimalFormatSymbols(locale).decimalSeparator.toString() | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -24,79 +24,122 @@ class FormatMoneyUseCaseTest { | |
val amount: Double, | ||
val showDecimal: Boolean, | ||
val shortenAmount: Boolean, | ||
val isCrypto: Boolean, | ||
val locale: Locale, | ||
val expectedOutput: String | ||
) { | ||
ENG_SHOW_DECIMAL( | ||
amount = 1_000.12, | ||
showDecimal = true, | ||
shortenAmount = false, | ||
isCrypto = false, | ||
locale = Locale.ENGLISH, | ||
expectedOutput = "1,000.12" | ||
), | ||
ENG_HIDE_DECIMAL( | ||
amount = 1_000.12, | ||
showDecimal = false, | ||
shortenAmount = false, | ||
isCrypto = false, | ||
locale = Locale.ENGLISH, | ||
expectedOutput = "1,000" | ||
), | ||
GERMAN_SHOW_DECIMAL( | ||
amount = 1_000.12, | ||
showDecimal = true, | ||
shortenAmount = false, | ||
isCrypto = false, | ||
locale = Locale.GERMAN, | ||
expectedOutput = "1.000,12" | ||
), | ||
GERMAN_HIDE_DECIMAL( | ||
amount = 1_000.12, | ||
showDecimal = false, | ||
shortenAmount = false, | ||
isCrypto = false, | ||
locale = Locale.GERMAN, | ||
expectedOutput = "1.000" | ||
), | ||
ENGLISH_1K_SHORT_AMT( | ||
amount = 13_000.10, | ||
showDecimal = true, | ||
shortenAmount = true, | ||
isCrypto = false, | ||
locale = Locale.ENGLISH, | ||
expectedOutput = "13k" | ||
), | ||
ENGLISH_MILLION_SHORT_AMT( | ||
amount = 1_233_500.10, | ||
showDecimal = true, | ||
shortenAmount = true, | ||
isCrypto = false, | ||
locale = Locale.ENGLISH, | ||
expectedOutput = "1.23m" | ||
), | ||
ENGLISH_BILLION_SHORT_AMT( | ||
amount = 1_233_000_000.10, | ||
showDecimal = true, | ||
shortenAmount = true, | ||
isCrypto = false, | ||
locale = Locale.ENGLISH, | ||
expectedOutput = "1.23b" | ||
), | ||
GERMAN_1K_SHORT_AMT( | ||
amount = 13_000.10, | ||
showDecimal = true, | ||
shortenAmount = true, | ||
isCrypto = false, | ||
locale = Locale.GERMAN, | ||
expectedOutput = "13k" | ||
), | ||
GERMAN_MILLION_SHORT_AMT( | ||
amount = 1_233_500.10, | ||
showDecimal = true, | ||
shortenAmount = true, | ||
isCrypto = false, | ||
locale = Locale.GERMAN, | ||
expectedOutput = "1,23m" | ||
), | ||
GERMAN_BILLION_SHORT_AMT( | ||
amount = 1_233_000_000.10, | ||
showDecimal = true, | ||
shortenAmount = true, | ||
isCrypto = false, | ||
locale = Locale.GERMAN, | ||
expectedOutput = "1,23b" | ||
), | ||
ENG_SHOW_DECIMAL_CRYPTO( | ||
amount = 123_456.0, | ||
showDecimal = true, | ||
shortenAmount = false, | ||
isCrypto = true, | ||
locale = Locale.ENGLISH, | ||
expectedOutput = "123,456.000000000" | ||
), | ||
ENG_HIDE_DECIMAL_CRYPTO( | ||
amount = 123_456.0, | ||
showDecimal = false, | ||
shortenAmount = false, | ||
isCrypto = true, | ||
locale = Locale.ENGLISH, | ||
expectedOutput = "123,456.000000000" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We don't expect such zeros There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Updated the result as-per the suggested formatter expected result |
||
), | ||
GERMAN_SHOW_DECIMAL_CRYPTO( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We need a tear case for "0.000345 BTC" for example There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done |
||
amount = 123_456.0, | ||
showDecimal = true, | ||
shortenAmount = false, | ||
isCrypto = true, | ||
locale = Locale.GERMAN, | ||
expectedOutput = "123.456,000000000" | ||
), | ||
GERMAN_HIDE_DECIMAL_CRYPTO( | ||
amount = 123_456.0, | ||
showDecimal = false, | ||
shortenAmount = false, | ||
isCrypto = true, | ||
locale = Locale.GERMAN, | ||
expectedOutput = "123.456,000000000" | ||
), | ||
} | ||
|
||
private lateinit var formatMoneyUseCase: FormatMoneyUseCase | ||
|
@@ -114,7 +157,8 @@ class FormatMoneyUseCaseTest { | |
// when | ||
val result = formatMoneyUseCase.format( | ||
value = testCase.amount, | ||
shortenAmount = testCase.shortenAmount | ||
shortenAmount = testCase.shortenAmount, | ||
isCrypto = testCase.isCrypto | ||
) | ||
|
||
// then | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What caused this package change?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Package directive of
IvyCurrency
data class was wrong. It resides incom.ivy.legacy.domain.data.IvyCurrency
but directive wascom.ivy.wallet.domain.data.IvyCurrency
in the data class file. I fixed it thus these changes appeared as-per correct package directive in places where it's used.