amount decimal
This commit is contained in:
parent
d9134e84f4
commit
750dc8dc25
@ -2,7 +2,6 @@ package com.mob.ustmm.ui.amount
|
||||
|
||||
import androidx.compose.foundation.background
|
||||
import androidx.compose.foundation.layout.*
|
||||
import androidx.compose.foundation.layout.Arrangement
|
||||
import androidx.compose.foundation.rememberScrollState
|
||||
import androidx.compose.foundation.shape.RoundedCornerShape
|
||||
import androidx.compose.foundation.verticalScroll
|
||||
@ -18,7 +17,6 @@ import androidx.compose.material3.TopAppBarDefaults
|
||||
import androidx.compose.runtime.*
|
||||
import androidx.compose.ui.Alignment
|
||||
import androidx.compose.ui.Modifier
|
||||
import androidx.compose.ui.platform.LocalConfiguration
|
||||
import androidx.compose.ui.res.painterResource
|
||||
import androidx.compose.ui.text.font.FontWeight
|
||||
import androidx.compose.ui.tooling.preview.Preview
|
||||
@ -39,10 +37,9 @@ fun AmountScreen(
|
||||
onNextClick: (String) -> Unit = {}
|
||||
) {
|
||||
|
||||
var amount by remember { mutableStateOf("0") }
|
||||
|
||||
val config = LocalConfiguration.current
|
||||
val isSmallScreen = config.screenHeightDp < 700
|
||||
var amount by remember {
|
||||
mutableStateOf("0")
|
||||
}
|
||||
|
||||
Scaffold(
|
||||
topBar = {
|
||||
@ -79,67 +76,28 @@ fun AmountScreen(
|
||||
.padding(paddingValues)
|
||||
.background(White)
|
||||
.verticalScroll(rememberScrollState())
|
||||
.padding(horizontal = if (isSmallScreen) 20.dp else 24.dp),
|
||||
horizontalAlignment = Alignment.CenterHorizontally,
|
||||
verticalArrangement = Arrangement.SpaceBetween
|
||||
.padding(horizontal = 24.dp),
|
||||
horizontalAlignment = Alignment.CenterHorizontally
|
||||
) {
|
||||
|
||||
Spacer(
|
||||
modifier = Modifier.height(
|
||||
16.dp
|
||||
)
|
||||
)
|
||||
Spacer(modifier = Modifier.height(16.dp))
|
||||
|
||||
AmountBox(
|
||||
amount = amount,
|
||||
height = if (isSmallScreen) 115.dp else 142.dp
|
||||
amount = formatTypedAmount(amount),
|
||||
height = 134.dp
|
||||
)
|
||||
|
||||
Spacer(
|
||||
modifier = Modifier.height(
|
||||
100.dp
|
||||
)
|
||||
)
|
||||
Spacer(modifier = Modifier.height(24.dp))
|
||||
|
||||
Keypad(
|
||||
buttonHeight = if (isSmallScreen) 60.dp else 74.dp,
|
||||
buttonSpacing = if (isSmallScreen) 8.dp else 12.dp,
|
||||
buttonHeight = 74.dp,
|
||||
buttonSpacing = 12.dp,
|
||||
onKeyClick = { key ->
|
||||
|
||||
amount = when (key) {
|
||||
|
||||
"del" -> {
|
||||
if (amount.length <= 1) {
|
||||
"0"
|
||||
} else {
|
||||
amount.dropLast(1)
|
||||
}
|
||||
}
|
||||
|
||||
"." -> {
|
||||
if (amount.contains(".")) {
|
||||
amount
|
||||
} else {
|
||||
"$amount."
|
||||
}
|
||||
}
|
||||
|
||||
else -> {
|
||||
if (amount == "0") {
|
||||
key
|
||||
} else {
|
||||
amount + key
|
||||
}
|
||||
}
|
||||
}
|
||||
amount = handleAmountInput(amount, key)
|
||||
}
|
||||
)
|
||||
|
||||
Spacer(
|
||||
modifier = Modifier.height(
|
||||
if (isSmallScreen) 14.dp else 18.dp
|
||||
)
|
||||
)
|
||||
Spacer(modifier = Modifier.height(18.dp))
|
||||
|
||||
Row(
|
||||
modifier = Modifier.fillMaxWidth(),
|
||||
@ -150,7 +108,7 @@ fun AmountScreen(
|
||||
onClick = onCancelClick,
|
||||
modifier = Modifier
|
||||
.weight(1f)
|
||||
.height(if (isSmallScreen) 58.dp else 68.dp),
|
||||
.height(68.dp),
|
||||
shape = RoundedCornerShape(18.dp),
|
||||
colors = ButtonDefaults.buttonColors(
|
||||
containerColor = White,
|
||||
@ -160,18 +118,18 @@ fun AmountScreen(
|
||||
) {
|
||||
Text(
|
||||
text = "Cancel",
|
||||
fontSize = if (isSmallScreen) 18.sp else 20.sp,
|
||||
fontSize = 20.sp,
|
||||
fontWeight = FontWeight.Bold
|
||||
)
|
||||
}
|
||||
|
||||
Button(
|
||||
onClick = {
|
||||
onNextClick(amount)
|
||||
onNextClick(normalizeAmount(amount))
|
||||
},
|
||||
modifier = Modifier
|
||||
.weight(1f)
|
||||
.height(if (isSmallScreen) 58.dp else 68.dp),
|
||||
.height(68.dp),
|
||||
shape = RoundedCornerShape(18.dp),
|
||||
colors = ButtonDefaults.buttonColors(
|
||||
containerColor = Primary,
|
||||
@ -180,7 +138,7 @@ fun AmountScreen(
|
||||
) {
|
||||
Text(
|
||||
text = "Next",
|
||||
fontSize = if (isSmallScreen) 18.sp else 20.sp,
|
||||
fontSize = 20.sp,
|
||||
fontWeight = FontWeight.Bold
|
||||
)
|
||||
}
|
||||
@ -216,7 +174,7 @@ private fun AmountBox(
|
||||
Text(
|
||||
text = amount,
|
||||
color = White,
|
||||
fontSize = 26.sp,
|
||||
fontSize = 30.sp,
|
||||
fontWeight = FontWeight.Bold
|
||||
)
|
||||
|
||||
@ -256,7 +214,7 @@ private fun Keypad(
|
||||
listOf("1", "2", "3"),
|
||||
listOf("4", "5", "6"),
|
||||
listOf("7", "8", "9"),
|
||||
listOf(".", "0", "del")
|
||||
listOf(".", "0", "DEL")
|
||||
)
|
||||
|
||||
Column(
|
||||
@ -287,9 +245,19 @@ private fun Keypad(
|
||||
contentPadding = PaddingValues(0.dp)
|
||||
) {
|
||||
|
||||
if (key == "DEL") {
|
||||
|
||||
Text(
|
||||
text = "⌫",
|
||||
fontSize = 26.sp,
|
||||
fontWeight = FontWeight.Bold
|
||||
)
|
||||
|
||||
} else {
|
||||
|
||||
Text(
|
||||
text = key,
|
||||
fontSize = if (buttonHeight < 70.dp) 20.sp else 22.sp,
|
||||
fontSize = 22.sp,
|
||||
fontWeight = FontWeight.Bold
|
||||
)
|
||||
}
|
||||
@ -297,9 +265,90 @@ private fun Keypad(
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Preview(showBackground = true, showSystemUi = false)
|
||||
private fun handleAmountInput(
|
||||
current: String,
|
||||
key: String
|
||||
): String {
|
||||
|
||||
return when (key.uppercase()) {
|
||||
|
||||
"DEL" -> {
|
||||
current
|
||||
.dropLast(1)
|
||||
.ifEmpty { "0" }
|
||||
}
|
||||
|
||||
"." -> {
|
||||
if (current.contains(".")) {
|
||||
current
|
||||
} else {
|
||||
"${current.ifEmpty { "0" }}."
|
||||
}
|
||||
}
|
||||
|
||||
in "0".."9" -> {
|
||||
val sanitizedCurrent = if (current == "0") "" else current
|
||||
|
||||
if (sanitizedCurrent.contains(".")) {
|
||||
val decimal = sanitizedCurrent.substringAfter(".")
|
||||
|
||||
if (decimal.length >= 2) {
|
||||
current
|
||||
} else {
|
||||
sanitizedCurrent + key
|
||||
}
|
||||
} else {
|
||||
val nextValue = sanitizedCurrent + key
|
||||
nextValue.trimStart('0').ifEmpty { "0" }
|
||||
}
|
||||
}
|
||||
|
||||
else -> current
|
||||
}
|
||||
}
|
||||
|
||||
private fun normalizeAmount(value: String): String {
|
||||
if (value.isBlank()) return "0.00"
|
||||
|
||||
val normalized = if (value.endsWith(".")) {
|
||||
value.dropLast(1)
|
||||
} else {
|
||||
value
|
||||
}
|
||||
|
||||
return when {
|
||||
normalized.isBlank() -> "0.00"
|
||||
normalized.contains(".") -> {
|
||||
val whole = normalized.substringBefore(".").ifEmpty { "0" }
|
||||
val decimal = normalized.substringAfter(".")
|
||||
|
||||
when (decimal.length) {
|
||||
0 -> "$whole.00"
|
||||
1 -> "$whole.${decimal}0"
|
||||
else -> "$whole.${decimal.take(2)}"
|
||||
}
|
||||
}
|
||||
else -> "${normalized}.00"
|
||||
}
|
||||
}
|
||||
|
||||
private fun formatTypedAmount(value: String): String {
|
||||
if (!value.contains(".")) return "$value.00"
|
||||
|
||||
val whole = value.substringBefore(".").ifEmpty { "0" }
|
||||
val decimal = value.substringAfter(".")
|
||||
|
||||
return when (decimal.length) {
|
||||
0 -> "$whole."
|
||||
1 -> "$whole.${decimal}0"
|
||||
else -> "$whole.${decimal.take(2)}"
|
||||
}
|
||||
}
|
||||
|
||||
@Preview(showBackground = true)
|
||||
@Composable
|
||||
private fun AmountScreenPreview() {
|
||||
|
||||
|
||||
Loading…
Reference in New Issue
Block a user