keypad, modify

This commit is contained in:
moon 2026-05-21 11:25:32 +06:30
parent 0f435192ee
commit 72ecfbe4e7
2 changed files with 181 additions and 134 deletions

View File

@ -6,6 +6,7 @@ import androidx.compose.foundation.layout.*
import androidx.compose.foundation.layout.Arrangement import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.shape.RoundedCornerShape import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material.icons.Icons import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.rounded.Backspace
import androidx.compose.material.icons.rounded.KeyboardArrowLeft import androidx.compose.material.icons.rounded.KeyboardArrowLeft
import androidx.compose.material3.* import androidx.compose.material3.*
import androidx.compose.runtime.* import androidx.compose.runtime.*
@ -28,13 +29,17 @@ fun AmountScreen(
val displayAmount = amount.ifEmpty { "0" } val displayAmount = amount.ifEmpty { "0" }
Column( Box(
modifier = Modifier modifier = Modifier
.fillMaxSize() .fillMaxSize()
.background(Color.IvoryBeige) .background(Color.IvoryBeige)
.padding(horizontal = 20.dp)
.navigationBarsPadding() .navigationBarsPadding()
.statusBarsPadding() .statusBarsPadding()
) {
Column(
modifier = Modifier
.fillMaxSize()
.padding(horizontal = 20.dp)
) { ) {
Box( Box(
modifier = Modifier modifier = Modifier
@ -105,12 +110,7 @@ fun AmountScreen(
AmountKeypad( AmountKeypad(
onNumberClick = { value -> onNumberClick = { value ->
if (amount.length < 9) { amount = appendAmountValue(amount, value)
amount += value
}
},
onDeleteClick = {
amount = amount.dropLast(1)
} }
) )
@ -162,18 +162,37 @@ fun AmountScreen(
Spacer(modifier = Modifier.height(18.dp)) Spacer(modifier = Modifier.height(18.dp))
} }
Card(
modifier = Modifier
.align(Alignment.TopEnd)//?
.padding(top = 110.dp, end = 20.dp)
.clickable(enabled = amount.isNotEmpty()) {
amount = amount.dropLast(1)
},
shape = RoundedCornerShape(18.dp),
colors = CardDefaults.cardColors(containerColor = Color.White),
elevation = CardDefaults.cardElevation(defaultElevation = 6.dp)
) {
Icon(
imageVector = Icons.Rounded.Backspace,
contentDescription = "Delete",
tint = if (amount.isNotEmpty()) Color.LegacyRed else Color.Gray,
modifier = Modifier.padding(horizontal = 14.dp, vertical = 12.dp)
)
}
}
} }
@Composable @Composable
private fun AmountKeypad( private fun AmountKeypad(
onNumberClick: (String) -> Unit, onNumberClick: (String) -> Unit
onDeleteClick: () -> Unit
) { ) {
val keys = listOf( val keys = listOf(
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", "00") listOf(".", "0", "00")
) )
Column( Column(
@ -186,13 +205,6 @@ private fun AmountKeypad(
horizontalArrangement = Arrangement.spacedBy(6.dp) horizontalArrangement = Arrangement.spacedBy(6.dp)
) { ) {
row.forEach { key -> row.forEach { key ->
if (key.isEmpty()) {
Box(
modifier = Modifier
.weight(1f)
.height(66.dp)
)
} else {
KeypadButton( KeypadButton(
text = key, text = key,
modifier = Modifier.weight(1f), modifier = Modifier.weight(1f),
@ -202,7 +214,6 @@ private fun AmountKeypad(
} }
} }
} }
}
} }
@Composable @Composable
@ -237,8 +248,46 @@ private fun KeypadButton(
} }
private fun formatAmount(value: String): String { private fun formatAmount(value: String): String {
val number = value.toLongOrNull() ?: 0L val normalized = value.ifEmpty { "0" }
return "%,d".format(number) val wholePart = normalized.substringBefore(".").ifEmpty { "0" }
val groupedWholePart = "%,d".format(wholePart.toLongOrNull() ?: 0L)
if (!normalized.contains(".")) {
return groupedWholePart
}
val decimalPart = normalized.substringAfter(".", "")
return if (normalized.endsWith(".")) {
"$groupedWholePart."
} else {
"$groupedWholePart.$decimalPart"
}
}
private fun appendAmountValue(current: String, value: String): String {
if (value == ".") {
if (current.contains(".")) return current
return if (current.isEmpty()) "0." else "$current."
}
val decimalIndex = current.indexOf('.')
return if (decimalIndex >= 0) {
val decimalPart = current.substring(decimalIndex + 1)
val remainingDecimalDigits = 2 - decimalPart.length
if (remainingDecimalDigits <= 0) {
current
} else {
current + value.take(remainingDecimalDigits)
}
} else {
val wholeDigitsCount = current.filter(Char::isDigit).length
val remainingWholeDigits = 9 - wholeDigitsCount
if (remainingWholeDigits <= 0) {
current
} else {
current + value.take(remainingWholeDigits)
}
}
} }
@P2Preview @P2Preview

View File

@ -61,10 +61,8 @@ fun DashboardScreen2(
private fun AdvertisingArea() { private fun AdvertisingArea() {
Card( Card(
modifier = Modifier.fillMaxWidth(), modifier = Modifier.fillMaxWidth(),
shape = RoundedCornerShape(0.dp), shape = RoundedCornerShape(0.dp),
colors = CardDefaults.cardColors(containerColor = Color.White), colors = CardDefaults.cardColors(containerColor = Color.White),
elevation = CardDefaults.cardElevation(8.dp)
) { ) {
Row( Row(
modifier = Modifier.height(170.dp) ) { modifier = Modifier.height(170.dp) ) {