EntryEditScreen.kt

15.7 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.padding17import androidx.compose.foundation.layout.statusBarsPadding18import androidx.compose.foundation.layout.width19import androidx.compose.foundation.rememberScrollState20import androidx.compose.foundation.shape.RoundedCornerShape21import androidx.compose.foundation.text.BasicTextField22import androidx.compose.foundation.verticalScroll23import androidx.compose.material3.Slider24import androidx.compose.material3.SliderDefaults25import androidx.compose.material3.Text26import androidx.compose.runtime.Composable27import androidx.compose.runtime.getValue28import androidx.compose.runtime.mutableStateOf29import androidx.compose.runtime.remember30import androidx.compose.runtime.setValue31import androidx.compose.ui.Alignment32import androidx.compose.ui.Modifier33import androidx.compose.ui.draw.clip34import androidx.compose.ui.draw.clipToBounds35import androidx.compose.ui.graphics.Color36import androidx.compose.ui.graphics.SolidColor37import androidx.compose.ui.text.TextStyle38import androidx.compose.ui.text.font.FontStyle39import androidx.compose.ui.text.font.FontWeight40import androidx.compose.ui.text.input.PasswordVisualTransformation41import androidx.compose.ui.text.input.VisualTransformation42import androidx.compose.ui.unit.dp43import androidx.compose.ui.unit.em44import androidx.compose.ui.unit.sp45import androidx.compose.runtime.collectAsState46import androidx.hilt.navigation.compose.hiltViewModel47import com.peachpasswords.ui.theme.LocalPeachAccent48import com.peachpasswords.ui.theme.PeachColors49import com.peachpasswords.ui.theme.PeachMotion50import com.peachpasswords.ui.theme.PeachRadius51import com.peachpasswords.ui.theme.PeachType52import com.peachpasswords.ui.theme.ShellBackground53import com.peachpasswords.vault.PasswordHistoryItem5455@Composable56fun EntryEditScreen(57    id: String?,58    onBack: () -> Unit,59    vm: EntryEditViewModel = hiltViewModel(),60) {61    val f by vm.form.collectAsState()62    val accent = LocalPeachAccent.current6364    ShellBackground {65        Column(Modifier.fillMaxSize().statusBarsPadding()) {66            Row(67                Modifier.fillMaxWidth().height(56.dp).padding(horizontal = 18.dp),68                verticalAlignment = Alignment.CenterVertically,69                horizontalArrangement = Arrangement.SpaceBetween,70            ) {71                Text("cancel", color = PeachColors.T1, fontFamily = PeachType.Mono, fontSize = 12.sp,72                    modifier = Modifier.clip(RoundedCornerShape(8.dp)).clickable(onClick = onBack).padding(8.dp))73                Text(if (vm.isNew) "new entry" else "edit", color = PeachColors.T1,74                    fontFamily = PeachType.Display, fontStyle = FontStyle.Italic, fontSize = 16.sp)75                Text("save", color = accent, fontFamily = PeachType.Mono, fontSize = 13.sp,76                    modifier = Modifier.clip(RoundedCornerShape(8.dp)).clickable { vm.save(onBack) }.padding(8.dp))77            }7879            Column(80                Modifier.fillMaxSize().verticalScroll(rememberScrollState()).padding(horizontal = 22.dp),81            ) {82                Row(horizontalArrangement = Arrangement.spacedBy(8.dp)) {83                    listOf("login", "note", "card").forEach { t ->84                        EntryTypePill(t, f.type == t) { vm.setType(t) }85                    }86                }87                Spacer(Modifier.height(18.dp))8889                Field("title", f.title, vm::setTitle)90                if (f.type == "login") {91                    Field("url", f.url, vm::setUrl)92                    Field("username", f.username, vm::setUsername)93                    Field("email", f.email, vm::setEmail)94                    PasswordField(95                        value = f.password,96                        show = f.showPassword,97                        onChange = vm::setPassword,98                        onToggleShow = vm::toggleShowPassword,99                        onToggleGenerator = vm::toggleGenerator,100                    )101                    GeneratorDrawer(102                        open = f.generatorOpen,103                        length = f.genLength,104                        upper = f.genUpper, lower = f.genLower, numbers = f.genNumbers, symbols = f.genSymbols,105                        onLength = vm::setGenLength,106                        onUpper = vm::setGenUpper, onLower = vm::setGenLower,107                        onNumbers = vm::setGenNumbers, onSymbols = vm::setGenSymbols,108                        onGenerate = vm::generate,109                    )110                    PasswordHistorySection(111                        history = f.history,112                        expanded = f.showHistory,113                        onToggle = vm::toggleHistory,114                        onRestore = vm::restorePassword,115                        onCopy = vm::copyHistoryPassword,116                    )117                    Field("2FA secret (otpauth:// or base32)", f.totp, vm::setTotp)118                }119                Field("notes", f.notes, vm::setNotes, singleLine = false)120121                f.error?.let {122                    Spacer(Modifier.height(8.dp))123                    Text(it, color = PeachColors.Breach, fontFamily = PeachType.Mono, fontSize = 11.sp)124                }125                Spacer(Modifier.height(40.dp))126            }127        }128    }129}130131@Composable132private fun EntryTypePill(type: String, selected: Boolean, onClick: () -> Unit) {133    val accent = LocalPeachAccent.current134    Box(135        modifier = Modifier136            .clip(RoundedCornerShape(PeachRadius.TypeTab))137            .border(138                1.dp,139                if (selected) accent.copy(alpha = 0.5f) else PeachColors.T3,140                RoundedCornerShape(PeachRadius.TypeTab),141            )142            .background(if (selected) accent.copy(alpha = 0.08f) else Color.Transparent)143            .clickable(onClick = onClick)144            .padding(horizontal = 12.dp, vertical = 5.dp),145    ) {146        Text(147            text = type,148            color = if (selected) accent else PeachColors.T2,149            fontFamily = PeachType.Mono,150            fontSize = 9.sp,151            letterSpacing = 0.08.em,152        )153    }154}155156@Composable157private fun Field(158    label: String,159    value: String,160    onChange: (String) -> Unit,161    singleLine: Boolean = true,162    transformation: VisualTransformation = VisualTransformation.None,163    trailing: (@Composable () -> Unit)? = null,164) {165    val accent = LocalPeachAccent.current166    Column(Modifier.fillMaxWidth().padding(bottom = 14.dp)) {167        Text(label.uppercase(), color = PeachColors.T1, fontFamily = PeachType.Mono, fontSize = 8.sp)168        Spacer(Modifier.height(6.dp))169        Row(verticalAlignment = Alignment.CenterVertically) {170            Box(171                Modifier.weight(1f)172                    .border(1.dp, PeachColors.T3, RoundedCornerShape(10.dp))173                    .padding(horizontal = 12.dp, vertical = 12.dp),174            ) {175                BasicTextField(176                    value = value,177                    onValueChange = onChange,178                    singleLine = singleLine,179                    textStyle = TextStyle(color = PeachColors.T0, fontFamily = PeachType.Mono, fontSize = 13.sp),180                    cursorBrush = SolidColor(accent),181                    visualTransformation = transformation,182                    modifier = Modifier.fillMaxWidth(),183                )184            }185            if (trailing != null) { Spacer(Modifier.width(8.dp)); trailing() }186        }187    }188}189190@Composable191private fun PasswordField(192    value: String,193    show: Boolean,194    onChange: (String) -> Unit,195    onToggleShow: () -> Unit,196    onToggleGenerator: () -> Unit,197) {198    Field(199        label = "password",200        value = value,201        onChange = onChange,202        transformation = if (show) VisualTransformation.None else PasswordVisualTransformation(),203        trailing = {204            Text(if (show) "hide" else "show", color = LocalPeachAccent.current,205                fontFamily = PeachType.Mono, fontSize = 9.sp,206                modifier = Modifier.clip(RoundedCornerShape(6.dp)).clickable(onClick = onToggleShow).padding(6.dp))207            Text("⟳", color = LocalPeachAccent.current, fontSize = 16.sp,208                modifier = Modifier.clip(RoundedCornerShape(6.dp)).clickable(onClick = onToggleGenerator).padding(6.dp))209        },210    )211}212213@Composable214private fun GeneratorDrawer(215    open: Boolean,216    length: Int,217    upper: Boolean, lower: Boolean, numbers: Boolean, symbols: Boolean,218    onLength: (Int) -> Unit,219    onUpper: (Boolean) -> Unit, onLower: (Boolean) -> Unit,220    onNumbers: (Boolean) -> Unit, onSymbols: (Boolean) -> Unit,221    onGenerate: () -> Unit,222) {223    val accent = LocalPeachAccent.current224    val height by animateDpAsState(225        targetValue = if (open) 188.dp else 0.dp,226        animationSpec = tween(320, easing = PeachMotion.Spring),227        label = "gen-drawer",228    )229    Box(230        Modifier.fillMaxWidth().height(height).clipToBounds()231            .clip(RoundedCornerShape(12.dp)).background(PeachColors.S2),232    ) {233        Column(Modifier.fillMaxWidth().padding(16.dp)) {234            Row(Modifier.fillMaxWidth(), horizontalArrangement = Arrangement.SpaceBetween) {235                Text("GENERATOR", color = PeachColors.T1, fontFamily = PeachType.Mono, fontSize = 8.sp)236                Text("length $length", color = accent, fontFamily = PeachType.Mono, fontSize = 9.sp)237            }238            Slider(239                value = length.toFloat(),240                onValueChange = { onLength(it.toInt()) },241                valueRange = 8f..64f,242                colors = SliderDefaults.colors(thumbColor = accent, activeTrackColor = accent,243                    inactiveTrackColor = PeachColors.T3),244            )245            Row(horizontalArrangement = Arrangement.spacedBy(8.dp)) {246                TogglePill("A-Z", upper, onUpper)247                TogglePill("a-z", lower, onLower)248                TogglePill("0-9", numbers, onNumbers)249                TogglePill("!@#", symbols, onSymbols)250            }251            Spacer(Modifier.height(12.dp))252            Box(253                Modifier.fillMaxWidth().clip(RoundedCornerShape(10.dp))254                    .border(1.dp, accent.copy(alpha = 0.5f), RoundedCornerShape(10.dp))255                    .background(accent.copy(alpha = 0.10f))256                    .clickable(onClick = onGenerate).padding(vertical = 12.dp),257                contentAlignment = Alignment.Center,258            ) {259                Text("generate", color = accent, fontFamily = PeachType.Mono, fontSize = 12.sp)260            }261        }262    }263    if (open) Spacer(Modifier.height(14.dp))264}265266@Composable267private fun TogglePill(label: String, on: Boolean, onToggle: (Boolean) -> Unit) {268    val accent = LocalPeachAccent.current269    Box(270        Modifier.clip(RoundedCornerShape(PeachRadius.TypeTab))271            .border(1.dp, if (on) accent.copy(alpha = 0.5f) else PeachColors.T3, RoundedCornerShape(PeachRadius.TypeTab))272            .background(if (on) accent.copy(alpha = 0.10f) else Color.Transparent)273            .clickable { onToggle(!on) }274            .padding(horizontal = 10.dp, vertical = 6.dp),275    ) {276        Text(label, color = if (on) accent else PeachColors.T2, fontFamily = PeachType.Mono, fontSize = 9.sp)277    }278}279280/**281 * Past-password history. The toggle is always present (even with no history) so282 * the affordance is discoverable — empty state explains how it fills. Rows are283 * masked until revealed; "use" loads the value back into the editor so the284 * normal save records the swap.285 */286@Composable287private fun PasswordHistorySection(288    history: List<PasswordHistoryItem>,289    expanded: Boolean,290    onToggle: () -> Unit,291    onRestore: (String) -> Unit,292    onCopy: (String) -> Unit,293) {294    val accent = LocalPeachAccent.current295    Column(Modifier.fillMaxWidth().padding(bottom = 14.dp)) {296        val label = if (history.isEmpty()) "↺ history" else "↺ history (${history.size})"297        Text(298            if (expanded) "↺ hide history" else label,299            color = accent.copy(alpha = 0.7f),300            fontFamily = PeachType.Mono,301            fontSize = 11.sp,302            modifier = Modifier.clip(RoundedCornerShape(6.dp)).clickable(onClick = onToggle).padding(vertical = 4.dp),303        )304        if (expanded) {305            Spacer(Modifier.height(8.dp))306            if (history.isEmpty()) {307                Box(308                    Modifier.fillMaxWidth().clip(RoundedCornerShape(8.dp))309                        .border(1.dp, accent.copy(alpha = 0.12f), RoundedCornerShape(8.dp))310                        .background(accent.copy(alpha = 0.05f)).padding(10.dp),311                ) {312                    Text(313                        "No earlier passwords yet. When you change this password, the old one is saved here so you can copy or restore it.",314                        color = PeachColors.T2, fontFamily = PeachType.Mono, fontSize = 10.sp,315                    )316                }317            } else {318                Column(verticalArrangement = Arrangement.spacedBy(6.dp)) {319                    history.forEach { item -> HistoryRow(item, onRestore, onCopy) }320                }321            }322        }323    }324}325326@Composable327private fun HistoryRow(328    item: PasswordHistoryItem,329    onRestore: (String) -> Unit,330    onCopy: (String) -> Unit,331) {332    val accent = LocalPeachAccent.current333    var revealed by remember { mutableStateOf(false) }334    Row(335        Modifier.fillMaxWidth().clip(RoundedCornerShape(8.dp))336            .border(1.dp, accent.copy(alpha = 0.12f), RoundedCornerShape(8.dp))337            .background(accent.copy(alpha = 0.06f)).padding(horizontal = 10.dp, vertical = 8.dp),338        verticalAlignment = Alignment.CenterVertically,339        horizontalArrangement = Arrangement.SpaceBetween,340    ) {341        Column(Modifier.weight(1f)) {342            Text(343                if (revealed) item.password else "••••••••••••",344                color = PeachColors.T0, fontFamily = PeachType.Mono, fontSize = 12.sp, maxLines = 1,345            )346            Text(347                timeSince(item.changedAt),348                color = accent.copy(alpha = 0.55f), fontFamily = PeachType.Mono, fontSize = 8.sp,349            )350        }351        Spacer(Modifier.width(6.dp))352        HistoryAction(if (revealed) "hide" else "show") { revealed = !revealed }353        HistoryAction("copy") { onCopy(item.password) }354        HistoryAction("use") { onRestore(item.password) }355    }356}357358@Composable359private fun HistoryAction(label: String, onClick: () -> Unit) {360    Text(361        label, color = LocalPeachAccent.current.copy(alpha = 0.7f),362        fontFamily = PeachType.Mono, fontSize = 9.sp,363        modifier = Modifier.clip(RoundedCornerShape(5.dp)).clickable(onClick = onClick).padding(horizontal = 5.dp, vertical = 3.dp),364    )365}366367/** Coarse "time since" label for the history list. Mirrors the browser helper. */368private fun timeSince(whenMs: Long): String {369    val secs = ((System.currentTimeMillis() - whenMs) / 1000).coerceAtLeast(0)370    if (secs < 60) return "just now"371    val mins = secs / 60372    if (mins < 60) return "${mins}m ago"373    val hours = mins / 60374    if (hours < 24) return "${hours}h ago"375    val days = hours / 24376    if (days < 30) return "${days}d ago"377    val months = days / 30378    if (months < 12) return "${months}mo ago"379    return "${months / 12}y ago"380}