CardEditFields.kt

5.9 KB · raw

1package com.peachpasswords.ui.vault23import androidx.compose.foundation.border4import androidx.compose.foundation.clickable5import androidx.compose.foundation.layout.Arrangement6import androidx.compose.foundation.layout.Box7import androidx.compose.foundation.layout.Column8import androidx.compose.foundation.layout.Row9import androidx.compose.foundation.layout.Spacer10import androidx.compose.foundation.layout.fillMaxWidth11import androidx.compose.foundation.layout.height12import androidx.compose.foundation.layout.padding13import androidx.compose.foundation.layout.width14import androidx.compose.foundation.shape.RoundedCornerShape15import androidx.compose.foundation.text.BasicTextField16import androidx.compose.material3.Text17import androidx.compose.runtime.Composable18import androidx.compose.ui.Alignment19import androidx.compose.ui.Modifier20import androidx.compose.ui.draw.clip21import androidx.compose.ui.graphics.SolidColor22import androidx.compose.ui.text.TextStyle23import androidx.compose.ui.text.input.KeyboardType24import androidx.compose.ui.text.input.PasswordVisualTransformation25import androidx.compose.foundation.text.KeyboardOptions26import androidx.compose.ui.unit.dp27import androidx.compose.ui.unit.sp28import com.peachpasswords.ui.theme.LocalPeachAccent29import com.peachpasswords.ui.theme.peachColors30import com.peachpasswords.ui.theme.PeachType3132/**33 * D2 — EntryEdit CARD field block. Self-contained: drop a single call to34 * [CardEditFields] in place of the login fields when `f.type == "card"`.35 *36 * Mirrors the existing private `Field` underline style in EntryEditScreen.kt37 * (1.dp T3 border, mono 13sp, accent cursor). Includes its own label+box38 * primitive (`CardField`) so it does not depend on that file's private members.39 *40 * Network is detected live from the card number; CVC maxLength + PAN grouping41 * follow the detected network (Amex 15/4, others 16/3).42 */43@Composable44fun CardEditFields(45    cardNumber: String,46    cardholderName: String,47    expiry: String,48    cvc: String,49    showCvc: Boolean,50    network: String,51    onCardNumber: (String) -> Unit,52    onCardholder: (String) -> Unit,53    onExpiry: (String) -> Unit,54    onCvc: (String) -> Unit,55    onToggleShowCvc: () -> Unit,56) {57    // CARD NUMBER — auto-formatted, network label beside the label.58    CardField(59        label = "card number",60        value = cardNumber,61        onChange = { raw -> onCardNumber(CardFieldUtils.formatPan(raw)) },62        keyboardType = KeyboardType.Number,63        labelTrailing = {64            val net = CardFieldUtils.networkLabel(network)65            if (net.isNotEmpty()) {66                Text(net, color = peachColors.T2, fontFamily = PeachType.Mono, fontSize = 9.sp)67            }68        },69    )7071    // CARDHOLDER — Inter body, not mono.72    CardField(73        label = "cardholder",74        value = cardholderName,75        onChange = onCardholder,76        mono = false,77    )7879    // EXPIRY — auto "MM/YY".80    CardField(81        label = "expiry",82        value = expiry,83        onChange = { raw -> onExpiry(CardFieldUtils.formatExpiry(raw)) },84        keyboardType = KeyboardType.Number,85    )8687    // CVC — masked by default, eye toggle, maxLength by network.88    val cvcMax = CardFieldUtils.cvcMaxDigits(network)89    CardField(90        label = "cvc",91        value = cvc,92        onChange = { raw -> onCvc(raw.filter { it.isDigit() }.take(cvcMax)) },93        keyboardType = KeyboardType.NumberPassword,94        masked = !showCvc,95        trailing = {96            Text(97                if (showCvc) "hide" else "show",98                color = LocalPeachAccent.current, fontFamily = PeachType.Mono, fontSize = 9.sp,99                modifier = Modifier.clip(RoundedCornerShape(6.dp)).clickable(onClick = onToggleShowCvc).padding(6.dp),100            )101        },102    )103}104105/**106 * Underline-style field matching EntryEditScreen's private `Field`. Duplicated107 * here (rather than reused) so this draft is a self-contained drop-in; the108 * orchestrator may instead route these through the existing `Field`/`PasswordField`109 * — see INTEGRATION.md for the cheaper wiring option.110 */111@Composable112private fun CardField(113    label: String,114    value: String,115    onChange: (String) -> Unit,116    mono: Boolean = true,117    masked: Boolean = false,118    keyboardType: KeyboardType = KeyboardType.Text,119    labelTrailing: (@Composable () -> Unit)? = null,120    trailing: (@Composable () -> Unit)? = null,121) {122    val accent = LocalPeachAccent.current123    Column(Modifier.fillMaxWidth().padding(bottom = 14.dp)) {124        Row(verticalAlignment = Alignment.CenterVertically) {125            Text(label.uppercase(), color = peachColors.T1, fontFamily = PeachType.Mono, fontSize = 8.sp)126            if (labelTrailing != null) {127                Spacer(Modifier.width(8.dp))128                labelTrailing()129            }130        }131        Spacer(Modifier.height(6.dp))132        Row(verticalAlignment = Alignment.CenterVertically) {133            Box(134                Modifier.weight(1f)135                    .border(1.dp, peachColors.T3, RoundedCornerShape(10.dp))136                    .padding(horizontal = 12.dp, vertical = 12.dp),137            ) {138                BasicTextField(139                    value = value,140                    onValueChange = onChange,141                    singleLine = true,142                    textStyle = TextStyle(143                        color = peachColors.T0,144                        fontFamily = if (mono) PeachType.Mono else PeachType.Body,145                        fontSize = 13.sp,146                    ),147                    cursorBrush = SolidColor(accent),148                    keyboardOptions = KeyboardOptions(keyboardType = keyboardType),149                    visualTransformation = if (masked) PasswordVisualTransformation() else androidx.compose.ui.text.input.VisualTransformation.None,150                    modifier = Modifier.fillMaxWidth(),151                )152            }153            if (trailing != null) { Spacer(Modifier.width(8.dp)); trailing() }154        }155    }156}