1package com.peachpasswords.ui.vault23import androidx.compose.animation.core.animateDpAsState4import androidx.compose.animation.core.tween5import androidx.compose.foundation.background6import androidx.compose.foundation.border7import androidx.compose.foundation.clickable8import androidx.compose.foundation.layout.Arrangement9import androidx.compose.foundation.layout.Box10import androidx.compose.foundation.layout.Column11import androidx.compose.foundation.layout.Row12import androidx.compose.foundation.layout.Spacer13import androidx.compose.foundation.layout.fillMaxSize14import androidx.compose.foundation.layout.fillMaxWidth15import androidx.compose.foundation.layout.height16import androidx.compose.foundation.layout.offset17import androidx.compose.foundation.layout.padding18import androidx.compose.foundation.layout.statusBarsPadding19import androidx.compose.foundation.layout.width20import androidx.compose.foundation.rememberScrollState21import androidx.compose.foundation.shape.RoundedCornerShape22import androidx.compose.foundation.text.BasicTextField23import androidx.compose.foundation.verticalScroll24import androidx.compose.material3.Slider25import androidx.compose.material3.SliderDefaults26import androidx.compose.material3.Text27import androidx.compose.runtime.Composable28import androidx.compose.runtime.getValue29import androidx.compose.runtime.mutableStateOf30import androidx.compose.runtime.remember31import androidx.compose.runtime.setValue32import androidx.compose.ui.Alignment33import androidx.compose.ui.Modifier34import androidx.compose.ui.draw.clip35import androidx.compose.ui.draw.clipToBounds36import androidx.compose.ui.graphics.Color37import androidx.compose.ui.graphics.SolidColor38import androidx.compose.ui.text.TextStyle39import androidx.compose.ui.text.font.FontStyle40import androidx.compose.ui.text.font.FontWeight41import androidx.compose.ui.text.input.PasswordVisualTransformation42import androidx.compose.ui.text.input.VisualTransformation43import androidx.compose.ui.unit.dp44import androidx.compose.ui.unit.em45import androidx.compose.ui.unit.sp46import androidx.compose.runtime.collectAsState47import androidx.hilt.navigation.compose.hiltViewModel48import com.peachpasswords.ui.theme.LocalPeachAccent49import com.peachpasswords.ui.theme.PeachColors50import com.peachpasswords.ui.theme.PeachMotion51import com.peachpasswords.ui.theme.PeachRadius52import com.peachpasswords.ui.theme.PeachType53import com.peachpasswords.ui.theme.ShellBackground54import com.peachpasswords.vault.PasswordHistoryItem5556@Composable57fun EntryEditScreen(58 id: String?,59 onBack: () -> Unit,60 vm: EntryEditViewModel = hiltViewModel(),61) {62 val f by vm.form.collectAsState()63 val accent = LocalPeachAccent.current6465 ShellBackground {66 Column(Modifier.fillMaxSize().statusBarsPadding()) {67 Row(68 Modifier.fillMaxWidth().height(56.dp).padding(horizontal = 18.dp),69 verticalAlignment = Alignment.CenterVertically,70 horizontalArrangement = Arrangement.SpaceBetween,71 ) {72 Text("cancel", color = PeachColors.T1, fontFamily = PeachType.Mono, fontSize = 12.sp,73 modifier = Modifier.clip(RoundedCornerShape(8.dp)).clickable(onClick = onBack).padding(8.dp))74 Text(if (vm.isNew) "new entry" else "edit", color = PeachColors.T1,75 fontFamily = PeachType.Display, fontStyle = FontStyle.Italic, fontSize = 16.sp)76 Text("save", color = accent, fontFamily = PeachType.Mono, fontSize = 13.sp,77 modifier = Modifier.clip(RoundedCornerShape(8.dp)).clickable { vm.save(onBack) }.padding(8.dp))78 }7980 Box(Modifier.fillMaxSize()) {81 82 if (f.title.isNotBlank()) {83 Text(84 text = f.title.first().uppercase(),85 style = TextStyle(fontFamily = PeachType.Script, fontSize = 140.sp),86 color = PeachColors.T0.copy(alpha = 0.03f),87 modifier = Modifier.align(Alignment.TopEnd).offset(x = (-16).dp, y = 8.dp),88 )89 }90 Column(91 Modifier.fillMaxSize().verticalScroll(rememberScrollState()).padding(horizontal = 22.dp),92 ) {93 Row(horizontalArrangement = Arrangement.spacedBy(8.dp)) {94 listOf("login", "note", "card").forEach { t ->95 EntryTypePill(t, f.type == t) { vm.setType(t) }96 }97 }98 Spacer(Modifier.height(18.dp))99100 Field("title", f.title, vm::setTitle)101 if (f.type == "login") {102 Field("url", f.url, vm::setUrl)103 Field("username", f.username, vm::setUsername)104 Field("email", f.email, vm::setEmail)105 PasswordField(106 value = f.password,107 show = f.showPassword,108 onChange = vm::setPassword,109 onToggleShow = vm::toggleShowPassword,110 onToggleGenerator = vm::toggleGenerator,111 )112 GeneratorDrawer(113 open = f.generatorOpen,114 length = f.genLength,115 upper = f.genUpper, lower = f.genLower, numbers = f.genNumbers, symbols = f.genSymbols,116 onLength = vm::setGenLength,117 onUpper = vm::setGenUpper, onLower = vm::setGenLower,118 onNumbers = vm::setGenNumbers, onSymbols = vm::setGenSymbols,119 onGenerate = vm::generate,120 )121 PasswordHistorySection(122 history = f.history,123 expanded = f.showHistory,124 onToggle = vm::toggleHistory,125 onRestore = vm::restorePassword,126 onCopy = vm::copyHistoryPassword,127 )128 Field("2FA secret (otpauth:// or base32)", f.totp, vm::setTotp)129 }130 if (f.type == "card") {131 CardEditFields(132 cardNumber = f.cardNumber,133 cardholderName = f.cardholderName,134 expiry = f.expiry,135 cvc = f.cvc,136 showCvc = f.showCvc,137 network = f.cardNetwork,138 onCardNumber = vm::setCardNumber,139 onCardholder = vm::setCardholderName,140 onExpiry = vm::setExpiry,141 onCvc = vm::setCvc,142 onToggleShowCvc = vm::toggleShowCvc,143 )144 }145 Field("notes", f.notes, vm::setNotes, singleLine = false)146 TagChipEditor(147 tags = f.tags,148 allTags = vm.allTags,149 onAdd = vm::addTag,150 onRemove = vm::removeTag,151 )152153 f.error?.let {154 Spacer(Modifier.height(8.dp))155 Text(it, color = PeachColors.Breach, fontFamily = PeachType.Mono, fontSize = 11.sp)156 }157 Spacer(Modifier.height(40.dp))158 }159 }160 }161 }162}163164@Composable165private fun EntryTypePill(type: String, selected: Boolean, onClick: () -> Unit) {166 val accent = LocalPeachAccent.current167 Box(168 modifier = Modifier169 .clip(RoundedCornerShape(PeachRadius.TypeTab))170 .border(171 1.dp,172 if (selected) accent.copy(alpha = 0.5f) else PeachColors.T3,173 RoundedCornerShape(PeachRadius.TypeTab),174 )175 .background(if (selected) accent.copy(alpha = 0.08f) else Color.Transparent)176 .clickable(onClick = onClick)177 .padding(horizontal = 12.dp, vertical = 5.dp),178 ) {179 Text(180 text = type.uppercase(),181 color = if (selected) accent else PeachColors.T2,182 fontFamily = PeachType.Mono,183 fontSize = 9.sp,184 letterSpacing = 0.08.em,185 )186 }187}188189@Composable190private fun Field(191 label: String,192 value: String,193 onChange: (String) -> Unit,194 singleLine: Boolean = true,195 transformation: VisualTransformation = VisualTransformation.None,196 trailing: (@Composable () -> Unit)? = null,197) {198 val accent = LocalPeachAccent.current199 Column(Modifier.fillMaxWidth().padding(bottom = 14.dp)) {200 Text(label.uppercase(), color = PeachColors.T1, fontFamily = PeachType.Mono, fontSize = 8.sp)201 Spacer(Modifier.height(6.dp))202 Row(verticalAlignment = Alignment.CenterVertically) {203 Box(204 Modifier.weight(1f)205 .border(1.dp, PeachColors.T3, RoundedCornerShape(10.dp))206 .padding(horizontal = 12.dp, vertical = 12.dp),207 ) {208 BasicTextField(209 value = value,210 onValueChange = onChange,211 singleLine = singleLine,212 textStyle = TextStyle(color = PeachColors.T0, fontFamily = PeachType.Mono, fontSize = 13.sp),213 cursorBrush = SolidColor(accent),214 visualTransformation = transformation,215 modifier = Modifier.fillMaxWidth(),216 )217 }218 if (trailing != null) { Spacer(Modifier.width(8.dp)); trailing() }219 }220 }221}222223@Composable224private fun PasswordField(225 value: String,226 show: Boolean,227 onChange: (String) -> Unit,228 onToggleShow: () -> Unit,229 onToggleGenerator: () -> Unit,230) {231 Field(232 label = "password",233 value = value,234 onChange = onChange,235 transformation = if (show) VisualTransformation.None else PasswordVisualTransformation(),236 trailing = {237 Text(if (show) "hide" else "show", color = LocalPeachAccent.current,238 fontFamily = PeachType.Mono, fontSize = 9.sp,239 modifier = Modifier.clip(RoundedCornerShape(6.dp)).clickable(onClick = onToggleShow).padding(6.dp))240 Text("⟳", color = LocalPeachAccent.current, fontSize = 16.sp,241 modifier = Modifier.clip(RoundedCornerShape(6.dp)).clickable(onClick = onToggleGenerator).padding(6.dp))242 },243 )244}245246@Composable247private fun GeneratorDrawer(248 open: Boolean,249 length: Int,250 upper: Boolean, lower: Boolean, numbers: Boolean, symbols: Boolean,251 onLength: (Int) -> Unit,252 onUpper: (Boolean) -> Unit, onLower: (Boolean) -> Unit,253 onNumbers: (Boolean) -> Unit, onSymbols: (Boolean) -> Unit,254 onGenerate: () -> Unit,255) {256 val accent = LocalPeachAccent.current257 val height by animateDpAsState(258 targetValue = if (open) 188.dp else 0.dp,259 animationSpec = tween(320, easing = PeachMotion.Spring),260 label = "gen-drawer",261 )262 Box(263 Modifier.fillMaxWidth().height(height).clipToBounds()264 .clip(RoundedCornerShape(12.dp)).background(PeachColors.S2),265 ) {266 Column(Modifier.fillMaxWidth().padding(16.dp)) {267 Row(Modifier.fillMaxWidth(), horizontalArrangement = Arrangement.SpaceBetween) {268 Text("GENERATOR", color = PeachColors.T1, fontFamily = PeachType.Mono, fontSize = 8.sp)269 Text("length $length", color = accent, fontFamily = PeachType.Mono, fontSize = 9.sp)270 }271 Slider(272 value = length.toFloat(),273 onValueChange = { onLength(it.toInt()) },274 valueRange = 8f..128f,275 colors = SliderDefaults.colors(thumbColor = accent, activeTrackColor = accent,276 inactiveTrackColor = PeachColors.T3),277 )278 Row(horizontalArrangement = Arrangement.spacedBy(8.dp)) {279 TogglePill("A-Z", upper, onUpper)280 TogglePill("a-z", lower, onLower)281 TogglePill("0-9", numbers, onNumbers)282 TogglePill("!@#", symbols, onSymbols)283 }284 Spacer(Modifier.height(12.dp))285 Box(286 Modifier.fillMaxWidth().clip(RoundedCornerShape(10.dp))287 .border(1.dp, accent.copy(alpha = 0.5f), RoundedCornerShape(10.dp))288 .background(accent.copy(alpha = 0.10f))289 .clickable(onClick = onGenerate).padding(vertical = 12.dp),290 contentAlignment = Alignment.Center,291 ) {292 Text("generate", color = accent, fontFamily = PeachType.Mono, fontSize = 12.sp)293 }294 }295 }296 if (open) Spacer(Modifier.height(14.dp))297}298299@Composable300private fun TogglePill(label: String, on: Boolean, onToggle: (Boolean) -> Unit) {301 val accent = LocalPeachAccent.current302 Box(303 Modifier.clip(RoundedCornerShape(PeachRadius.TypeTab))304 .border(1.dp, if (on) accent.copy(alpha = 0.5f) else PeachColors.T3, RoundedCornerShape(PeachRadius.TypeTab))305 .background(if (on) accent.copy(alpha = 0.10f) else Color.Transparent)306 .clickable { onToggle(!on) }307 .padding(horizontal = 10.dp, vertical = 6.dp),308 ) {309 Text(label, color = if (on) accent else PeachColors.T2, fontFamily = PeachType.Mono, fontSize = 9.sp)310 }311}312313314315316317318319@Composable320private fun PasswordHistorySection(321 history: List<PasswordHistoryItem>,322 expanded: Boolean,323 onToggle: () -> Unit,324 onRestore: (String) -> Unit,325 onCopy: (String) -> Unit,326) {327 val accent = LocalPeachAccent.current328 Column(Modifier.fillMaxWidth().padding(bottom = 14.dp)) {329 val label = if (history.isEmpty()) "↺ history" else "↺ history (${history.size})"330 Text(331 if (expanded) "↺ hide history" else label,332 color = accent.copy(alpha = 0.7f),333 fontFamily = PeachType.Display,334 fontStyle = FontStyle.Italic,335 fontSize = 15.sp,336 modifier = Modifier.clip(RoundedCornerShape(6.dp)).clickable(onClick = onToggle).padding(vertical = 4.dp),337 )338 if (expanded) {339 Spacer(Modifier.height(8.dp))340 if (history.isEmpty()) {341 Box(342 Modifier.fillMaxWidth().clip(RoundedCornerShape(8.dp))343 .border(1.dp, accent.copy(alpha = 0.12f), RoundedCornerShape(8.dp))344 .background(accent.copy(alpha = 0.05f)).padding(10.dp),345 ) {346 Text(347 "No earlier passwords yet. When you change this password, the old one is saved here so you can copy or restore it.",348 color = PeachColors.T2, fontFamily = PeachType.Mono, fontSize = 10.sp,349 )350 }351 } else {352 Column(verticalArrangement = Arrangement.spacedBy(6.dp)) {353 history.forEach { item -> HistoryRow(item, onRestore, onCopy) }354 }355 }356 }357 }358}359360@Composable361private fun HistoryRow(362 item: PasswordHistoryItem,363 onRestore: (String) -> Unit,364 onCopy: (String) -> Unit,365) {366 val accent = LocalPeachAccent.current367 var revealed by remember { mutableStateOf(false) }368 Row(369 Modifier.fillMaxWidth().clip(RoundedCornerShape(8.dp))370 .border(1.dp, accent.copy(alpha = 0.12f), RoundedCornerShape(8.dp))371 .background(accent.copy(alpha = 0.06f)).padding(horizontal = 10.dp, vertical = 8.dp),372 verticalAlignment = Alignment.CenterVertically,373 horizontalArrangement = Arrangement.SpaceBetween,374 ) {375 Column(Modifier.weight(1f)) {376 Text(377 if (revealed) item.password else "••••••••••••",378 color = PeachColors.T0, fontFamily = PeachType.Mono, fontSize = 12.sp, maxLines = 1,379 )380 Text(381 timeSince(item.changedAt),382 color = accent.copy(alpha = 0.55f), fontFamily = PeachType.Mono, fontSize = 8.sp,383 )384 }385 Spacer(Modifier.width(6.dp))386 HistoryAction(if (revealed) "hide" else "show") { revealed = !revealed }387 HistoryAction("copy") { onCopy(item.password) }388 HistoryAction("use") { onRestore(item.password) }389 }390}391392@Composable393private fun HistoryAction(label: String, onClick: () -> Unit) {394 Text(395 label, color = LocalPeachAccent.current.copy(alpha = 0.7f),396 fontFamily = PeachType.Mono, fontSize = 9.sp,397 modifier = Modifier.clip(RoundedCornerShape(5.dp)).clickable(onClick = onClick).padding(horizontal = 5.dp, vertical = 3.dp),398 )399}400401402private fun timeSince(whenMs: Long): String {403 val secs = ((System.currentTimeMillis() - whenMs) / 1000).coerceAtLeast(0)404 if (secs < 60) return "just now"405 val mins = secs / 60406 if (mins < 60) return "${mins}m ago"407 val hours = mins / 60408 if (hours < 24) return "${hours}h ago"409 val days = hours / 24410 if (days < 30) return "${days}d ago"411 val months = days / 30412 if (months < 12) return "${months}mo ago"413 return "${months / 12}y ago"414}