This repository has been archived by the owner on Nov 5, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 733
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
1d77b56
commit 88eee88
Showing
5 changed files
with
101 additions
and
40 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
23 changes: 13 additions & 10 deletions
23
shared/ui/core/src/main/java/com/ivy/ui/FormatMoneyUseCase.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,26 +1,29 @@ | ||
package com.ivy.ui | ||
|
||
import androidx.compose.runtime.Composable | ||
import android.content.Context | ||
import com.ivy.domain.features.Features | ||
import com.ivy.ui.time.DevicePreferences | ||
import dagger.hilt.android.qualifiers.ApplicationContext | ||
import java.text.DecimalFormat | ||
import java.text.DecimalFormatSymbols | ||
import java.util.Locale | ||
import javax.inject.Inject | ||
|
||
class FormatMoneyUseCase @Inject constructor( | ||
private val feature: Features, | ||
private val devicePreferences: DevicePreferences, | ||
@ApplicationContext private val context: Context | ||
) { | ||
|
||
private val locale = devicePreferences.locale() | ||
private val formatterWithoutDecimal = DecimalFormat("###,###", DecimalFormatSymbols(locale)) | ||
private val formatterWithDecimal = DecimalFormat("###,###.00", DecimalFormatSymbols(locale)) | ||
@Composable | ||
fun format(value: Double): String { | ||
val showDecimal = feature.showDecimalNumber.asEnabledState() | ||
return when (showDecimal) { | ||
true -> formatterWithDecimal.format(value) | ||
else -> formatterWithoutDecimal.format(value) | ||
private val withoutDecimalFormatter = DecimalFormat("###,###", DecimalFormatSymbols(locale)) | ||
private val withDecimalFormatter = DecimalFormat("###,###.00", DecimalFormatSymbols(locale)) | ||
|
||
suspend fun format(value: Double): String { | ||
val showDecimalPoint = feature.showDecimalNumber.isEnabled(context) | ||
|
||
return when (showDecimalPoint) { | ||
true -> withDecimalFormatter.format(value) | ||
false -> withoutDecimalFormatter.format(value) | ||
} | ||
} | ||
} |
106 changes: 78 additions & 28 deletions
106
shared/ui/core/src/test/java/com/ivy/ui/FormatMoneyUseCaseTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,52 +1,102 @@ | ||
package com.ivy.ui | ||
|
||
import androidx.compose.runtime.Composable | ||
import android.content.Context | ||
import androidx.test.platform.app.InstrumentationRegistry | ||
import com.ivy.domain.features.BoolFeature | ||
import com.ivy.domain.features.FeatureGroup | ||
import com.ivy.domain.features.Features | ||
import com.ivy.ui.time.DevicePreferences | ||
import io.kotest.common.runBlocking | ||
import io.kotest.matchers.shouldBe | ||
import io.mockk.every | ||
import io.mockk.mockk | ||
import org.junit.Before | ||
import org.junit.Test | ||
import org.junit.runner.RunWith | ||
import org.robolectric.ParameterizedRobolectricTestRunner | ||
import java.util.Locale | ||
|
||
class FormatMoneyUseCaseTest { | ||
@RunWith(ParameterizedRobolectricTestRunner::class) | ||
class FormatMoneyUseCaseTest(private val param: TestData) { | ||
|
||
private val context: Context = InstrumentationRegistry.getInstrumentation().context | ||
private val features = mockk<Features>() | ||
private val devicePreferences = mockk<DevicePreferences>() | ||
private val showDecimal = BoolFeature( | ||
key = "show_decimal_number", | ||
group = FeatureGroup.Other, | ||
name = "Show Decimal Number", | ||
description = "Show Decimal Number in amounts", | ||
defaultValue = true | ||
) | ||
|
||
private val hideDecimal = BoolFeature( | ||
key = "show_decimal_number", | ||
group = FeatureGroup.Other, | ||
name = "Show Decimal Number", | ||
description = "Show Decimal Number in amounts", | ||
defaultValue = false | ||
class TestData( | ||
val givenInput: Double, | ||
val showDecimal: BoolFeature, | ||
val locale: Locale, | ||
val expectedOutPut: String | ||
) | ||
|
||
private lateinit var formatMoneyUseCase: FormatMoneyUseCase | ||
|
||
@Before | ||
fun setup() { | ||
formatMoneyUseCase = FormatMoneyUseCase(features,devicePreferences) | ||
} | ||
|
||
@Composable | ||
@Test | ||
fun `Format with no decimal place locale ENGLISH`() { | ||
//given | ||
every { features.showDecimalNumber } returns hideDecimal | ||
every { devicePreferences.locale() } returns Locale.ENGLISH | ||
fun `validate decimal formatting`(): Unit = runBlocking { | ||
// given | ||
every { features.showDecimalNumber } returns param.showDecimal | ||
every { devicePreferences.locale() } returns param.locale | ||
formatMoneyUseCase = FormatMoneyUseCase(features, devicePreferences, context) | ||
|
||
// when | ||
val result = formatMoneyUseCase.format(value = param.givenInput) | ||
|
||
// then | ||
result shouldBe param.expectedOutPut | ||
} | ||
|
||
val result = formatMoneyUseCase.format(value = 1000.12) | ||
result shouldBe "1,000" | ||
companion object { | ||
@JvmStatic | ||
@ParameterizedRobolectricTestRunner.Parameters(name = "Input: {0}") | ||
fun params() = listOf( | ||
TestData( | ||
givenInput = 1000.12, | ||
showDecimal = BoolFeature( | ||
key = "show_decimal_number", | ||
group = FeatureGroup.Other, | ||
name = "Show Decimal Number", | ||
description = "Whether to show the decimal part in amounts", | ||
defaultValue = true | ||
), | ||
locale = Locale.ENGLISH, | ||
expectedOutPut = "1,000.12" | ||
), | ||
TestData( | ||
givenInput = 1000.12, | ||
showDecimal = BoolFeature( | ||
key = "show_decimal_number", | ||
group = FeatureGroup.Other, | ||
name = "Show Decimal Number", | ||
description = "Whether to show the decimal part in amounts", | ||
defaultValue = false | ||
), | ||
locale = Locale.ENGLISH, | ||
expectedOutPut = "1,000" | ||
), | ||
TestData( | ||
givenInput = 1000.12, | ||
showDecimal = BoolFeature( | ||
key = "show_decimal_number", | ||
group = FeatureGroup.Other, | ||
name = "Show Decimal Number", | ||
description = "Whether to show the decimal part in amounts", | ||
defaultValue = true | ||
), | ||
locale = Locale.GERMAN, | ||
expectedOutPut = "1.000,12" | ||
), | ||
TestData( | ||
givenInput = 1000.12, | ||
showDecimal = BoolFeature( | ||
key = "show_decimal_number", | ||
group = FeatureGroup.Other, | ||
name = "Show Decimal Number", | ||
description = "Whether to show the decimal part in amounts", | ||
defaultValue = false | ||
), | ||
locale = Locale.GERMAN, | ||
expectedOutPut = "1.000" | ||
), | ||
) | ||
} | ||
} |