EntryEditScreen.kt

17.1 KB · raw

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                // Ghost PeachScript watermark behind the form (matches the detail hero).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)146147                f.error?.let {148                    Spacer(Modifier.height(8.dp))149                    Text(it, color = PeachColors.Breach, fontFamily = PeachType.Mono, fontSize = 11.sp)150                }151                Spacer(Modifier.height(40.dp))152                }153            }154        }155    }156}157158@Composable159private fun EntryTypePill(type: String, selected: Boolean, onClick: () -> Unit) {160    val accent = LocalPeachAccent.current161    Box(162        modifier = Modifier163            .clip(RoundedCornerShape(PeachRadius.TypeTab))164            .border(165                1.dp,166                if (selected) accent.copy(alpha = 0.5f) else PeachColors.T3,167                RoundedCornerShape(PeachRadius.TypeTab),168            )169            .background(if (selected) accent.copy(alpha = 0.08f) else Color.Transparent)170            .clickable(onClick = onClick)171            .padding(horizontal = 12.dp, vertical = 5.dp),172    ) {173        Text(174            text = type.uppercase(),175            color = if (selected) accent else PeachColors.T2,176            fontFamily = PeachType.Mono,177            fontSize = 9.sp,178            letterSpacing = 0.08.em,179        )180    }181}182183@Composable184private fun Field(185    label: String,186    value: String,187    onChange: (String) -> Unit,188    singleLine: Boolean = true,189    transformation: VisualTransformation = VisualTransformation.None,190    trailing: (@Composable () -> Unit)? = null,191) {192    val accent = LocalPeachAccent.current193    Column(Modifier.fillMaxWidth().padding(bottom = 14.dp)) {194        Text(label.uppercase(), color = PeachColors.T1, fontFamily = PeachType.Mono, fontSize = 8.sp)195        Spacer(Modifier.height(6.dp))196        Row(verticalAlignment = Alignment.CenterVertically) {197            Box(198                Modifier.weight(1f)199                    .border(1.dp, PeachColors.T3, RoundedCornerShape(10.dp))200                    .padding(horizontal = 12.dp, vertical = 12.dp),201            ) {202                BasicTextField(203                    value = value,204                    onValueChange = onChange,205                    singleLine = singleLine,206                    textStyle = TextStyle(color = PeachColors.T0, fontFamily = PeachType.Mono, fontSize = 13.sp),207                    cursorBrush = SolidColor(accent),208                    visualTransformation = transformation,209                    modifier = Modifier.fillMaxWidth(),210                )211            }212            if (trailing != null) { Spacer(Modifier.width(8.dp)); trailing() }213        }214    }215}216217@Composable218private fun PasswordField(219    value: String,220    show: Boolean,221    onChange: (String) -> Unit,222    onToggleShow: () -> Unit,223    onToggleGenerator: () -> Unit,224) {225    Field(226        label = "password",227        value = value,228        onChange = onChange,229        transformation = if (show) VisualTransformation.None else PasswordVisualTransformation(),230        trailing = {231            Text(if (show) "hide" else "show", color = LocalPeachAccent.current,232                fontFamily = PeachType.Mono, fontSize = 9.sp,233                modifier = Modifier.clip(RoundedCornerShape(6.dp)).clickable(onClick = onToggleShow).padding(6.dp))234            Text("⟳", color = LocalPeachAccent.current, fontSize = 16.sp,235                modifier = Modifier.clip(RoundedCornerShape(6.dp)).clickable(onClick = onToggleGenerator).padding(6.dp))236        },237    )238}239240@Composable241private fun GeneratorDrawer(242    open: Boolean,243    length: Int,244    upper: Boolean, lower: Boolean, numbers: Boolean, symbols: Boolean,245    onLength: (Int) -> Unit,246    onUpper: (Boolean) -> Unit, onLower: (Boolean) -> Unit,247    onNumbers: (Boolean) -> Unit, onSymbols: (Boolean) -> Unit,248    onGenerate: () -> Unit,249) {250    val accent = LocalPeachAccent.current251    val height by animateDpAsState(252        targetValue = if (open) 188.dp else 0.dp,253        animationSpec = tween(320, easing = PeachMotion.Spring),254        label = "gen-drawer",255    )256    Box(257        Modifier.fillMaxWidth().height(height).clipToBounds()258            .clip(RoundedCornerShape(12.dp)).background(PeachColors.S2),259    ) {260        Column(Modifier.fillMaxWidth().padding(16.dp)) {261            Row(Modifier.fillMaxWidth(), horizontalArrangement = Arrangement.SpaceBetween) {262                Text("GENERATOR", color = PeachColors.T1, fontFamily = PeachType.Mono, fontSize = 8.sp)263                Text("length $length", color = accent, fontFamily = PeachType.Mono, fontSize = 9.sp)264            }265            Slider(266                value = length.toFloat(),267                onValueChange = { onLength(it.toInt()) },268                valueRange = 8f..128f,269                colors = SliderDefaults.colors(thumbColor = accent, activeTrackColor = accent,270                    inactiveTrackColor = PeachColors.T3),271            )272            Row(horizontalArrangement = Arrangement.spacedBy(8.dp)) {273                TogglePill("A-Z", upper, onUpper)274                TogglePill("a-z", lower, onLower)275                TogglePill("0-9", numbers, onNumbers)276                TogglePill("!@#", symbols, onSymbols)277            }278            Spacer(Modifier.height(12.dp))279            Box(280                Modifier.fillMaxWidth().clip(RoundedCornerShape(10.dp))281                    .border(1.dp, accent.copy(alpha = 0.5f), RoundedCornerShape(10.dp))282                    .background(accent.copy(alpha = 0.10f))283                    .clickable(onClick = onGenerate).padding(vertical = 12.dp),284                contentAlignment = Alignment.Center,285            ) {286                Text("generate", color = accent, fontFamily = PeachType.Mono, fontSize = 12.sp)287            }288        }289    }290    if (open) Spacer(Modifier.height(14.dp))291}292293@Composable294private fun TogglePill(label: String, on: Boolean, onToggle: (Boolean) -> Unit) {295    val accent = LocalPeachAccent.current296    Box(297        Modifier.clip(RoundedCornerShape(PeachRadius.TypeTab))298            .border(1.dp, if (on) accent.copy(alpha = 0.5f) else PeachColors.T3, RoundedCornerShape(PeachRadius.TypeTab))299            .background(if (on) accent.copy(alpha = 0.10f) else Color.Transparent)300            .clickable { onToggle(!on) }301            .padding(horizontal = 10.dp, vertical = 6.dp),302    ) {303        Text(label, color = if (on) accent else PeachColors.T2, fontFamily = PeachType.Mono, fontSize = 9.sp)304    }305}306307/**308 * Past-password history. The toggle is always present (even with no history) so309 * the affordance is discoverable — empty state explains how it fills. Rows are310 * masked until revealed; "use" loads the value back into the editor so the311 * normal save records the swap.312 */313@Composable314private fun PasswordHistorySection(315    history: List<PasswordHistoryItem>,316    expanded: Boolean,317    onToggle: () -> Unit,318    onRestore: (String) -> Unit,319    onCopy: (String) -> Unit,320) {321    val accent = LocalPeachAccent.current322    Column(Modifier.fillMaxWidth().padding(bottom = 14.dp)) {323        val label = if (history.isEmpty()) "↺ history" else "↺ history (${history.size})"324        Text(325            if (expanded) "↺ hide history" else label,326            color = accent.copy(alpha = 0.7f),327            fontFamily = PeachType.Display,328            fontStyle = FontStyle.Italic,329            fontSize = 15.sp,330            modifier = Modifier.clip(RoundedCornerShape(6.dp)).clickable(onClick = onToggle).padding(vertical = 4.dp),331        )332        if (expanded) {333            Spacer(Modifier.height(8.dp))334            if (history.isEmpty()) {335                Box(336                    Modifier.fillMaxWidth().clip(RoundedCornerShape(8.dp))337                        .border(1.dp, accent.copy(alpha = 0.12f), RoundedCornerShape(8.dp))338                        .background(accent.copy(alpha = 0.05f)).padding(10.dp),339                ) {340                    Text(341                        "No earlier passwords yet. When you change this password, the old one is saved here so you can copy or restore it.",342                        color = PeachColors.T2, fontFamily = PeachType.Mono, fontSize = 10.sp,343                    )344                }345            } else {346                Column(verticalArrangement = Arrangement.spacedBy(6.dp)) {347                    history.forEach { item -> HistoryRow(item, onRestore, onCopy) }348                }349            }350        }351    }352}353354@Composable355private fun HistoryRow(356    item: PasswordHistoryItem,357    onRestore: (String) -> Unit,358    onCopy: (String) -> Unit,359) {360    val accent = LocalPeachAccent.current361    var revealed by remember { mutableStateOf(false) }362    Row(363        Modifier.fillMaxWidth().clip(RoundedCornerShape(8.dp))364            .border(1.dp, accent.copy(alpha = 0.12f), RoundedCornerShape(8.dp))365            .background(accent.copy(alpha = 0.06f)).padding(horizontal = 10.dp, vertical = 8.dp),366        verticalAlignment = Alignment.CenterVertically,367        horizontalArrangement = Arrangement.SpaceBetween,368    ) {369        Column(Modifier.weight(1f)) {370            Text(371                if (revealed) item.password else "••••••••••••",372                color = PeachColors.T0, fontFamily = PeachType.Mono, fontSize = 12.sp, maxLines = 1,373            )374            Text(375                timeSince(item.changedAt),376                color = accent.copy(alpha = 0.55f), fontFamily = PeachType.Mono, fontSize = 8.sp,377            )378        }379        Spacer(Modifier.width(6.dp))380        HistoryAction(if (revealed) "hide" else "show") { revealed = !revealed }381        HistoryAction("copy") { onCopy(item.password) }382        HistoryAction("use") { onRestore(item.password) }383    }384}385386@Composable387private fun HistoryAction(label: String, onClick: () -> Unit) {388    Text(389        label, color = LocalPeachAccent.current.copy(alpha = 0.7f),390        fontFamily = PeachType.Mono, fontSize = 9.sp,391        modifier = Modifier.clip(RoundedCornerShape(5.dp)).clickable(onClick = onClick).padding(horizontal = 5.dp, vertical = 3.dp),392    )393}394395/** Coarse "time since" label for the history list. Mirrors the browser helper. */396private fun timeSince(whenMs: Long): String {397    val secs = ((System.currentTimeMillis() - whenMs) / 1000).coerceAtLeast(0)398    if (secs < 60) return "just now"399    val mins = secs / 60400    if (mins < 60) return "${mins}m ago"401    val hours = mins / 60402    if (hours < 24) return "${hours}h ago"403    val days = hours / 24404    if (days < 30) return "${days}d ago"405    val months = days / 30406    if (months < 12) return "${months}mo ago"407    return "${months / 12}y ago"408}