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.background
|
||||||
import androidx.compose.foundation.layout.*
|
import androidx.compose.foundation.layout.*
|
||||||
import androidx.compose.foundation.layout.Arrangement
|
|
||||||
import androidx.compose.foundation.rememberScrollState
|
import androidx.compose.foundation.rememberScrollState
|
||||||
import androidx.compose.foundation.shape.RoundedCornerShape
|
import androidx.compose.foundation.shape.RoundedCornerShape
|
||||||
import androidx.compose.foundation.verticalScroll
|
import androidx.compose.foundation.verticalScroll
|
||||||
@ -18,7 +17,6 @@ import androidx.compose.material3.TopAppBarDefaults
|
|||||||
import androidx.compose.runtime.*
|
import androidx.compose.runtime.*
|
||||||
import androidx.compose.ui.Alignment
|
import androidx.compose.ui.Alignment
|
||||||
import androidx.compose.ui.Modifier
|
import androidx.compose.ui.Modifier
|
||||||
import androidx.compose.ui.platform.LocalConfiguration
|
|
||||||
import androidx.compose.ui.res.painterResource
|
import androidx.compose.ui.res.painterResource
|
||||||
import androidx.compose.ui.text.font.FontWeight
|
import androidx.compose.ui.text.font.FontWeight
|
||||||
import androidx.compose.ui.tooling.preview.Preview
|
import androidx.compose.ui.tooling.preview.Preview
|
||||||
@ -39,10 +37,9 @@ fun AmountScreen(
|
|||||||
onNextClick: (String) -> Unit = {}
|
onNextClick: (String) -> Unit = {}
|
||||||
) {
|
) {
|
||||||
|
|
||||||
var amount by remember { mutableStateOf("0") }
|
var amount by remember {
|
||||||
|
mutableStateOf("0")
|
||||||
val config = LocalConfiguration.current
|
}
|
||||||
val isSmallScreen = config.screenHeightDp < 700
|
|
||||||
|
|
||||||
Scaffold(
|
Scaffold(
|
||||||
topBar = {
|
topBar = {
|
||||||
@ -79,67 +76,28 @@ fun AmountScreen(
|
|||||||
.padding(paddingValues)
|
.padding(paddingValues)
|
||||||
.background(White)
|
.background(White)
|
||||||
.verticalScroll(rememberScrollState())
|
.verticalScroll(rememberScrollState())
|
||||||
.padding(horizontal = if (isSmallScreen) 20.dp else 24.dp),
|
.padding(horizontal = 24.dp),
|
||||||
horizontalAlignment = Alignment.CenterHorizontally,
|
horizontalAlignment = Alignment.CenterHorizontally
|
||||||
verticalArrangement = Arrangement.SpaceBetween
|
|
||||||
) {
|
) {
|
||||||
|
|
||||||
Spacer(
|
Spacer(modifier = Modifier.height(16.dp))
|
||||||
modifier = Modifier.height(
|
|
||||||
16.dp
|
|
||||||
)
|
|
||||||
)
|
|
||||||
|
|
||||||
AmountBox(
|
AmountBox(
|
||||||
amount = amount,
|
amount = formatTypedAmount(amount),
|
||||||
height = if (isSmallScreen) 115.dp else 142.dp
|
height = 134.dp
|
||||||
)
|
)
|
||||||
|
|
||||||
Spacer(
|
Spacer(modifier = Modifier.height(24.dp))
|
||||||
modifier = Modifier.height(
|
|
||||||
100.dp
|
|
||||||
)
|
|
||||||
)
|
|
||||||
|
|
||||||
Keypad(
|
Keypad(
|
||||||
buttonHeight = if (isSmallScreen) 60.dp else 74.dp,
|
buttonHeight = 74.dp,
|
||||||
buttonSpacing = if (isSmallScreen) 8.dp else 12.dp,
|
buttonSpacing = 12.dp,
|
||||||
onKeyClick = { key ->
|
onKeyClick = { key ->
|
||||||
|
amount = handleAmountInput(amount, 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
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
Spacer(
|
Spacer(modifier = Modifier.height(18.dp))
|
||||||
modifier = Modifier.height(
|
|
||||||
if (isSmallScreen) 14.dp else 18.dp
|
|
||||||
)
|
|
||||||
)
|
|
||||||
|
|
||||||
Row(
|
Row(
|
||||||
modifier = Modifier.fillMaxWidth(),
|
modifier = Modifier.fillMaxWidth(),
|
||||||
@ -150,7 +108,7 @@ fun AmountScreen(
|
|||||||
onClick = onCancelClick,
|
onClick = onCancelClick,
|
||||||
modifier = Modifier
|
modifier = Modifier
|
||||||
.weight(1f)
|
.weight(1f)
|
||||||
.height(if (isSmallScreen) 58.dp else 68.dp),
|
.height(68.dp),
|
||||||
shape = RoundedCornerShape(18.dp),
|
shape = RoundedCornerShape(18.dp),
|
||||||
colors = ButtonDefaults.buttonColors(
|
colors = ButtonDefaults.buttonColors(
|
||||||
containerColor = White,
|
containerColor = White,
|
||||||
@ -160,18 +118,18 @@ fun AmountScreen(
|
|||||||
) {
|
) {
|
||||||
Text(
|
Text(
|
||||||
text = "Cancel",
|
text = "Cancel",
|
||||||
fontSize = if (isSmallScreen) 18.sp else 20.sp,
|
fontSize = 20.sp,
|
||||||
fontWeight = FontWeight.Bold
|
fontWeight = FontWeight.Bold
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
Button(
|
Button(
|
||||||
onClick = {
|
onClick = {
|
||||||
onNextClick(amount)
|
onNextClick(normalizeAmount(amount))
|
||||||
},
|
},
|
||||||
modifier = Modifier
|
modifier = Modifier
|
||||||
.weight(1f)
|
.weight(1f)
|
||||||
.height(if (isSmallScreen) 58.dp else 68.dp),
|
.height(68.dp),
|
||||||
shape = RoundedCornerShape(18.dp),
|
shape = RoundedCornerShape(18.dp),
|
||||||
colors = ButtonDefaults.buttonColors(
|
colors = ButtonDefaults.buttonColors(
|
||||||
containerColor = Primary,
|
containerColor = Primary,
|
||||||
@ -180,7 +138,7 @@ fun AmountScreen(
|
|||||||
) {
|
) {
|
||||||
Text(
|
Text(
|
||||||
text = "Next",
|
text = "Next",
|
||||||
fontSize = if (isSmallScreen) 18.sp else 20.sp,
|
fontSize = 20.sp,
|
||||||
fontWeight = FontWeight.Bold
|
fontWeight = FontWeight.Bold
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
@ -216,7 +174,7 @@ private fun AmountBox(
|
|||||||
Text(
|
Text(
|
||||||
text = amount,
|
text = amount,
|
||||||
color = White,
|
color = White,
|
||||||
fontSize = 26.sp,
|
fontSize = 30.sp,
|
||||||
fontWeight = FontWeight.Bold
|
fontWeight = FontWeight.Bold
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -256,7 +214,7 @@ private fun Keypad(
|
|||||||
listOf("1", "2", "3"),
|
listOf("1", "2", "3"),
|
||||||
listOf("4", "5", "6"),
|
listOf("4", "5", "6"),
|
||||||
listOf("7", "8", "9"),
|
listOf("7", "8", "9"),
|
||||||
listOf(".", "0", "del")
|
listOf(".", "0", "DEL")
|
||||||
)
|
)
|
||||||
|
|
||||||
Column(
|
Column(
|
||||||
@ -287,11 +245,22 @@ private fun Keypad(
|
|||||||
contentPadding = PaddingValues(0.dp)
|
contentPadding = PaddingValues(0.dp)
|
||||||
) {
|
) {
|
||||||
|
|
||||||
Text(
|
if (key == "DEL") {
|
||||||
text = key,
|
|
||||||
fontSize = if (buttonHeight < 70.dp) 20.sp else 22.sp,
|
Text(
|
||||||
fontWeight = FontWeight.Bold
|
text = "⌫",
|
||||||
)
|
fontSize = 26.sp,
|
||||||
|
fontWeight = FontWeight.Bold
|
||||||
|
)
|
||||||
|
|
||||||
|
} else {
|
||||||
|
|
||||||
|
Text(
|
||||||
|
text = key,
|
||||||
|
fontSize = 22.sp,
|
||||||
|
fontWeight = FontWeight.Bold
|
||||||
|
)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -299,7 +268,87 @@ 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
|
@Composable
|
||||||
private fun AmountScreenPreview() {
|
private fun AmountScreenPreview() {
|
||||||
|
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user