SettingsCardBodies.kt

22.6 KB · raw

1package com.peachpasswords.ui.settings23import android.content.Intent4import android.net.Uri5import androidx.compose.animation.AnimatedVisibility6import androidx.compose.animation.core.animateDpAsState7import androidx.compose.foundation.background8import androidx.compose.foundation.border9import androidx.compose.foundation.clickable10import androidx.compose.foundation.layout.Arrangement11import androidx.compose.foundation.layout.Box12import androidx.compose.foundation.layout.Column13import androidx.compose.foundation.layout.FlowRow14import androidx.compose.foundation.layout.ExperimentalLayoutApi15import androidx.compose.foundation.layout.Row16import androidx.compose.foundation.layout.RowScope17import androidx.compose.foundation.layout.Spacer18import androidx.compose.foundation.layout.fillMaxWidth19import androidx.compose.foundation.layout.height20import androidx.compose.foundation.layout.padding21import androidx.compose.foundation.layout.size22import androidx.compose.foundation.layout.width23import androidx.compose.foundation.shape.CircleShape24import androidx.compose.foundation.shape.RoundedCornerShape25import androidx.compose.foundation.text.BasicTextField26import androidx.compose.runtime.Composable27import androidx.compose.runtime.getValue28import androidx.compose.runtime.mutableStateOf29import androidx.compose.runtime.remember30import androidx.compose.runtime.rememberCoroutineScope31import androidx.compose.runtime.setValue32import androidx.compose.ui.Alignment33import androidx.compose.ui.Modifier34import androidx.compose.ui.draw.clip35import androidx.compose.ui.graphics.Color36import androidx.compose.ui.graphics.SolidColor37import androidx.compose.ui.platform.LocalContext38import androidx.compose.ui.text.TextStyle39import androidx.compose.ui.text.font.FontStyle40import androidx.compose.ui.text.input.PasswordVisualTransformation41import androidx.compose.ui.unit.Dp42import androidx.compose.ui.unit.dp43import androidx.compose.ui.unit.em44import androidx.compose.ui.unit.sp45import androidx.compose.material3.Text46import com.peachpasswords.ui.PeachNav47import com.peachpasswords.ui.theme.ACCENT_COLORS48import com.peachpasswords.ui.theme.PeachType49import com.peachpasswords.vault.VaultSettings50import kotlinx.coroutines.launch5152private const val PRO_URL = "https://buy.polar.sh/polar_cl_IXSFIPojx67rcOTksn8LkH5mnU3osPHtYqBIl1eyuIz"5354/**55 * Body content for the active stack card. Everything is tinted with the card's56 * own foreground [fg] (the browser's `currentColor` convention), so each card's57 * rows inherit its palette. Rows whose backend doesn't exist yet (duress,58 * canary, license activation) render their browser visual but defer the action59 * to a later phase — noted inline where that's the case.60 */61@Composable62fun CardBody(63    id: CardId,64    fg: Color,65    vm: SettingsViewModel,66    settings: VaultSettings,67    entriesCount: Int,68    onNavigate: (PeachNav) -> Unit,69) {70    when (id) {71        CardId.Appearance -> AppearanceBody(fg, vm, settings)72        CardId.Protection -> ProtectionBody(fg, vm, settings)73        CardId.Vault -> VaultBody(fg, vm, settings, entriesCount, onNavigate)74        CardId.Sync -> SyncBody(fg, onNavigate)75        CardId.Shortcuts -> ShortcutsBody(fg)76        CardId.Pro -> ProBody(fg)77        CardId.Feedback -> FeedbackBody(fg)78    }79}8081/* ─────────────────────────  shared row vocabulary  ───────────────────────── */8283@Composable84private fun SubLabel(text: String, fg: Color, top: Dp = 14.dp) {85    Text(86        text,87        modifier = Modifier.padding(top = top, bottom = 4.dp),88        color = fg.copy(alpha = 0.58f),89        fontFamily = PeachType.Mono,90        fontSize = 8.sp,91        letterSpacing = 0.22.em,92    )93}9495@Composable96private fun RowLine(fg: Color, content: @Composable RowScope.() -> Unit) {97    Row(98        Modifier.fillMaxWidth().padding(vertical = 9.dp),99        verticalAlignment = Alignment.CenterVertically,100        horizontalArrangement = Arrangement.SpaceBetween,101        content = content,102    )103    Box(Modifier.fillMaxWidth().height(1.dp).background(fg.copy(alpha = 0.12f)))104}105106@Composable107private fun RowScope.Name(text: String, fg: Color) {108    Text(text, color = fg, fontFamily = PeachType.Body, fontSize = 13.sp, modifier = Modifier.weight(1f))109}110111@Composable112private fun ValueText(text: String, fg: Color) {113    Text(text, color = fg.copy(alpha = 0.7f), fontFamily = PeachType.Mono, fontSize = 11.sp)114}115116@Composable117private fun SubtleButton(label: String, fg: Color, onClick: () -> Unit) {118    Box(119        Modifier.clip(RoundedCornerShape(8.dp))120            .border(1.dp, fg.copy(alpha = 0.28f), RoundedCornerShape(8.dp))121            .background(fg.copy(alpha = 0.08f))122            .clickable(onClick = onClick)123            .padding(horizontal = 12.dp, vertical = 6.dp),124    ) {125        Text(label, color = fg, fontFamily = PeachType.Mono, fontSize = 11.sp)126    }127}128129@Composable130private fun Toggle(on: Boolean, fg: Color, onToggle: (Boolean) -> Unit) {131    val knob by animateDpAsState(if (on) 16.dp else 2.dp, label = "knob")132    Box(133        Modifier.size(width = 34.dp, height = 20.dp).clip(RoundedCornerShape(999.dp))134            .background(if (on) fg.copy(alpha = 0.28f) else fg.copy(alpha = 0.08f))135            .border(1.dp, fg.copy(alpha = if (on) 0.5f else 0.32f), RoundedCornerShape(999.dp))136            .clickable { onToggle(!on) },137    ) {138        Box(139            Modifier.padding(top = 2.dp).width(16.dp).fillMaxWidth().height(16.dp)140                .padding(start = knob - 2.dp)141                .size(14.dp).clip(CircleShape)142                .background(if (on) fg else fg.copy(alpha = 0.58f)),143        )144    }145}146147@Composable148private fun Pill(label: String, active: Boolean, fg: Color, onClick: () -> Unit) {149    Box(150        Modifier.clip(RoundedCornerShape(999.dp))151            .border(1.dp, fg.copy(alpha = if (active) 0.55f else 0.32f), RoundedCornerShape(999.dp))152            .background(if (active) fg.copy(alpha = 0.18f) else Color.Transparent)153            .clickable(onClick = onClick)154            .padding(horizontal = 12.dp, vertical = 5.dp),155    ) {156        Text(label, color = fg, fontFamily = PeachType.Mono, fontSize = 10.sp)157    }158}159160@Composable161private fun ProBadge(fg: Color) {162    Box(163        Modifier.padding(start = 6.dp).clip(RoundedCornerShape(4.dp))164            .background(fg.copy(alpha = 0.16f)).padding(horizontal = 5.dp, vertical = 1.dp),165    ) {166        Text("Pro", color = fg.copy(alpha = 0.85f), fontFamily = PeachType.Mono, fontSize = 8.sp)167    }168}169170@Composable171private fun Hint(text: String, fg: Color) {172    Text(173        text,174        modifier = Modifier.padding(top = 6.dp),175        color = fg.copy(alpha = 0.6f),176        fontFamily = PeachType.Display,177        fontStyle = FontStyle.Italic,178        fontSize = 11.5.sp,179        lineHeight = 16.sp,180    )181}182183/* ──────────────────────────────  Appearance  ─────────────────────────────── */184185@OptIn(ExperimentalLayoutApi::class)186@Composable187private fun AppearanceBody(fg: Color, vm: SettingsViewModel, s: VaultSettings) {188    SubLabel("THEME", fg, top = 0.dp)189    RowLine(fg) {190        Name("Theme", fg)191        SubtleButton(if (s.theme == "dark") "Switch to light" else "Switch to dark", fg) {192            vm.setTheme(if (s.theme == "dark") "light" else "dark")193        }194    }195    SubLabel("ACCENT", fg)196    FlowRow(horizontalArrangement = Arrangement.spacedBy(8.dp), verticalArrangement = Arrangement.spacedBy(8.dp)) {197        ACCENT_COLORS.forEach { (name, color) ->198            Box(199                Modifier.size(22.dp).clip(CircleShape).background(color)200                    .border(201                        if (s.accentColor == name) 2.dp else 1.dp,202                        if (s.accentColor == name) fg else fg.copy(alpha = 0.2f),203                        CircleShape,204                    )205                    .clickable { vm.setAccent(name) },206            )207        }208    }209    SubLabel("TEXT SIZE", fg)210    RowLine(fg) {211        Name("Size", fg)212        Row(horizontalArrangement = Arrangement.spacedBy(6.dp)) {213            Pill("Default", s.textSize == "medium", fg) { vm.setTextSize("medium") }214            Pill("Large", s.textSize == "large", fg) { vm.setTextSize("large") }215        }216    }217    SubLabel("MOTION", fg)218    RowLine(fg) {219        Name("Reduce motion", fg)220        SubtleButton(if (s.reduceMotion) "On" else "Off", fg) { vm.setReduceMotion(!s.reduceMotion) }221    }222}223224/* ──────────────────────────────  Protection  ─────────────────────────────── */225226@Composable227private fun ProtectionBody(fg: Color, vm: SettingsViewModel, s: VaultSettings) {228    val lockOptions = listOf(1 to "1 minute", 5 to "5 minutes", 15 to "15 minutes", 30 to "30 minutes", 0 to "Never")229    val current = s.autoLockMinutes ?: 0230231    SubLabel("LOCKING", fg, top = 0.dp)232    RowLine(fg) {233        Name("Auto-lock after", fg)234        val idx = lockOptions.indexOfFirst { it.first == current }.coerceAtLeast(0)235        SubtleButton(lockOptions[idx].second, fg) {236            val next = lockOptions[(idx + 1) % lockOptions.size].first237            vm.setAutoLockMinutes(if (next == 0) 0 else next)238        }239    }240    SubLabel("BIOMETRICS", fg)241    RowLine(fg) {242        Name("Unlock with biometrics", fg)243        Toggle(s.biometricsEnabled, fg) { vm.setBiometrics(it) }244    }245    SubLabel("BREACH PROTECTION", fg)246    RowLine(fg) {247        Name("Breach monitoring", fg)248        Toggle(s.breachMonitoring, fg) { vm.setBreachMonitoring(it) }249    }250251    SubLabel("MASTER PASSWORD", fg)252    ChangePasswordRow(fg, vm)253254    SubLabel("DURESS PASSWORD", fg)255    RowLine(fg) {256        Row(Modifier.weight(1f), verticalAlignment = Alignment.CenterVertically) {257            Name("Duress password", fg); ProBadge(fg)258        }259        ValueText("off", fg)260    }261    Hint("If entered under coercion, a decoy empty vault opens; your real vault stays hidden. Arrives with Pro sync.", fg)262}263264@Composable265private fun ChangePasswordRow(fg: Color, vm: SettingsViewModel) {266    var open by remember { mutableStateOf(false) }267    var current by remember { mutableStateOf("") }268    var next by remember { mutableStateOf("") }269    var confirm by remember { mutableStateOf("") }270    var error by remember { mutableStateOf<String?>(null) }271    var done by remember { mutableStateOf(false) }272    val scope = rememberCoroutineScope()273274    RowLine(fg) {275        Name("Master password", fg)276        SubtleButton(if (open) "cancel" else "change", fg) { open = !open; error = null; done = false }277    }278    AnimatedVisibility(open) {279        Column(Modifier.fillMaxWidth().padding(top = 8.dp), verticalArrangement = Arrangement.spacedBy(6.dp)) {280            PwField("current password", current, fg) { current = it }281            PwField("new password (min 12 chars)", next, fg) { next = it }282            PwField("confirm new password", confirm, fg) { confirm = it }283            error?.let { Text(it, color = Color(0xFFE5805C), fontFamily = PeachType.Mono, fontSize = 10.sp) }284            if (done) Text("password changed ✓", color = fg, fontFamily = PeachType.Mono, fontSize = 10.sp)285            SubtleButton("save new password", fg) {286                error = when {287                    current.isBlank() -> "enter your current password"288                    next.length < 12 -> "new password must be at least 12 characters"289                    next != confirm -> "new passwords do not match"290                    next == current -> "new password must differ from current"291                    else -> null292                }293                if (error == null) scope.launch {294                    if (vm.changePassword(current, next)) {295                        current = ""; next = ""; confirm = ""; open = false; done = true296                    } else error = "incorrect current password"297                }298            }299        }300    }301}302303@Composable304private fun PwField(placeholder: String, value: String, fg: Color, onChange: (String) -> Unit) {305    Box(306        Modifier.fillMaxWidth().clip(RoundedCornerShape(8.dp))307            .background(Color(0x2E000000))308            .border(1.dp, fg.copy(alpha = 0.22f), RoundedCornerShape(8.dp))309            .padding(horizontal = 10.dp, vertical = 10.dp),310    ) {311        if (value.isEmpty()) {312            Text(placeholder, color = fg.copy(alpha = 0.42f), fontFamily = PeachType.Mono, fontSize = 12.sp)313        }314        BasicTextField(315            value = value,316            onValueChange = onChange,317            singleLine = true,318            textStyle = TextStyle(color = fg, fontFamily = PeachType.Mono, fontSize = 12.sp),319            cursorBrush = SolidColor(fg),320            visualTransformation = PasswordVisualTransformation(),321            modifier = Modifier.fillMaxWidth(),322        )323    }324}325326/* ────────────────────────────────  Vault  ───────────────────────────────── */327328@Composable329private fun VaultBody(330    fg: Color,331    vm: SettingsViewModel,332    s: VaultSettings,333    entriesCount: Int,334    onNavigate: (PeachNav) -> Unit,335) {336    val trashOptions = listOf(7 to "7 days", 30 to "30 days", 90 to "90 days", 0 to "Never")337    val current = s.trashRetentionDays ?: 0338339    SubLabel("ENTRIES", fg, top = 0.dp)340    RowLine(fg) { Name("Entries", fg); ValueText("$entriesCount", fg) }341342    SubLabel("MAINTENANCE", fg)343    RowLine(fg) {344        Name("Trash auto-delete", fg)345        val idx = trashOptions.indexOfFirst { it.first == current }.coerceAtLeast(0)346        SubtleButton(trashOptions[idx].second, fg) {347            vm.setTrashRetentionDays(trashOptions[(idx + 1) % trashOptions.size].first)348        }349    }350    RowLine(fg) { Name("Advanced Vault Health", fg); SubtleButton("open", fg) { onNavigate(PeachNav.Health) } }351    RowLine(fg) { Name("Trash", fg); SubtleButton("open", fg) { onNavigate(PeachNav.Trash) } }352353    SubLabel("DATA", fg)354    RowLine(fg) { Name("Import passwords", fg); SubtleButton("open", fg) { onNavigate(PeachNav.Import) } }355    RowLine(fg) { Name("Export backup", fg); SubtleButton("open", fg) { onNavigate(PeachNav.Export) } }356    RowLine(fg) {357        Row(Modifier.weight(1f), verticalAlignment = Alignment.CenterVertically) { Name("Peach Codex", fg); ProBadge(fg) }358        SubtleButton("open", fg) { onNavigate(PeachNav.CodexGenerate) }359    }360    RowLine(fg) { Name("Restore from Codex", fg); SubtleButton("open", fg) { onNavigate(PeachNav.CodexRestore) } }361362    var advancedOpen by remember { mutableStateOf(false) }363    SubLabel("ADVANCED", fg)364    RowLine(fg) {365        Name("Advanced", fg)366        SubtleButton(if (advancedOpen) "hide" else "show", fg) { advancedOpen = !advancedOpen }367    }368    if (advancedOpen) {369        RowLine(fg) {370            Name("Replace vault from backup", com.peachpasswords.ui.theme.PeachColors.SyncBusy)371            SubtleButton("open", com.peachpasswords.ui.theme.PeachColors.SyncBusy) { onNavigate(PeachNav.Import) }372        }373        RowLine(fg) {374            Name("Destroy vault", com.peachpasswords.ui.theme.PeachColors.Breach)375            SubtleButton("destroy", com.peachpasswords.ui.theme.PeachColors.Breach) { onNavigate(PeachNav.Panic) }376        }377    }378}379380/* ────────────────────────────────  Sync  ────────────────────────────────── */381382@Composable383private fun SyncBody(fg: Color, onNavigate: (PeachNav) -> Unit) {384    SubLabel("PAIRING", fg, top = 0.dp)385    RowLine(fg) {386        Column(Modifier.weight(1f)) {387            Row(verticalAlignment = Alignment.CenterVertically) {388                Text("Peach Sync", color = fg, fontFamily = PeachType.Body, fontSize = 13.sp); ProBadge(fg)389            }390            Text(391                "Pair nearby devices by chirp and keep encrypted vault blobs in step without a central account.",392                color = fg.copy(alpha = 0.7f), fontFamily = PeachType.Body, fontSize = 11.sp, lineHeight = 16.sp,393                modifier = Modifier.padding(top = 2.dp),394            )395        }396    }397    RowLine(fg) { Name("Status", fg); ValueText("Requires Pro", fg) }398    RowLine(fg) { Name("Sync hub", fg); SubtleButton("open", fg) { onNavigate(PeachNav.ChirpPair) } }399400    SubLabel("DEVICE FLOW", fg)401    RowLine(fg) { Name("Pair new device", fg); SubtleButton("pair", fg) { onNavigate(PeachNav.ChirpPair) } }402    Hint("Sync uses the same zero-knowledge vault format as backup and restore; pairing authorizes a device, not a cloud account.", fg)403}404405/* ──────────────────────────────  Shortcuts  ──────────────────────────────── */406407@Composable408private fun ShortcutsBody(fg: Color) {409    val bindings = listOf(410        "Copy password" to "⌘C", "Copy username" to "⌘⇧C", "Open entry" to "↵",411        "Edit entry" to "E", "New entry" to "⌘N", "Search" to "/",412    )413    SubLabel("KEYBINDINGS", fg, top = 0.dp)414    Hint("Shortcuts apply when a hardware keyboard is attached. Defaults shown below.", fg)415    Spacer(Modifier.height(8.dp))416    bindings.forEach { (label, key) ->417        RowLine(fg) {418            Name(label, fg)419            Box(420                Modifier.clip(RoundedCornerShape(4.dp)).background(fg.copy(alpha = 0.06f))421                    .border(1.dp, fg.copy(alpha = 0.32f), RoundedCornerShape(4.dp))422                    .padding(horizontal = 7.dp, vertical = 2.dp),423            ) { Text(key, color = fg, fontFamily = PeachType.Mono, fontSize = 10.sp) }424        }425    }426}427428/* ────────────────────────────────  Pro  ─────────────────────────────────── */429430@Composable431private fun ProBody(fg: Color) {432    val ctx = LocalContext.current433    val features = listOf(434        "Peach Sync" to "Encrypted · peer-to-peer",435        "Peach Codex" to "Restore from paper",436        "Passkeys" to "Zero-knowledge WebAuthn",437        "Advanced Vault Health" to "Reuse · age · exposure",438        "Canary entries" to "Detects vault compromise",439        "Duress vault" to "Decoy under coercion",440    )441    Text("PEACH PRO · ONE-TIME", color = fg.copy(alpha = 0.6f), fontFamily = PeachType.Mono, fontSize = 8.sp, letterSpacing = 0.22.em)442    Text(443        "Everything, everywhere.",444        modifier = Modifier.padding(top = 8.dp),445        color = fg, fontFamily = PeachType.Display, fontStyle = FontStyle.Italic, fontSize = 24.sp,446    )447    Text(448        "One key unlocks every Pro feature — for the lifetime of the app.",449        modifier = Modifier.padding(top = 6.dp),450        color = fg.copy(alpha = 0.72f), fontFamily = PeachType.Body, fontSize = 12.sp, lineHeight = 17.sp,451    )452    Spacer(Modifier.height(12.dp))453    features.forEachIndexed { i, (name, desc) ->454        RowLine(fg) {455            Row(Modifier.weight(1f), verticalAlignment = Alignment.CenterVertically) {456                Text("%02d".format(i + 1), color = fg.copy(alpha = 0.5f), fontFamily = PeachType.Mono, fontSize = 10.sp, modifier = Modifier.padding(end = 10.dp))457                Text(name, color = fg, fontFamily = PeachType.Body, fontSize = 13.sp)458            }459            Text(desc, color = fg.copy(alpha = 0.6f), fontFamily = PeachType.Display, fontStyle = FontStyle.Italic, fontSize = 11.sp)460        }461    }462    Spacer(Modifier.height(14.dp))463    Text(464        "$39",465        color = fg, fontFamily = PeachType.Display, fontStyle = FontStyle.Italic, fontSize = 28.sp,466    )467    Text("once · yours forever", color = fg.copy(alpha = 0.6f), fontFamily = PeachType.Mono, fontSize = 9.sp, letterSpacing = 0.18.em)468    Spacer(Modifier.height(10.dp))469    Box(470        Modifier.fillMaxWidth().clip(RoundedCornerShape(10.dp))471            .background(fg.copy(alpha = 0.16f))472            .border(1.dp, fg.copy(alpha = 0.4f), RoundedCornerShape(10.dp))473            .clickable {474                runCatching { ctx.startActivity(Intent(Intent.ACTION_VIEW, Uri.parse(PRO_URL))) }475            }476            .padding(vertical = 12.dp),477        contentAlignment = Alignment.Center,478    ) { Text("Get Peach Pro", color = fg, fontFamily = PeachType.Mono, fontSize = 12.sp) }479    Hint("Already have a key? License activation arrives with Pro sync in a later update.", fg)480}481482/* ──────────────────────────────  Feedback  ───────────────────────────────── */483484@OptIn(ExperimentalLayoutApi::class)485@Composable486private fun FeedbackBody(fg: Color) {487    val categories = listOf("Bug report", "Feature request", "Question", "Other")488    var category by remember { mutableStateOf(categories[0]) }489    var message by remember { mutableStateOf("") }490    var sent by remember { mutableStateOf(false) }491492    if (sent) {493        Box(494            Modifier.fillMaxWidth().clip(RoundedCornerShape(10.dp)).background(fg.copy(alpha = 0.08f))495                .border(1.dp, fg.copy(alpha = 0.32f), RoundedCornerShape(10.dp)).padding(16.dp),496        ) {497            Column {498                Text("Thank you", color = fg, fontFamily = PeachType.Display, fontStyle = FontStyle.Italic, fontSize = 18.sp)499                Text("Your note is in the queue. Someone will process it soon.", color = fg.copy(alpha = 0.7f), fontFamily = PeachType.Body, fontSize = 12.sp, modifier = Modifier.padding(top = 4.dp))500            }501        }502        return503    }504505    Text("Help us improve Peach. Every report goes directly to our inbox.", color = fg.copy(alpha = 0.7f), fontFamily = PeachType.Body, fontSize = 12.sp, lineHeight = 17.sp)506    SubLabel("TYPE", fg)507    FlowRow(horizontalArrangement = Arrangement.spacedBy(6.dp), verticalArrangement = Arrangement.spacedBy(6.dp)) {508        categories.forEach { cat -> Pill(cat, category == cat, fg) { category = cat } }509    }510    SubLabel("MESSAGE", fg)511    Box(512        Modifier.fillMaxWidth().height(110.dp).clip(RoundedCornerShape(8.dp))513            .background(Color(0x2E000000)).border(1.dp, fg.copy(alpha = 0.22f), RoundedCornerShape(8.dp))514            .padding(10.dp),515    ) {516        if (message.isEmpty()) Text("Describe the issue or idea...", color = fg.copy(alpha = 0.42f), fontFamily = PeachType.Body, fontSize = 12.sp)517        BasicTextField(518            value = message, onValueChange = { message = it },519            textStyle = TextStyle(color = fg, fontFamily = PeachType.Body, fontSize = 12.sp),520            cursorBrush = SolidColor(fg), modifier = Modifier.fillMaxWidth(),521        )522    }523    Spacer(Modifier.height(8.dp))524    SubtleButton("Send", fg) { if (message.isNotBlank()) sent = true }525    Hint("Sent anonymously · no account required", fg)526}