package com.peachpasswords.ui.settings import androidx.compose.animation.AnimatedVisibility import androidx.compose.foundation.background import androidx.compose.foundation.border import androidx.compose.foundation.clickable import androidx.compose.foundation.layout.Arrangement import androidx.compose.foundation.layout.Box import androidx.compose.foundation.layout.Column import androidx.compose.foundation.layout.Row import androidx.compose.foundation.layout.Spacer import androidx.compose.foundation.layout.fillMaxWidth import androidx.compose.foundation.layout.height import androidx.compose.foundation.layout.padding import androidx.compose.foundation.layout.size import androidx.compose.foundation.layout.width import androidx.compose.foundation.shape.CircleShape import androidx.compose.foundation.shape.RoundedCornerShape import androidx.compose.foundation.text.BasicTextField import androidx.compose.material3.Text import androidx.compose.runtime.Composable import androidx.compose.runtime.collectAsState import androidx.compose.runtime.getValue import androidx.compose.runtime.mutableStateOf import androidx.compose.runtime.remember import androidx.compose.runtime.rememberCoroutineScope import androidx.compose.runtime.setValue import androidx.compose.ui.Alignment import androidx.compose.ui.Modifier import androidx.compose.ui.draw.clip import androidx.compose.ui.graphics.Color import androidx.compose.ui.graphics.SolidColor import androidx.compose.ui.text.TextStyle import androidx.compose.ui.text.input.PasswordVisualTransformation import androidx.compose.ui.unit.dp import androidx.compose.ui.unit.sp import com.peachpasswords.ui.theme.PeachColors import com.peachpasswords.ui.theme.PeachType import com.peachpasswords.vault.DuressAction import kotlinx.coroutines.launch /** * D13 — Duress password setup, rendered inside ProtectionBody under the * "DURESS PASSWORD" sub-label. Self-contained so it can drop into the existing * Protection card without restructuring it. * * Pro gate is the caller's responsibility: ProtectionBody renders the static * ProBadge row when !isPro and this composable only when isPro (see * INTEGRATION.md). All visuals use the card foreground [fg] like sibling rows. * * Relies on these row helpers already private in SettingsCardBodies.kt: * [RowLine], [RowScope.Name], [Toggle], [SubtleButton], [Hint], [PwField]. * Because this lives in the same package it can call them directly once merged. */ @Composable internal fun DuressSection(fg: Color, vm: SettingsViewModel) { val configured by vm.duressConfigured.collectAsState() if (configured) { DuressActiveState(fg, vm) } else { DuressForm(fg, vm) } } @Composable private fun DuressActiveState(fg: Color, vm: SettingsViewModel) { RowLine(fg) { Name("Duress password is active", fg) } Text( "Duress password is active", color = PeachColors.HealthOk, fontFamily = PeachType.Mono, fontSize = 11.sp, modifier = Modifier.padding(top = 6.dp), ) Spacer(Modifier.height(8.dp)) DangerGhostButton("Disable duress password", fg) { vm.disableDuress() } Hint("Entering your duress password instead of your real one triggers the action you chose.", fg) } @Composable private fun DuressForm(fg: Color, vm: SettingsViewModel) { var enabled by remember { mutableStateOf(false) } var password by remember { mutableStateOf("") } var confirm by remember { mutableStateOf("") } var action by remember { mutableStateOf(DuressAction.DECOY) } var error by remember { mutableStateOf(null) } val scope = rememberCoroutineScope() RowLine(fg) { Name("Duress password", fg) Toggle(enabled, fg) { on -> enabled = on if (!on) { password = ""; confirm = ""; error = null } } } Text( "Entering your duress password instead of your real one opens a decoy vault or wipes the real one.", color = fg.copy(alpha = 0.7f), fontFamily = PeachType.Mono, fontSize = 10.sp, modifier = Modifier.padding(top = 6.dp), ) AnimatedVisibility(enabled) { Column(Modifier.fillMaxWidth().padding(top = 10.dp), verticalArrangement = Arrangement.spacedBy(6.dp)) { PwField("DURESS PASSWORD", password, fg) { password = it; error = null } PwField("CONFIRM DURESS PASSWORD", confirm, fg) { confirm = it; error = null } Text( "When your duress password is entered at the lock screen, Peach will:", color = fg.copy(alpha = 0.7f), fontFamily = PeachType.Mono, fontSize = 10.sp, modifier = Modifier.padding(top = 4.dp), ) RadioOption("Show a decoy vault", action == DuressAction.DECOY, fg) { action = DuressAction.DECOY } RadioOption("Destroy the real vault", action == DuressAction.DESTROY, fg) { action = DuressAction.DESTROY } error?.let { Text(it, color = PeachColors.Breach, fontFamily = PeachType.Mono, fontSize = 10.sp) } val canSave = password.isNotBlank() && confirm.isNotBlank() SaveButton("Save duress password", fg, canSave) { error = when { password.isBlank() || confirm.isBlank() -> "Enter and confirm a duress password" password != confirm -> "Passwords do not match" else -> null } if (error == null) scope.launch { if (vm.saveDuress(password, action)) { password = ""; confirm = ""; enabled = false } else { error = "Couldn't save duress password" } } } } } } /* ── local controls (kept private to D13; the radio + variants don't exist in the shared row vocabulary) ─────────────────────────────────────────────── */ @Composable private fun RadioOption(label: String, selected: Boolean, fg: Color, onClick: () -> Unit) { val accent = com.peachpasswords.ui.theme.LocalPeachAccent.current Row( Modifier.fillMaxWidth().clip(RoundedCornerShape(8.dp)).clickable(onClick = onClick) .padding(vertical = 7.dp), verticalAlignment = Alignment.CenterVertically, horizontalArrangement = Arrangement.spacedBy(10.dp), ) { Box( Modifier.size(16.dp).clip(CircleShape) .border(1.dp, if (selected) accent else fg.copy(alpha = 0.4f), CircleShape), contentAlignment = Alignment.Center, ) { if (selected) Box(Modifier.size(8.dp).clip(CircleShape).background(accent)) } Text( label, color = if (selected) accent else fg, fontFamily = PeachType.Mono, fontSize = 11.sp, ) } } @Composable private fun SaveButton(label: String, fg: Color, enabled: Boolean, onClick: () -> Unit) { val accent = com.peachpasswords.ui.theme.LocalPeachAccent.current val tint = if (enabled) accent else fg.copy(alpha = 0.3f) Box( Modifier.fillMaxWidth().clip(RoundedCornerShape(8.dp)) .border(1.dp, tint.copy(alpha = if (enabled) 0.6f else 0.3f), RoundedCornerShape(8.dp)) .background(tint.copy(alpha = if (enabled) 0.16f else 0.06f)) .let { if (enabled) it.clickable(onClick = onClick) else it } .padding(vertical = 11.dp), contentAlignment = Alignment.Center, ) { Text(label, color = if (enabled) accent else fg.copy(alpha = 0.4f), fontFamily = PeachType.Mono, fontSize = 11.sp) } } @Composable private fun DangerGhostButton(label: String, fg: Color, onClick: () -> Unit) { Box( Modifier.fillMaxWidth().clip(RoundedCornerShape(8.dp)) .border(1.dp, PeachColors.Breach.copy(alpha = 0.5f), RoundedCornerShape(8.dp)) .clickable(onClick = onClick) .padding(vertical = 11.dp), contentAlignment = Alignment.Center, ) { Text(label, color = PeachColors.Breach, fontFamily = PeachType.Mono, fontSize = 11.sp) } }