INTEGRATION.md

9.8 KB · raw

rendered · source

1# D3: Tags UI — Integration23UI-only. Data model + repo are already landed and verified:45- `VaultEntry.tags: List<String>? = null` (VaultEntry.kt:28)6- `VaultRepository.getActiveEntries(tagFilter: String? = null)` (VaultRepository.kt:347)7- `VaultRepository.getDistinctTags(): List<String>` (VaultRepository.kt:355)89## Files in this draft1011| Draft file | Destination | Composables |12|---|---|---|13| `TagChipEditor.kt` | `app/src/main/java/com/peachpasswords/ui/vault/TagChipEditor.kt` | `TagChipEditor`, internal `normalizeTag` |14| `TagListWidgets.kt` | `app/src/main/java/com/peachpasswords/ui/vault/TagListWidgets.kt` | `TagFilterStrip`, `EntryRowTags` |15| `DetailTagRow.kt` | `app/src/main/java/com/peachpasswords/ui/vault/DetailTagRow.kt` | `DetailTagRow` |1617All three are `package com.peachpasswords.ui.vault` and `internal`, so they18drop in as new files with no import churn in the target screens.1920> **FlowRow note:** Used in `TagChipEditor` and `DetailTagRow`, annotated21> `@OptIn(ExperimentalLayoutApi::class)`. `FlowRow` is `androidx.compose.foundation.layout.FlowRow`22> and is present in compose-bom 2024.06.00 (already on classpath). No new23> dependency. If a build warning about the opt-in surfaces elsewhere, it is24> contained to these two files.2526## Constraints (enforced in `TagChipEditor` / `normalizeTag`)2728- Max **10** tags per entry.29- Max **32** chars per tag.30- Lowercase, trimmed.31- De-duplicated (case-insensitive — input is already lowered).32- Commit triggers: space character or IME Done/enter.33- At limit: input field hidden, `"Max 10 tags"` hint shown.3435`normalizeTag` is `internal` so the orchestrator can reuse it in36`EntryEditViewModel.addTag` to keep storage canonical.3738---3940## 1. EntryEditViewModel.kt — form state + setters4142### 1a. Add `tags` to `EditForm`4344Anchor (EntryEditViewModel.kt:30, inside `data class EditForm`):4546```kotlin47    val notes: String = "",48```4950Add immediately after:5152```kotlin53    val tags: List<String> = emptyList(),54```5556### 1b. Hydrate from existing entry5758Anchor (EntryEditViewModel.kt:64-65):5960```kotlin61                notes = it.notes.orEmpty(),62                history = it.passwordHistory.orEmpty(),63```6465Add a line between them (or after `notes`):6667```kotlin68                tags = it.tags.orEmpty(),69```7071### 1c. Add setters (mirror `setNotes` at line 78)7273Place after `fun setNotes(...)`:7475```kotlin76    fun addTag(raw: String) = _form.update { f ->77        val tag = raw.trim().lowercase().take(32)78        if (tag.isEmpty() || f.tags.size >= 10 || f.tags.any { it == tag }) f79        else f.copy(tags = f.tags + tag)80    }81    fun removeTag(tag: String) = _form.update { it.copy(tags = it.tags - tag) }82```8384> The `TagChipEditor` already normalizes/dedupes before calling `onAdd`, but the85> VM re-guards so the invariant holds regardless of caller.8687### 1d. Expose distinct-tag suggestions8889Add a property (sibling style to other VM state; `vault` is already injected):9091```kotlin92    val allTags: List<String> = vault.getDistinctTags()93```9495If a live flow is preferred over a snapshot, wrap it; a snapshot at VM96construction is sufficient for autocomplete.9798### 1e. Persist on save — `addEntry` branch99100Anchor (EntryEditViewModel.kt:129):101102```kotlin103                            notes = f.notes.ifBlank { null },104                            createdAt = 0, modifiedAt = 0,105```106107Add between them:108109```kotlin110                            tags = f.tags.ifEmpty { null },111```112113### 1f. Persist on save — `updateEntry` branch114115Anchor (EntryEditViewModel.kt:142):116117```kotlin118                            notes = f.notes.ifBlank { null },119                        ),120```121122Add before the closing paren:123124```kotlin125                            tags = f.tags.ifEmpty { null },126```127128---129130## 2. EntryEditScreen.kt — tag chip editor131132Insert the editor **below the NOTES field**. Anchor (EntryEditScreen.kt:130):133134```kotlin135                Field("notes", f.notes, vm::setNotes, singleLine = false)136```137138Add immediately after that line:139140```kotlin141                TagChipEditor(142                    tags = f.tags,143                    allTags = vm.allTags,144                    onAdd = vm::addTag,145                    onRemove = vm::removeTag,146                )147```148149No new imports needed (same package).150151---152153## 3. VaultListViewModel.kt — activeTagFilter state154155### 3a. Add the StateFlow + setter156157Anchor (VaultListViewModel.kt:34-35):158159```kotlin160    private val _search = MutableStateFlow("")161    val search: StateFlow<String> = _search.asStateFlow()162```163164Add after:165166```kotlin167    private val _tagFilter = MutableStateFlow<String?>(null)168    val activeTagFilter: StateFlow<String?> = _tagFilter.asStateFlow()169```170171Add the setter near `setSearch` (line 53):172173```kotlin174    fun setTagFilter(tag: String?) { _tagFilter.value = tag }175```176177### 3b. Expose distinct tags for the strip178179Add (snapshot is fine; recomputed on each VM creation):180181```kotlin182    val allTags: StateFlow<List<String>> =183        vault.entries.map { vault.getDistinctTags() }184            .stateIn(viewModelScope, SharingStarted.WhileSubscribed(5000), emptyList())185```186187`vault.entries`, `map`, `stateIn`, `SharingStarted`, `viewModelScope` are all188already imported in this file.189190### 3c. Fold the tag filter into the entries flow191192The current `entries` flow combines `vault.entries, _sort, _search`193(VaultListViewModel.kt:43-46). `combine` with four sources is fine:194195```kotlin196    val entries: StateFlow<List<VaultEntry>> =197        combine(vault.entries, _sort, _search, _tagFilter) { list, sort, query, tag ->198            val base = if (tag == null) list199                       else list.filter { e -> e.tags?.any { it.equals(tag, ignoreCase = true) } == true }200            applyFilterSort(base, sort, query)201        }.stateIn(viewModelScope, SharingStarted.WhileSubscribed(5000), emptyList())202```203204> Filtering inline against `vault.entries` (rather than calling205> `vault.getActiveEntries(tag)`) keeps the reactive flow intact — `getActiveEntries`206> is a synchronous snapshot and would not re-emit. The filter logic matches207> `getActiveEntries`'s case-insensitive `tags.any` semantics exactly. If the208> orchestrator prefers the repo method, it must still be invoked inside the209> `combine` block (on the `_tagFilter` trigger) so emissions stay live.210211---212213## 4. VaultListScreen.kt — filter strip + row chips214215### 4a. Collect new state216217Anchor (VaultListScreen.kt:78):218219```kotlin220    val flashId by vm.flashId.collectAsState()221```222223Add after:224225```kotlin226    val allTags by vm.allTags.collectAsState()227    val activeTagFilter by vm.activeTagFilter.collectAsState()228```229230### 4b. Insert the strip between SearchBar and SortBar231232Anchor (VaultListScreen.kt:86-96): the `SearchBar(...)` call is followed by the233`SortBar(...)` call. Insert between them:234235```kotlin236            SearchBar(237                value = search,238                onChange = vm::setSearch,239                count = if (search.isNotBlank()) "${entries.size}/$total" else "$total",240            )241            if (allTags.isNotEmpty()) {242                TagFilterStrip(243                    allTags = allTags,244                    activeTagFilter = activeTagFilter,245                    onSelect = vm::setTagFilter,246                )247            }248            SortBar(249```250251### 4c. Row chips below the domain line252253`EntryRow` renders the title + meta in a `Column(Modifier.weight(1f))`254(VaultListScreen.kt:290-305). Anchor — the close of the meta `if` block:255256```kotlin257            if (meta.isNotEmpty()) {258                Text(259                    text = meta,260                    color = PeachColors.T2,261                    style = TextStyle(fontFamily = PeachType.Mono, fontSize = 9.sp),262                    maxLines = 1,263                )264            }265```266267Add immediately after that `if` block, still inside the `Column`:268269```kotlin270            entry.tags?.takeIf { it.isNotEmpty() }?.let { tags ->271                Spacer(Modifier.height(4.dp))272                EntryRowTags(tags)273            }274```275276`Spacer` and `Modifier.height` are already imported in VaultListScreen.kt.277278---279280## 5. EntryDetailScreen.kt — read-only tag row281282Insert below the NOTES field. Anchor (EntryDetailScreen.kt:229-231):283284```kotlin285                e.notes?.takeIf { it.isNotBlank() }?.let {286                    FieldRow("notes", it, copiedField == "notes", mono = false) { vm.copy("notes", it) }287                }288```289290Add immediately after that `let` block:291292```kotlin293                e.tags?.takeIf { it.isNotEmpty() }?.let { tags ->294                    DetailTagRow(295                        tags = tags,296                        onEditTags = { onNavigate(PeachNav.EntryEdit(e.id)) },297                    )298                }299```300301`onNavigate` and `PeachNav` are already in scope/imported in EntryDetailScreen.kt.302303> The brief asks the "+" chip to focus the editor on the tags field. There is no304> existing deep-link/focus-target mechanism in `EntryEditScreen` or305> `PeachNav.EntryEdit` (which carries only `id: String?`). This draft navigates to306> the editor (tags section is already visible at the bottom of the form). Adding307> true field-focus would require a new nav arg + a focus requester in308> EntryEditScreen — out of scope for UI-only; flagged as a follow-up.309310---311312## Assumptions / risks313314- `getDistinctTags()` / `getActiveEntries()` are synchronous snapshots over the315  in-memory `entryList`; for the **list filter** the integration re-implements the316  filter inside the reactive `combine` so the UI updates live (see §3c rationale).317- The brief's pseudocode names (`setTagFilter`, `activeTagFilter`, `addTag`,318  `removeTag`, `allTags`) do not yet exist — all are introduced by this319  integration (§1, §3).320- No `PeachField` / `FlowRow` existed in the vault screens before; the editor uses321  the existing `BasicTextField` underline-less bordered style local to these322  drafts (matching the `Field` box style) and `FlowRow` from foundation.323- Detail "+" chip navigates to the editor without field focus (see §5 note).