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"535455565758596061@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, onNavigate)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}80818283@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}182183184185@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}223224225226@Composable227private fun ProtectionBody(fg: Color, vm: SettingsViewModel, s: VaultSettings, onNavigate: (PeachNav) -> Unit) {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("RECOVERY", fg)246 RowLine(fg) {247 Name("View recovery words", fg)248 SubtleButton("view", fg) { onNavigate(PeachNav.RecoveryView) }249 }250 SubLabel("BREACH PROTECTION", fg)251 RowLine(fg) {252 Name("Breach monitoring", fg)253 Toggle(s.breachMonitoring, fg) { vm.setBreachMonitoring(it) }254 }255256 SubLabel("MASTER PASSWORD", fg)257 ChangePasswordRow(fg, vm)258259 SubLabel("DURESS PASSWORD", fg)260 RowLine(fg) {261 Row(Modifier.weight(1f), verticalAlignment = Alignment.CenterVertically) {262 Name("Duress password", fg); ProBadge(fg)263 }264 ValueText("off", fg)265 }266 Hint("If entered under coercion, a decoy empty vault opens; your real vault stays hidden. Arrives with Pro sync.", fg)267}268269@Composable270private fun ChangePasswordRow(fg: Color, vm: SettingsViewModel) {271 var open by remember { mutableStateOf(false) }272 var current by remember { mutableStateOf("") }273 var next by remember { mutableStateOf("") }274 var confirm by remember { mutableStateOf("") }275 var error by remember { mutableStateOf<String?>(null) }276 var done by remember { mutableStateOf(false) }277 val scope = rememberCoroutineScope()278279 RowLine(fg) {280 Name("Master password", fg)281 SubtleButton(if (open) "cancel" else "change", fg) { open = !open; error = null; done = false }282 }283 AnimatedVisibility(open) {284 Column(Modifier.fillMaxWidth().padding(top = 8.dp), verticalArrangement = Arrangement.spacedBy(6.dp)) {285 PwField("current password", current, fg) { current = it }286 PwField("new password (min 12 chars)", next, fg) { next = it }287 PwField("confirm new password", confirm, fg) { confirm = it }288 error?.let { Text(it, color = Color(0xFFE5805C), fontFamily = PeachType.Mono, fontSize = 10.sp) }289 if (done) Text("password changed ✓", color = fg, fontFamily = PeachType.Mono, fontSize = 10.sp)290 SubtleButton("save new password", fg) {291 error = when {292 current.isBlank() -> "enter your current password"293 next.length < 12 -> "new password must be at least 12 characters"294 next != confirm -> "new passwords do not match"295 next == current -> "new password must differ from current"296 else -> null297 }298 if (error == null) scope.launch {299 if (vm.changePassword(current, next)) {300 current = ""; next = ""; confirm = ""; open = false; done = true301 } else error = "incorrect current password"302 }303 }304 }305 }306}307308@Composable309private fun PwField(placeholder: String, value: String, fg: Color, onChange: (String) -> Unit) {310 Box(311 Modifier.fillMaxWidth().clip(RoundedCornerShape(8.dp))312 .background(Color(0x2E000000))313 .border(1.dp, fg.copy(alpha = 0.22f), RoundedCornerShape(8.dp))314 .padding(horizontal = 10.dp, vertical = 10.dp),315 ) {316 if (value.isEmpty()) {317 Text(placeholder, color = fg.copy(alpha = 0.42f), fontFamily = PeachType.Mono, fontSize = 12.sp)318 }319 BasicTextField(320 value = value,321 onValueChange = onChange,322 singleLine = true,323 textStyle = TextStyle(color = fg, fontFamily = PeachType.Mono, fontSize = 12.sp),324 cursorBrush = SolidColor(fg),325 visualTransformation = PasswordVisualTransformation(),326 modifier = Modifier.fillMaxWidth(),327 )328 }329}330331332333@Composable334private fun VaultBody(335 fg: Color,336 vm: SettingsViewModel,337 s: VaultSettings,338 entriesCount: Int,339 onNavigate: (PeachNav) -> Unit,340) {341 val trashOptions = listOf(7 to "7 days", 30 to "30 days", 90 to "90 days", 0 to "Never")342 val current = s.trashRetentionDays ?: 0343344 SubLabel("ENTRIES", fg, top = 0.dp)345 RowLine(fg) { Name("Entries", fg); ValueText("$entriesCount", fg) }346347 SubLabel("MAINTENANCE", fg)348 RowLine(fg) {349 Name("Trash auto-delete", fg)350 val idx = trashOptions.indexOfFirst { it.first == current }.coerceAtLeast(0)351 SubtleButton(trashOptions[idx].second, fg) {352 vm.setTrashRetentionDays(trashOptions[(idx + 1) % trashOptions.size].first)353 }354 }355 RowLine(fg) { Name("Advanced Vault Health", fg); SubtleButton("open", fg) { onNavigate(PeachNav.Health) } }356 RowLine(fg) { Name("Trash", fg); SubtleButton("open", fg) { onNavigate(PeachNav.Trash) } }357358 SubLabel("DATA", fg)359 RowLine(fg) { Name("Import passwords", fg); SubtleButton("open", fg) { onNavigate(PeachNav.Import) } }360 RowLine(fg) { Name("Export backup", fg); SubtleButton("open", fg) { onNavigate(PeachNav.Export) } }361 RowLine(fg) {362 Row(Modifier.weight(1f), verticalAlignment = Alignment.CenterVertically) { Name("Peach Codex", fg); ProBadge(fg) }363 SubtleButton("open", fg) { onNavigate(PeachNav.CodexGenerate) }364 }365 RowLine(fg) { Name("Restore from Codex", fg); SubtleButton("open", fg) { onNavigate(PeachNav.CodexRestore) } }366367 var advancedOpen by remember { mutableStateOf(false) }368 SubLabel("ADVANCED", fg)369 RowLine(fg) {370 Name("Advanced", fg)371 SubtleButton(if (advancedOpen) "hide" else "show", fg) { advancedOpen = !advancedOpen }372 }373 if (advancedOpen) {374 RowLine(fg) {375 Name("Replace vault from backup", com.peachpasswords.ui.theme.PeachColors.SyncBusy)376 SubtleButton("open", com.peachpasswords.ui.theme.PeachColors.SyncBusy) { onNavigate(PeachNav.Import) }377 }378 RowLine(fg) {379 Name("Destroy vault", com.peachpasswords.ui.theme.PeachColors.Breach)380 SubtleButton("destroy", com.peachpasswords.ui.theme.PeachColors.Breach) { onNavigate(PeachNav.Panic) }381 }382 }383}384385386387@Composable388private fun SyncBody(fg: Color, onNavigate: (PeachNav) -> Unit) {389 SubLabel("PAIRING", fg, top = 0.dp)390 RowLine(fg) {391 Column(Modifier.weight(1f)) {392 Row(verticalAlignment = Alignment.CenterVertically) {393 Text("Peach Sync", color = fg, fontFamily = PeachType.Body, fontSize = 13.sp); ProBadge(fg)394 }395 Text(396 "Pair nearby devices by chirp and keep encrypted vault blobs in step without a central account.",397 color = fg.copy(alpha = 0.7f), fontFamily = PeachType.Body, fontSize = 11.sp, lineHeight = 16.sp,398 modifier = Modifier.padding(top = 2.dp),399 )400 }401 }402 RowLine(fg) { Name("Status", fg); ValueText("Requires Pro", fg) }403 RowLine(fg) { Name("Sync hub", fg); SubtleButton("open", fg) { onNavigate(PeachNav.ChirpPair) } }404405 SubLabel("DEVICE FLOW", fg)406 RowLine(fg) { Name("Pair new device", fg); SubtleButton("pair", fg) { onNavigate(PeachNav.ChirpPair) } }407 Hint("Sync uses the same zero-knowledge vault format as backup and restore; pairing authorizes a device, not a cloud account.", fg)408}409410411412@Composable413private fun ShortcutsBody(fg: Color) {414 val bindings = listOf(415 "Copy password" to "⌘C", "Copy username" to "⌘⇧C", "Open entry" to "↵",416 "Edit entry" to "E", "New entry" to "⌘N", "Search" to "/",417 )418 SubLabel("KEYBINDINGS", fg, top = 0.dp)419 Hint("Shortcuts apply when a hardware keyboard is attached. Defaults shown below.", fg)420 Spacer(Modifier.height(8.dp))421 bindings.forEach { (label, key) ->422 RowLine(fg) {423 Name(label, fg)424 Box(425 Modifier.clip(RoundedCornerShape(4.dp)).background(fg.copy(alpha = 0.06f))426 .border(1.dp, fg.copy(alpha = 0.32f), RoundedCornerShape(4.dp))427 .padding(horizontal = 7.dp, vertical = 2.dp),428 ) { Text(key, color = fg, fontFamily = PeachType.Mono, fontSize = 10.sp) }429 }430 }431}432433434435@Composable436private fun ProBody(fg: Color) {437 val ctx = LocalContext.current438 val features = listOf(439 "Peach Sync" to "Encrypted · peer-to-peer",440 "Peach Codex" to "Restore from paper",441 "Passkeys" to "Zero-knowledge WebAuthn",442 "Advanced Vault Health" to "Reuse · age · exposure",443 "Canary entries" to "Detects vault compromise",444 "Duress vault" to "Decoy under coercion",445 )446 Text("PEACH PRO · ONE-TIME", color = fg.copy(alpha = 0.6f), fontFamily = PeachType.Mono, fontSize = 8.sp, letterSpacing = 0.22.em)447 Text(448 "Everything, everywhere.",449 modifier = Modifier.padding(top = 8.dp),450 color = fg, fontFamily = PeachType.Display, fontStyle = FontStyle.Italic, fontSize = 24.sp,451 )452 Text(453 "One key unlocks every Pro feature — for the lifetime of the app.",454 modifier = Modifier.padding(top = 6.dp),455 color = fg.copy(alpha = 0.72f), fontFamily = PeachType.Body, fontSize = 12.sp, lineHeight = 17.sp,456 )457 Spacer(Modifier.height(12.dp))458 features.forEachIndexed { i, (name, desc) ->459 RowLine(fg) {460 Row(Modifier.weight(1f), verticalAlignment = Alignment.CenterVertically) {461 Text("%02d".format(i + 1), color = fg.copy(alpha = 0.5f), fontFamily = PeachType.Mono, fontSize = 10.sp, modifier = Modifier.padding(end = 10.dp))462 Text(name, color = fg, fontFamily = PeachType.Body, fontSize = 13.sp)463 }464 Text(desc, color = fg.copy(alpha = 0.6f), fontFamily = PeachType.Display, fontStyle = FontStyle.Italic, fontSize = 11.sp)465 }466 }467 Spacer(Modifier.height(14.dp))468 Text(469 "$39",470 color = fg, fontFamily = PeachType.Display, fontStyle = FontStyle.Italic, fontSize = 28.sp,471 )472 Text("once · yours forever", color = fg.copy(alpha = 0.6f), fontFamily = PeachType.Mono, fontSize = 9.sp, letterSpacing = 0.18.em)473 Spacer(Modifier.height(10.dp))474 Box(475 Modifier.fillMaxWidth().clip(RoundedCornerShape(10.dp))476 .background(fg.copy(alpha = 0.16f))477 .border(1.dp, fg.copy(alpha = 0.4f), RoundedCornerShape(10.dp))478 .clickable {479 runCatching { ctx.startActivity(Intent(Intent.ACTION_VIEW, Uri.parse(PRO_URL))) }480 }481 .padding(vertical = 12.dp),482 contentAlignment = Alignment.Center,483 ) { Text("Get Peach Pro", color = fg, fontFamily = PeachType.Mono, fontSize = 12.sp) }484 Hint("Already have a key? License activation arrives with Pro sync in a later update.", fg)485}486487488489@OptIn(ExperimentalLayoutApi::class)490@Composable491private fun FeedbackBody(fg: Color) {492 val categories = listOf("Bug report", "Feature request", "Question", "Other")493 var category by remember { mutableStateOf(categories[0]) }494 var message by remember { mutableStateOf("") }495 var sent by remember { mutableStateOf(false) }496497 if (sent) {498 Box(499 Modifier.fillMaxWidth().clip(RoundedCornerShape(10.dp)).background(fg.copy(alpha = 0.08f))500 .border(1.dp, fg.copy(alpha = 0.32f), RoundedCornerShape(10.dp)).padding(16.dp),501 ) {502 Column {503 Text("Thank you", color = fg, fontFamily = PeachType.Display, fontStyle = FontStyle.Italic, fontSize = 18.sp)504 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))505 }506 }507 return508 }509510 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)511 SubLabel("TYPE", fg)512 FlowRow(horizontalArrangement = Arrangement.spacedBy(6.dp), verticalArrangement = Arrangement.spacedBy(6.dp)) {513 categories.forEach { cat -> Pill(cat, category == cat, fg) { category = cat } }514 }515 SubLabel("MESSAGE", fg)516 Box(517 Modifier.fillMaxWidth().height(110.dp).clip(RoundedCornerShape(8.dp))518 .background(Color(0x2E000000)).border(1.dp, fg.copy(alpha = 0.22f), RoundedCornerShape(8.dp))519 .padding(10.dp),520 ) {521 if (message.isEmpty()) Text("Describe the issue or idea...", color = fg.copy(alpha = 0.42f), fontFamily = PeachType.Body, fontSize = 12.sp)522 BasicTextField(523 value = message, onValueChange = { message = it },524 textStyle = TextStyle(color = fg, fontFamily = PeachType.Body, fontSize = 12.sp),525 cursorBrush = SolidColor(fg), modifier = Modifier.fillMaxWidth(),526 )527 }528 Spacer(Modifier.height(8.dp))529 SubtleButton("Send", fg) { if (message.isNotBlank()) sent = true }530 Hint("Sent anonymously · no account required", fg)531}