# D3: Tags UI — Integration UI-only. Data model + repo are already landed and verified: - `VaultEntry.tags: List? = null` (VaultEntry.kt:28) - `VaultRepository.getActiveEntries(tagFilter: String? = null)` (VaultRepository.kt:347) - `VaultRepository.getDistinctTags(): List` (VaultRepository.kt:355) ## Files in this draft | Draft file | Destination | Composables | |---|---|---| | `TagChipEditor.kt` | `app/src/main/java/com/peachpasswords/ui/vault/TagChipEditor.kt` | `TagChipEditor`, internal `normalizeTag` | | `TagListWidgets.kt` | `app/src/main/java/com/peachpasswords/ui/vault/TagListWidgets.kt` | `TagFilterStrip`, `EntryRowTags` | | `DetailTagRow.kt` | `app/src/main/java/com/peachpasswords/ui/vault/DetailTagRow.kt` | `DetailTagRow` | All three are `package com.peachpasswords.ui.vault` and `internal`, so they drop in as new files with no import churn in the target screens. > **FlowRow note:** Used in `TagChipEditor` and `DetailTagRow`, annotated > `@OptIn(ExperimentalLayoutApi::class)`. `FlowRow` is `androidx.compose.foundation.layout.FlowRow` > and is present in compose-bom 2024.06.00 (already on classpath). No new > dependency. If a build warning about the opt-in surfaces elsewhere, it is > contained to these two files. ## Constraints (enforced in `TagChipEditor` / `normalizeTag`) - Max **10** tags per entry. - Max **32** chars per tag. - Lowercase, trimmed. - De-duplicated (case-insensitive — input is already lowered). - Commit triggers: space character or IME Done/enter. - At limit: input field hidden, `"Max 10 tags"` hint shown. `normalizeTag` is `internal` so the orchestrator can reuse it in `EntryEditViewModel.addTag` to keep storage canonical. --- ## 1. EntryEditViewModel.kt — form state + setters ### 1a. Add `tags` to `EditForm` Anchor (EntryEditViewModel.kt:30, inside `data class EditForm`): ```kotlin val notes: String = "", ``` Add immediately after: ```kotlin val tags: List = emptyList(), ``` ### 1b. Hydrate from existing entry Anchor (EntryEditViewModel.kt:64-65): ```kotlin notes = it.notes.orEmpty(), history = it.passwordHistory.orEmpty(), ``` Add a line between them (or after `notes`): ```kotlin tags = it.tags.orEmpty(), ``` ### 1c. Add setters (mirror `setNotes` at line 78) Place after `fun setNotes(...)`: ```kotlin fun addTag(raw: String) = _form.update { f -> val tag = raw.trim().lowercase().take(32) if (tag.isEmpty() || f.tags.size >= 10 || f.tags.any { it == tag }) f else f.copy(tags = f.tags + tag) } fun removeTag(tag: String) = _form.update { it.copy(tags = it.tags - tag) } ``` > The `TagChipEditor` already normalizes/dedupes before calling `onAdd`, but the > VM re-guards so the invariant holds regardless of caller. ### 1d. Expose distinct-tag suggestions Add a property (sibling style to other VM state; `vault` is already injected): ```kotlin val allTags: List = vault.getDistinctTags() ``` If a live flow is preferred over a snapshot, wrap it; a snapshot at VM construction is sufficient for autocomplete. ### 1e. Persist on save — `addEntry` branch Anchor (EntryEditViewModel.kt:129): ```kotlin notes = f.notes.ifBlank { null }, createdAt = 0, modifiedAt = 0, ``` Add between them: ```kotlin tags = f.tags.ifEmpty { null }, ``` ### 1f. Persist on save — `updateEntry` branch Anchor (EntryEditViewModel.kt:142): ```kotlin notes = f.notes.ifBlank { null }, ), ``` Add before the closing paren: ```kotlin tags = f.tags.ifEmpty { null }, ``` --- ## 2. EntryEditScreen.kt — tag chip editor Insert the editor **below the NOTES field**. Anchor (EntryEditScreen.kt:130): ```kotlin Field("notes", f.notes, vm::setNotes, singleLine = false) ``` Add immediately after that line: ```kotlin TagChipEditor( tags = f.tags, allTags = vm.allTags, onAdd = vm::addTag, onRemove = vm::removeTag, ) ``` No new imports needed (same package). --- ## 3. VaultListViewModel.kt — activeTagFilter state ### 3a. Add the StateFlow + setter Anchor (VaultListViewModel.kt:34-35): ```kotlin private val _search = MutableStateFlow("") val search: StateFlow = _search.asStateFlow() ``` Add after: ```kotlin private val _tagFilter = MutableStateFlow(null) val activeTagFilter: StateFlow = _tagFilter.asStateFlow() ``` Add the setter near `setSearch` (line 53): ```kotlin fun setTagFilter(tag: String?) { _tagFilter.value = tag } ``` ### 3b. Expose distinct tags for the strip Add (snapshot is fine; recomputed on each VM creation): ```kotlin val allTags: StateFlow> = vault.entries.map { vault.getDistinctTags() } .stateIn(viewModelScope, SharingStarted.WhileSubscribed(5000), emptyList()) ``` `vault.entries`, `map`, `stateIn`, `SharingStarted`, `viewModelScope` are all already imported in this file. ### 3c. Fold the tag filter into the entries flow The current `entries` flow combines `vault.entries, _sort, _search` (VaultListViewModel.kt:43-46). `combine` with four sources is fine: ```kotlin val entries: StateFlow> = combine(vault.entries, _sort, _search, _tagFilter) { list, sort, query, tag -> val base = if (tag == null) list else list.filter { e -> e.tags?.any { it.equals(tag, ignoreCase = true) } == true } applyFilterSort(base, sort, query) }.stateIn(viewModelScope, SharingStarted.WhileSubscribed(5000), emptyList()) ``` > Filtering inline against `vault.entries` (rather than calling > `vault.getActiveEntries(tag)`) keeps the reactive flow intact — `getActiveEntries` > is a synchronous snapshot and would not re-emit. The filter logic matches > `getActiveEntries`'s case-insensitive `tags.any` semantics exactly. If the > orchestrator prefers the repo method, it must still be invoked inside the > `combine` block (on the `_tagFilter` trigger) so emissions stay live. --- ## 4. VaultListScreen.kt — filter strip + row chips ### 4a. Collect new state Anchor (VaultListScreen.kt:78): ```kotlin val flashId by vm.flashId.collectAsState() ``` Add after: ```kotlin val allTags by vm.allTags.collectAsState() val activeTagFilter by vm.activeTagFilter.collectAsState() ``` ### 4b. Insert the strip between SearchBar and SortBar Anchor (VaultListScreen.kt:86-96): the `SearchBar(...)` call is followed by the `SortBar(...)` call. Insert between them: ```kotlin SearchBar( value = search, onChange = vm::setSearch, count = if (search.isNotBlank()) "${entries.size}/$total" else "$total", ) if (allTags.isNotEmpty()) { TagFilterStrip( allTags = allTags, activeTagFilter = activeTagFilter, onSelect = vm::setTagFilter, ) } SortBar( ``` ### 4c. Row chips below the domain line `EntryRow` renders the title + meta in a `Column(Modifier.weight(1f))` (VaultListScreen.kt:290-305). Anchor — the close of the meta `if` block: ```kotlin if (meta.isNotEmpty()) { Text( text = meta, color = PeachColors.T2, style = TextStyle(fontFamily = PeachType.Mono, fontSize = 9.sp), maxLines = 1, ) } ``` Add immediately after that `if` block, still inside the `Column`: ```kotlin entry.tags?.takeIf { it.isNotEmpty() }?.let { tags -> Spacer(Modifier.height(4.dp)) EntryRowTags(tags) } ``` `Spacer` and `Modifier.height` are already imported in VaultListScreen.kt. --- ## 5. EntryDetailScreen.kt — read-only tag row Insert below the NOTES field. Anchor (EntryDetailScreen.kt:229-231): ```kotlin e.notes?.takeIf { it.isNotBlank() }?.let { FieldRow("notes", it, copiedField == "notes", mono = false) { vm.copy("notes", it) } } ``` Add immediately after that `let` block: ```kotlin e.tags?.takeIf { it.isNotEmpty() }?.let { tags -> DetailTagRow( tags = tags, onEditTags = { onNavigate(PeachNav.EntryEdit(e.id)) }, ) } ``` `onNavigate` and `PeachNav` are already in scope/imported in EntryDetailScreen.kt. > The brief asks the "+" chip to focus the editor on the tags field. There is no > existing deep-link/focus-target mechanism in `EntryEditScreen` or > `PeachNav.EntryEdit` (which carries only `id: String?`). This draft navigates to > the editor (tags section is already visible at the bottom of the form). Adding > true field-focus would require a new nav arg + a focus requester in > EntryEditScreen — out of scope for UI-only; flagged as a follow-up. --- ## Assumptions / risks - `getDistinctTags()` / `getActiveEntries()` are synchronous snapshots over the in-memory `entryList`; for the **list filter** the integration re-implements the filter inside the reactive `combine` so the UI updates live (see §3c rationale). - The brief's pseudocode names (`setTagFilter`, `activeTagFilter`, `addTag`, `removeTag`, `allTags`) do not yet exist — all are introduced by this integration (§1, §3). - No `PeachField` / `FlowRow` existed in the vault screens before; the editor uses the existing `BasicTextField` underline-less bordered style local to these drafts (matching the `Field` box style) and `FlowRow` from foundation. - Detail "+" chip navigates to the editor without field focus (see §5 note).