INTEGRATION.md

11.6 KB · raw

rendered · source

D6 — Inline Field Editing in EntryDetailScreen — Integration

Per-field inline edit (view↔edit toggle, underlined BasicTextField, ✓ save / ✗ cancel), a biometric/password gate before editing the password field, and an identity-suggest DropdownMenu on USERNAME/EMAIL fed by a new repo method.

Shared-file note: D6, D7, E6 all touch EntryDetailScreen.kt. This block is self-contained — the composables live in EditableFieldRow.kt and only the call sites + one VM method + one repo method change in the real files. Anchors below.


1. Files

A. Drop-in composable file

app/drafts/D6/EditableFieldRow.kt → app/src/main/java/com/peachpasswords/ui/vault/EditableFieldRow.kt

It reuses two private helpers that already exist in EntryDetailScreen.kt: FieldShell and CopyButton.

Choose ONE wiring approach:

Either works; inline is recommended since the orchestrator is already editing that file for D7/E6.


2. EntryDetailScreen.kt — call-site swaps

Collect the suggestion list at the top of the composable, next to the existing state collectors.

Anchor (existing, ~line 83-85):

    val entry by vm.entry.collectAsState()
    val revealed by vm.revealed.collectAsState()
    val copiedField by vm.copiedField.collectAsState()

Add immediately below:

    val identitySuggestions by vm.identitySuggestions.collectAsState()

USERNAME / EMAIL rows

Anchor (existing, ~line 202-207):

                e.username?.takeIf { it.isNotBlank() }?.let {
                    FieldRow("username", it, copiedField == "username") { vm.copy("username", it) }
                }
                e.email?.takeIf { it.isNotBlank() }?.let {
                    FieldRow("email", it, copiedField == "email") { vm.copy("email", it) }
                }

Replace with:

                e.username?.takeIf { it.isNotBlank() }?.let { v ->
                    EditableFieldRow(
                        label = "username",
                        field = "username",
                        value = v,
                        copied = copiedField == "username",
                        onCopy = { vm.copy("username", v) },
                        onSave = { vm.updateField("username", it) },
                        suggestions = identitySuggestions,
                    )
                }
                e.email?.takeIf { it.isNotBlank() }?.let { v ->
                    EditableFieldRow(
                        label = "email",
                        field = "email",
                        value = v,
                        copied = copiedField == "email",
                        onCopy = { vm.copy("email", v) },
                        onSave = { vm.updateField("email", it) },
                        suggestions = identitySuggestions,
                    )
                }

Email and username share the same value pool in the Peach data model, so both use getDistinctFieldValues("username") (see §4).

PASSWORD row

Anchor (existing, ~line 208-224): the whole PasswordRow(...) block inside e.password?.takeIf { it.isNotBlank() }?.let { pw -> ... }. Replace the PasswordRow(...) call with:

                    EditablePasswordRow(
                        value = pw,
                        copied = copiedField == "password",
                        entryTitle = e.title,
                        onCopy = { vm.copy("password", pw) },
                        onSave = { vm.updateField("password", it) },
                    )

This drops the reveal-only PasswordRow in favor of the gated editable row, which itself runs the biometric prompt before entering edit mode (same Biometrics.isAvailable / Biometrics.authenticate flow). If you want to KEEP the separate reveal-only view alongside inline edit, leave PasswordRow in and only add EditablePasswordRow — but the brief specifies inline edit replaces the read-only rows, so the swap above is the intended path. vm.reveal/mask and the revealed state become unused if PasswordRow is fully removed — delete them only if no other row needs them.

NOTES row (optional, recommended for parity)

Anchor (existing, ~line 229-231):

                e.notes?.takeIf { it.isNotBlank() }?.let {
                    FieldRow("notes", it, copiedField == "notes", mono = false) { vm.copy("notes", it) }
                }

