EntryEditScreen.kt

16.4 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                Field("notes", f.notes, vm::setNotes, singleLine = false)131132                f.error?.let {133                    Spacer(Modifier.height(8.dp))134                    Text(it, color = PeachColors.Breach, fontFamily = PeachType.Mono, fontSize = 11.sp)135                }136                Spacer(Modifier.height(40.dp))137                }138            }139        }140    }141}142143@Composable144private fun EntryTypePill(type: String, selected: Boolean, onClick: () -> Unit) {145    val accent = LocalPeachAccent.current146    Box(147        modifier = Modifier148            .clip(RoundedCornerShape(PeachRadius.TypeTab))149            .border(150                1.dp,151                if (selected) accent.copy(alpha = 0.5f) else PeachColors.T3,152                RoundedCornerShape(PeachRadius.TypeTab),153            )154            .background(if (selected) accent.copy(alpha = 0.08f) else Color.Transparent)155            .clickable(onClick = onClick)156            .padding(horizontal = 12.dp, vertical = 5.dp),157    ) {158        Text(159            text = type.uppercase(),160            color = if (selected) accent else PeachColors.T2,161            fontFamily = PeachType.Mono,162            fontSize = 9.sp,163            letterSpacing = 0.08.em,164        )165    }166}167168@Composable169private fun Field(170    label: String,171    value: String,172    onChange: (String) -> Unit,173    singleLine: Boolean = true,174    transformation: VisualTransformation = VisualTransformation.None,175    trailing: (@Composable () -> Unit)? = null,176) {177    val accent = LocalPeachAccent.current178    Column(Modifier.fillMaxWidth().padding(bottom = 14.dp)) {179        Text(label.uppercase(), color = PeachColors.T1, fontFamily = PeachType.Mono, fontSize = 8.sp)180        Spacer(Modifier.height(6.dp))181        Row(verticalAlignment = Alignment.CenterVertically) {182            Box(183                Modifier.weight(1f)184                    .border(1.dp, PeachColors.T3, RoundedCornerShape(10.dp))185                    .padding(horizontal = 12.dp, vertical = 12.dp),186            ) {187                BasicTextField(188                    value = value,189                    onValueChange = onChange,190                    singleLine = singleLine,191                    textStyle = TextStyle(color = PeachColors.T0, fontFamily = PeachType.Mono, fontSize = 13.sp),192                    cursorBrush = SolidColor(accent),193                    visualTransformation = transformation,194                    modifier = Modifier.fillMaxWidth(),195                )196            }197            if (trailing != null) { Spacer(Modifier.width(8.dp)); trailing() }198        }199    }200}201202@Composable203private fun PasswordField(204    value: String,205    show: Boolean,206    onChange: (String) -> Unit,207    onToggleShow: () -> Unit,208    onToggleGenerator: () -> Unit,209) {210    Field(211        label = "password",212        value = value,213        onChange = onChange,214        transformation = if (show) VisualTransformation.None else PasswordVisualTransformation(),215        trailing = {216            Text(if (show) "hide" else "show", color = LocalPeachAccent.current,217                fontFamily = PeachType.Mono, fontSize = 9.sp,218                modifier = Modifier.clip(RoundedCornerShape(6.dp)).clickable(onClick = onToggleShow).padding(6.dp))219            Text("⟳", color = LocalPeachAccent.current, fontSize = 16.sp,220                modifier = Modifier.clip(RoundedCornerShape(6.dp)).clickable(onClick = onToggleGenerator).padding(6.dp))221        },222    )223}224225@Composable226private fun GeneratorDrawer(227    open: Boolean,228    length: Int,229    upper: Boolean, lower: Boolean, numbers: Boolean, symbols: Boolean,230    onLength: (Int) -> Unit,231    onUpper: (Boolean) -> Unit, onLower: (Boolean) -> Unit,232    onNumbers: (Boolean) -> Unit, onSymbols: (Boolean) -> Unit,233    onGenerate: () -> Unit,234) {235    val accent = LocalPeachAccent.current236    val height by animateDpAsState(237        targetValue = if (open) 188.dp else 0.dp,238        animationSpec = tween(320, easing = PeachMotion.Spring),239        label = "gen-drawer",240    )241    Box(242        Modifier.fillMaxWidth().height(height).clipToBounds()243            .clip(RoundedCornerShape(12.dp)).background(PeachColors.S2),244    ) {245        Column(Modifier.fillMaxWidth().padding(16.dp)) {246            Row(Modifier.fillMaxWidth(), horizontalArrangement = Arrangement.SpaceBetween) {247                Text("GENERATOR", color = PeachColors.T1, fontFamily = PeachType.Mono, fontSize = 8.sp)248                Text("length $length", color = accent, fontFamily = PeachType.Mono, fontSize = 9.sp)249            }250            Slider(251                value = length.toFloat(),252                onValueChange = { onLength(it.toInt()) },253                valueRange = 8f..64f,254                colors = SliderDefaults.colors(thumbColor = accent, activeTrackColor = accent,255                    inactiveTrackColor = PeachColors.T3),256            )257            Row(horizontalArrangement = Arrangement.spacedBy(8.dp)) {258                TogglePill("A-Z", upper, onUpper)259                TogglePill("a-z", lower, onLower)260                TogglePill("0-9", numbers, onNumbers)261                TogglePill("!@#", symbols, onSymbols)262            }263            Spacer(Modifier.height(12.dp))264            Box(265                Modifier.fillMaxWidth().clip(RoundedCornerShape(10.dp))266                    .border(1.dp, accent.copy(alpha = 0.5f), RoundedCornerShape(10.dp))267                    .background(accent.copy(alpha = 0.10f))268                    .clickable(onClick = onGenerate).padding(vertical = 12.dp),269                contentAlignment = Alignment.Center,270            ) {271                Text("generate", color = accent, fontFamily = PeachType.Mono, fontSize = 12.sp)272            }273        }274    }275    if (open) Spacer(Modifier.height(14.dp))276}277278@Composable279private fun TogglePill(label: String, on: Boolean, onToggle: (Boolean) -> Unit) {280    val accent = LocalPeachAccent.current281    Box(282        Modifier.clip(RoundedCornerShape(PeachRadius.TypeTab))283            .border(1.dp, if (on) accent.copy(alpha = 0.5f) else PeachColors.T3, RoundedCornerShape(PeachRadius.TypeTab))284            .background(if (on) accent.copy(alpha = 0.10f) else Color.Transparent)285            .clickable { onToggle(!on) }286            .padding(horizontal = 10.dp, vertical = 6.dp),287    ) {288        Text(label, color = if (on) accent else PeachColors.T2, fontFamily = PeachType.Mono, fontSize = 9.sp)289    }290}291292/**293 * Past-password history. The toggle is always present (even with no history) so294 * the affordance is discoverable — empty state explains how it fills. Rows are295 * masked until revealed; "use" loads the value back into the editor so the296 * normal save records the swap.297 */298@Composable299private fun PasswordHistorySection(300    history: List<PasswordHistoryItem>,301    expanded: Boolean,302    onToggle: () -> Unit,303    onRestore: (String) -> Unit,304    onCopy: (String) -> Unit,305) {306    val accent = LocalPeachAccent.current307    Column(Modifier.fillMaxWidth().padding(bottom = 14.dp)) {308        val label = if (history.isEmpty()) "↺ history" else "↺ history (${history.size})"309        Text(310            if (expanded) "↺ hide history" else label,311            color = accent.copy(alpha = 0.7f),312            fontFamily = PeachType.Display,313            fontStyle = FontStyle.Italic,314            fontSize = 15.sp,315            modifier = Modifier.clip(RoundedCornerShape(6.dp)).clickable(onClick = onToggle).padding(vertical = 4.dp),316        )317        if (expanded) {318            Spacer(Modifier.height(8.dp))319            if (history.isEmpty()) {320                Box(321                    Modifier.fillMaxWidth().clip(RoundedCornerShape(8.dp))322                        .border(1.dp, accent.copy(alpha = 0.12f), RoundedCornerShape(8.dp))323                        .background(accent.copy(alpha = 0.05f)).padding(10.dp),324                ) {325                    Text(326                        "No earlier passwords yet. When you change this password, the old one is saved here so you can copy or restore it.",327                        color = PeachColors.T2, fontFamily = PeachType.Mono, fontSize = 10.sp,328                    )329                }330            } else {331                Column(verticalArrangement = Arrangement.spacedBy(6.dp)) {332                    history.forEach { item -> HistoryRow(item, onRestore, onCopy) }333                }334            }335        }336    }337}338339@Composable340private fun HistoryRow(341    item: PasswordHistoryItem,342    onRestore: (String) -> Unit,343    onCopy: (String) -> Unit,344) {345    val accent = LocalPeachAccent.current346    var revealed by remember { mutableStateOf(false) }347    Row(348        Modifier.fillMaxWidth().clip(RoundedCornerShape(8.dp))349            .border(1.dp, accent.copy(alpha = 0.12f), RoundedCornerShape(8.dp))350            .background(accent.copy(alpha = 0.06f)).padding(horizontal = 10.dp, vertical = 8.dp),351        verticalAlignment = Alignment.CenterVertically,352        horizontalArrangement = Arrangement.SpaceBetween,353    ) {354        Column(Modifier.weight(1f)) {355            Text(356                if (revealed) item.password else "••••••••••••",357                color = PeachColors.T0, fontFamily = PeachType.Mono, fontSize = 12.sp, maxLines = 1,358            )359            Text(360                timeSince(item.changedAt),361                color = accent.copy(alpha = 0.55f), fontFamily = PeachType.Mono, fontSize = 8.sp,362            )363        }364        Spacer(Modifier.width(6.dp))365        HistoryAction(if (revealed) "hide" else "show") { revealed = !revealed }366        HistoryAction("copy") { onCopy(item.password) }367        HistoryAction("use") { onRestore(item.password) }368    }369}370371@Composable372private fun HistoryAction(label: String, onClick: () -> Unit) {373    Text(374        label, color = LocalPeachAccent.current.copy(alpha = 0.7f),375        fontFamily = PeachType.Mono, fontSize = 9.sp,376        modifier = Modifier.clip(RoundedCornerShape(5.dp)).clickable(onClick = onClick).padding(horizontal = 5.dp, vertical = 3.dp),377    )378}379380/** Coarse "time since" label for the history list. Mirrors the browser helper. */381private fun timeSince(whenMs: Long): String {382    val secs = ((System.currentTimeMillis() - whenMs) / 1000).coerceAtLeast(0)383    if (secs < 60) return "just now"384    val mins = secs / 60385    if (mins < 60) return "${mins}m ago"386    val hours = mins / 60387    if (hours < 24) return "${hours}h ago"388    val days = hours / 24389    if (days < 30) return "${days}d ago"390    val months = days / 30391    if (months < 12) return "${months}mo ago"392    return "${months / 12}y ago"393}