1# E4 — Identity Suggest Dropdown (EntryEditScreen username/email)23Adds a focus-driven suggestion dropdown beneath the USERNAME and EMAIL fields in4`EntryEditScreen`, offering up to 4 previously-used values. Username and email5share one underlying field in the Peach model, so **both fields suggest from6`getDistinctFieldValues("username")`** (matches D6's detail-side suggest).78This is the EntryEditScreen counterpart to D6. The9`VaultRepository.getDistinctFieldValues(field)` method below is shared with D6 —10**define it once, identically**.1112---1314## Files in this draft1516- `IdentitySuggestDropdown.kt` — reusable composables:17 - `IdentitySuggestDropdown(expanded, suggestions, onPick, onDismiss)` — bare18 anchored `DropdownMenu`.19 - `IdentitySuggestField(label, value, suggestions, onChange)` — field + dropdown20 wired to focus state (drop-in for the username/email `Field(...)` calls).2122Both are in package `com.peachpasswords.ui.vault`, same package as23`EntryEditScreen.kt`/`EntryEditViewModel.kt`.2425---2627## 1. VaultRepository — new method (shared with D6)2829`EntryEditViewModel` only needs `getDistinctFieldValues`; do not duplicate it if30D6 already added it. Place it next to `getDistinctTags()`.3132**Anchor** (`vault/VaultRepository.kt`, currently around lines 354-363):3334```kotlin35 /** Sorted, de-duplicated tags across all active entries. */36 fun getDistinctTags(): List<String> =37 entryList.asSequence()38 .filter { it.deletedAt == null }39 .flatMap { it.tags?.asSequence() ?: emptySequence() }40 .map { it.trim().lowercase() }41 .filter { it.isNotEmpty() }42 .distinct()43 .sorted()44 .toList()45```4647**Insert immediately after that method:**4849```kotlin50 /**51 * Distinct non-blank values for an identity field across active entries,52 * most-frequently-used first. Currently supports "username" and "email";53 * unknown field names return an empty list. Values keep original casing54 * (deduped case-insensitively) so the suggestion matches what the user typed.55 */56 fun getDistinctFieldValues(field: String): List<String> {57 val select: (VaultEntry) -> String? = when (field) {58 "username" -> { e -> e.username }59 "email" -> { e -> e.email }60 else -> return emptyList()61 }62 return entryList.asSequence()63 .filter { it.deletedAt == null }64 .mapNotNull { select(it)?.trim() }65 .filter { it.isNotEmpty() }66 .groupingBy { it.lowercase() }67 .aggregate { _, acc: Pair<String, Int>?, value, _ ->68 Pair(acc?.first ?: value, (acc?.second ?: 0) + 1)69 }70 .values71 .sortedWith(compareByDescending<Pair<String, Int>> { it.second }.thenBy { it.first.lowercase() })72 .map { it.first }73 }74```7576Notes:77- Frequency-sorted (spec: "sorted by frequency"); ties broken alphabetically for78 stable ordering.79- `VaultEntry.username`/`.email` are `String?` — handled via `mapNotNull`.80- D6 calls `getDistinctFieldValues("username")`; identical signature/behavior.8182---8384## 2. EntryEditViewModel — usernameSuggestions StateFlow8586`getDistinctFieldValues` reads the in-memory `entryList`, which is fixed for the87lifetime of this editor screen, so computing once at construction is sufficient88(no need for a reactive flow over the repo). Exposed as a `StateFlow` to match89the brief's `collectAsState()` call site.9091**Add imports** (top of `EntryEditViewModel.kt`):9293```kotlin94import kotlinx.coroutines.flow.MutableStateFlow // already imported95import kotlinx.coroutines.flow.StateFlow // already imported96import kotlinx.coroutines.flow.asStateFlow // already imported97```9899(No new imports needed — all three are already present.)100101**Anchor** (`EntryEditViewModel.kt`, around line 69):102103```kotlin104 val form: StateFlow<EditForm> = _form.asStateFlow()105```106107**Insert immediately after:**108109```kotlin110 /**111 * Up to 4 previously-used identity values, frequency-sorted. Username and112 * email share one field in the Peach model, so both editor fields suggest113 * from "username".114 */115 val usernameSuggestions: StateFlow<List<String>> =116 MutableStateFlow(vault.getDistinctFieldValues("username").take(4)).asStateFlow()117```118119(`vault` is the injected `VaultRepository` constructor param — already in scope.)120121---122123## 3. EntryEditScreen — wire the dropdown into the login fields124125### Imports126Add to the import block of `EntryEditScreen.kt`:127128```kotlin129import androidx.compose.runtime.collectAsState // already imported130```131132(No new imports needed — `collectAsState` and `getValue` are already present;133the composables live in the same package.)134135### Collect the suggestions136137**Anchor** (`EntryEditScreen.kt`, lines 62-63):138139```kotlin140 val f by vm.form.collectAsState()141 val accent = LocalPeachAccent.current142```143144**Change to:**145146```kotlin147 val f by vm.form.collectAsState()148 val usernameSuggestions by vm.usernameSuggestions.collectAsState()149 val accent = LocalPeachAccent.current150```151152### Replace the username/email field calls153154**Anchor** (`EntryEditScreen.kt`, lines 103-104):155156```kotlin157 Field("username", f.username, vm::setUsername)158 Field("email", f.email, vm::setEmail)159```160161**Replace with:**162163```kotlin164 IdentitySuggestField("username", f.username, usernameSuggestions, vm::setUsername)165 IdentitySuggestField("email", f.email, usernameSuggestions, vm::setEmail)166```167168Both use `usernameSuggestions` per the shared-field model. `IdentitySuggestField`169already filters out a suggestion equal to the current value, so a fully-typed170match won't show a redundant row.171172---173174## Option A (optional cleanup)175176`IdentitySuggestField` re-implements the underline input because the existing177`Field` composable is `private` and has no focus callback. If you prefer one178input definition, instead:1791801. Add `onFocusChanged: ((Boolean) -> Unit)? = null` to the private `Field`181 composable and attach `.onFocusChanged { onFocusChanged?.invoke(it.isFocused) }`182 to its `BasicTextField` modifier.1832. Drop `IdentitySuggestField` and inline focus state + `IdentitySuggestDropdown`184 in `EntryEditScreen` at the two call sites.185186Either approach compiles; the drop-in `IdentitySuggestField` is the lower-risk187default and touches the fewest existing lines.188189---190191## Risks / assumptions192193- **Popup focusability:** the dropdown is non-focusable (`PopupProperties(focusable194 = false)`) so opening it does not blur the text field that gates `expanded`.195 This is the standard pattern for inline autocomplete; verify on-device that the196 menu still dismisses on outside tap (it does — `onDismissRequest` fires).197- **Suggestions are static for the screen's lifetime** (computed at VM198 construction). Acceptable: the entry list doesn't change while editing one199 entry. If reactive behavior is ever wanted, back it with `vault` flows instead.200- **Self-suggestion:** when editing an existing entry, that entry's own username201 appears in the distinct set. The `!= value` filter hides the exact current202 value; a different historical value still shows, which is the intended affordance.203- `getDistinctFieldValues` only handles `"username"`/`"email"`; other field names204 return empty (defensive, matches the only two callers).