INTEGRATION.md

11.6 KB · raw

rendered · source

1# D6 — Inline Field Editing in EntryDetailScreen — Integration23Per-field inline edit (view↔edit toggle, underlined `BasicTextField`, ✓ save / ✗4cancel), a biometric/password gate before editing the password field, and an5identity-suggest `DropdownMenu` on USERNAME/EMAIL fed by a new repo method.67**Shared-file note:** D6, D7, E6 all touch `EntryDetailScreen.kt`. This block is8self-contained — the composables live in `EditableFieldRow.kt` and only the call9sites + one VM method + one repo method change in the real files. Anchors below.1011---1213## 1. Files1415### A. Drop-in composable file16`app/drafts/D6/EditableFieldRow.kt` →17`app/src/main/java/com/peachpasswords/ui/vault/EditableFieldRow.kt`1819It reuses two private helpers that already exist in `EntryDetailScreen.kt`:20`FieldShell` and `CopyButton`.2122**Choose ONE wiring approach:**2324- **Inline (simplest):** paste the public composables `EditableFieldRow` and25  `EditablePasswordRow` (plus the private `InlineEditor` / `UnderlinedField` /26  `SaveCancelRow`) directly into `EntryDetailScreen.kt`. No visibility changes27  needed — they share the file with `FieldShell`/`CopyButton`. Drop the package28  + import lines that duplicate ones already in the target.2930- **Separate file (as drafted):** keep `EditableFieldRow.kt` as its own file and31  change the visibility of the two helpers in `EntryDetailScreen.kt` from32  `private` to `internal`:33  - `private fun FieldShell(` → `internal fun FieldShell(`34  - `private fun CopyButton(` → `internal fun CopyButton(`3536Either works; inline is recommended since the orchestrator is already editing37that file for D7/E6.3839---4041## 2. EntryDetailScreen.kt — call-site swaps4243Collect the suggestion list at the top of the composable, next to the existing44state collectors.4546**Anchor (existing, ~line 83-85):**47```kotlin48    val entry by vm.entry.collectAsState()49    val revealed by vm.revealed.collectAsState()50    val copiedField by vm.copiedField.collectAsState()51```52**Add immediately below:**53```kotlin54    val identitySuggestions by vm.identitySuggestions.collectAsState()55```5657### USERNAME / EMAIL rows5859**Anchor (existing, ~line 202-207):**60```kotlin61                e.username?.takeIf { it.isNotBlank() }?.let {62                    FieldRow("username", it, copiedField == "username") { vm.copy("username", it) }63                }64                e.email?.takeIf { it.isNotBlank() }?.let {65                    FieldRow("email", it, copiedField == "email") { vm.copy("email", it) }66                }67```68**Replace with:**69```kotlin70                e.username?.takeIf { it.isNotBlank() }?.let { v ->71                    EditableFieldRow(72                        label = "username",73                        field = "username",74                        value = v,75                        copied = copiedField == "username",76                        onCopy = { vm.copy("username", v) },77                        onSave = { vm.updateField("username", it) },78                        suggestions = identitySuggestions,79                    )80                }81                e.email?.takeIf { it.isNotBlank() }?.let { v ->82                    EditableFieldRow(83                        label = "email",84                        field = "email",85                        value = v,86                        copied = copiedField == "email",87                        onCopy = { vm.copy("email", v) },88                        onSave = { vm.updateField("email", it) },89                        suggestions = identitySuggestions,90                    )91                }92```93> Email and username share the same value pool in the Peach data model, so both94> use `getDistinctFieldValues("username")` (see §4).9596### PASSWORD row9798**Anchor (existing, ~line 208-224):** the whole `PasswordRow(...)` block inside99`e.password?.takeIf { it.isNotBlank() }?.let { pw -> ... }`.100**Replace the `PasswordRow(...)` call with:**101```kotlin102                    EditablePasswordRow(103                        value = pw,104                        copied = copiedField == "password",105                        entryTitle = e.title,106                        onCopy = { vm.copy("password", pw) },107                        onSave = { vm.updateField("password", it) },108                    )109```110> This drops the reveal-only `PasswordRow` in favor of the gated editable row,111> which itself runs the biometric prompt before entering edit mode (same112> `Biometrics.isAvailable` / `Biometrics.authenticate` flow). If you want to KEEP113> the separate reveal-only view alongside inline edit, leave `PasswordRow` in and114> only add `EditablePasswordRow` — but the brief specifies inline edit replaces115> the read-only rows, so the swap above is the intended path. `vm.reveal/mask`116> and the `revealed` state become unused if `PasswordRow` is fully removed —117> delete them only if no other row needs them.118119### NOTES row (optional, recommended for parity)120121**Anchor (existing, ~line 229-231):**122```kotlin123                e.notes?.takeIf { it.isNotBlank() }?.let {124                    FieldRow("notes", it, copiedField == "notes", mono = false) { vm.copy("notes", it) }125                }126```127**Replace with:**128```kotlin129                e.notes?.takeIf { it.isNotBlank() }?.let { v ->130                    EditableFieldRow(131                        label = "notes",132                        field = "notes",133                        value = v,134                        copied = copiedField == "notes",135                        onCopy = { vm.copy("notes", v) },136                        onSave = { vm.updateField("notes", it) },137                        mono = false,138                        singleLine = false,139                    )140                }141```142143### Imports to add to EntryDetailScreen.kt144Only if using the **inline** approach the imports already land with the pasted145code. For the **separate-file** approach the call sites need nothing new beyond146what is already imported (the new composables are same-package). If they end up147in a different package, add:148```kotlin149import com.peachpasswords.ui.vault.EditableFieldRow150import com.peachpasswords.ui.vault.EditablePasswordRow151```152153---154155## 3. EntryDetailViewModel.kt — additions156157Add the `updateField` method and the `identitySuggestions` flow.158159**Anchor (existing, ~line 33):** end of the `entry` StateFlow declaration:160```kotlin161    val entry: StateFlow<VaultEntry?> =162        vault.entries.map { list -> list.firstOrNull { it.id == id } }163            .stateIn(viewModelScope, SharingStarted.WhileSubscribed(5000), vault.getEntry(id))164```165**Add below it:**166```kotlin167    /** Distinct prior username/email values for the identity-suggest dropdown. */168    val identitySuggestions: StateFlow<List<String>> =169        vault.entries170            .map { vault.getDistinctFieldValues("username").take(4) }171            .stateIn(viewModelScope, SharingStarted.WhileSubscribed(5000), emptyList())172```173174**Anchor (existing, ~line 69):** the `delete(...)` method. Add this method175alongside it (e.g. just above `delete`):176```kotlin177    /**178     * Persist a single field's inline edit. Reads the live entry, applies the new179     * value to the named field, and writes a full-entry patch via updateEntry180     * (which handles password-history capture and timestamps). No-op if the entry181     * is gone. Trim/normalisation is the caller's responsibility for text fields;182     * the password is written verbatim.183     */184    fun updateField(field: String, value: String) {185        val current = entry.value ?: return186        val patch = when (field) {187            "username" -> current.copy(username = value.ifBlank { null })188            "email" -> current.copy(email = value.ifBlank { null })189            "password" -> current.copy(password = value.ifBlank { null })190            "notes" -> current.copy(notes = value.ifBlank { null })191            "totp" -> current.copy(totp = value.ifBlank { null })192            else -> return193        }194        viewModelScope.launch {195            runCatching { vault.updateEntry(patch) }196        }197    }198```199200`launch`, `map`, `stateIn`, `SharingStarted` are all already imported in the VM.201202> The `"totp"` branch is included for E6 (TOTP inline edit) so that item can reuse203> this same method — harmless for D6. Remove if the orchestrator prefers E6 to add204> its own branch.205206---207208## 4. VaultRepository.kt — new method209210Add `getDistinctFieldValues` next to the existing `getDistinctTags` (~line 355).211212**Anchor (existing, ~line 354-363):**213```kotlin214    /** Sorted, de-duplicated tags across all active entries. */215    fun getDistinctTags(): List<String> =216        entryList.asSequence()217            .filter { it.deletedAt == null }218            .flatMap { it.tags?.asSequence() ?: emptySequence() }219            .map { it.trim().lowercase() }220            .filter { it.isNotEmpty() }221            .distinct()222            .sorted()223            .toList()224```225**Add below it:**226```kotlin227    /**228     * Distinct non-blank values for [field] across all active entries, ordered by229     * descending frequency (most-used first). Currently supports "username" —230     * which pools both the username and email columns, since the Peach data model231     * treats them as the same identity field for suggestions. Used by the232     * identity-suggest dropdown in inline field editing (D6) and EntryEditScreen (E4).233     */234    fun getDistinctFieldValues(field: String): List<String> {235        val active = entryList.filter { it.deletedAt == null }236        val values: Sequence<String> = when (field) {237            "username" -> active.asSequence().flatMap {238                sequenceOf(it.username, it.email)239            }.filterNotNull()240            "email" -> active.asSequence().mapNotNull { it.email }241            "password" -> emptySequence()242            else -> emptySequence()243        }244        return values245            .map { it.trim() }246            .filter { it.isNotEmpty() }247            .groupingBy { it }248            .eachCount()249            .entries250            .sortedWith(compareByDescending<Map.Entry<String, Int>> { it.value }.thenBy { it.key })251            .map { it.key }252    }253```254> Frequency-sorted as the brief (§14) requires. "password" deliberately returns255> empty — never suggest passwords. "email" is provided for completeness; D6 only256> calls "username".257258---259260## 5. Risks / assumptions261262- **Verified:** `VaultEntry` has `username`, `email`, `password`, `notes`, `totp`263  (all `String?`). `VaultRepository.updateEntry(patch: VaultEntry)` exists and264  handles password-history + timestamps. `Biometrics.isAvailable` /265  `Biometrics.authenticate(activity, title, subtitle, onSuccess)` exist and are266  already used by the current `PasswordRow` reveal flow — `EditablePasswordRow`267  mirrors it exactly.268- **No new imports/deps** beyond Compose Material3 `DropdownMenu` /269  `DropdownMenuItem` (already on the classpath; EntryEditScreen-area uses M3).270- **`remember(value)` keying:** `editing`/`draft` are keyed on `value` so an271  external entry update (e.g. from sync) resets a stale open editor. Acceptable;272  if a sync lands mid-edit the editor closes rather than clobbering — matches273  last-write-wins intent.274- **Password reveal state:** if you fully replace `PasswordRow`, the VM's275  `reveal()`/`mask()`/`revealed` may become dead code. Leave them if any other276  surface (D7/E6) needs reveal; otherwise the orchestrator can prune.277- **DropdownMenu anchoring:** M3 `DropdownMenu` anchors to its parent `Box`; it278  opens on entering edit mode (when suggestions exist) and on dismiss/pick. If a279  focus-driven open is preferred, gate `dropdownOpen` on a280  `FocusRequester`/`onFocusChanged` instead — left simple per the brief.281- **`✓`/`✗` glyphs:** rendered via `PeachType.labelMono` (IBM Plex Mono). If the282  bundled Mono lacks these glyphs, substitute the rounded `Check`/`Close`283  Material icons already imported in EntryDetailScreen.kt.