Replace with:

                e.notes?.takeIf { it.isNotBlank() }?.let { v ->
                    EditableFieldRow(
                        label = "notes",
                        field = "notes",
                        value = v,
                        copied = copiedField == "notes",
                        onCopy = { vm.copy("notes", v) },
                        onSave = { vm.updateField("notes", it) },
                        mono = false,
                        singleLine = false,
                    )
                }

Imports to add to EntryDetailScreen.kt

Only if using the inline approach the imports already land with the pasted code. For the separate-file approach the call sites need nothing new beyond what is already imported (the new composables are same-package). If they end up in a different package, add:

import com.peachpasswords.ui.vault.EditableFieldRow
import com.peachpasswords.ui.vault.EditablePasswordRow

3. EntryDetailViewModel.kt — additions

Add the updateField method and the identitySuggestions flow.

Anchor (existing, ~line 33): end of the entry StateFlow declaration:

    val entry: StateFlow<VaultEntry?> =
        vault.entries.map { list -> list.firstOrNull { it.id == id } }
            .stateIn(viewModelScope, SharingStarted.WhileSubscribed(5000), vault.getEntry(id))

Add below it:

    /** Distinct prior username/email values for the identity-suggest dropdown. */
    val identitySuggestions: StateFlow<List<String>> =
        vault.entries
            .map { vault.getDistinctFieldValues("username").take(4) }
            .stateIn(viewModelScope, SharingStarted.WhileSubscribed(5000), emptyList())

Anchor (existing, ~line 69): the delete(...) method. Add this method alongside it (e.g. just above delete):

    /**
     * Persist a single field's inline edit. Reads the live entry, applies the new
     * value to the named field, and writes a full-entry patch via updateEntry
     * (which handles password-history capture and timestamps). No-op if the entry
     * is gone. Trim/normalisation is the caller's responsibility for text fields;
     * the password is written verbatim.
     */
    fun updateField(field: String, value: String) {
        val current = entry.value ?: return
        val patch = when (field) {
            "username" -> current.copy(username = value.ifBlank { null })
            "email" -> current.copy(email = value.ifBlank { null })
            "password" -> current.copy(password = value.ifBlank { null })
            "notes" -> current.copy(notes = value.ifBlank { null })
            "totp" -> current.copy(totp = value.ifBlank { null })
            else -> return
        }
        viewModelScope.launch {
            runCatching { vault.updateEntry(patch) }
        }
    }

launch, map, stateIn, SharingStarted are all already imported in the VM.

The "totp" branch is included for E6 (TOTP inline edit) so that item can reuse this same method — harmless for D6. Remove if the orchestrator prefers E6 to add its own branch.


4. VaultRepository.kt — new method

Add getDistinctFieldValues next to the existing getDistinctTags (~line 355).

Anchor (existing, ~line 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()

Add below it:

    /**
     * Distinct non-blank values for [field] across all active entries, ordered by
     * descending frequency (most-used first). Currently supports "username" —
     * which pools both the username and email columns, since the Peach data model
     * treats them as the same identity field for suggestions. Used by the
     * identity-suggest dropdown in inline field editing (D6) and EntryEditScreen (E4).
     */
    fun getDistinctFieldValues(field: String): List<String> {
        val active = entryList.filter { it.deletedAt == null }
        val values: Sequence<String> = when (field) {
            "username" -> active.asSequence().flatMap {
                sequenceOf(it.username, it.email)
            }.filterNotNull()
            "email" -> active.asSequence().mapNotNull { it.email }
            "password" -> emptySequence()
            else -> emptySequence()
        }
        return values
            .map { it.trim() }
            .filter { it.isNotEmpty() }
            .groupingBy { it }
            .eachCount()
            .entries
            .sortedWith(compareByDescending<Map.Entry<String, Int>> { it.value }.thenBy { it.key })
            .map { it.key }
    }

Frequency-sorted as the brief (§14) requires. "password" deliberately returns empty — never suggest passwords. "email" is provided for completeness; D6 only calls "username".


5. Risks / assumptions