SettingsCardBodies.kt

41.8 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.material3.CircularProgressIndicator28import androidx.compose.runtime.DisposableEffect29import androidx.compose.runtime.collectAsState30import androidx.compose.runtime.getValue31import com.peachpasswords.feedback.FeedbackState32import com.peachpasswords.ui.theme.LocalPeachAccent33import com.peachpasswords.vault.DuressAction34import com.peachpasswords.ui.theme.peachColors35import androidx.compose.runtime.mutableStateOf36import androidx.compose.runtime.remember37import androidx.compose.runtime.rememberCoroutineScope38import androidx.compose.runtime.setValue39import androidx.compose.ui.Alignment40import androidx.compose.ui.Modifier41import androidx.compose.ui.draw.clip42import androidx.compose.ui.graphics.Color43import androidx.compose.ui.graphics.SolidColor44import androidx.compose.ui.platform.LocalContext45import androidx.compose.ui.text.TextStyle46import androidx.compose.ui.text.font.FontStyle47import androidx.compose.ui.text.input.PasswordVisualTransformation48import androidx.compose.ui.unit.Dp49import androidx.compose.ui.unit.dp50import androidx.compose.ui.unit.em51import androidx.compose.ui.unit.sp52import androidx.compose.material3.Text53import com.peachpasswords.ui.PeachNav54import com.peachpasswords.ui.sync.SyncViewModel55import com.peachpasswords.ui.theme.ACCENT_COLORS56import com.peachpasswords.ui.theme.PeachType57import androidx.hilt.navigation.compose.hiltViewModel58import com.peachpasswords.vault.VaultSettings59import com.peachpasswords.vault.VaultSnapshot60import kotlinx.coroutines.launch61import java.text.SimpleDateFormat62import java.util.Date63import java.util.Locale6465private const val PRO_URL = "https://buy.polar.sh/polar_cl_IXSFIPojx67rcOTksn8LkH5mnU3osPHtYqBIl1eyuIz"6667/**68 * Body content for the active stack card. Everything is tinted with the card's69 * own foreground [fg] (the browser's `currentColor` convention), so each card's70 * rows inherit its palette. Rows whose backend doesn't exist yet (duress,71 * canary, license activation) render their browser visual but defer the action72 * to a later phase — noted inline where that's the case.73 */74@Composable75fun CardBody(76    id: CardId,77    fg: Color,78    vm: SettingsViewModel,79    settings: VaultSettings,80    entriesCount: Int,81    onNavigate: (PeachNav) -> Unit,82) {83    when (id) {84        CardId.Appearance -> AppearanceBody(fg, vm, settings)85        CardId.Protection -> ProtectionBody(fg, vm, settings, onNavigate)86        CardId.Vault -> VaultBody(fg, vm, settings, entriesCount, onNavigate)87        CardId.Sync -> SyncBody(fg, onNavigate)88        CardId.Shortcuts -> ShortcutsBody(fg)89        CardId.Pro -> ProBody(fg, vm)90        CardId.Feedback -> FeedbackBody(fg, vm)91    }92}9394/* ─────────────────────────  shared row vocabulary  ───────────────────────── */9596@Composable97private fun SubLabel(text: String, fg: Color, top: Dp = 14.dp) {98    Text(99        text,100        modifier = Modifier.padding(top = top, bottom = 4.dp),101        color = fg.copy(alpha = 0.58f),102        fontFamily = PeachType.Mono,103        fontSize = 8.sp,104        letterSpacing = 0.22.em,105    )106}107108@Composable109private fun RowLine(fg: Color, modifier: Modifier = Modifier, content: @Composable RowScope.() -> Unit) {110    Row(111        Modifier.fillMaxWidth().then(modifier).padding(vertical = 9.dp),112        verticalAlignment = Alignment.CenterVertically,113        horizontalArrangement = Arrangement.SpaceBetween,114        content = content,115    )116    Box(Modifier.fillMaxWidth().height(1.dp).background(fg.copy(alpha = 0.12f)))117}118119@Composable120private fun RowScope.Name(text: String, fg: Color) {121    Text(text, color = fg, fontFamily = PeachType.Body, fontSize = 13.sp, modifier = Modifier.weight(1f))122}123124@Composable125private fun ValueText(text: String, fg: Color) {126    Text(text, color = fg.copy(alpha = 0.7f), fontFamily = PeachType.Mono, fontSize = 11.sp)127}128129@Composable130private fun SubtleButton(label: String, fg: Color, onClick: () -> Unit) {131    Box(132        Modifier.clip(RoundedCornerShape(8.dp))133            .border(1.dp, fg.copy(alpha = 0.28f), RoundedCornerShape(8.dp))134            .background(fg.copy(alpha = 0.08f))135            .clickable(onClick = onClick)136            .padding(horizontal = 12.dp, vertical = 6.dp),137    ) {138        Text(label, color = fg, fontFamily = PeachType.Mono, fontSize = 11.sp)139    }140}141142@Composable143private fun Toggle(on: Boolean, fg: Color, onToggle: (Boolean) -> Unit) {144    val knob by animateDpAsState(if (on) 16.dp else 2.dp, label = "knob")145    Box(146        Modifier.size(width = 34.dp, height = 20.dp).clip(RoundedCornerShape(999.dp))147            .background(if (on) fg.copy(alpha = 0.28f) else fg.copy(alpha = 0.08f))148            .border(1.dp, fg.copy(alpha = if (on) 0.5f else 0.32f), RoundedCornerShape(999.dp))149            .clickable { onToggle(!on) },150    ) {151        Box(152            Modifier.padding(top = 2.dp).width(16.dp).fillMaxWidth().height(16.dp)153                .padding(start = knob - 2.dp)154                .size(14.dp).clip(CircleShape)155                .background(if (on) fg else fg.copy(alpha = 0.58f)),156        )157    }158}159160@Composable161private fun Pill(label: String, active: Boolean, fg: Color, onClick: () -> Unit) {162    Box(163        Modifier.clip(RoundedCornerShape(999.dp))164            .border(1.dp, fg.copy(alpha = if (active) 0.55f else 0.32f), RoundedCornerShape(999.dp))165            .background(if (active) fg.copy(alpha = 0.18f) else Color.Transparent)166            .clickable(onClick = onClick)167            .padding(horizontal = 12.dp, vertical = 5.dp),168    ) {169        Text(label, color = fg, fontFamily = PeachType.Mono, fontSize = 10.sp)170    }171}172173@Composable174private fun ProBadge(fg: Color) {175    Box(176        Modifier.padding(start = 6.dp).clip(RoundedCornerShape(4.dp))177            .background(fg.copy(alpha = 0.16f)).padding(horizontal = 5.dp, vertical = 1.dp),178    ) {179        Text("Pro", color = fg.copy(alpha = 0.85f), fontFamily = PeachType.Mono, fontSize = 8.sp)180    }181}182183@Composable184private fun Hint(text: String, fg: Color) {185    Text(186        text,187        modifier = Modifier.padding(top = 6.dp),188        color = fg.copy(alpha = 0.6f),189        fontFamily = PeachType.Display,190        fontStyle = FontStyle.Italic,191        fontSize = 11.5.sp,192        lineHeight = 16.sp,193    )194}195196/* ──────────────────────────────  Appearance  ─────────────────────────────── */197198@OptIn(ExperimentalLayoutApi::class)199@Composable200private fun AppearanceBody(fg: Color, vm: SettingsViewModel, s: VaultSettings) {201    SubLabel("THEME", fg, top = 0.dp)202    RowLine(fg) {203        Name("Theme", fg)204        // Cycle dark → light → system → dark. The button shows the current mode;205        // tapping advances to the next.206        val (label, next) = when (s.theme) {207            "light" -> "Light" to "system"208            "system" -> "System" to "dark"209            else -> "Dark" to "light"210        }211        SubtleButton(label, fg) { vm.setTheme(next) }212    }213    SubLabel("ACCENT", fg)214    FlowRow(horizontalArrangement = Arrangement.spacedBy(8.dp), verticalArrangement = Arrangement.spacedBy(8.dp)) {215        ACCENT_COLORS.forEach { (name, color) ->216            Box(217                Modifier.size(22.dp).clip(CircleShape).background(color)218                    .border(219                        if (s.accentColor == name) 2.dp else 1.dp,220                        if (s.accentColor == name) fg else fg.copy(alpha = 0.2f),221                        CircleShape,222                    )223                    .clickable { vm.setAccent(name) },224            )225        }226    }227    SubLabel("TEXT SIZE", fg)228    RowLine(fg) {229        Name("Size", fg)230        Row(horizontalArrangement = Arrangement.spacedBy(6.dp)) {231            Pill("Default", s.textSize == "medium", fg) { vm.setTextSize("medium") }232            Pill("Large", s.textSize == "large", fg) { vm.setTextSize("large") }233        }234    }235    SubLabel("MOTION", fg)236    RowLine(fg) {237        Name("Reduce motion", fg)238        SubtleButton(if (s.reduceMotion) "On" else "Off", fg) { vm.setReduceMotion(!s.reduceMotion) }239    }240}241242/* ──────────────────────────────  Protection  ─────────────────────────────── */243244@Composable245private fun ProtectionBody(fg: Color, vm: SettingsViewModel, s: VaultSettings, onNavigate: (PeachNav) -> Unit) {246    val lockOptions = listOf(1 to "1 minute", 5 to "5 minutes", 15 to "15 minutes", 30 to "30 minutes", 0 to "Never")247    val current = s.autoLockMinutes ?: 0248249    SubLabel("LOCKING", fg, top = 0.dp)250    RowLine(fg) {251        Name("Auto-lock after", fg)252        val idx = lockOptions.indexOfFirst { it.first == current }.coerceAtLeast(0)253        SubtleButton(lockOptions[idx].second, fg) {254            val next = lockOptions[(idx + 1) % lockOptions.size].first255            vm.setAutoLockMinutes(if (next == 0) 0 else next)256        }257    }258    SubLabel("BIOMETRICS", fg)259    val bioActivity = LocalContext.current as? androidx.fragment.app.FragmentActivity260    RowLine(fg) {261        Name("Unlock with biometrics", fg)262        Toggle(s.biometricsEnabled, fg) { on ->263            if (on && bioActivity != null) vm.enableBiometrics(bioActivity) else vm.disableBiometrics()264        }265    }266    val bioError = com.peachpasswords.ui.lock.Biometrics.statusMessage(LocalContext.current)267    if (bioError != null) {268        Text(269            bioError,270            color = com.peachpasswords.ui.theme.peachColors.Breach,271            fontFamily = PeachType.Mono, fontSize = 10.sp,272            modifier = Modifier.padding(start = 2.dp, top = 2.dp, bottom = 4.dp),273        )274    }275    SubLabel("RECOVERY", fg)276    RowLine(fg) {277        Name("View recovery words", fg)278        SubtleButton("view", fg) { onNavigate(PeachNav.RecoveryView) }279    }280    SubLabel("BREACH PROTECTION", fg)281    RowLine(fg) {282        Name("Breach monitoring", fg)283        Toggle(s.breachMonitoring, fg) { vm.setBreachMonitoring(it) }284    }285286    SubLabel("MASTER PASSWORD", fg)287    ChangePasswordRow(fg, vm)288289    SubLabel("DURESS PASSWORD", fg)290    val duressIsPro by vm.isPro.collectAsState()291    if (!duressIsPro) {292        RowLine(fg) {293            Row(Modifier.weight(1f), verticalAlignment = Alignment.CenterVertically) {294                Name("Duress password", fg); ProBadge(fg)295            }296            ValueText("off", fg)297        }298        Hint("If entered under coercion, a decoy empty vault opens; your real vault stays hidden. Pro feature.", fg)299    } else {300        DuressSection(fg, vm)301    }302}303304@Composable305private fun ChangePasswordRow(fg: Color, vm: SettingsViewModel) {306    var open by remember { mutableStateOf(false) }307    var current by remember { mutableStateOf("") }308    var next by remember { mutableStateOf("") }309    var confirm by remember { mutableStateOf("") }310    var error by remember { mutableStateOf<String?>(null) }311    var done by remember { mutableStateOf(false) }312    val scope = rememberCoroutineScope()313314    RowLine(fg) {315        Name("Master password", fg)316        SubtleButton(if (open) "cancel" else "change", fg) { open = !open; error = null; done = false }317    }318    AnimatedVisibility(open) {319        Column(Modifier.fillMaxWidth().padding(top = 8.dp), verticalArrangement = Arrangement.spacedBy(6.dp)) {320            PwField("current password", current, fg) { current = it }321            PwField("new password (min 12 chars)", next, fg) { next = it }322            PwField("confirm new password", confirm, fg) { confirm = it }323            error?.let { Text(it, color = Color(0xFFE5805C), fontFamily = PeachType.Mono, fontSize = 10.sp) }324            if (done) Text("password changed ✓", color = fg, fontFamily = PeachType.Mono, fontSize = 10.sp)325            SubtleButton("save new password", fg) {326                error = when {327                    current.isBlank() -> "enter your current password"328                    next.length < 12 -> "new password must be at least 12 characters"329                    next != confirm -> "new passwords do not match"330                    next == current -> "new password must differ from current"331                    else -> null332                }333                if (error == null) scope.launch {334                    if (vm.changePassword(current, next)) {335                        current = ""; next = ""; confirm = ""; open = false; done = true336                    } else error = "incorrect current password"337                }338            }339        }340    }341}342343@Composable344private fun PwField(placeholder: String, value: String, fg: Color, onChange: (String) -> Unit) {345    Box(346        Modifier.fillMaxWidth().clip(RoundedCornerShape(8.dp))347            .background(Color(0x2E000000))348            .border(1.dp, fg.copy(alpha = 0.22f), RoundedCornerShape(8.dp))349            .padding(horizontal = 10.dp, vertical = 10.dp),350    ) {351        if (value.isEmpty()) {352            Text(placeholder, color = fg.copy(alpha = 0.42f), fontFamily = PeachType.Mono, fontSize = 12.sp)353        }354        BasicTextField(355            value = value,356            onValueChange = onChange,357            singleLine = true,358            textStyle = TextStyle(color = fg, fontFamily = PeachType.Mono, fontSize = 12.sp),359            cursorBrush = SolidColor(fg),360            visualTransformation = PasswordVisualTransformation(),361            modifier = Modifier.fillMaxWidth(),362        )363    }364}365366/* ────────────────────────────────  Vault  ───────────────────────────────── */367368@Composable369private fun VaultBody(370    fg: Color,371    vm: SettingsViewModel,372    s: VaultSettings,373    entriesCount: Int,374    onNavigate: (PeachNav) -> Unit,375) {376    val trashOptions = listOf(7 to "7 days", 30 to "30 days", 90 to "90 days", 0 to "Never")377    val current = s.trashRetentionDays ?: 0378    val isPro by vm.isPro.collectAsState()379    val dedupCount by vm.dedupCount.collectAsState()380    val dedupRunning by vm.dedupRunning.collectAsState()381    val dedupDone by vm.dedupDone.collectAsState()382    val history by vm.vaultHistory.collectAsState()383    val restoringSnapshotId by vm.restoringSnapshotId.collectAsState()384385    SubLabel("ENTRIES", fg, top = 0.dp)386    RowLine(fg) { Name("Entries", fg); ValueText("$entriesCount", fg) }387    RowLine(fg) {388        Name("Duplicates", fg)389        val done = dedupDone390        if (done != null) {391            ValueText(if (done == 0) "already clean" else "removed $done", com.peachpasswords.ui.theme.peachColors.SyncOk)392        } else {393            val count = dedupCount394            val label = when {395                dedupRunning -> "scanning..."396                count == null -> "scan"397                count == 0 -> "none found"398                else -> "$count found"399            }400            SubtleButton(label, fg) {401                if (count == null || count == 0) vm.scanDuplicates()402            }403        }404    }405    if ((dedupCount ?: 0) > 0 && dedupDone == null) {406        Column(407            Modifier.fillMaxWidth()408                .padding(top = 2.dp, bottom = 8.dp)409                .clip(RoundedCornerShape(8.dp))410                .background(fg.copy(alpha = 0.08f))411                .border(1.dp, fg.copy(alpha = 0.18f), RoundedCornerShape(8.dp))412                .padding(10.dp),413            verticalArrangement = Arrangement.spacedBy(8.dp),414        ) {415            val count = dedupCount ?: 0416            Text(417                "$count duplicate ${if (count == 1) "entry" else "entries"} found. The newest copy of each will be kept; older duplicates will be moved to trash.",418                color = fg.copy(alpha = 0.82f),419                fontFamily = PeachType.Body,420                fontSize = 11.sp,421                lineHeight = 16.sp,422            )423            Row(horizontalArrangement = Arrangement.spacedBy(8.dp)) {424                SubtleButton("cancel", fg) { vm.cancelDuplicateRemoval() }425                SubtleButton(if (dedupRunning) "removing..." else "remove duplicates", fg) { vm.removeDuplicates() }426            }427        }428    }429430    SubLabel("MAINTENANCE", fg)431    RowLine(fg) {432        Name("Trash auto-delete", fg)433        val idx = trashOptions.indexOfFirst { it.first == current }.coerceAtLeast(0)434        SubtleButton(trashOptions[idx].second, fg) {435            vm.setTrashRetentionDays(trashOptions[(idx + 1) % trashOptions.size].first)436        }437    }438    RowLine(fg) { Name("Advanced Vault Health", fg); SubtleButton("open", fg) { onNavigate(PeachNav.Health) } }439    RowLine(fg) { Name("Trash", fg); SubtleButton("open", fg) { onNavigate(PeachNav.Trash) } }440441    SubLabel("DATA", fg)442    RowLine(fg) { Name("Import passwords", fg); SubtleButton("open", fg) { onNavigate(PeachNav.Import) } }443    RowLine(fg) { Name("Export backup", fg); SubtleButton("open", fg) { onNavigate(PeachNav.Export) } }444    RowLine(fg) {445        Row(Modifier.weight(1f), verticalAlignment = Alignment.CenterVertically) { Name("Peach Codex", fg); if (!isPro) ProBadge(fg) }446        SubtleButton(if (isPro) "open" else "Pro", fg) { if (isPro) onNavigate(PeachNav.CodexGenerate) }447    }448    RowLine(fg) { Name("Restore from Codex", fg); SubtleButton("open", fg) { onNavigate(PeachNav.CodexRestore) } }449450    var historyShowAll by remember { mutableStateOf(false) }451    SubLabel("VAULT HISTORY", fg)452    if (history.isEmpty()) {453        Text(454            "No snapshots yet. Peach saves automatically before every change — saves, syncs, and imports.",455            color = fg.copy(alpha = 0.68f),456            fontFamily = PeachType.Body,457            fontSize = 11.sp,458            lineHeight = 16.sp,459            modifier = Modifier.padding(bottom = 6.dp),460        )461    } else {462        Text(463            "Restoring locks your vault. Unlock with the password that encrypted that snapshot to confirm.",464            color = fg.copy(alpha = 0.68f),465            fontFamily = PeachType.Body,466            fontSize = 11.sp,467            lineHeight = 16.sp,468            modifier = Modifier.padding(bottom = 4.dp),469        )470        history.take(if (historyShowAll) history.size else 3).forEach { snap ->471            RowLine(fg) {472                Column(Modifier.weight(1f)) {473                    Text(snapshotTimeLabel(snap.savedAt), color = fg, fontFamily = PeachType.Body, fontSize = 12.sp)474                    Text(snapshotSubLabel(snap), color = fg.copy(alpha = 0.62f), fontFamily = PeachType.Mono, fontSize = 10.sp)475                }476                SubtleButton(if (restoringSnapshotId == snap.id) "..." else "restore", fg) {477                    vm.restoreVaultSnapshot(snap.id)478                }479            }480        }481        if (history.size > 3) {482            RowLine(fg) {483                Name(if (historyShowAll) "show less" else "${history.size - 3} older", fg)484                SubtleButton(if (historyShowAll) "hide" else "show", fg) { historyShowAll = !historyShowAll }485            }486        }487        Text(488            "${history.size} of 5 snapshots",489            color = fg.copy(alpha = 0.58f),490            fontFamily = PeachType.Mono,491            fontSize = 10.sp,492            modifier = Modifier.padding(top = 2.dp, bottom = 6.dp),493        )494    }495496    var advancedOpen by remember { mutableStateOf(false) }497    SubLabel("ADVANCED", fg)498    RowLine(fg) {499        Name("Advanced", fg)500        SubtleButton(if (advancedOpen) "hide" else "show", fg) { advancedOpen = !advancedOpen }501    }502    if (advancedOpen) {503        RowLine(fg) {504            Name("Replace vault from backup", com.peachpasswords.ui.theme.peachColors.SyncBusy)505            SubtleButton("open", com.peachpasswords.ui.theme.peachColors.SyncBusy) { onNavigate(PeachNav.Import) }506        }507        RowLine(fg) {508            Name("Destroy vault", com.peachpasswords.ui.theme.peachColors.Breach)509            SubtleButton("destroy", com.peachpasswords.ui.theme.peachColors.Breach) { onNavigate(PeachNav.Panic) }510        }511    }512}513514private fun snapshotSubLabel(snapshot: VaultSnapshot): String {515    val count = snapshot.entryCount?.let { "up to $it ${if (it == 1) "entry" else "entries"}" }516    return listOfNotNull(count, snapshotReasonLabel(snapshot.reason)).joinToString(" · ")517}518519private fun snapshotReasonLabel(reason: String): String = when (reason) {520    "local_save" -> "saved"521    "sync_received" -> "synced"522    "import" -> "imported"523    "restore" -> "restored"524    else -> reason525}526527private fun snapshotTimeLabel(savedAt: Long): String {528    val diff = System.currentTimeMillis() - savedAt529    if (diff < 60_000) return "Just now"530    if (diff < 3_600_000) return "${diff / 60_000}m ago"531    val date = Date(savedAt)532    val today = SimpleDateFormat("yyyyMMdd", Locale.getDefault()).format(Date())533    val thenDay = SimpleDateFormat("yyyyMMdd", Locale.getDefault()).format(date)534    val time = SimpleDateFormat("h:mm a", Locale.getDefault()).format(date)535    if (today == thenDay) return "Today $time"536    val yesterday = SimpleDateFormat("yyyyMMdd", Locale.getDefault()).format(Date(System.currentTimeMillis() - 86_400_000))537    if (yesterday == thenDay) return "Yesterday $time"538    return SimpleDateFormat("MMM d h:mm a", Locale.getDefault()).format(date)539}540541/* ────────────────────────────────  Sync  ────────────────────────────────── */542543@Composable544private fun SyncBody(fg: Color, onNavigate: (PeachNav) -> Unit) {545    val syncVm: SyncViewModel = hiltViewModel()546    val pairedDevices by syncVm.pairedDevices.collectAsState()547548    SubLabel("PAIRING", fg, top = 0.dp)549    RowLine(fg) {550        Column(Modifier.weight(1f)) {551            Row(verticalAlignment = Alignment.CenterVertically) {552                Text("Peach Sync", color = fg, fontFamily = PeachType.Body, fontSize = 13.sp); ProBadge(fg)553            }554            Text(555                "Pair nearby devices by chirp and keep encrypted vault blobs in step without a central account.",556                color = fg.copy(alpha = 0.7f), fontFamily = PeachType.Body, fontSize = 11.sp, lineHeight = 16.sp,557                modifier = Modifier.padding(top = 2.dp),558            )559        }560    }561    // The whole row is the tap target — no separate "open" button (browser562    // sync-hub parity: every settings row is tappable, the chevron signals it).563    RowLine(fg, Modifier.clickable { onNavigate(PeachNav.ChirpPair) }) {564        Name("Sync hub", fg)565        Text("›", color = fg.copy(alpha = 0.6f), fontFamily = PeachType.Mono, fontSize = 15.sp)566    }567    RowLine(fg) {568        Column(Modifier.weight(1f)) {569            Text("Pair new device", color = fg, fontFamily = PeachType.Body, fontSize = 13.sp)570            Text(571                "Add a phone, another browser, or your desktop sync helper.",572                color = fg.copy(alpha = 0.7f), fontFamily = PeachType.Body, fontSize = 11.sp, lineHeight = 16.sp,573                modifier = Modifier.padding(top = 2.dp),574            )575        }576        SubtleButton("pair", fg) { onNavigate(PeachNav.ChirpPair) }577    }578579    SubLabel("PAIRED DEVICES", fg)580    if (pairedDevices.isEmpty()) {581        Text(582            "No paired devices yet.",583            color = fg.copy(alpha = 0.7f), fontFamily = PeachType.Body, fontSize = 12.sp,584            modifier = Modifier.padding(top = 2.dp),585        )586    } else {587        pairedDevices.forEach { d ->588            RowLine(fg) {589                Name(d.deviceName ?: d.deviceId, fg)590                ValueText(if (d.lastSyncedAt != null) "synced" else "never", fg)591            }592        }593        RowLine(fg) { Name("Sync", fg); SubtleButton("sync now", fg) { syncVm.syncNow() } }594    }595596    Hint("Sync uses the same zero-knowledge vault format as backup and restore; pairing authorizes a device, not a cloud account.", fg)597}598599/* ──────────────────────────────  Shortcuts  ──────────────────────────────── */600601@Composable602private fun ShortcutsBody(fg: Color) {603    val bindings = listOf(604        "Copy password" to "⌘C", "Copy username" to "⌘⇧C", "Open entry" to "↵",605        "Edit entry" to "E", "New entry" to "⌘N", "Search" to "/",606    )607    SubLabel("KEYBINDINGS", fg, top = 0.dp)608    Hint("Shortcuts apply when a hardware keyboard is attached. Defaults shown below.", fg)609    Spacer(Modifier.height(8.dp))610    bindings.forEach { (label, key) ->611        RowLine(fg) {612            Name(label, fg)613            Box(614                Modifier.clip(RoundedCornerShape(4.dp)).background(fg.copy(alpha = 0.06f))615                    .border(1.dp, fg.copy(alpha = 0.32f), RoundedCornerShape(4.dp))616                    .padding(horizontal = 7.dp, vertical = 2.dp),617            ) { Text(key, color = fg, fontFamily = PeachType.Mono, fontSize = 10.sp) }618        }619    }620}621622/* ────────────────────────────────  Pro  ─────────────────────────────────── */623624@Composable625private fun ProBody(fg: Color, vm: SettingsViewModel) {626    val ctx = LocalContext.current627    val isPro by vm.isPro.collectAsState()628    if (isPro) {629        ProActiveBody(fg, vm)630        return631    }632    val features = listOf(633        "Peach Sync" to "Encrypted · peer-to-peer",634        "Peach Codex" to "Restore from paper",635        "Passkeys" to "Zero-knowledge WebAuthn",636        "Advanced Vault Health" to "Reuse · age · exposure",637        "Canary entries" to "Detects vault compromise",638        "Duress vault" to "Decoy under coercion",639    )640    Text("PEACH PRO · ONE-TIME", color = fg.copy(alpha = 0.6f), fontFamily = PeachType.Mono, fontSize = 8.sp, letterSpacing = 0.22.em)641    Text(642        "Everything, everywhere.",643        modifier = Modifier.padding(top = 8.dp),644        color = fg, fontFamily = PeachType.Display, fontStyle = FontStyle.Italic, fontSize = 24.sp,645    )646    Text(647        "One key unlocks every Pro feature — for the lifetime of the app.",648        modifier = Modifier.padding(top = 6.dp),649        color = fg.copy(alpha = 0.72f), fontFamily = PeachType.Body, fontSize = 12.sp, lineHeight = 17.sp,650    )651    Spacer(Modifier.height(12.dp))652    features.forEachIndexed { i, (name, desc) ->653        RowLine(fg) {654            Row(Modifier.weight(1f), verticalAlignment = Alignment.CenterVertically) {655                Text("%02d".format(i + 1), color = fg.copy(alpha = 0.5f), fontFamily = PeachType.Mono, fontSize = 10.sp, modifier = Modifier.padding(end = 10.dp))656                Text(name, color = fg, fontFamily = PeachType.Body, fontSize = 13.sp)657            }658            Text(desc, color = fg.copy(alpha = 0.6f), fontFamily = PeachType.Display, fontStyle = FontStyle.Italic, fontSize = 11.sp)659        }660    }661    Spacer(Modifier.height(14.dp))662    Text(663        "$39",664        color = fg, fontFamily = PeachType.Display, fontStyle = FontStyle.Italic, fontSize = 28.sp,665    )666    Text("once · yours forever", color = fg.copy(alpha = 0.6f), fontFamily = PeachType.Mono, fontSize = 9.sp, letterSpacing = 0.18.em)667    Spacer(Modifier.height(10.dp))668    Box(669        Modifier.fillMaxWidth().clip(RoundedCornerShape(10.dp))670            .background(fg.copy(alpha = 0.16f))671            .border(1.dp, fg.copy(alpha = 0.4f), RoundedCornerShape(10.dp))672            .clickable {673                runCatching { ctx.startActivity(Intent(Intent.ACTION_VIEW, Uri.parse(PRO_URL))) }674            }675            .padding(vertical = 12.dp),676        contentAlignment = Alignment.Center,677    ) { Text("Get Peach Pro", color = fg, fontFamily = PeachType.Mono, fontSize = 12.sp) }678679    Spacer(Modifier.height(16.dp))680    Text("LICENSE KEY", color = fg.copy(alpha = 0.6f), fontFamily = PeachType.Mono, fontSize = 8.sp, letterSpacing = 0.22.em)681    var key by remember { mutableStateOf("") }682    val activating by vm.activating.collectAsState()683    val activationError by vm.activationError.collectAsState()684    Box(685        Modifier.fillMaxWidth().padding(top = 6.dp)686            .border(1.dp, fg.copy(alpha = 0.3f), RoundedCornerShape(8.dp))687            .padding(horizontal = 12.dp, vertical = 10.dp),688    ) {689        BasicTextField(690            value = key,691            onValueChange = { key = it.uppercase(); vm.clearActivationError() },692            singleLine = true,693            textStyle = TextStyle(fontFamily = PeachType.Mono, fontSize = 13.sp, letterSpacing = 0.12.em, color = fg),694            cursorBrush = SolidColor(fg),695            modifier = Modifier.fillMaxWidth(),696            decorationBox = { inner ->697                if (key.isEmpty()) Text("PPK-XXXX-XXXX-XXXX", color = fg.copy(alpha = 0.3f), fontFamily = PeachType.Mono, fontSize = 13.sp, letterSpacing = 0.12.em)698                inner()699            },700        )701    }702    if (activationError != null) {703        Text(activationError!!, color = com.peachpasswords.ui.theme.peachColors.Breach, fontFamily = PeachType.Mono, fontSize = 10.sp, modifier = Modifier.padding(top = 6.dp))704    }705    Spacer(Modifier.height(8.dp))706    Box(707        Modifier.fillMaxWidth().clip(RoundedCornerShape(10.dp))708            .background(fg.copy(alpha = if (activating || key.isBlank()) 0.08f else 0.16f))709            .border(1.dp, fg.copy(alpha = 0.4f), RoundedCornerShape(10.dp))710            .clickable(enabled = !activating && key.isNotBlank()) { vm.activateLicense(key) }711            .padding(vertical = 12.dp),712        contentAlignment = Alignment.Center,713    ) { Text(if (activating) "Activating…" else "Activate", color = fg, fontFamily = PeachType.Mono, fontSize = 12.sp) }714}715716@Composable717private fun ProActiveBody(fg: Color, vm: SettingsViewModel) {718    val features = listOf("Peach Sync", "Peach Codex", "Passkeys", "Advanced Vault Health", "Canary entries", "Duress vault")719    Box(Modifier.fillMaxWidth().padding(vertical = 8.dp), contentAlignment = Alignment.Center) {720        com.peachpasswords.ui.components.PeachMark(size = 60.dp)721    }722    Text(723        "Peach Pro",724        modifier = Modifier.fillMaxWidth(),725        color = fg, fontFamily = PeachType.Display, fontStyle = FontStyle.Italic, fontSize = 26.sp,726        textAlign = androidx.compose.ui.text.style.TextAlign.Center,727    )728    Text(729        "active · yours forever",730        modifier = Modifier.fillMaxWidth().padding(top = 4.dp),731        color = fg.copy(alpha = 0.6f), fontFamily = PeachType.Mono, fontSize = 9.sp, letterSpacing = 0.18.em,732        textAlign = androidx.compose.ui.text.style.TextAlign.Center,733    )734    Spacer(Modifier.height(12.dp))735    features.forEachIndexed { i, name ->736        RowLine(fg) {737            Row(Modifier.weight(1f), verticalAlignment = Alignment.CenterVertically) {738                Text("%02d".format(i + 1), color = fg.copy(alpha = 0.5f), fontFamily = PeachType.Mono, fontSize = 10.sp, modifier = Modifier.padding(end = 10.dp))739                Text(name, color = fg, fontFamily = PeachType.Body, fontSize = 13.sp)740            }741            Text("unlocked", color = com.peachpasswords.ui.theme.peachColors.HealthOk, fontFamily = PeachType.Display, fontStyle = FontStyle.Italic, fontSize = 11.sp)742        }743    }744    Spacer(Modifier.height(12.dp))745    Box(746        Modifier.fillMaxWidth().clip(RoundedCornerShape(10.dp))747            .border(1.dp, com.peachpasswords.ui.theme.peachColors.Breach.copy(alpha = 0.4f), RoundedCornerShape(10.dp))748            .clickable { vm.deactivate() }749            .padding(vertical = 12.dp),750        contentAlignment = Alignment.Center,751    ) { Text("Deactivate", color = com.peachpasswords.ui.theme.peachColors.Breach, fontFamily = PeachType.Mono, fontSize = 12.sp) }752}753754/* ──────────────────────────────  Feedback  ───────────────────────────────── */755756@OptIn(ExperimentalLayoutApi::class)757@Composable758private fun FeedbackBody(fg: Color, vm: SettingsViewModel) {759    val categories = listOf("Bug report", "Feature request", "Question", "Other")760    var category by remember { mutableStateOf(categories[0]) }761    var message by remember { mutableStateOf("") }762    val state by vm.feedbackState.collectAsState()763764    // Reset transient state when the card leaves composition so reopening starts fresh.765    DisposableEffect(Unit) { onDispose { vm.resetFeedback() } }766767    if (state is FeedbackState.Sent) {768        Box(769            Modifier.fillMaxWidth().clip(RoundedCornerShape(10.dp)).background(fg.copy(alpha = 0.08f))770                .border(1.dp, fg.copy(alpha = 0.32f), RoundedCornerShape(10.dp)).padding(16.dp),771        ) {772            Column {773                Text("Thank you", color = fg, fontFamily = PeachType.Display, fontStyle = FontStyle.Italic, fontSize = 18.sp)774                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))775            }776        }777        return778    }779780    val sending = state is FeedbackState.Sending781782    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)783    SubLabel("TYPE", fg)784    FlowRow(horizontalArrangement = Arrangement.spacedBy(6.dp), verticalArrangement = Arrangement.spacedBy(6.dp)) {785        categories.forEach { cat -> Pill(cat, category == cat, fg) { if (!sending) category = cat } }786    }787    SubLabel("MESSAGE", fg)788    Box(789        Modifier.fillMaxWidth().height(110.dp).clip(RoundedCornerShape(8.dp))790            .background(Color(0x2E000000)).border(1.dp, fg.copy(alpha = 0.22f), RoundedCornerShape(8.dp))791            .padding(10.dp),792    ) {793        if (message.isEmpty()) Text("Describe the issue or idea...", color = fg.copy(alpha = 0.42f), fontFamily = PeachType.Body, fontSize = 12.sp)794        BasicTextField(795            value = message, onValueChange = { message = it },796            enabled = !sending,797            textStyle = TextStyle(color = fg, fontFamily = PeachType.Body, fontSize = 12.sp),798            cursorBrush = SolidColor(fg), modifier = Modifier.fillMaxWidth(),799        )800    }801    Spacer(Modifier.height(8.dp))802803    Row(verticalAlignment = Alignment.CenterVertically, horizontalArrangement = Arrangement.spacedBy(10.dp)) {804        val canSend = !sending && message.isNotBlank()805        Box(806            Modifier.clip(RoundedCornerShape(8.dp))807                .border(1.dp, fg.copy(alpha = if (canSend) 0.28f else 0.14f), RoundedCornerShape(8.dp))808                .background(fg.copy(alpha = if (canSend) 0.08f else 0.04f))809                .clickable(enabled = canSend) { vm.sendFeedback(category, message) }810                .padding(horizontal = 12.dp, vertical = 6.dp),811        ) {812            Text(813                if (state is FeedbackState.Error) "Retry" else "Send",814                color = fg.copy(alpha = if (canSend) 1f else 0.4f),815                fontFamily = PeachType.Mono, fontSize = 11.sp,816            )817        }818        if (sending) {819            CircularProgressIndicator(820                color = LocalPeachAccent.current,821                strokeWidth = 2.dp,822                modifier = Modifier.size(16.dp),823            )824        }825    }826827    (state as? FeedbackState.Error)?.let { err ->828        Text(829            err.message,830            modifier = Modifier.padding(top = 8.dp),831            color = peachColors.Breach,832            fontFamily = PeachType.Mono,833            fontSize = 11.sp,834            lineHeight = 15.sp,835        )836    }837838    Hint("Sent anonymously · no account required", fg)839}840841/* ──────────────────────────────  D13 Duress  ─────────────────────────────── */842843@Composable844internal fun DuressSection(fg: Color, vm: SettingsViewModel) {845    val configured by vm.duressConfigured.collectAsState()846    if (configured) DuressActiveState(fg, vm) else DuressForm(fg, vm)847}848849@Composable850private fun DuressActiveState(fg: Color, vm: SettingsViewModel) {851    Text(852        "Duress password is active",853        color = peachColors.HealthOk,854        fontFamily = PeachType.Mono,855        fontSize = 11.sp,856        modifier = Modifier.padding(top = 6.dp),857    )858    Spacer(Modifier.height(8.dp))859    DuressDangerGhostButton("Disable duress password", fg) { vm.disableDuress() }860    Hint("Entering your duress password instead of your real one triggers the action you chose.", fg)861}862863@Composable864private fun DuressForm(fg: Color, vm: SettingsViewModel) {865    var enabled by remember { mutableStateOf(false) }866    var password by remember { mutableStateOf("") }867    var confirm by remember { mutableStateOf("") }868    var action by remember { mutableStateOf(DuressAction.DECOY) }869    var error by remember { mutableStateOf<String?>(null) }870    val scope = rememberCoroutineScope()871872    RowLine(fg) {873        Name("Duress password", fg)874        Toggle(enabled, fg) { on ->875            enabled = on876            if (!on) { password = ""; confirm = ""; error = null }877        }878    }879    Text(880        "Entering your duress password instead of your real one opens a decoy vault or wipes the real one.",881        color = fg.copy(alpha = 0.7f),882        fontFamily = PeachType.Mono,883        fontSize = 10.sp,884        modifier = Modifier.padding(top = 6.dp),885    )886887    AnimatedVisibility(enabled) {888        Column(Modifier.fillMaxWidth().padding(top = 10.dp), verticalArrangement = Arrangement.spacedBy(6.dp)) {889            PwField("DURESS PASSPHRASE", password, fg) { password = it; error = null }890            PwField("CONFIRM DURESS PASSPHRASE", confirm, fg) { confirm = it; error = null }891892            Text(893                "When your duress password is entered at the lock screen, Peach will:",894                color = fg.copy(alpha = 0.7f),895                fontFamily = PeachType.Mono,896                fontSize = 10.sp,897                modifier = Modifier.padding(top = 4.dp),898            )899900            DuressRadioOption("Show a decoy vault", action == DuressAction.DECOY, fg) { action = DuressAction.DECOY }901            DuressRadioOption("Destroy the real vault", action == DuressAction.DESTROY, fg) { action = DuressAction.DESTROY }902903            error?.let { Text(it, color = peachColors.Breach, fontFamily = PeachType.Mono, fontSize = 10.sp) }904905            val canSave = password.isNotBlank() && confirm.isNotBlank()906            DuressSaveButton("Save duress password", fg, canSave) {907                error = when {908                    password.isBlank() || confirm.isBlank() -> "Enter and confirm a duress password"909                    password != confirm -> "Passwords do not match"910                    else -> null911                }912                if (error == null) scope.launch {913                    if (vm.saveDuress(password, action)) {914                        password = ""; confirm = ""; enabled = false915                    } else {916                        error = "Couldn't save — the duress passphrase must differ from your real password"917                    }918                }919            }920        }921    }922}923924@Composable925private fun DuressRadioOption(label: String, selected: Boolean, fg: Color, onClick: () -> Unit) {926    val accent = LocalPeachAccent.current927    Row(928        Modifier.fillMaxWidth().clip(RoundedCornerShape(8.dp)).clickable(onClick = onClick).padding(vertical = 7.dp),929        verticalAlignment = Alignment.CenterVertically,930        horizontalArrangement = Arrangement.spacedBy(10.dp),931    ) {932        Box(933            Modifier.size(16.dp).clip(CircleShape)934                .border(1.dp, if (selected) accent else fg.copy(alpha = 0.4f), CircleShape),935            contentAlignment = Alignment.Center,936        ) {937            if (selected) Box(Modifier.size(8.dp).clip(CircleShape).background(accent))938        }939        Text(label, color = if (selected) accent else fg, fontFamily = PeachType.Mono, fontSize = 11.sp)940    }941}942943@Composable944private fun DuressSaveButton(label: String, fg: Color, enabled: Boolean, onClick: () -> Unit) {945    val accent = LocalPeachAccent.current946    val tint = if (enabled) accent else fg.copy(alpha = 0.3f)947    Box(948        Modifier.fillMaxWidth().clip(RoundedCornerShape(8.dp))949            .border(1.dp, tint.copy(alpha = if (enabled) 0.6f else 0.3f), RoundedCornerShape(8.dp))950            .background(tint.copy(alpha = if (enabled) 0.16f else 0.06f))951            .let { if (enabled) it.clickable(onClick = onClick) else it }952            .padding(vertical = 11.dp),953        contentAlignment = Alignment.Center,954    ) {955        Text(label, color = if (enabled) accent else fg.copy(alpha = 0.4f), fontFamily = PeachType.Mono, fontSize = 11.sp)956    }957}958959@Composable960private fun DuressDangerGhostButton(label: String, fg: Color, onClick: () -> Unit) {961    Box(962        Modifier.fillMaxWidth().clip(RoundedCornerShape(8.dp))963            .border(1.dp, peachColors.Breach.copy(alpha = 0.5f), RoundedCornerShape(8.dp))964            .clickable(onClick = onClick)965            .padding(vertical = 11.dp),966        contentAlignment = Alignment.Center,967    ) {968        Text(label, color = peachColors.Breach, fontFamily = PeachType.Mono, fontSize = 11.sp)969    }970}