package com.peachpasswords.ui.vault import androidx.compose.animation.core.animateDpAsState import androidx.compose.animation.core.tween import androidx.compose.foundation.background import androidx.compose.foundation.border import androidx.compose.foundation.clickable import androidx.compose.foundation.layout.Arrangement import androidx.compose.foundation.layout.Box import androidx.compose.foundation.layout.Column import androidx.compose.foundation.layout.Row import androidx.compose.foundation.layout.Spacer import androidx.compose.foundation.layout.fillMaxSize import androidx.compose.foundation.layout.fillMaxWidth import androidx.compose.foundation.layout.height import androidx.compose.foundation.layout.offset import androidx.compose.foundation.layout.padding import androidx.compose.foundation.layout.statusBarsPadding import androidx.compose.foundation.layout.width import androidx.compose.foundation.rememberScrollState import androidx.compose.foundation.shape.RoundedCornerShape import androidx.compose.foundation.text.BasicTextField import androidx.compose.foundation.verticalScroll import androidx.compose.material3.Slider import androidx.compose.material3.SliderDefaults import androidx.compose.material3.Text import androidx.compose.runtime.Composable import androidx.compose.runtime.getValue import androidx.compose.runtime.mutableStateOf import androidx.compose.runtime.remember import androidx.compose.runtime.setValue import androidx.compose.ui.Alignment import androidx.compose.ui.Modifier import androidx.compose.ui.draw.clip import androidx.compose.ui.draw.clipToBounds import androidx.compose.ui.graphics.Color import androidx.compose.ui.graphics.SolidColor import androidx.compose.ui.text.TextStyle import androidx.compose.ui.text.font.FontStyle import androidx.compose.ui.text.font.FontWeight import androidx.compose.ui.text.input.PasswordVisualTransformation import androidx.compose.ui.text.input.VisualTransformation import androidx.compose.ui.unit.dp import androidx.compose.ui.unit.em import androidx.compose.ui.unit.sp import androidx.compose.runtime.collectAsState import androidx.hilt.navigation.compose.hiltViewModel import com.peachpasswords.ui.theme.LocalPeachAccent import com.peachpasswords.ui.theme.PeachColors import com.peachpasswords.ui.theme.PeachMotion import com.peachpasswords.ui.theme.PeachRadius import com.peachpasswords.ui.theme.PeachType import com.peachpasswords.ui.theme.ShellBackground import com.peachpasswords.vault.PasswordHistoryItem @Composable fun EntryEditScreen( id: String?, onBack: () -> Unit, vm: EntryEditViewModel = hiltViewModel(), ) { val f by vm.form.collectAsState() val accent = LocalPeachAccent.current ShellBackground { Column(Modifier.fillMaxSize().statusBarsPadding()) { Row( Modifier.fillMaxWidth().height(56.dp).padding(horizontal = 18.dp), verticalAlignment = Alignment.CenterVertically, horizontalArrangement = Arrangement.SpaceBetween, ) { Text("cancel", color = PeachColors.T1, fontFamily = PeachType.Mono, fontSize = 12.sp, modifier = Modifier.clip(RoundedCornerShape(8.dp)).clickable(onClick = onBack).padding(8.dp)) Text(if (vm.isNew) "new entry" else "edit", color = PeachColors.T1, fontFamily = PeachType.Display, fontStyle = FontStyle.Italic, fontSize = 16.sp) Text("save", color = accent, fontFamily = PeachType.Mono, fontSize = 13.sp, modifier = Modifier.clip(RoundedCornerShape(8.dp)).clickable { vm.save(onBack) }.padding(8.dp)) } Box(Modifier.fillMaxSize()) { // Ghost PeachScript watermark behind the form (matches the detail hero). if (f.title.isNotBlank()) { Text( text = f.title.first().uppercase(), style = TextStyle(fontFamily = PeachType.Script, fontSize = 140.sp), color = PeachColors.T0.copy(alpha = 0.03f), modifier = Modifier.align(Alignment.TopEnd).offset(x = (-16).dp, y = 8.dp), ) } Column( Modifier.fillMaxSize().verticalScroll(rememberScrollState()).padding(horizontal = 22.dp), ) { Row(horizontalArrangement = Arrangement.spacedBy(8.dp)) { listOf("login", "note", "card").forEach { t -> EntryTypePill(t, f.type == t) { vm.setType(t) } } } Spacer(Modifier.height(18.dp)) Field("title", f.title, vm::setTitle) if (f.type == "login") { Field("url", f.url, vm::setUrl) Field("username", f.username, vm::setUsername) Field("email", f.email, vm::setEmail) PasswordField( value = f.password, show = f.showPassword, onChange = vm::setPassword, onToggleShow = vm::toggleShowPassword, onToggleGenerator = vm::toggleGenerator, ) GeneratorDrawer( open = f.generatorOpen, length = f.genLength, upper = f.genUpper, lower = f.genLower, numbers = f.genNumbers, symbols = f.genSymbols, onLength = vm::setGenLength, onUpper = vm::setGenUpper, onLower = vm::setGenLower, onNumbers = vm::setGenNumbers, onSymbols = vm::setGenSymbols, onGenerate = vm::generate, ) PasswordHistorySection( history = f.history, expanded = f.showHistory, onToggle = vm::toggleHistory, onRestore = vm::restorePassword, onCopy = vm::copyHistoryPassword, ) Field("2FA secret (otpauth:// or base32)", f.totp, vm::setTotp) } Field("notes", f.notes, vm::setNotes, singleLine = false) f.error?.let { Spacer(Modifier.height(8.dp)) Text(it, color = PeachColors.Breach, fontFamily = PeachType.Mono, fontSize = 11.sp) } Spacer(Modifier.height(40.dp)) } } } } } @Composable private fun EntryTypePill(type: String, selected: Boolean, onClick: () -> Unit) { val accent = LocalPeachAccent.current Box( modifier = Modifier .clip(RoundedCornerShape(PeachRadius.TypeTab)) .border( 1.dp, if (selected) accent.copy(alpha = 0.5f) else PeachColors.T3, RoundedCornerShape(PeachRadius.TypeTab), ) .background(if (selected) accent.copy(alpha = 0.08f) else Color.Transparent) .clickable(onClick = onClick) .padding(horizontal = 12.dp, vertical = 5.dp), ) { Text( text = type.uppercase(), color = if (selected) accent else PeachColors.T2, fontFamily = PeachType.Mono, fontSize = 9.sp, letterSpacing = 0.08.em, ) } } @Composable private fun Field( label: String, value: String, onChange: (String) -> Unit, singleLine: Boolean = true, transformation: VisualTransformation = VisualTransformation.None, trailing: (@Composable () -> Unit)? = null, ) { val accent = LocalPeachAccent.current Column(Modifier.fillMaxWidth().padding(bottom = 14.dp)) { Text(label.uppercase(), color = PeachColors.T1, fontFamily = PeachType.Mono, fontSize = 8.sp) Spacer(Modifier.height(6.dp)) Row(verticalAlignment = Alignment.CenterVertically) { Box( Modifier.weight(1f) .border(1.dp, PeachColors.T3, RoundedCornerShape(10.dp)) .padding(horizontal = 12.dp, vertical = 12.dp), ) { BasicTextField( value = value, onValueChange = onChange, singleLine = singleLine, textStyle = TextStyle(color = PeachColors.T0, fontFamily = PeachType.Mono, fontSize = 13.sp), cursorBrush = SolidColor(accent), visualTransformation = transformation, modifier = Modifier.fillMaxWidth(), ) } if (trailing != null) { Spacer(Modifier.width(8.dp)); trailing() } } } } @Composable private fun PasswordField( value: String, show: Boolean, onChange: (String) -> Unit, onToggleShow: () -> Unit, onToggleGenerator: () -> Unit, ) { Field( label = "password", value = value, onChange = onChange, transformation = if (show) VisualTransformation.None else PasswordVisualTransformation(), trailing = { Text(if (show) "hide" else "show", color = LocalPeachAccent.current, fontFamily = PeachType.Mono, fontSize = 9.sp, modifier = Modifier.clip(RoundedCornerShape(6.dp)).clickable(onClick = onToggleShow).padding(6.dp)) Text("⟳", color = LocalPeachAccent.current, fontSize = 16.sp, modifier = Modifier.clip(RoundedCornerShape(6.dp)).clickable(onClick = onToggleGenerator).padding(6.dp)) }, ) } @Composable private fun GeneratorDrawer( open: Boolean, length: Int, upper: Boolean, lower: Boolean, numbers: Boolean, symbols: Boolean, onLength: (Int) -> Unit, onUpper: (Boolean) -> Unit, onLower: (Boolean) -> Unit, onNumbers: (Boolean) -> Unit, onSymbols: (Boolean) -> Unit, onGenerate: () -> Unit, ) { val accent = LocalPeachAccent.current val height by animateDpAsState( targetValue = if (open) 188.dp else 0.dp, animationSpec = tween(320, easing = PeachMotion.Spring), label = "gen-drawer", ) Box( Modifier.fillMaxWidth().height(height).clipToBounds() .clip(RoundedCornerShape(12.dp)).background(PeachColors.S2), ) { Column(Modifier.fillMaxWidth().padding(16.dp)) { Row(Modifier.fillMaxWidth(), horizontalArrangement = Arrangement.SpaceBetween) { Text("GENERATOR", color = PeachColors.T1, fontFamily = PeachType.Mono, fontSize = 8.sp) Text("length $length", color = accent, fontFamily = PeachType.Mono, fontSize = 9.sp) } Slider( value = length.toFloat(), onValueChange = { onLength(it.toInt()) }, valueRange = 8f..64f, colors = SliderDefaults.colors(thumbColor = accent, activeTrackColor = accent, inactiveTrackColor = PeachColors.T3), ) Row(horizontalArrangement = Arrangement.spacedBy(8.dp)) { TogglePill("A-Z", upper, onUpper) TogglePill("a-z", lower, onLower) TogglePill("0-9", numbers, onNumbers) TogglePill("!@#", symbols, onSymbols) } Spacer(Modifier.height(12.dp)) Box( Modifier.fillMaxWidth().clip(RoundedCornerShape(10.dp)) .border(1.dp, accent.copy(alpha = 0.5f), RoundedCornerShape(10.dp)) .background(accent.copy(alpha = 0.10f)) .clickable(onClick = onGenerate).padding(vertical = 12.dp), contentAlignment = Alignment.Center, ) { Text("generate", color = accent, fontFamily = PeachType.Mono, fontSize = 12.sp) } } } if (open) Spacer(Modifier.height(14.dp)) } @Composable private fun TogglePill(label: String, on: Boolean, onToggle: (Boolean) -> Unit) { val accent = LocalPeachAccent.current Box( Modifier.clip(RoundedCornerShape(PeachRadius.TypeTab)) .border(1.dp, if (on) accent.copy(alpha = 0.5f) else PeachColors.T3, RoundedCornerShape(PeachRadius.TypeTab)) .background(if (on) accent.copy(alpha = 0.10f) else Color.Transparent) .clickable { onToggle(!on) } .padding(horizontal = 10.dp, vertical = 6.dp), ) { Text(label, color = if (on) accent else PeachColors.T2, fontFamily = PeachType.Mono, fontSize = 9.sp) } } /** * Past-password history. The toggle is always present (even with no history) so * the affordance is discoverable — empty state explains how it fills. Rows are * masked until revealed; "use" loads the value back into the editor so the * normal save records the swap. */ @Composable private fun PasswordHistorySection( history: List, expanded: Boolean, onToggle: () -> Unit, onRestore: (String) -> Unit, onCopy: (String) -> Unit, ) { val accent = LocalPeachAccent.current Column(Modifier.fillMaxWidth().padding(bottom = 14.dp)) { val label = if (history.isEmpty()) "↺ history" else "↺ history (${history.size})" Text( if (expanded) "↺ hide history" else label, color = accent.copy(alpha = 0.7f), fontFamily = PeachType.Display, fontStyle = FontStyle.Italic, fontSize = 15.sp, modifier = Modifier.clip(RoundedCornerShape(6.dp)).clickable(onClick = onToggle).padding(vertical = 4.dp), ) if (expanded) { Spacer(Modifier.height(8.dp)) if (history.isEmpty()) { Box( Modifier.fillMaxWidth().clip(RoundedCornerShape(8.dp)) .border(1.dp, accent.copy(alpha = 0.12f), RoundedCornerShape(8.dp)) .background(accent.copy(alpha = 0.05f)).padding(10.dp), ) { Text( "No earlier passwords yet. When you change this password, the old one is saved here so you can copy or restore it.", color = PeachColors.T2, fontFamily = PeachType.Mono, fontSize = 10.sp, ) } } else { Column(verticalArrangement = Arrangement.spacedBy(6.dp)) { history.forEach { item -> HistoryRow(item, onRestore, onCopy) } } } } } } @Composable private fun HistoryRow( item: PasswordHistoryItem, onRestore: (String) -> Unit, onCopy: (String) -> Unit, ) { val accent = LocalPeachAccent.current var revealed by remember { mutableStateOf(false) } Row( Modifier.fillMaxWidth().clip(RoundedCornerShape(8.dp)) .border(1.dp, accent.copy(alpha = 0.12f), RoundedCornerShape(8.dp)) .background(accent.copy(alpha = 0.06f)).padding(horizontal = 10.dp, vertical = 8.dp), verticalAlignment = Alignment.CenterVertically, horizontalArrangement = Arrangement.SpaceBetween, ) { Column(Modifier.weight(1f)) { Text( if (revealed) item.password else "••••••••••••", color = PeachColors.T0, fontFamily = PeachType.Mono, fontSize = 12.sp, maxLines = 1, ) Text( timeSince(item.changedAt), color = accent.copy(alpha = 0.55f), fontFamily = PeachType.Mono, fontSize = 8.sp, ) } Spacer(Modifier.width(6.dp)) HistoryAction(if (revealed) "hide" else "show") { revealed = !revealed } HistoryAction("copy") { onCopy(item.password) } HistoryAction("use") { onRestore(item.password) } } } @Composable private fun HistoryAction(label: String, onClick: () -> Unit) { Text( label, color = LocalPeachAccent.current.copy(alpha = 0.7f), fontFamily = PeachType.Mono, fontSize = 9.sp, modifier = Modifier.clip(RoundedCornerShape(5.dp)).clickable(onClick = onClick).padding(horizontal = 5.dp, vertical = 3.dp), ) } /** Coarse "time since" label for the history list. Mirrors the browser helper. */ private fun timeSince(whenMs: Long): String { val secs = ((System.currentTimeMillis() - whenMs) / 1000).coerceAtLeast(0) if (secs < 60) return "just now" val mins = secs / 60 if (mins < 60) return "${mins}m ago" val hours = mins / 60 if (hours < 24) return "${hours}h ago" val days = hours / 24 if (days < 30) return "${days}d ago" val months = days / 30 if (months < 12) return "${months}mo ago" return "${months / 12}y ago" }