EntryDetailGenerator.kt

6.7 KB · raw

1package com.peachpasswords.ui.vault23/*4 * D4 drafts — generator access from EntryDetailScreen.5 *6 * Provides:7 *   - WrenchButton: a 18dp wrench icon to place beside the password copy button.8 *   - GeneratorSheet: a ModalBottomSheet wrapping the shared GeneratorBody. It9 *     holds LOCAL generator option + draft-password state, never auto-saves, and10 *     exposes [Copy] (drain) + [Use this password] (accent). "Use this password"11 *     calls onUse(draft) which the screen wires to the VM's setPassword path.12 *13 * This is a DRAFT. The orchestrator integrates per INTEGRATION.md: it must add a14 * `setPassword`/`updateEntry` method to EntryDetailViewModel (see INTEGRATION.md)15 * and a `copy(...)`-backed copied latch for the sheet's draft.16 */1718import androidx.compose.foundation.background19import androidx.compose.foundation.border20import androidx.compose.foundation.clickable21import androidx.compose.foundation.layout.Box22import androidx.compose.foundation.layout.Column23import androidx.compose.foundation.layout.Spacer24import androidx.compose.foundation.layout.fillMaxWidth25import androidx.compose.foundation.layout.height26import androidx.compose.foundation.layout.padding27import androidx.compose.foundation.layout.size28import androidx.compose.foundation.shape.CircleShape29import androidx.compose.foundation.shape.RoundedCornerShape30import androidx.compose.material.icons.Icons31import androidx.compose.material.icons.outlined.Build32import androidx.compose.material3.ExperimentalMaterial3Api33import androidx.compose.material3.Icon34import androidx.compose.material3.ModalBottomSheet35import androidx.compose.material3.Text36import androidx.compose.material3.rememberModalBottomSheetState37import androidx.compose.runtime.Composable38import androidx.compose.runtime.getValue39import androidx.compose.runtime.mutableStateOf40import androidx.compose.runtime.remember41import androidx.compose.runtime.setValue42import androidx.compose.ui.Alignment43import androidx.compose.ui.Modifier44import androidx.compose.ui.draw.clip45import androidx.compose.ui.text.font.FontStyle46import androidx.compose.ui.unit.dp47import androidx.compose.ui.unit.sp48import com.peachpasswords.ui.theme.LocalPeachAccent49import com.peachpasswords.ui.theme.PeachColors50import com.peachpasswords.ui.theme.PeachType51import com.peachpasswords.vault.PasswordGenerator5253/** Wrench icon (spec: tint t2, 18dp) — opens the generator sheet from the detail password row. */54@Composable55internal fun WrenchButton(onClick: () -> Unit) {56    Icon(57        Icons.Outlined.Build,58        contentDescription = "generate password",59        tint = PeachColors.T2,60        modifier = Modifier61            .size(36.dp)62            .clip(CircleShape)63            .clickable(onClick = onClick)64            .padding(9.dp), // 36 - 2*9 = 18dp glyph65    )66}6768/**69 * Generator bottom sheet for EntryDetailScreen. Local-only: generating populates70 * a draft password (never persisted). The user copies it (drain) or commits it71 * via [Use this password], which fires onUse(draft) and dismisses.72 *73 * @param copied        latch for the [Copy] drain animation (driven by the screen/VM).74 * @param onCopy        copy the current draft to clipboard (sets the latch).75 * @param onUse         commit the draft password (VM updateEntry + history capture).76 * @param onDismiss     close the sheet without committing.77 */78@OptIn(ExperimentalMaterial3Api::class)79@Composable80internal fun GeneratorSheet(81    copied: Boolean,82    onCopy: (String) -> Unit,83    onUse: (String) -> Unit,84    onDismiss: () -> Unit,85) {86    val accent = LocalPeachAccent.current87    val sheetState = rememberModalBottomSheetState(skipPartiallyExpanded = true)8889    var length by remember { mutableStateOf(20) }90    var upper by remember { mutableStateOf(true) }91    var lower by remember { mutableStateOf(true) }92    var numbers by remember { mutableStateOf(true) }93    var symbols by remember { mutableStateOf(true) }94    var draft by remember { mutableStateOf("") }9596    fun gen() {97        draft = PasswordGenerator.generate(98            PasswordGenerator.Options(99                length = length,100                includeUppercase = upper,101                includeLowercase = lower,102                includeNumbers = numbers,103                includeSymbols = symbols,104            ),105        )106    }107108    ModalBottomSheet(109        onDismissRequest = onDismiss,110        sheetState = sheetState,111        containerColor = PeachColors.S1,112    ) {113        Column(Modifier.fillMaxWidth().padding(horizontal = 20.dp, vertical = 4.dp)) {114            Text(115                "generate password",116                color = PeachColors.T0,117                fontFamily = PeachType.Display, fontStyle = FontStyle.Italic, fontSize = 18.sp,118            )119            Spacer(Modifier.height(8.dp))120121            // Draft preview — empty until first generate.122            Box(123                Modifier.fillMaxWidth().clip(RoundedCornerShape(10.dp))124                    .border(1.dp, PeachColors.T3, RoundedCornerShape(10.dp))125                    .padding(horizontal = 12.dp, vertical = 14.dp),126            ) {127                Text(128                    text = draft.ifEmpty { "tap generate" },129                    color = if (draft.isEmpty()) PeachColors.T2 else PeachColors.T0,130                    fontFamily = PeachType.Mono, fontSize = 13.sp,131                )132            }133            Spacer(Modifier.height(6.dp))134135            // Shared generator controls + [Copy] drain button on the generate row.136            GeneratorBody(137                length = length,138                upper = upper, lower = lower, numbers = numbers, symbols = symbols,139                password = draft, copied = copied,140                onLength = { length = it },141                onUpper = { upper = it }, onLower = { lower = it },142                onNumbers = { numbers = it }, onSymbols = { symbols = it },143                onGenerate = { gen() },144                onCopy = { if (draft.isNotEmpty()) onCopy(draft) },145            )146147            // [Use this password] — accent button, commits + closes. No-op while empty.148            Box(149                Modifier.fillMaxWidth().clip(RoundedCornerShape(10.dp))150                    .background(if (draft.isEmpty()) accent.copy(alpha = 0.10f) else accent.copy(alpha = 0.18f))151                    .border(1.dp, accent.copy(alpha = 0.5f), RoundedCornerShape(10.dp))152                    .clickable(enabled = draft.isNotEmpty()) { onUse(draft) }153                    .padding(vertical = 14.dp),154                contentAlignment = Alignment.Center,155            ) {156                Text(157                    "use this password",158                    color = if (draft.isEmpty()) accent.copy(alpha = 0.5f) else accent,159                    fontFamily = PeachType.Mono, fontSize = 12.sp,160                )161            }162            Spacer(Modifier.height(20.dp))163        }164    }165}