DuressSection.kt

8.1 KB · raw

1package com.peachpasswords.ui.settings23import androidx.compose.animation.AnimatedVisibility4import androidx.compose.foundation.background5import androidx.compose.foundation.border6import androidx.compose.foundation.clickable7import androidx.compose.foundation.layout.Arrangement8import androidx.compose.foundation.layout.Box9import androidx.compose.foundation.layout.Column10import androidx.compose.foundation.layout.Row11import androidx.compose.foundation.layout.Spacer12import androidx.compose.foundation.layout.fillMaxWidth13import androidx.compose.foundation.layout.height14import androidx.compose.foundation.layout.padding15import androidx.compose.foundation.layout.size16import androidx.compose.foundation.layout.width17import androidx.compose.foundation.shape.CircleShape18import androidx.compose.foundation.shape.RoundedCornerShape19import androidx.compose.foundation.text.BasicTextField20import androidx.compose.material3.Text21import androidx.compose.runtime.Composable22import androidx.compose.runtime.collectAsState23import androidx.compose.runtime.getValue24import androidx.compose.runtime.mutableStateOf25import androidx.compose.runtime.remember26import androidx.compose.runtime.rememberCoroutineScope27import androidx.compose.runtime.setValue28import androidx.compose.ui.Alignment29import androidx.compose.ui.Modifier30import androidx.compose.ui.draw.clip31import androidx.compose.ui.graphics.Color32import androidx.compose.ui.graphics.SolidColor33import androidx.compose.ui.text.TextStyle34import androidx.compose.ui.text.input.PasswordVisualTransformation35import androidx.compose.ui.unit.dp36import androidx.compose.ui.unit.sp37import com.peachpasswords.ui.theme.PeachColors38import com.peachpasswords.ui.theme.PeachType39import com.peachpasswords.vault.DuressAction40import kotlinx.coroutines.launch4142/**43 * D13 — Duress password setup, rendered inside ProtectionBody under the44 * "DURESS PASSWORD" sub-label. Self-contained so it can drop into the existing45 * Protection card without restructuring it.46 *47 * Pro gate is the caller's responsibility: ProtectionBody renders the static48 * ProBadge row when !isPro and this composable only when isPro (see49 * INTEGRATION.md). All visuals use the card foreground [fg] like sibling rows.50 *51 * Relies on these row helpers already private in SettingsCardBodies.kt:52 * [RowLine], [RowScope.Name], [Toggle], [SubtleButton], [Hint], [PwField].53 * Because this lives in the same package it can call them directly once merged.54 */55@Composable56internal fun DuressSection(fg: Color, vm: SettingsViewModel) {57    val configured by vm.duressConfigured.collectAsState()5859    if (configured) {60        DuressActiveState(fg, vm)61    } else {62        DuressForm(fg, vm)63    }64}6566@Composable67private fun DuressActiveState(fg: Color, vm: SettingsViewModel) {68    RowLine(fg) {69        Name("Duress password is active", fg)70    }71    Text(72        "Duress password is active",73        color = PeachColors.HealthOk,74        fontFamily = PeachType.Mono,75        fontSize = 11.sp,76        modifier = Modifier.padding(top = 6.dp),77    )78    Spacer(Modifier.height(8.dp))79    DangerGhostButton("Disable duress password", fg) { vm.disableDuress() }80    Hint("Entering your duress password instead of your real one triggers the action you chose.", fg)81}8283@Composable84private fun DuressForm(fg: Color, vm: SettingsViewModel) {85    var enabled by remember { mutableStateOf(false) }86    var password by remember { mutableStateOf("") }87    var confirm by remember { mutableStateOf("") }88    var action by remember { mutableStateOf(DuressAction.DECOY) }89    var error by remember { mutableStateOf<String?>(null) }90    val scope = rememberCoroutineScope()9192    RowLine(fg) {93        Name("Duress password", fg)94        Toggle(enabled, fg) { on ->95            enabled = on96            if (!on) { password = ""; confirm = ""; error = null }97        }98    }99    Text(100        "Entering your duress password instead of your real one opens a decoy vault or wipes the real one.",101        color = fg.copy(alpha = 0.7f),102        fontFamily = PeachType.Mono,103        fontSize = 10.sp,104        modifier = Modifier.padding(top = 6.dp),105    )106107    AnimatedVisibility(enabled) {108        Column(Modifier.fillMaxWidth().padding(top = 10.dp), verticalArrangement = Arrangement.spacedBy(6.dp)) {109            PwField("DURESS PASSWORD", password, fg) { password = it; error = null }110            PwField("CONFIRM DURESS PASSWORD", confirm, fg) { confirm = it; error = null }111112            Text(113                "When your duress password is entered at the lock screen, Peach will:",114                color = fg.copy(alpha = 0.7f),115                fontFamily = PeachType.Mono,116                fontSize = 10.sp,117                modifier = Modifier.padding(top = 4.dp),118            )119120            RadioOption("Show a decoy vault", action == DuressAction.DECOY, fg) { action = DuressAction.DECOY }121            RadioOption("Destroy the real vault", action == DuressAction.DESTROY, fg) { action = DuressAction.DESTROY }122123            error?.let {124                Text(it, color = PeachColors.Breach, fontFamily = PeachType.Mono, fontSize = 10.sp)125            }126127            val canSave = password.isNotBlank() && confirm.isNotBlank()128            SaveButton("Save duress password", fg, canSave) {129                error = when {130                    password.isBlank() || confirm.isBlank() -> "Enter and confirm a duress password"131                    password != confirm -> "Passwords do not match"132                    else -> null133                }134                if (error == null) scope.launch {135                    if (vm.saveDuress(password, action)) {136                        password = ""; confirm = ""; enabled = false137                    } else {138                        error = "Couldn't save duress password"139                    }140                }141            }142        }143    }144}145146/* ── local controls (kept private to D13; the radio + variants don't exist in147   the shared row vocabulary) ─────────────────────────────────────────────── */148149@Composable150private fun RadioOption(label: String, selected: Boolean, fg: Color, onClick: () -> Unit) {151    val accent = com.peachpasswords.ui.theme.LocalPeachAccent.current152    Row(153        Modifier.fillMaxWidth().clip(RoundedCornerShape(8.dp)).clickable(onClick = onClick)154            .padding(vertical = 7.dp),155        verticalAlignment = Alignment.CenterVertically,156        horizontalArrangement = Arrangement.spacedBy(10.dp),157    ) {158        Box(159            Modifier.size(16.dp).clip(CircleShape)160                .border(1.dp, if (selected) accent else fg.copy(alpha = 0.4f), CircleShape),161            contentAlignment = Alignment.Center,162        ) {163            if (selected) Box(Modifier.size(8.dp).clip(CircleShape).background(accent))164        }165        Text(166            label,167            color = if (selected) accent else fg,168            fontFamily = PeachType.Mono,169            fontSize = 11.sp,170        )171    }172}173174@Composable175private fun SaveButton(label: String, fg: Color, enabled: Boolean, onClick: () -> Unit) {176    val accent = com.peachpasswords.ui.theme.LocalPeachAccent.current177    val tint = if (enabled) accent else fg.copy(alpha = 0.3f)178    Box(179        Modifier.fillMaxWidth().clip(RoundedCornerShape(8.dp))180            .border(1.dp, tint.copy(alpha = if (enabled) 0.6f else 0.3f), RoundedCornerShape(8.dp))181            .background(tint.copy(alpha = if (enabled) 0.16f else 0.06f))182            .let { if (enabled) it.clickable(onClick = onClick) else it }183            .padding(vertical = 11.dp),184        contentAlignment = Alignment.Center,185    ) {186        Text(label, color = if (enabled) accent else fg.copy(alpha = 0.4f), fontFamily = PeachType.Mono, fontSize = 11.sp)187    }188}189190@Composable191private fun DangerGhostButton(label: String, fg: Color, onClick: () -> Unit) {192    Box(193        Modifier.fillMaxWidth().clip(RoundedCornerShape(8.dp))194            .border(1.dp, PeachColors.Breach.copy(alpha = 0.5f), RoundedCornerShape(8.dp))195            .clickable(onClick = onClick)196            .padding(vertical = 11.dp),197        contentAlignment = Alignment.Center,198    ) {199        Text(label, color = PeachColors.Breach, fontFamily = PeachType.Mono, fontSize = 11.sp)200    }201}