# E4 — Identity Suggest Dropdown (EntryEditScreen username/email) Adds a focus-driven suggestion dropdown beneath the USERNAME and EMAIL fields in `EntryEditScreen`, offering up to 4 previously-used values. Username and email share one underlying field in the Peach model, so **both fields suggest from `getDistinctFieldValues("username")`** (matches D6's detail-side suggest). This is the EntryEditScreen counterpart to D6. The `VaultRepository.getDistinctFieldValues(field)` method below is shared with D6 — **define it once, identically**. --- ## Files in this draft - `IdentitySuggestDropdown.kt` — reusable composables: - `IdentitySuggestDropdown(expanded, suggestions, onPick, onDismiss)` — bare anchored `DropdownMenu`. - `IdentitySuggestField(label, value, suggestions, onChange)` — field + dropdown wired to focus state (drop-in for the username/email `Field(...)` calls). Both are in package `com.peachpasswords.ui.vault`, same package as `EntryEditScreen.kt`/`EntryEditViewModel.kt`. --- ## 1. VaultRepository — new method (shared with D6) `EntryEditViewModel` only needs `getDistinctFieldValues`; do not duplicate it if D6 already added it. Place it next to `getDistinctTags()`. **Anchor** (`vault/VaultRepository.kt`, currently around lines 354-363): ```kotlin /** Sorted, de-duplicated tags across all active entries. */ fun getDistinctTags(): List = entryList.asSequence() .filter { it.deletedAt == null } .flatMap { it.tags?.asSequence() ?: emptySequence() } .map { it.trim().lowercase() } .filter { it.isNotEmpty() } .distinct() .sorted() .toList() ``` **Insert immediately after that method:** ```kotlin /** * Distinct non-blank values for an identity field across active entries, * most-frequently-used first. Currently supports "username" and "email"; * unknown field names return an empty list. Values keep original casing * (deduped case-insensitively) so the suggestion matches what the user typed. */ fun getDistinctFieldValues(field: String): List { val select: (VaultEntry) -> String? = when (field) { "username" -> { e -> e.username } "email" -> { e -> e.email } else -> return emptyList() } return entryList.asSequence() .filter { it.deletedAt == null } .mapNotNull { select(it)?.trim() } .filter { it.isNotEmpty() } .groupingBy { it.lowercase() } .aggregate { _, acc: Pair?, value, _ -> Pair(acc?.first ?: value, (acc?.second ?: 0) + 1) } .values .sortedWith(compareByDescending> { it.second }.thenBy { it.first.lowercase() }) .map { it.first } } ``` Notes: - Frequency-sorted (spec: "sorted by frequency"); ties broken alphabetically for stable ordering. - `VaultEntry.username`/`.email` are `String?` — handled via `mapNotNull`. - D6 calls `getDistinctFieldValues("username")`; identical signature/behavior. --- ## 2. EntryEditViewModel — usernameSuggestions StateFlow `getDistinctFieldValues` reads the in-memory `entryList`, which is fixed for the lifetime of this editor screen, so computing once at construction is sufficient (no need for a reactive flow over the repo). Exposed as a `StateFlow` to match the brief's `collectAsState()` call site. **Add imports** (top of `EntryEditViewModel.kt`): ```kotlin import kotlinx.coroutines.flow.MutableStateFlow // already imported import kotlinx.coroutines.flow.StateFlow // already imported import kotlinx.coroutines.flow.asStateFlow // already imported ``` (No new imports needed — all three are already present.) **Anchor** (`EntryEditViewModel.kt`, around line 69): ```kotlin val form: StateFlow = _form.asStateFlow() ``` **Insert immediately after:** ```kotlin /** * Up to 4 previously-used identity values, frequency-sorted. Username and * email share one field in the Peach model, so both editor fields suggest * from "username". */ val usernameSuggestions: StateFlow> = MutableStateFlow(vault.getDistinctFieldValues("username").take(4)).asStateFlow() ``` (`vault` is the injected `VaultRepository` constructor param — already in scope.) --- ## 3. EntryEditScreen — wire the dropdown into the login fields ### Imports Add to the import block of `EntryEditScreen.kt`: ```kotlin import androidx.compose.runtime.collectAsState // already imported ``` (No new imports needed — `collectAsState` and `getValue` are already present; the composables live in the same package.) ### Collect the suggestions **Anchor** (`EntryEditScreen.kt`, lines 62-63): ```kotlin val f by vm.form.collectAsState() val accent = LocalPeachAccent.current ``` **Change to:** ```kotlin val f by vm.form.collectAsState() val usernameSuggestions by vm.usernameSuggestions.collectAsState() val accent = LocalPeachAccent.current ``` ### Replace the username/email field calls **Anchor** (`EntryEditScreen.kt`, lines 103-104): ```kotlin Field("username", f.username, vm::setUsername) Field("email", f.email, vm::setEmail) ``` **Replace with:** ```kotlin IdentitySuggestField("username", f.username, usernameSuggestions, vm::setUsername) IdentitySuggestField("email", f.email, usernameSuggestions, vm::setEmail) ``` Both use `usernameSuggestions` per the shared-field model. `IdentitySuggestField` already filters out a suggestion equal to the current value, so a fully-typed match won't show a redundant row. --- ## Option A (optional cleanup) `IdentitySuggestField` re-implements the underline input because the existing `Field` composable is `private` and has no focus callback. If you prefer one input definition, instead: 1. Add `onFocusChanged: ((Boolean) -> Unit)? = null` to the private `Field` composable and attach `.onFocusChanged { onFocusChanged?.invoke(it.isFocused) }` to its `BasicTextField` modifier. 2. Drop `IdentitySuggestField` and inline focus state + `IdentitySuggestDropdown` in `EntryEditScreen` at the two call sites. Either approach compiles; the drop-in `IdentitySuggestField` is the lower-risk default and touches the fewest existing lines. --- ## Risks / assumptions - **Popup focusability:** the dropdown is non-focusable (`PopupProperties(focusable = false)`) so opening it does not blur the text field that gates `expanded`. This is the standard pattern for inline autocomplete; verify on-device that the menu still dismisses on outside tap (it does — `onDismissRequest` fires). - **Suggestions are static for the screen's lifetime** (computed at VM construction). Acceptable: the entry list doesn't change while editing one entry. If reactive behavior is ever wanted, back it with `vault` flows instead. - **Self-suggestion:** when editing an existing entry, that entry's own username appears in the distinct set. The `!= value` filter hides the exact current value; a different historical value still shows, which is the intended affordance. - `getDistinctFieldValues` only handles `"username"`/`"email"`; other field names return empty (defensive, matches the only two callers).