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 usernameSuggestions by vm.usernameSuggestions.collectAsState()64 val accent = LocalPeachAccent.current6566 ShellBackground {67 Column(Modifier.fillMaxSize().statusBarsPadding()) {68 Row(69 Modifier.fillMaxWidth().height(56.dp).padding(horizontal = 18.dp),70 verticalAlignment = Alignment.CenterVertically,71 horizontalArrangement = Arrangement.SpaceBetween,72 ) {73 Text("cancel", color = PeachColors.T1, fontFamily = PeachType.Mono, fontSize = 12.sp,74 modifier = Modifier.clip(RoundedCornerShape(8.dp)).clickable(onClick = onBack).padding(8.dp))75 Text(if (vm.isNew) "new entry" else "edit", color = PeachColors.T1,76 fontFamily = PeachType.Display, fontStyle = FontStyle.Italic, fontSize = 16.sp)77 Text("save", color = accent, fontFamily = PeachType.Mono, fontSize = 13.sp,78 modifier = Modifier.clip(RoundedCornerShape(8.dp)).clickable { vm.save(onBack) }.padding(8.dp))79 }8081 Box(Modifier.fillMaxSize()) {82 83 if (f.title.isNotBlank()) {84 Text(85 text = f.title.first().uppercase(),86 style = TextStyle(fontFamily = PeachType.Script, fontSize = 140.sp),87 color = PeachColors.T0.copy(alpha = 0.03f),88 modifier = Modifier.align(Alignment.TopEnd).offset(x = (-16).dp, y = 8.dp),89 )90 }91 Column(92 Modifier.fillMaxSize().verticalScroll(rememberScrollState()).padding(horizontal = 22.dp),93 ) {94 Row(horizontalArrangement = Arrangement.spacedBy(8.dp)) {95 listOf("login", "note", "card").forEach { t ->96 EntryTypePill(t, f.type == t) { vm.setType(t) }97 }98 }99 Spacer(Modifier.height(18.dp))100101 Field("title", f.title, vm::setTitle)102 if (f.type == "login") {103 Field("url", f.url, vm::setUrl)104 AdditionalUrlsEditor(105 urls = f.additionalUrls,106 onChange = vm::setAdditionalUrl,107 onAdd = vm::addAdditionalUrl,108 onRemove = vm::removeAdditionalUrl,109 )110 IdentitySuggestField("username", f.username, usernameSuggestions, vm::setUsername)111 IdentitySuggestField("email", f.email, usernameSuggestions, vm::setEmail)112 PasswordField(113 value = f.password,114 show = f.showPassword,115 onChange = vm::setPassword,116 onToggleShow = vm::toggleShowPassword,117 onToggleGenerator = vm::toggleGenerator,118 )119 GeneratorDrawer(120 open = f.generatorOpen,121 length = f.genLength,122 upper = f.genUpper, lower = f.genLower, numbers = f.genNumbers, symbols = f.genSymbols,123 onLength = vm::setGenLength,124 onUpper = vm::setGenUpper, onLower = vm::setGenLower,125 onNumbers = vm::setGenNumbers, onSymbols = vm::setGenSymbols,126 onGenerate = vm::generate,127 )128 PasswordHistorySection(129 history = f.history,130 expanded = f.showHistory,131 onToggle = vm::toggleHistory,132 onRestore = vm::restorePassword,133 onCopy = vm::copyHistoryPassword,134 )135 Field("2FA secret (otpauth:// or base32)", f.totp, vm::setTotp)136 }137 if (f.type == "card") {138 CardEditFields(139 cardNumber = f.cardNumber,140 cardholderName = f.cardholderName,141 expiry = f.expiry,142 cvc = f.cvc,143 showCvc = f.showCvc,144 network = f.cardNetwork,145 onCardNumber = vm::setCardNumber,146 onCardholder = vm::setCardholderName,147 onExpiry = vm::setExpiry,148 onCvc = vm::setCvc,149 onToggleShowCvc = vm::toggleShowCvc,150 )151 }152 Field("notes", f.notes, vm::setNotes, singleLine = false)153 TagChipEditor(154 tags = f.tags,155 allTags = vm.allTags,156 onAdd = vm::addTag,157 onRemove = vm::removeTag,158 )159160 f.error?.let {161 Spacer(Modifier.height(8.dp))162 Text(it, color = PeachColors.Breach, fontFamily = PeachType.Mono, fontSize = 11.sp)163 }164 Spacer(Modifier.height(40.dp))165 }166 }167 }168 }169}170171@Composable172private fun EntryTypePill(type: String, selected: Boolean, onClick: () -> Unit) {173 val accent = LocalPeachAccent.current174 Box(175 modifier = Modifier176 .clip(RoundedCornerShape(PeachRadius.TypeTab))177 .border(178 1.dp,179 if (selected) accent.copy(alpha = 0.5f) else PeachColors.T3,180 RoundedCornerShape(PeachRadius.TypeTab),181 )182 .background(if (selected) accent.copy(alpha = 0.08f) else Color.Transparent)183 .clickable(onClick = onClick)184 .padding(horizontal = 12.dp, vertical = 5.dp),185 ) {186 Text(187 text = type.uppercase(),188 color = if (selected) accent else PeachColors.T2,189 fontFamily = PeachType.Mono,190 fontSize = 9.sp,191 letterSpacing = 0.08.em,192 )193 }194}195196@Composable197private fun Field(198 label: String,199 value: String,200 onChange: (String) -> Unit,201 singleLine: Boolean = true,202 transformation: VisualTransformation = VisualTransformation.None,203 trailing: (@Composable () -> Unit)? = null,204) {205 val accent = LocalPeachAccent.current206 Column(Modifier.fillMaxWidth().padding(bottom = 14.dp)) {207 Text(label.uppercase(), color = PeachColors.T1, fontFamily = PeachType.Mono, fontSize = 8.sp)208 Spacer(Modifier.height(6.dp))209 Row(verticalAlignment = Alignment.CenterVertically) {210 Box(211 Modifier.weight(1f)212 .border(1.dp, PeachColors.T3, RoundedCornerShape(10.dp))213 .padding(horizontal = 12.dp, vertical = 12.dp),214 ) {215 BasicTextField(216 value = value,217 onValueChange = onChange,218 singleLine = singleLine,219 textStyle = TextStyle(color = PeachColors.T0, fontFamily = PeachType.Mono, fontSize = 13.sp),220 cursorBrush = SolidColor(accent),221 visualTransformation = transformation,222 modifier = Modifier.fillMaxWidth(),223 )224 }225 if (trailing != null) { Spacer(Modifier.width(8.dp)); trailing() }226 }227 }228}229230@Composable231private fun PasswordField(232 value: String,233 show: Boolean,234 onChange: (String) -> Unit,235 onToggleShow: () -> Unit,236 onToggleGenerator: () -> Unit,237) {238 Field(239 label = "password",240 value = value,241 onChange = onChange,242 transformation = if (show) VisualTransformation.None else PasswordVisualTransformation(),243 trailing = {244 Text(if (show) "hide" else "show", color = LocalPeachAccent.current,245 fontFamily = PeachType.Mono, fontSize = 9.sp,246 modifier = Modifier.clip(RoundedCornerShape(6.dp)).clickable(onClick = onToggleShow).padding(6.dp))247 Text("⟳", color = LocalPeachAccent.current, fontSize = 16.sp,248 modifier = Modifier.clip(RoundedCornerShape(6.dp)).clickable(onClick = onToggleGenerator).padding(6.dp))249 },250 )251}252253@Composable254private fun GeneratorDrawer(255 open: Boolean,256 length: Int,257 upper: Boolean, lower: Boolean, numbers: Boolean, symbols: Boolean,258 onLength: (Int) -> Unit,259 onUpper: (Boolean) -> Unit, onLower: (Boolean) -> Unit,260 onNumbers: (Boolean) -> Unit, onSymbols: (Boolean) -> Unit,261 onGenerate: () -> Unit,262) {263 val accent = LocalPeachAccent.current264 val height by animateDpAsState(265 targetValue = if (open) 188.dp else 0.dp,266 animationSpec = tween(320, easing = PeachMotion.Spring),267 label = "gen-drawer",268 )269 Box(270 Modifier.fillMaxWidth().height(height).clipToBounds()271 .clip(RoundedCornerShape(12.dp)).background(PeachColors.S2),272 ) {273 Column(Modifier.fillMaxWidth().padding(16.dp)) {274 Row(Modifier.fillMaxWidth(), horizontalArrangement = Arrangement.SpaceBetween) {275 Text("GENERATOR", color = PeachColors.T1, fontFamily = PeachType.Mono, fontSize = 8.sp)276 Text("length $length", color = accent, fontFamily = PeachType.Mono, fontSize = 9.sp)277 }278 Slider(279 value = length.toFloat(),280 onValueChange = { onLength(it.toInt()) },281 valueRange = 8f..128f,282 colors = SliderDefaults.colors(thumbColor = accent, activeTrackColor = accent,283 inactiveTrackColor = PeachColors.T3),284 )285 Row(horizontalArrangement = Arrangement.spacedBy(8.dp)) {286 TogglePill("A-Z", upper, onUpper)287 TogglePill("a-z", lower, onLower)288 TogglePill("0-9", numbers, onNumbers)289 TogglePill("!@#", symbols, onSymbols)290 }291 Spacer(Modifier.height(12.dp))292 Box(293 Modifier.fillMaxWidth().clip(RoundedCornerShape(10.dp))294 .border(1.dp, accent.copy(alpha = 0.5f), RoundedCornerShape(10.dp))295 .background(accent.copy(alpha = 0.10f))296 .clickable(onClick = onGenerate).padding(vertical = 12.dp),297 contentAlignment = Alignment.Center,298 ) {299 Text("generate", color = accent, fontFamily = PeachType.Mono, fontSize = 12.sp)300 }301 }302 }303 if (open) Spacer(Modifier.height(14.dp))304}305306@Composable307private fun TogglePill(label: String, on: Boolean, onToggle: (Boolean) -> Unit) {308 val accent = LocalPeachAccent.current309 Box(310 Modifier.clip(RoundedCornerShape(PeachRadius.TypeTab))311 .border(1.dp, if (on) accent.copy(alpha = 0.5f) else PeachColors.T3, RoundedCornerShape(PeachRadius.TypeTab))312 .background(if (on) accent.copy(alpha = 0.10f) else Color.Transparent)313 .clickable { onToggle(!on) }314 .padding(horizontal = 10.dp, vertical = 6.dp),315 ) {316 Text(label, color = if (on) accent else PeachColors.T2, fontFamily = PeachType.Mono, fontSize = 9.sp)317 }318}319320321322323324325326@Composable327private fun PasswordHistorySection(328 history: List<PasswordHistoryItem>,329 expanded: Boolean,330 onToggle: () -> Unit,331 onRestore: (String) -> Unit,332 onCopy: (String) -> Unit,333) {334 val accent = LocalPeachAccent.current335 Column(Modifier.fillMaxWidth().padding(bottom = 14.dp)) {336 val label = if (history.isEmpty()) "↺ history" else "↺ history (${history.size})"337 Text(338 if (expanded) "↺ hide history" else label,339 color = accent.copy(alpha = 0.7f),340 fontFamily = PeachType.Display,341 fontStyle = FontStyle.Italic,342 fontSize = 15.sp,343 modifier = Modifier.clip(RoundedCornerShape(6.dp)).clickable(onClick = onToggle).padding(vertical = 4.dp),344 )345 if (expanded) {346 Spacer(Modifier.height(8.dp))347 if (history.isEmpty()) {348 Box(349 Modifier.fillMaxWidth().clip(RoundedCornerShape(8.dp))350 .border(1.dp, accent.copy(alpha = 0.12f), RoundedCornerShape(8.dp))351 .background(accent.copy(alpha = 0.05f)).padding(10.dp),352 ) {353 Text(354 "No earlier passwords yet. When you change this password, the old one is saved here so you can copy or restore it.",355 color = PeachColors.T2, fontFamily = PeachType.Mono, fontSize = 10.sp,356 )357 }358 } else {359 Column(verticalArrangement = Arrangement.spacedBy(6.dp)) {360 history.forEach { item -> HistoryRow(item, onRestore, onCopy) }361 }362 }363 }364 }365}366367@Composable368private fun HistoryRow(369 item: PasswordHistoryItem,370 onRestore: (String) -> Unit,371 onCopy: (String) -> Unit,372) {373 val accent = LocalPeachAccent.current374 var revealed by remember { mutableStateOf(false) }375 Row(376 Modifier.fillMaxWidth().clip(RoundedCornerShape(8.dp))377 .border(1.dp, accent.copy(alpha = 0.12f), RoundedCornerShape(8.dp))378 .background(accent.copy(alpha = 0.06f)).padding(horizontal = 10.dp, vertical = 8.dp),379 verticalAlignment = Alignment.CenterVertically,380 horizontalArrangement = Arrangement.SpaceBetween,381 ) {382 Column(Modifier.weight(1f)) {383 Text(384 if (revealed) item.password else "••••••••••••",385 color = PeachColors.T0, fontFamily = PeachType.Mono, fontSize = 12.sp, maxLines = 1,386 )387 Text(388 timeSince(item.changedAt),389 color = accent.copy(alpha = 0.55f), fontFamily = PeachType.Mono, fontSize = 8.sp,390 )391 }392 Spacer(Modifier.width(6.dp))393 HistoryAction(if (revealed) "hide" else "show") { revealed = !revealed }394 HistoryAction("copy") { onCopy(item.password) }395 HistoryAction("use") { onRestore(item.password) }396 }397}398399@Composable400private fun HistoryAction(label: String, onClick: () -> Unit) {401 Text(402 label, color = LocalPeachAccent.current.copy(alpha = 0.7f),403 fontFamily = PeachType.Mono, fontSize = 9.sp,404 modifier = Modifier.clip(RoundedCornerShape(5.dp)).clickable(onClick = onClick).padding(horizontal = 5.dp, vertical = 3.dp),405 )406}407408409private fun timeSince(whenMs: Long): String {410 val secs = ((System.currentTimeMillis() - whenMs) / 1000).coerceAtLeast(0)411 if (secs < 60) return "just now"412 val mins = secs / 60413 if (mins < 60) return "${mins}m ago"414 val hours = mins / 60415 if (hours < 24) return "${hours}h ago"416 val days = hours / 24417 if (days < 30) return "${days}d ago"418 val months = days / 30419 if (months < 12) return "${months}mo ago"420 return "${months / 12}y ago"421}