1# D2 — Card Type Fields: Integration Guide23UI-only work. `VaultEntry` already has every card field4(`cardholderName cardNumber expMonth expYear cvc cardNickname cardNetwork`) and5`isCard()` (`type == "card"`). No model changes needed.67## Files in this draft89| Draft file | Destination | Notes |10|---|---|---|11| `CardFieldUtils.kt` | `app/src/main/java/com/peachpasswords/ui/vault/CardFieldUtils.kt` | Pure helpers (format/detect/mask/validate). No deps. |12| `CardEditFields.kt` | `app/src/main/java/com/peachpasswords/ui/vault/CardEditFields.kt` | `CardEditFields(...)` composable for EntryEdit. |13| `CardDetailFields.kt` | `app/src/main/java/com/peachpasswords/ui/vault/CardDetailFields.kt` | `CardDetailFields(...)` composable for EntryDetail. |1415All three are package `com.peachpasswords.ui.vault`, so they merge into the same16package as the target screens with no import churn.1718---1920## Part A — EntryEditScreen + EntryEditViewModel2122### A1. ViewModel state additions (`EntryEditViewModel.kt`)2324Add fields to `EditForm` (after `notes`, before `showPassword`):2526```kotlin27val cardNumber: String = "",28val cardholderName: String = "",29val expiry: String = "", // "MM/YY", display+edit form; split into expMonth/expYear on save30val cvc: String = "",31val showCvc: Boolean = false,32val cardNetwork: String = "unknown",33```3435Hydrate them in the `existing?.let { EditForm(...) }` block (the model stores36`expMonth`/`expYear` separately, so recombine):3738```kotlin39cardNumber = it.cardNumber.orEmpty().let { n -> CardFieldUtils.formatPan(n) },40cardholderName = it.cardholderName.orEmpty(),41expiry = CardFieldUtils.displayExpiry(it.expMonth, it.expYear),42cvc = it.cvc.orEmpty(),43cardNetwork = it.cardNetwork ?: CardFieldUtils.detectNetwork(it.cardNumber.orEmpty()),44```4546Add setters (place near `setNotes`):4748```kotlin49fun setCardNumber(v: String) = _form.update {50 val formatted = CardFieldUtils.formatPan(v)51 it.copy(cardNumber = formatted, cardNetwork = CardFieldUtils.detectNetwork(formatted), error = null)52}53fun setCardholderName(v: String) = _form.update { it.copy(cardholderName = v, error = null) }54fun setExpiry(v: String) = _form.update { it.copy(expiry = CardFieldUtils.formatExpiry(v), error = null) }55fun setCvc(v: String) = _form.update {56 it.copy(cvc = v.filter { c -> c.isDigit() }.take(CardFieldUtils.cvcMaxDigits(it.cardNetwork)))57}58fun toggleShowCvc() = _form.update { it.copy(showCvc = !it.showCvc) }59```6061> Note: `CardEditFields` already calls `CardFieldUtils.formatPan/formatExpiry`62> on input. If you wire setters that re-format (as above), pass *raw* setters63> into the composable instead, OR pass these and let the composable's formatting64> be idempotent (it is — formatting an already-formatted string is a no-op).65> Simplest: have the composable call `vm::setCardNumber` directly and change its66> `onCardNumber` lambda to `onCardNumber(raw)` (drop the inner `formatPan`).6768### A2. Save-time write + validation (`EntryEditViewModel.save`)6970Add the card validation at the top of `save()`, right after the `title.isBlank()`71check:7273```kotlin74if (f.type == "card" && (f.cardNumber.isBlank() || f.cardholderName.isBlank())) {75 _form.update { it.copy(error = "Cardholder name and card number are required") }76 return77}78```7980(The inline error renders via the existing `f.error?.let { ... }` block in the81breach color — no new UI needed.)8283In **both** the `addEntry(VaultEntry(...))` and `updateEntry(existing!!.copy(...))`84branches, add the card columns. Expiry "MM/YY" splits into `expMonth`/`expYear`:8586```kotlin87cardNumber = f.cardNumber.filter { it.isDigit() }.ifBlank { null },88cardholderName = f.cardholderName.ifBlank { null },89expMonth = f.expiry.filter { it.isDigit() }.take(2).ifBlank { null },90expYear = f.expiry.filter { it.isDigit() }.drop(2).take(2).ifBlank { null },91cvc = f.cvc.ifBlank { null },92cardNetwork = f.cardNetwork.takeIf { it != "unknown" },93```9495> The `addEntry` branch currently passes only login-ish fields; just append the96> six lines above into the `VaultEntry(...)` constructor call.9798### A3. Insertion anchor (`EntryEditScreen.kt`)99100The type-conditional block currently reads (lines ~100–130):101102```kotlin103 Field("title", f.title, vm::setTitle)104 if (f.type == "login") {105 Field("url", f.url, vm::setUrl)106 ...107 Field("2FA secret (otpauth:// or base32)", f.totp, vm::setTotp)108 }109 Field("notes", f.notes, vm::setNotes, singleLine = false)110```111112Insert the card branch between the closing `}` of the `if (f.type == "login")`113block and the `Field("notes", ...)` line:114115```kotlin116 }117 if (f.type == "card") {118 CardEditFields(119 cardNumber = f.cardNumber,120 cardholderName = f.cardholderName,121 expiry = f.expiry,122 cvc = f.cvc,123 showCvc = f.showCvc,124 network = f.cardNetwork,125 onCardNumber = vm::setCardNumber,126 onCardholder = vm::setCardholderName,127 onExpiry = vm::setExpiry,128 onCvc = vm::setCvc,129 onToggleShowCvc = vm::toggleShowCvc,130 )131 }132 Field("notes", f.notes, vm::setNotes, singleLine = false)133```134135The type pills already include `"card"` (line 94: `listOf("login", "note", "card")`),136so type selection works as-is.137138> Double-formatting caveat: if you keep the composable's internal `formatPan`139> AND the VM setter's `formatPan`, that is fine (idempotent). To avoid the140> redundancy, drop the inner format in `CardEditFields` (change141> `onChange = { raw -> onCardNumber(CardFieldUtils.formatPan(raw)) }` to142> `onChange = onCardNumber`) and likewise for expiry.143144---145146## Part B — EntryDetailScreen + EntryDetailViewModel147148### B1. ViewModel state additions (`EntryDetailViewModel.kt`)149150The existing `_revealed` is reused for the **password** path. Card number reveal151is non-sensitive enough to be a plain local toggle (the spec doesn't biometric-gate152it), but CVC reveal mirrors the password biometric gate. Cleanest split:153154- **CARD NUMBER reveal** → keep entirely local in the composable (no VM change),155 OR reuse `_revealed` since cards never show a password row. The draft uses a156 caller-supplied `cardRevealed` flag — wire it to a `remember { mutableStateOf }`157 in the screen (simplest), no VM needed.158- **CVC reveal** → add a dedicated flow mirroring `reveal()/mask()`:159160```kotlin161private val _cvcRevealed = MutableStateFlow(false)162val cvcRevealed: StateFlow<Boolean> = _cvcRevealed.asStateFlow()163164/** Reveal the CVC and auto-mask after 10s — same policy as password reveal. */165fun revealCvc() {166 _cvcRevealed.value = true167 viewModelScope.launch {168 kotlinx.coroutines.delay(10_000)169 _cvcRevealed.value = false170 }171}172fun maskCvc() { _cvcRevealed.value = false }173```174175`copy(field, value)` already exists and handles the copied-latch/clipboard for176arbitrary field names — reuse it directly for `cardNumber`/`cardholderName`/`expiry`/`cvc`.177178### B2. Insertion anchor (`EntryDetailScreen.kt`)179180The login field rows are rendered at lines ~202–231:181182```kotlin183 e.username?.takeIf { it.isNotBlank() }?.let {184 FieldRow("username", it, copiedField == "username") { vm.copy("username", it) }185 }186 ...187 e.notes?.takeIf { it.isNotBlank() }?.let {188 FieldRow("notes", it, copiedField == "notes", mono = false) { vm.copy("notes", it) }189 }190```191192Wrap the username/email/password/TOTP rows so they only show for non-card193entries, and add the card branch. Replace from the `e.username?...` line through194the `if (e.hasTOTP()) { ... }` block with:195196```kotlin197 if (e.isCard()) {198 CardDetailFields(199 entry = e,200 cardRevealed = cardRevealed,201 cvcRevealed = cvcRevealed,202 copiedField = copiedField,203 onToggleCardReveal = { cardRevealed = !cardRevealed },204 onRevealCvc = {205 val activity = context as? FragmentActivity206 if (activity != null && Biometrics.isAvailable(context)) {207 Biometrics.authenticate(activity, "Unlock Peach", e.title, onSuccess = vm::revealCvc)208 } else vm.revealCvc()209 },210 onMaskCvc = vm::maskCvc,211 onCopy = vm::copy,212 copyButton = { copied, onCopy -> CopyButton(copied, onCopy) },213 )214 } else {215 e.username?.takeIf { it.isNotBlank() }?.let { ... } // existing rows unchanged216 e.email?.takeIf { it.isNotBlank() }?.let { ... }217 e.password?.takeIf { it.isNotBlank() }?.let { pw -> PasswordRow(...) }218 if (e.hasTOTP()) { ... }219 }220 // notes row stays OUTSIDE the if/else — cards can have notes too221 e.notes?.takeIf { it.isNotBlank() }?.let { ... }222```223224Add the new collectors + local state near the existing ones at the top of225`EntryDetailScreen` (lines ~83–88):226227```kotlin228 val cvcRevealed by vm.cvcRevealed.collectAsState()229 var cardRevealed by remember { mutableStateOf(false) }230```231232`context`, `Biometrics`, `FragmentActivity`, `CopyButton` are all already233imported/in-scope in this file.234235### B3. Why CardDetailFields takes a `copyButton` lambda236237`CopyButton` and `FieldShell` are **private** in `EntryDetailScreen.kt`. Rather238than duplicate `CopyButton` (which carries the spec §4.4 sheen+drain animation),239the draft accepts it as a parameter so the host passes its real one. `CardShell`240(the field-shell mirror) is bundled in the draft to keep it self-contained; if241you prefer, make `FieldShell` non-private and delete `CardShell`.242243---244245## How reveal / biometric gating mirrors the existing password reveal246247The password row (EntryDetailScreen ~209–223) does:248`if (revealed) vm.mask() else { if Biometrics.isAvailable -> Biometrics.authenticate(..., onSuccess = vm::reveal) else vm.reveal() }`,249with `vm.reveal()` auto-masking after 10s.250251CVC reveal copies this 1:1: `revealCvc()`/`maskCvc()` + the same252`Biometrics.isAvailable`/`authenticate` branch, same 10s auto-mask. Card number253reveal is intentionally **not** gated (no PAN secret beyond clipboard, matching254the spec which gates only CVC), implemented as a plain local toggle.255256---257258## Assumptions / risks259260- **Double-format idempotency**: composable and VM both may call `formatPan`/261 `formatExpiry`. Both are idempotent, so this is safe; the note above shows how262 to remove the redundancy if desired.263- **expMonth/expYear storage shape**: assumed both are stored as zero-padded264 2-digit strings ("09", "27"). `displayExpiry` tolerates 1–4 digit inputs.265- **cardNetwork values** match the model doc comment266 (`visa|mastercard|amex|discover|unknown`). `unknown` is stored as `null`.267- **No tests touched** — these are additive composables/helpers. Recommend a268 small unit test for `CardFieldUtils` (formatPan Amex 4-6-5, maskedPan last-4,269 isExpiryValid boundaries) but none is included per the draft scope.270- **Imports to add to `EntryEditScreen.kt`**: none — `CardEditFields` is same271 package. To `EntryDetailScreen.kt`: add `import com.peachpasswords.vault.isCard`272 (and `CardDetailFields` is same-package).273```