Skip to content

Commit c907bfe

Browse files
authored
Allow configuring decimal separator and ignore any other grouping separators (#219)
This is only used when parsing the numbers for showing the amount hint, other functionality is unaffected. Fixes #78. Fixes #186.
1 parent 5ba94e3 commit c907bfe

File tree

5 files changed

+67
-2
lines changed

5 files changed

+67
-2
lines changed

app/src/main/java/be/chvp/nanoledger/data/PreferencesDataSource.kt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import dagger.hilt.android.qualifiers.ApplicationContext
88
import javax.inject.Inject
99

1010
const val FILE_URI_KEY = "file_uri"
11+
const val DECIMAL_SEPARATOR_KEY = "decimal_separator"
1112
const val DEFAULT_CURRENCY_KEY = "default_currency"
1213
const val DEFAULT_STATUS_KEY = "default_status"
1314
const val CURRENCY_BEFORE_AMOUNT_KEY = "currency_before_amount"
@@ -64,6 +65,12 @@ class PreferencesDataSource
6465
status,
6566
).apply()
6667

68+
val decimalSeparator: LiveData<String> = sharedPreferences.stringLiveData(DECIMAL_SEPARATOR_KEY, ".").map { it!! }
69+
70+
fun getDecimalSeparator(): String = sharedPreferences.getString(DECIMAL_SEPARATOR_KEY, ".")!!
71+
72+
fun setDecimalSeparator(separator: String) = sharedPreferences.edit().putString(DECIMAL_SEPARATOR_KEY, separator).apply()
73+
6774
val currencyBeforeAmount: LiveData<Boolean> =
6875
sharedPreferences.booleanLiveData(
6976
CURRENCY_BEFORE_AMOUNT_KEY,

app/src/main/java/be/chvp/nanoledger/ui/common/TransactionFormViewModel.kt

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,15 +72,29 @@ abstract class TransactionFormViewModel
7272
.map { it.third }
7373
.filter { it != "" }
7474
.map {
75+
val cleaned =
76+
it.replace(
77+
Regex("[^-0-9${preferencesDataSource.getDecimalSeparator()}]"),
78+
"",
79+
).replace(preferencesDataSource.getDecimalSeparator(), ".")
7580
try {
76-
BigDecimal(it)
81+
BigDecimal(cleaned)
7782
} catch (e: NumberFormatException) {
7883
BigDecimal.ZERO
7984
}
8085
}
8186
.fold(BigDecimal.ZERO) { l, r -> l + r }
8287
.let { it.negate() }
83-
.let { if (it == BigDecimal.ZERO.setScale(it.scale())) "" else it.toString() }
88+
.let {
89+
if (it == BigDecimal.ZERO.setScale(it.scale())) {
90+
""
91+
} else {
92+
it.toString().replace(
93+
".",
94+
preferencesDataSource.getDecimalSeparator(),
95+
)
96+
}
97+
}
8498
}
8599

86100
val valid: LiveData<Boolean> =

app/src/main/java/be/chvp/nanoledger/ui/preferences/PreferencesActivity.kt

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,11 @@ class PreferencesActivity() : ComponentActivity() {
7979
true to stringResource(R.string.currency_order_before),
8080
false to stringResource(R.string.currency_order_after),
8181
)
82+
val separatorMap =
83+
mapOf(
84+
"." to stringResource(R.string.separator_point),
85+
"," to stringResource(R.string.separator_comma),
86+
)
8287
NanoLedgerTheme {
8388
Scaffold(topBar = { Bar() }) { contentPadding ->
8489
Column(
@@ -167,6 +172,39 @@ class PreferencesActivity() : ComponentActivity() {
167172
}
168173
}
169174
HorizontalDivider()
175+
val decimalSeparator by preferencesViewModel.decimalSeparator.observeAsState()
176+
var expandedSeparator by rememberSaveable { mutableStateOf(false) }
177+
ExposedDropdownMenuBox(
178+
expanded = expandedSeparator,
179+
onExpandedChange = { expandedSeparator = !expandedSeparator },
180+
modifier = Modifier.fillMaxWidth(),
181+
) {
182+
Setting(
183+
stringResource(R.string.decimal_separator),
184+
separatorMap[decimalSeparator ?: "."] ?: stringResource(
185+
R.string.separator_point,
186+
),
187+
modifier = Modifier.menuAnchor(MenuAnchorType.PrimaryEditable),
188+
) { expandedSeparator = true }
189+
ExposedDropdownMenu(
190+
expanded = expandedSeparator,
191+
onDismissRequest = { expandedSeparator = false },
192+
modifier = Modifier.exposedDropdownSize(true),
193+
) {
194+
separatorMap.forEach {
195+
DropdownMenuItem(
196+
text = { Text(it.value) },
197+
onClick = {
198+
preferencesViewModel.storeDecimalSeparator(it.key)
199+
expandedSeparator = false
200+
},
201+
contentPadding =
202+
ExposedDropdownMenuDefaults.ItemContentPadding,
203+
)
204+
}
205+
}
206+
}
207+
HorizontalDivider()
170208
val currencyBeforeAmount by
171209
preferencesViewModel.currencyBeforeAmount.observeAsState()
172210
var expandedCurrency by rememberSaveable { mutableStateOf(false) }

app/src/main/java/be/chvp/nanoledger/ui/preferences/PreferencesViewModel.kt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,16 @@ class PreferencesViewModel
1616
private val preferencesDataSource: PreferencesDataSource,
1717
) : AndroidViewModel(application) {
1818
val fileUri: LiveData<Uri?> = preferencesDataSource.fileUri
19+
val decimalSeparator: LiveData<String> = preferencesDataSource.decimalSeparator
1920
val defaultCurrency: LiveData<String> = preferencesDataSource.defaultCurrency
2021
val defaultStatus: LiveData<String> = preferencesDataSource.defaultStatus
2122
val currencyBeforeAmount: LiveData<Boolean> = preferencesDataSource.currencyBeforeAmount
2223
val postingWidth: LiveData<Int> = preferencesDataSource.postingWidth
2324

2425
fun storeFileUri(uri: Uri) = preferencesDataSource.setFileUri(uri)
2526

27+
fun storeDecimalSeparator(separator: String) = preferencesDataSource.setDecimalSeparator(separator)
28+
2629
fun storeDefaultCurrency(currency: String) = preferencesDataSource.setDefaultCurrency(currency)
2730

2831
fun storeDefaultStatus(status: String) = preferencesDataSource.setDefaultStatus(status)

app/src/main/res/values/strings.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
<string name="currency_order_after">Currency after amount</string>
1414
<string name="currency_order_before">Currency before amount</string>
1515
<string name="date">Date</string>
16+
<string name="decimal_separator">Decimal separator</string>
1617
<string name="default_currency">Default currency</string>
1718
<string name="default_status">Default status</string>
1819
<string name="delete">Delete</string>
@@ -31,6 +32,8 @@
3132
<string name="posting_width">Width of postings written to file</string>
3233
<string name="save">Save</string>
3334
<string name="select_file">Select file…</string>
35+
<string name="separator_point">Point (.)</string>
36+
<string name="separator_comma">Comma (,)</string>
3437
<string name="settings">Settings</string>
3538
<string name="show">Show</string>
3639
<string name="status_cleared">Cleared (*)</string>

0 commit comments

Comments
 (0)