diff --git a/app/src/main/java/net/helcel/cowspent/android/bill_edit/EditBillActivity.kt b/app/src/main/java/net/helcel/cowspent/android/bill_edit/EditBillActivity.kt index 2dc6c4e..885910d 100644 --- a/app/src/main/java/net/helcel/cowspent/android/bill_edit/EditBillActivity.kt +++ b/app/src/main/java/net/helcel/cowspent/android/bill_edit/EditBillActivity.kt @@ -445,13 +445,14 @@ class EditBillActivity : AppCompatActivity() { val uiAmount = viewModel.parseAmountFromUi(it.value) SupportUtil.round2(uiAmount * viewModel.selectedCurrencyRate) } - } else { // PERCENT + } else { // FRACTION val totalAmount = viewModel.amountAsDouble - viewModel.owersPercentSplit.filter { (id, percentStr) -> - viewModel.owersSelection[id] == true && viewModel.parseAmountFromUi(percentStr) > 0 + val fractionTotal = viewModel.getFractionTotal() + viewModel.owersFractionSplit.filter { (id, fraction) -> + viewModel.owersSelection[id] == true && (fraction.toIntOrNull() ?: 0) > 0 }.mapValues { - val percent = viewModel.parseAmountFromUi(it.value) - val uiAmount = SupportUtil.round2(totalAmount * percent / 100.0) + val fraction = it.value.toInt() + val uiAmount = SupportUtil.round2(totalAmount * fraction / fractionTotal) SupportUtil.round2(uiAmount * viewModel.selectedCurrencyRate) } } diff --git a/app/src/main/java/net/helcel/cowspent/android/bill_edit/EditBillScreen.kt b/app/src/main/java/net/helcel/cowspent/android/bill_edit/EditBillScreen.kt index 0b099f8..94d3729 100644 --- a/app/src/main/java/net/helcel/cowspent/android/bill_edit/EditBillScreen.kt +++ b/app/src/main/java/net/helcel/cowspent/android/bill_edit/EditBillScreen.kt @@ -79,10 +79,10 @@ fun EditBillScreen( val whatFirst = remember(context) { val preferences = PreferenceManager.getDefaultSharedPreferences(context) preferences.getBoolean(context.getString(R.string.pref_key_extra_features), false) && - preferences.getString( - context.getString(R.string.pref_key_bill_field_order), - context.getString(R.string.pref_value_bill_amount_first) - ) == context.getString(R.string.pref_value_bill_what_first) + preferences.getString( + context.getString(R.string.pref_key_bill_field_order), + context.getString(R.string.pref_value_bill_amount_first) + ) == context.getString(R.string.pref_value_bill_what_first) } var amountFocused by remember { mutableStateOf(false) } var amountSelection by remember { mutableStateOf(TextRange(viewModel.amount.length)) } @@ -309,18 +309,18 @@ private fun keyboardColors(): KeyboardColors { val (tray, edge) = when { Build.VERSION.SDK_INT >= Build.VERSION_CODES.UPSIDE_DOWN_CAKE -> if (dark) { colorResource(android.R.color.system_surface_container_dark) to - colorResource(android.R.color.system_surface_container_high_dark) + colorResource(android.R.color.system_surface_container_high_dark) } else { colorResource(android.R.color.system_surface_container_light) to - colorResource(android.R.color.system_surface_container_high_light) + colorResource(android.R.color.system_surface_container_high_light) } else -> if (dark) { colorResource(android.R.color.system_neutral1_900) to - colorResource(android.R.color.system_neutral1_800) + colorResource(android.R.color.system_neutral1_800) } else { colorResource(android.R.color.system_neutral1_50) to - colorResource(android.R.color.system_neutral1_100) + colorResource(android.R.color.system_neutral1_100) } } if (dark) KeyboardColors( @@ -402,88 +402,88 @@ fun BillBasicInfoSection( stringResource(R.string.currency_dialog_title, viewModel.mainCurrencyName) val amountField: @Composable () -> Unit = { - OutlinedTextField( - value = TextFieldValue( - text = viewModel.amount, - selection = TextRange( - amountSelection.start.coerceIn(0, viewModel.amount.length), - amountSelection.end.coerceIn(0, viewModel.amount.length) - ) - ), - onValueChange = { nv -> - val filteredValue = nv.text.filter { it in "0123456789.,+-*/()" } - onAmountSelectionChange( - if (filteredValue == nv.text) nv.selection else TextRange(filteredValue.length) - ) - if (filteredValue != viewModel.amount) { - viewModel.amount = filteredValue - viewModel.updateSplits() - } - }, - enabled = canEdit, - placeholder = { Text("0") }, - modifier = Modifier - .fillMaxWidth() - .focusRequester(amountFocusRequester) - .onFocusChanged { onAmountFocusChange(it.isFocused) }, - leadingIcon = { - val currencyToShow = viewModel.selectedCurrencyName.ifEmpty { - viewModel.mainCurrencyName.ifEmpty { "$" } - } - TextIconDisplay( - textIcon = TextIcon.Symbol(currencyToShow), - tint = MaterialTheme.colors.onSurface.copy(alpha = 0.6f) - ) - }, - trailingIcon = { - IconButton( - enabled = canEdit, - onClick = { - val mainLabel = viewModel.mainCurrencyName.ifEmpty { "$" } - val options = listOf("$mainLabel | Base") + viewModel.currencies.map { - "${it.name} | 1 $mainLabel = ${it.exchangeRate} ${it.name}" - } - viewModel.showDialog( - title = currencyDialogTitle, - items = options, - onItemSelected = { index -> - if (index == 0) { - viewModel.resetCurrency() - } else { - viewModel.convertCurrency(viewModel.currencies[index - 1]) - } - } - ) + OutlinedTextField( + value = TextFieldValue( + text = viewModel.amount, + selection = TextRange( + amountSelection.start.coerceIn(0, viewModel.amount.length), + amountSelection.end.coerceIn(0, viewModel.amount.length) + ) + ), + onValueChange = { nv -> + val filteredValue = nv.text.filter { it in "0123456789.,+-*/()" } + onAmountSelectionChange( + if (filteredValue == nv.text) nv.selection else TextRange(filteredValue.length) + ) + if (filteredValue != viewModel.amount) { + viewModel.amount = filteredValue + viewModel.updateSplits() } - ) { - Icon(Icons.Default.SwapHoriz, contentDescription = "Change Currency") - } - }, - keyboardOptions = KeyboardOptions( - keyboardType = KeyboardType.Decimal, - imeAction = if (whatFirst) ImeAction.Done else ImeAction.Next - ), - keyboardActions = KeyboardActions( - onNext = { focusManager.moveFocus(FocusDirection.Next) }, - onDone = { focusManager.clearFocus() } + }, + enabled = canEdit, + placeholder = { Text("0") }, + modifier = Modifier + .fillMaxWidth() + .focusRequester(amountFocusRequester) + .onFocusChanged { onAmountFocusChange(it.isFocused) }, + leadingIcon = { + val currencyToShow = viewModel.selectedCurrencyName.ifEmpty { + viewModel.mainCurrencyName.ifEmpty { "$" } + } + TextIconDisplay( + textIcon = TextIcon.Symbol(currencyToShow), + tint = MaterialTheme.colors.onSurface.copy(alpha = 0.6f) + ) + }, + trailingIcon = { + IconButton( + enabled = canEdit, + onClick = { + val mainLabel = viewModel.mainCurrencyName.ifEmpty { "$" } + val options = listOf("$mainLabel | Base") + viewModel.currencies.map { + "${it.name} | 1 $mainLabel = ${it.exchangeRate} ${it.name}" + } + viewModel.showDialog( + title = currencyDialogTitle, + items = options, + onItemSelected = { index -> + if (index == 0) { + viewModel.resetCurrency() + } else { + viewModel.convertCurrency(viewModel.currencies[index - 1]) + } + } + ) + } + ) { + Icon(Icons.Default.SwapHoriz, contentDescription = "Change Currency") + } + }, + keyboardOptions = KeyboardOptions( + keyboardType = KeyboardType.Decimal, + imeAction = if (whatFirst) ImeAction.Done else ImeAction.Next + ), + keyboardActions = KeyboardActions( + onNext = { focusManager.moveFocus(FocusDirection.Next) }, + onDone = { focusManager.clearFocus() } + ) ) - ) } val whatField: @Composable () -> Unit = { - OutlinedTextField( - value = viewModel.what, - onValueChange = { viewModel.updateWhat(it) }, - enabled = canEdit, - placeholder = { Text(stringResource(R.string.label_what)) }, - modifier = Modifier.fillMaxWidth().focusRequester(whatFocusRequester), - leadingIcon = { Icon(Icons.Default.Title, contentDescription = null) }, - keyboardOptions = KeyboardOptions(imeAction = if (whatFirst) ImeAction.Next else ImeAction.Done), - keyboardActions = KeyboardActions( - onNext = { focusManager.moveFocus(FocusDirection.Next) }, - onDone = { focusManager.clearFocus() } + OutlinedTextField( + value = viewModel.what, + onValueChange = { viewModel.updateWhat(it) }, + enabled = canEdit, + placeholder = { Text(stringResource(R.string.label_what)) }, + modifier = Modifier.fillMaxWidth().focusRequester(whatFocusRequester), + leadingIcon = { Icon(Icons.Default.Title, contentDescription = null) }, + keyboardOptions = KeyboardOptions(imeAction = if (whatFirst) ImeAction.Next else ImeAction.Done), + keyboardActions = KeyboardActions( + onNext = { focusManager.moveFocus(FocusDirection.Next) }, + onDone = { focusManager.clearFocus() } + ) ) - ) } if (whatFirst) { @@ -619,14 +619,13 @@ fun OwerSelectionSection( ) Spacer(Modifier.weight(1f)) - if (viewModel.splitMode != SplitMode.EVEN) { + if (viewModel.splitMode != SplitMode.EVEN && viewModel.splitMode != SplitMode.FRACTION) { val diff = viewModel.getDiffSplit() if (abs(diff) > 0.01) { - val unit = if (viewModel.splitMode == SplitMode.PERCENT) "%" else "" val diffText = - if (diff > 0) "Missing: ${SupportUtil.normalNumberFormat.format(diff)}$unit" else "Excess: ${ + if (diff > 0) "Missing: ${SupportUtil.normalNumberFormat.format(diff)}" else "Excess: ${ SupportUtil.normalNumberFormat.format(-diff) - }$unit" + }" Text( diffText, color = MaterialTheme.colors.error, @@ -636,7 +635,7 @@ fun OwerSelectionSection( } } - val splitOptions = listOf(SplitMode.EVEN, SplitMode.CUSTOM, SplitMode.PERCENT) + val splitOptions = listOf(SplitMode.EVEN, SplitMode.CUSTOM, SplitMode.FRACTION) Row( modifier = Modifier .padding(start = 8.dp) @@ -658,7 +657,7 @@ fun OwerSelectionSection( text = when (mode) { SplitMode.EVEN -> "=" SplitMode.CUSTOM -> "#" - SplitMode.PERCENT -> "%" + SplitMode.FRACTION -> "รท" }, color = if (isSelected) MaterialTheme.colors.onPrimary else MaterialTheme.colors.onSurface, fontSize = 14.sp, @@ -698,18 +697,23 @@ fun OwerSelectionSection( if (isSelected || viewModel.splitMode != SplitMode.EVEN) { val focusManager = LocalFocusManager.current val interactionSource = remember { MutableInteractionSource() } - val value = if (viewModel.splitMode == SplitMode.PERCENT) { - viewModel.owersPercentSplit[member.id] ?: "" + val value = if (viewModel.splitMode == SplitMode.FRACTION) { + viewModel.owersFractionSplit[member.id] ?: "" } else { viewModel.owersCustomSplit[member.id] ?: "" } + val fractionTotal = viewModel.getFractionTotal() BasicTextField( value = value, onValueChange = { nv -> - val filteredValue = nv.filter { it in "0123456789,.+-*/" } - if (viewModel.splitMode == SplitMode.PERCENT) { - viewModel.owersPercentSplit[member.id] = filteredValue + val filteredValue = if (viewModel.splitMode == SplitMode.FRACTION) { + nv.filter { it.isDigit() } + } else { + nv.filter { it in "0123456789,.+-*/" } + } + if (viewModel.splitMode == SplitMode.FRACTION) { + viewModel.owersFractionSplit[member.id] = filteredValue } else { viewModel.owersCustomSplit[member.id] = filteredValue } @@ -717,7 +721,7 @@ fun OwerSelectionSection( }, modifier = Modifier .width(80.dp) - .height(46.dp), + .height(40.dp), interactionSource = interactionSource, singleLine = true, enabled = viewModel.splitMode != SplitMode.EVEN && canEdit, @@ -737,12 +741,16 @@ fun OwerSelectionSection( enabled = viewModel.splitMode != SplitMode.EVEN && canEdit, interactionSource = interactionSource, contentPadding = PaddingValues(horizontal = 8.dp, vertical = 0.dp), - colors = TextFieldDefaults.textFieldColors( + colors = TextFieldDefaults.outlinedTextFieldColors( cursorColor = MaterialTheme.colors.onSurface, - backgroundColor = Color.Transparent, - unfocusedIndicatorColor = Color.Transparent, - disabledIndicatorColor = Color.Transparent - ) + backgroundColor = MaterialTheme.colors.onSurface.copy(alpha = 0.02f), + unfocusedBorderColor = MaterialTheme.colors.onSurface.copy(alpha = 0.2f), + focusedBorderColor = MaterialTheme.colors.primary.copy(alpha = 0.7f), + disabledBorderColor = MaterialTheme.colors.onSurface.copy(alpha = 0.14f) + ), + trailingIcon = if (viewModel.splitMode == SplitMode.FRACTION && isSelected && fractionTotal > 0) { + { Text("/$fractionTotal", color = MaterialTheme.colors.onSurface.copy(alpha = 0.65f)) } + } else null ) } } @@ -1011,24 +1019,24 @@ fun EditBillScreenCustomPreview() { } @SuppressLint("ViewModelConstructorInComposable") -@Preview(showBackground = true, name = "Percent Split (%)") +@Preview(showBackground = true, name = "Fraction Split") @Composable -fun EditBillScreenPercentPreview() { +fun EditBillScreenFractionPreview() { MaterialTheme { EditBillScreen( viewModel = EditBillViewModel().apply { - what = "Percent Split Pizza" + what = "Fraction Split Pizza" amount = "100.00" mainCurrencyName = "EUR" members = listOf( DBMember(1, 0, 0, "Alice", true, 1.0, 0, null, null, null, null, null), DBMember(2, 0, 0, "Bob", true, 1.0, 0, null, null, null, null, null) ) - splitMode = SplitMode.PERCENT + splitMode = SplitMode.FRACTION owersSelection[1] = true owersSelection[2] = true - owersPercentSplit[1] = "70" - owersPercentSplit[2] = "30" + owersFractionSplit[1] = "1" + owersFractionSplit[2] = "3" }, categories = emptyList(), paymentModes = emptyList(), diff --git a/app/src/main/java/net/helcel/cowspent/android/bill_edit/EditBillViewModel.kt b/app/src/main/java/net/helcel/cowspent/android/bill_edit/EditBillViewModel.kt index 7b292f5..f7e5cc9 100644 --- a/app/src/main/java/net/helcel/cowspent/android/bill_edit/EditBillViewModel.kt +++ b/app/src/main/java/net/helcel/cowspent/android/bill_edit/EditBillViewModel.kt @@ -18,7 +18,7 @@ import net.helcel.cowspent.util.SupportUtil import net.helcel.cowspent.util.evalMath enum class SplitMode { - EVEN, CUSTOM, PERCENT + EVEN, CUSTOM, FRACTION } class EditBillViewModel : ViewModel() { @@ -55,7 +55,7 @@ class EditBillViewModel : ViewModel() { var owersSelection = mutableStateMapOf() var splitMode by mutableStateOf(SplitMode.EVEN) var owersCustomSplit = mutableStateMapOf() - var owersPercentSplit = mutableStateMapOf() + var owersFractionSplit = mutableStateMapOf() var dialogState by mutableStateOf(null) @@ -93,18 +93,16 @@ class EditBillViewModel : ViewModel() { owersCustomSplit.remove(m.id) } } - } else if (splitMode == SplitMode.PERCENT) { + } else if (splitMode == SplitMode.FRACTION) { val selectedCount = owersSelection.count { it.value } if (selectedCount > 0) { - val evenPercent = 100.0 / selectedCount - val evenPercentStr = SupportUtil.round2(evenPercent).toString() members.forEach { m -> if (owersSelection[m.id] == true) { - if (owersPercentSplit[m.id].isNullOrEmpty()) { - owersPercentSplit[m.id] = evenPercentStr + if (owersFractionSplit[m.id].isNullOrEmpty()) { + owersFractionSplit[m.id] = "1" } } else { - owersPercentSplit.remove(m.id) + owersFractionSplit.remove(m.id) } } } @@ -117,12 +115,12 @@ class EditBillViewModel : ViewModel() { if (selected) { if (splitMode == SplitMode.CUSTOM && owersCustomSplit[id].isNullOrEmpty()) { owersCustomSplit[id] = "0" - } else if (splitMode == SplitMode.PERCENT && owersPercentSplit[id].isNullOrEmpty()) { - owersPercentSplit[id] = "0" + } else if (splitMode == SplitMode.FRACTION && owersFractionSplit[id].isNullOrEmpty()) { + owersFractionSplit[id] = "1" } } else { owersCustomSplit.remove(id) - owersPercentSplit.remove(id) + owersFractionSplit.remove(id) } } else { updateSplits() @@ -130,11 +128,8 @@ class EditBillViewModel : ViewModel() { } fun getDiffSplit(): Double { - return if (splitMode == SplitMode.PERCENT) { - val customTotal = owersPercentSplit.entries - .filter { owersSelection[it.key] == true } - .sumOf { parseAmountFromUi(it.value) } - SupportUtil.round2(100.0 - customTotal) + return if (splitMode == SplitMode.FRACTION) { + 0.0 } else { val customTotal = owersCustomSplit.entries .filter { owersSelection[it.key] == true } @@ -143,6 +138,10 @@ class EditBillViewModel : ViewModel() { } } + fun getFractionTotal(): Int = owersFractionSplit.entries + .filter { owersSelection[it.key] == true } + .sumOf { it.value.toIntOrNull()?.coerceAtLeast(0) ?: 0 } + fun getOwersIds(): List { return owersSelection.filter { it.value }.keys.toList() } @@ -258,7 +257,7 @@ class EditBillViewModel : ViewModel() { owersSelection.clear() owersCustomSplit.clear() - owersPercentSplit.clear() + owersFractionSplit.clear() if (customSplits != null) { splitMode = SplitMode.CUSTOM