# D4 (Remainder) — Integration Guide Two features, plus one shared-file extraction: - **D4-a** [Copy] drain button in `GeneratorDrawer` (EntryEditScreen). - **D4-b** Wrench → `GeneratorDrawer` as a `ModalBottomSheet` in EntryDetailScreen, producing a local draft password with [Copy] (drain) + [Use this password]. - **D4-0** Extract the private `CopyButton` from EntryDetailScreen to a shared file so both surfaces can reuse the exact spec §4.4 sheen+drain animation. Drafts in this folder: | Draft file | Destination | |---|---| | `CopyButton.kt` | `app/src/main/java/com/peachpasswords/ui/components/CopyButton.kt` (NEW) | | `GeneratorDrawerWithCopy.kt` | merge into `ui/vault/EntryEditScreen.kt` (replace `GeneratorDrawer`, add `GeneratorBody`) | | `EntryDetailGenerator.kt` | merge into `ui/vault/EntryDetailScreen.kt` (`WrenchButton`, `GeneratorSheet`) | All drafts already declare their final package (`com.peachpasswords.ui.components` or `com.peachpasswords.ui.vault`), so a plain move/merge compiles. --- ## D4-0 — Extract CopyButton (do this FIRST) `CopyButton` is currently a `private @Composable` at the bottom of `EntryDetailScreen.kt` (lines ~359–416). It is used by `FieldRow`, `PasswordRow`, and `TotpBand` in that file. ### Steps 1. Drop `CopyButton.kt` in at `ui/components/CopyButton.kt`. It is the identical body, package `com.peachpasswords.ui.components`, visibility changed `private` → public `fun CopyButton(...)`. The KDoc is preserved. 2. In `EntryDetailScreen.kt`, **delete** the private `CopyButton` composable (the whole block from its KDoc `/** Copy button with the two-effect ... */` through its closing brace, lines ~359–416). 3. Add the import to `EntryDetailScreen.kt` (alphabetical, beside the other `com.peachpasswords.ui.components.*` imports — there are already `CharEnterText`, `FaviconInitials`, `TOTPRing`): ```kotlin import com.peachpasswords.ui.components.CopyButton ``` Anchor (existing imports, lines ~63–65): ```kotlin import com.peachpasswords.ui.components.CharEnterText import com.peachpasswords.ui.components.FaviconInitials import com.peachpasswords.ui.components.TOTPRing ``` 4. After deleting the private copy, the following imports in `EntryDetailScreen.kt` become unused **only if** nothing else in the file uses them. `TotpBand` still uses `animateFloat`, `Brush`, etc., so re-check; the ones the deleted `CopyButton` *uniquely* relied on are `Icons.Rounded.Check`, `Icons.Rounded.ContentCopy` (lines 30–31). Verify with the compiler / lint and remove any that are now dangling. (Leave anything `TotpBand`/`PasswordRow` use.) No behaviour change — same composable, same call sites. --- ## D4-a — [Copy] drain button in the inline GeneratorDrawer ### VM additions (EntryEditViewModel) The drawer's [Copy] button needs (1) a copied latch for the drain animation and (2) a copy action. The VM already has `copyHistoryPassword` (clipboard) but no latch. Add a `genCopied` flag to `EditForm` and a `copyGenerated()`: ```kotlin // in EditForm: val genCopied: Boolean = false, ``` ```kotlin // in EntryEditViewModel: fun copyGenerated() { val pw = _form.value.password if (pw.isEmpty()) return val cm = appContext.getSystemService(Context.CLIPBOARD_SERVICE) as ClipboardManager cm.setPrimaryClip(ClipData.newPlainText("peach", pw)) viewModelScope.launch { _form.update { it.copy(genCopied = true) } kotlinx.coroutines.delay(1200) _form.update { it.copy(genCopied = false) } } } ``` (`ClipData`, `ClipboardManager`, `Context` are already imported in EntryEditViewModel.kt.) ### Composable merge (EntryEditScreen.kt) 1. Replace the entire private `GeneratorDrawer` composable (lines ~225–276) with the `GeneratorDrawer` + `GeneratorBody` from `GeneratorDrawerWithCopy.kt`. - The replacement `GeneratorDrawer` is `internal` (so the detail screen could reuse it too, though detail uses `GeneratorBody` directly). - **Delete** the duplicate `private fun TogglePill` at the bottom of `GeneratorDrawerWithCopy.kt` and keep EntryEditScreen's existing `TogglePill` (lines ~278–290). They are identical except corner radius: the existing one uses `PeachRadius.TypeTab`; the draft uses `8.dp`. **Keep the existing `PeachRadius.TypeTab` version** for consistency. 2. Add the import: ```kotlin import com.peachpasswords.ui.components.CopyButton ``` 3. Update the call site. Anchor (lines ~112–120): ```kotlin GeneratorDrawer( open = f.generatorOpen, length = f.genLength, upper = f.genUpper, lower = f.genLower, numbers = f.genNumbers, symbols = f.genSymbols, onLength = vm::setGenLength, onUpper = vm::setGenUpper, onLower = vm::setGenLower, onNumbers = vm::setGenNumbers, onSymbols = vm::setGenSymbols, onGenerate = vm::generate, ) ``` becomes: ```kotlin GeneratorDrawer( open = f.generatorOpen, length = f.genLength, upper = f.genUpper, lower = f.genLower, numbers = f.genNumbers, symbols = f.genSymbols, password = f.password, copied = f.genCopied, onLength = vm::setGenLength, onUpper = vm::setGenUpper, onLower = vm::setGenLower, onNumbers = vm::setGenNumbers, onSymbols = vm::setGenSymbols, onGenerate = vm::generate, onCopy = vm::copyGenerated, ) ``` Note: the drawer fixed height grew 188 → 234dp to fit the generate/copy row. --- ## D4-b — Generator in EntryDetailScreen (wrench + bottom sheet) ### VM addition (EntryDetailViewModel) Add a method that applies a new password via `updateEntry`. **Password history is captured automatically** by `VaultRepository.updateEntry` whenever the password value changes (see `VaultRepository.kt` lines ~268–290: it records the prior password into `passwordHistory`, capped at `PASSWORD_HISTORY_MAX`, skipping empty and dup priors). So we only pass `entry.copy(password = newPw)` — do NOT build history manually. ```kotlin fun applyGeneratedPassword(newPassword: String, onDone: () -> Unit = {}) { val current = entry.value ?: return if (newPassword.isEmpty() || newPassword == current.password) { onDone(); return } viewModelScope.launch { runCatching { vault.updateEntry(current.copy(password = newPassword)) } onDone() } } ``` The sheet's [Copy] uses the existing `copy(field, value)` + `copiedField` latch. Reuse a dedicated field name, e.g. `"genDraft"`: ```kotlin // already exists — just call with a distinct field id: vm.copy("genDraft", draftPassword) // and observe: copiedField == "genDraft" ``` No new copy method needed. (`updateEntry` is already exposed on the repo; `viewModelScope`, `launch`, `vault` are all in scope in EntryDetailViewModel.) ### Composable merge (EntryDetailScreen.kt) 1. Drop `WrenchButton` and `GeneratorSheet` from `EntryDetailGenerator.kt` into the file (or keep them in their own file in `ui/vault/` — same package, both `internal`, compiles either way). `GeneratorSheet` depends on `GeneratorBody` (from D4-a) — ensure D4-a is merged first. 2. Add imports to EntryDetailScreen.kt: ```kotlin import androidx.compose.runtime.mutableStateOf // already present import androidx.compose.runtime.remember // already present import androidx.compose.runtime.setValue // already present ``` (All three are already imported — nothing new needed for state. `GeneratorSheet` carries its own `ModalBottomSheet`/`Build`/`PasswordGenerator` imports in its file.) 3. Hoist sheet-open state in `EntryDetailScreen`. Anchor — beside the existing `confirmingDelete` state (line ~88): ```kotlin var confirmingDelete by remember { mutableStateOf(false) } ``` add: ```kotlin var generatorOpen by remember { mutableStateOf(false) } ``` 4. Add the wrench to the password row. The password value is rendered by `PasswordRow` (lines ~208–224 call site, composable ~258–280). Pass an `onGenerate` lambda through to it and render `WrenchButton` beside `CopyButton`. Call-site anchor (lines ~208–224): ```kotlin e.password?.takeIf { it.isNotBlank() }?.let { pw -> PasswordRow( value = pw, revealed = revealed, copied = copiedField == "password", onReveal = { ... }, onCopy = { vm.copy("password", pw) }, ) } ``` Add `onGenerate = { generatorOpen = true }` to the `PasswordRow(...)` call. In the `PasswordRow` composable (lines ~258–280), add the param `onGenerate: () -> Unit` and render the wrench between the show/hide text and the CopyButton: ```kotlin CopyButton(copied, onCopy) WrenchButton(onClick = onGenerate) // <-- add this line, after CopyButton ``` 5. Render the sheet (anywhere inside the `EntryDetailScreen` composable body, e.g. just before the closing of `ShellBackground { ... }`, after the scrolling Column at line ~234). Guard on `generatorOpen` and a non-null entry: ```kotlin if (generatorOpen && e != null) { GeneratorSheet( copied = copiedField == "genDraft", onCopy = { draft -> vm.copy("genDraft", draft) }, onUse = { draft -> vm.applyGeneratedPassword(draft) { generatorOpen = false } }, onDismiss = { generatorOpen = false }, ) } ``` Note `e` is captured as `val e = entry` at line ~97 — inside the `ShellBackground` lambda use the same `e`/`entry` you already have in scope. --- ## Summary of new public/internal API the orchestrator must add | Where | Member | Why | |---|---|---| | `ui/components/CopyButton.kt` | `fun CopyButton(copied, onCopy)` (public) | shared §4.4 drain button | | `EditForm` | `val genCopied: Boolean = false` | drain latch for inline drawer | | `EntryEditViewModel` | `fun copyGenerated()` | clipboard + latch for drawer [Copy] | | `EntryDetailViewModel` | `fun applyGeneratedPassword(newPassword, onDone)` | [Use this password] → updateEntry (auto history) | | `EntryDetailViewModel` | reuse `copy("genDraft", ...)` + `copiedField=="genDraft"` | sheet [Copy] drain | ## Risks / assumptions - **Icon**: `Icons.Outlined.Build` is a wrench glyph and is in `material-icons-extended` (already a dependency). If a closer wrench is desired, `Icons.Outlined.Construction` is also available. - **History capture is repo-driven**: do NOT hand-build `PasswordHistoryItem` in the detail path; `updateEntry` already records the prior password on change. Verified in `VaultRepository.kt` lines 268–290. - **Drawer height**: bumped to 234dp to fit the generate+copy row; tweak if the toggle row wraps on very small screens. - **TogglePill duplication**: the draft ships a local `TogglePill` so it compiles standalone — delete it on merge and keep EntryEditScreen's existing one (`PeachRadius.TypeTab` radius). - **ModalBottomSheet**: `@OptIn(ExperimentalMaterial3Api::class)` required on the caller (`GeneratorSheet` already annotates itself); pattern mirrors `AutofillEntryPickerActivity.kt`. - No nav routes added; the sheet is in-screen state only.