Skip to content

Commit

Permalink
🐛 Fix input bug with german locale
Browse files Browse the repository at this point in the history
  • Loading branch information
Importantus committed Dec 7, 2024
1 parent 6321313 commit 2a85094
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 24 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package digital.fischers.coinsaw.ui.bill

import android.icu.text.DecimalFormat
import androidx.compose.animation.core.animateFloatAsState
import androidx.compose.foundation.background
import androidx.compose.foundation.border
Expand Down Expand Up @@ -342,7 +343,7 @@ fun SplittingElement(
horizontalArrangement = Arrangement.SpaceBetween
) {
BasicTextField(
value = percentToString(splitting.percentage, if(textBoxFocused.value) "" else "0.00"),
value = percentToString(splitting.percentage, if(textBoxFocused.value) "" else String.format(Locale.getDefault(), "%.2f", 0.00)),
singleLine = true,
cursorBrush = SolidColor(MaterialTheme.colorScheme.onSurface),
keyboardOptions = KeyboardOptions.Default.copy(keyboardType = KeyboardType.Number),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,8 @@ class AddBillViewModel @Inject constructor(
edited = false
)
}

percentRemaining = 0.0
}

fun resetSplittings() {
Expand Down Expand Up @@ -145,18 +147,13 @@ class AddBillViewModel @Inject constructor(
val editedSplittings = newSplittings.filter { it.edited }
val editedSum = editedSplittings.sumOf { it.percentage }

var remainingSplittings = newSplittings.filter { !it.edited && it.percentage >= 0.0 && it.percentage <= 100.0 }
val remainingSplittings = newSplittings.filter { !it.edited && it.percentage >= 0.0 && it.percentage <= 100.0 }
val remainingSum = 100.0 - editedSum

/**
* TODO: Don't let the percents go below 0 or above 100
* The remaining sum has to be distributed to the remaining, non-edited splittings
* If all of these splittings are 0 or 100, the remaining sum has to be distributed to all of them
*/

if(remainingSplittings.isNotEmpty()) {
val remainingPerUser = remainingSum / remainingSplittings.size
val remainingPerUserRounded = DecimalFormat("#.##").format(remainingPerUser).toDouble()
// Round to 2 decimal places
val remainingPerUserRounded = remainingPerUser.roundHalfUp(2)

newSplittings.forEachIndexed { i, splitting ->
if (!splitting.edited) {
Expand All @@ -165,21 +162,6 @@ class AddBillViewModel @Inject constructor(
}
}

// remainingSplittings = newSplittings.filter { !it.edited && it.percentage >= 0.0 && it.percentage <= 100.0 }
//
// if(remainingSplittings.isEmpty() && remainingSum > 0.0) {
// // Normalize edited splittings
// val remainingSumRounded = DecimalFormat("#.##").format(remainingSum).toDouble()
//
// Log.d("AddBillViewModel", "onSplittingChanged: $remainingSumRounded, ${editedSplittings.size}")
//
// newSplittings.forEachIndexed { i, splitting ->
// if (splitting.edited && splitting.userId != userId) {
// newSplittings[i] = splitting.copy(percentage = (newSplittings[i].percentage + (remainingSumRounded / (editedSplittings.size - 1))))
// }
// }
// }

splittings.value = newSplittings

val percentageSum = splittings.value.sumOf { it.percentage }
Expand Down

0 comments on commit 2a85094

Please sign in to comment.