INTEGRATION.md

7.3 KB · raw

rendered · source

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

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):

    /** Sorted, de-duplicated tags across all active entries. */
    fun getDistinctTags(): List<String> =
        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:

    /**
     * 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<String> {
        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<String, Int>?, value, _ ->
                Pair(acc?.first ?: value, (acc?.second ?: 0) + 1)
            }
            .values
            .sortedWith(compareByDescending<Pair<String, Int>> { it.second }.thenBy { it.first.lowercase() })
            .map { it.first }
    }

Notes:


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):

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):

    val form: StateFlow<EditForm> = _form.asStateFlow()

Insert immediately after:

    /**
     * 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<List<String>> =
        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:

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):

    val f by vm.form.collectAsState()
    val accent = LocalPeachAccent.current

Change to:

    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):

                    Field("username", f.username, vm::setUsername)
                    Field("email", f.email, vm::setEmail)

Replace with:

                    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