# D2 — Card Type Fields: Integration Guide UI-only work. `VaultEntry` already has every card field (`cardholderName cardNumber expMonth expYear cvc cardNickname cardNetwork`) and `isCard()` (`type == "card"`). No model changes needed. ## Files in this draft | Draft file | Destination | Notes | |---|---|---| | `CardFieldUtils.kt` | `app/src/main/java/com/peachpasswords/ui/vault/CardFieldUtils.kt` | Pure helpers (format/detect/mask/validate). No deps. | | `CardEditFields.kt` | `app/src/main/java/com/peachpasswords/ui/vault/CardEditFields.kt` | `CardEditFields(...)` composable for EntryEdit. | | `CardDetailFields.kt` | `app/src/main/java/com/peachpasswords/ui/vault/CardDetailFields.kt` | `CardDetailFields(...)` composable for EntryDetail. | All three are package `com.peachpasswords.ui.vault`, so they merge into the same package as the target screens with no import churn. --- ## Part A — EntryEditScreen + EntryEditViewModel ### A1. ViewModel state additions (`EntryEditViewModel.kt`) Add fields to `EditForm` (after `notes`, before `showPassword`): ```kotlin val cardNumber: String = "", val cardholderName: String = "", val expiry: String = "", // "MM/YY", display+edit form; split into expMonth/expYear on save val cvc: String = "", val showCvc: Boolean = false, val cardNetwork: String = "unknown", ``` Hydrate them in the `existing?.let { EditForm(...) }` block (the model stores `expMonth`/`expYear` separately, so recombine): ```kotlin cardNumber = it.cardNumber.orEmpty().let { n -> CardFieldUtils.formatPan(n) }, cardholderName = it.cardholderName.orEmpty(), expiry = CardFieldUtils.displayExpiry(it.expMonth, it.expYear), cvc = it.cvc.orEmpty(), cardNetwork = it.cardNetwork ?: CardFieldUtils.detectNetwork(it.cardNumber.orEmpty()), ``` Add setters (place near `setNotes`): ```kotlin fun setCardNumber(v: String) = _form.update { val formatted = CardFieldUtils.formatPan(v) it.copy(cardNumber = formatted, cardNetwork = CardFieldUtils.detectNetwork(formatted), error = null) } fun setCardholderName(v: String) = _form.update { it.copy(cardholderName = v, error = null) } fun setExpiry(v: String) = _form.update { it.copy(expiry = CardFieldUtils.formatExpiry(v), error = null) } fun setCvc(v: String) = _form.update { it.copy(cvc = v.filter { c -> c.isDigit() }.take(CardFieldUtils.cvcMaxDigits(it.cardNetwork))) } fun toggleShowCvc() = _form.update { it.copy(showCvc = !it.showCvc) } ``` > Note: `CardEditFields` already calls `CardFieldUtils.formatPan/formatExpiry` > on input. If you wire setters that re-format (as above), pass *raw* setters > into the composable instead, OR pass these and let the composable's formatting > be idempotent (it is — formatting an already-formatted string is a no-op). > Simplest: have the composable call `vm::setCardNumber` directly and change its > `onCardNumber` lambda to `onCardNumber(raw)` (drop the inner `formatPan`). ### A2. Save-time write + validation (`EntryEditViewModel.save`) Add the card validation at the top of `save()`, right after the `title.isBlank()` check: ```kotlin if (f.type == "card" && (f.cardNumber.isBlank() || f.cardholderName.isBlank())) { _form.update { it.copy(error = "Cardholder name and card number are required") } return } ``` (The inline error renders via the existing `f.error?.let { ... }` block in the breach color — no new UI needed.) In **both** the `addEntry(VaultEntry(...))` and `updateEntry(existing!!.copy(...))` branches, add the card columns. Expiry "MM/YY" splits into `expMonth`/`expYear`: ```kotlin cardNumber = f.cardNumber.filter { it.isDigit() }.ifBlank { null }, cardholderName = f.cardholderName.ifBlank { null }, expMonth = f.expiry.filter { it.isDigit() }.take(2).ifBlank { null }, expYear = f.expiry.filter { it.isDigit() }.drop(2).take(2).ifBlank { null }, cvc = f.cvc.ifBlank { null }, cardNetwork = f.cardNetwork.takeIf { it != "unknown" }, ``` > The `addEntry` branch currently passes only login-ish fields; just append the > six lines above into the `VaultEntry(...)` constructor call. ### A3. Insertion anchor (`EntryEditScreen.kt`) The type-conditional block currently reads (lines ~100–130): ```kotlin Field("title", f.title, vm::setTitle) if (f.type == "login") { Field("url", f.url, vm::setUrl) ... Field("2FA secret (otpauth:// or base32)", f.totp, vm::setTotp) } Field("notes", f.notes, vm::setNotes, singleLine = false) ``` Insert the card branch between the closing `}` of the `if (f.type == "login")` block and the `Field("notes", ...)` line: ```kotlin } if (f.type == "card") { CardEditFields( cardNumber = f.cardNumber, cardholderName = f.cardholderName, expiry = f.expiry, cvc = f.cvc, showCvc = f.showCvc, network = f.cardNetwork, onCardNumber = vm::setCardNumber, onCardholder = vm::setCardholderName, onExpiry = vm::setExpiry, onCvc = vm::setCvc, onToggleShowCvc = vm::toggleShowCvc, ) } Field("notes", f.notes, vm::setNotes, singleLine = false) ``` The type pills already include `"card"` (line 94: `listOf("login", "note", "card")`), so type selection works as-is. > Double-formatting caveat: if you keep the composable's internal `formatPan` > AND the VM setter's `formatPan`, that is fine (idempotent). To avoid the > redundancy, drop the inner format in `CardEditFields` (change > `onChange = { raw -> onCardNumber(CardFieldUtils.formatPan(raw)) }` to > `onChange = onCardNumber`) and likewise for expiry. --- ## Part B — EntryDetailScreen + EntryDetailViewModel ### B1. ViewModel state additions (`EntryDetailViewModel.kt`) The existing `_revealed` is reused for the **password** path. Card number reveal is non-sensitive enough to be a plain local toggle (the spec doesn't biometric-gate it), but CVC reveal mirrors the password biometric gate. Cleanest split: - **CARD NUMBER reveal** → keep entirely local in the composable (no VM change), OR reuse `_revealed` since cards never show a password row. The draft uses a caller-supplied `cardRevealed` flag — wire it to a `remember { mutableStateOf }` in the screen (simplest), no VM needed. - **CVC reveal** → add a dedicated flow mirroring `reveal()/mask()`: ```kotlin private val _cvcRevealed = MutableStateFlow(false) val cvcRevealed: StateFlow = _cvcRevealed.asStateFlow() /** Reveal the CVC and auto-mask after 10s — same policy as password reveal. */ fun revealCvc() { _cvcRevealed.value = true viewModelScope.launch { kotlinx.coroutines.delay(10_000) _cvcRevealed.value = false } } fun maskCvc() { _cvcRevealed.value = false } ``` `copy(field, value)` already exists and handles the copied-latch/clipboard for arbitrary field names — reuse it directly for `cardNumber`/`cardholderName`/`expiry`/`cvc`. ### B2. Insertion anchor (`EntryDetailScreen.kt`) The login field rows are rendered at lines ~202–231: ```kotlin e.username?.takeIf { it.isNotBlank() }?.let { FieldRow("username", it, copiedField == "username") { vm.copy("username", it) } } ... e.notes?.takeIf { it.isNotBlank() }?.let { FieldRow("notes", it, copiedField == "notes", mono = false) { vm.copy("notes", it) } } ``` Wrap the username/email/password/TOTP rows so they only show for non-card entries, and add the card branch. Replace from the `e.username?...` line through the `if (e.hasTOTP()) { ... }` block with: ```kotlin if (e.isCard()) { CardDetailFields( entry = e, cardRevealed = cardRevealed, cvcRevealed = cvcRevealed, copiedField = copiedField, onToggleCardReveal = { cardRevealed = !cardRevealed }, onRevealCvc = { val activity = context as? FragmentActivity if (activity != null && Biometrics.isAvailable(context)) { Biometrics.authenticate(activity, "Unlock Peach", e.title, onSuccess = vm::revealCvc) } else vm.revealCvc() }, onMaskCvc = vm::maskCvc, onCopy = vm::copy, copyButton = { copied, onCopy -> CopyButton(copied, onCopy) }, ) } else { e.username?.takeIf { it.isNotBlank() }?.let { ... } // existing rows unchanged e.email?.takeIf { it.isNotBlank() }?.let { ... } e.password?.takeIf { it.isNotBlank() }?.let { pw -> PasswordRow(...) } if (e.hasTOTP()) { ... } } // notes row stays OUTSIDE the if/else — cards can have notes too e.notes?.takeIf { it.isNotBlank() }?.let { ... } ``` Add the new collectors + local state near the existing ones at the top of `EntryDetailScreen` (lines ~83–88): ```kotlin val cvcRevealed by vm.cvcRevealed.collectAsState() var cardRevealed by remember { mutableStateOf(false) } ``` `context`, `Biometrics`, `FragmentActivity`, `CopyButton` are all already imported/in-scope in this file. ### B3. Why CardDetailFields takes a `copyButton` lambda `CopyButton` and `FieldShell` are **private** in `EntryDetailScreen.kt`. Rather than duplicate `CopyButton` (which carries the spec §4.4 sheen+drain animation), the draft accepts it as a parameter so the host passes its real one. `CardShell` (the field-shell mirror) is bundled in the draft to keep it self-contained; if you prefer, make `FieldShell` non-private and delete `CardShell`. --- ## How reveal / biometric gating mirrors the existing password reveal The password row (EntryDetailScreen ~209–223) does: `if (revealed) vm.mask() else { if Biometrics.isAvailable -> Biometrics.authenticate(..., onSuccess = vm::reveal) else vm.reveal() }`, with `vm.reveal()` auto-masking after 10s. CVC reveal copies this 1:1: `revealCvc()`/`maskCvc()` + the same `Biometrics.isAvailable`/`authenticate` branch, same 10s auto-mask. Card number reveal is intentionally **not** gated (no PAN secret beyond clipboard, matching the spec which gates only CVC), implemented as a plain local toggle. --- ## Assumptions / risks - **Double-format idempotency**: composable and VM both may call `formatPan`/ `formatExpiry`. Both are idempotent, so this is safe; the note above shows how to remove the redundancy if desired. - **expMonth/expYear storage shape**: assumed both are stored as zero-padded 2-digit strings ("09", "27"). `displayExpiry` tolerates 1–4 digit inputs. - **cardNetwork values** match the model doc comment (`visa|mastercard|amex|discover|unknown`). `unknown` is stored as `null`. - **No tests touched** — these are additive composables/helpers. Recommend a small unit test for `CardFieldUtils` (formatPan Amex 4-6-5, maskedPan last-4, isExpiryValid boundaries) but none is included per the draft scope. - **Imports to add to `EntryEditScreen.kt`**: none — `CardEditFields` is same package. To `EntryDetailScreen.kt`: add `import com.peachpasswords.vault.isCard` (and `CardDetailFields` is same-package). ```