EntryEditScreen.kt

15.0 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.components.CopyButton49import com.peachpasswords.ui.theme.LocalPeachAccent50import com.peachpasswords.ui.theme.PeachColors51import com.peachpasswords.ui.theme.PeachMotion52import com.peachpasswords.ui.theme.PeachRadius53import com.peachpasswords.ui.theme.PeachType54import com.peachpasswords.ui.theme.ShellBackground5556@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                // Ghost PeachScript watermark behind the form (matches the detail hero).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                        password = f.password,124                        copied = f.genCopied,125                        onLength = vm::setGenLength,126                        onUpper = vm::setGenUpper, onLower = vm::setGenLower,127                        onNumbers = vm::setGenNumbers, onSymbols = vm::setGenSymbols,128                        onGenerate = vm::generate,129                        onCopy = vm::copyGenerated,130                    )131                    PasswordHistorySection(132                        history = f.history,133                        expanded = f.showHistory,134                        onToggle = vm::toggleHistory,135                        onRestore = vm::restorePassword,136                        onCopy = vm::copyHistoryPassword,137                    )138                    Field("2FA secret (otpauth:// or base32)", f.totp, vm::setTotp)139                }140                if (f.type == "card") {141                    CardEditFields(142                        cardNumber = f.cardNumber,143                        cardholderName = f.cardholderName,144                        expiry = f.expiry,145                        cvc = f.cvc,146                        showCvc = f.showCvc,147                        network = f.cardNetwork,148                        onCardNumber = vm::setCardNumber,149                        onCardholder = vm::setCardholderName,150                        onExpiry = vm::setExpiry,151                        onCvc = vm::setCvc,152                        onToggleShowCvc = vm::toggleShowCvc,153                    )154                }155                Field("notes", f.notes, vm::setNotes, singleLine = false)156                TagChipEditor(157                    tags = f.tags,158                    allTags = vm.allTags,159                    onAdd = vm::addTag,160                    onRemove = vm::removeTag,161                )162163                f.error?.let {164                    Spacer(Modifier.height(8.dp))165                    Text(it, color = PeachColors.Breach, fontFamily = PeachType.Mono, fontSize = 11.sp)166                }167                Spacer(Modifier.height(40.dp))168                }169            }170        }171    }172}173174@Composable175private fun EntryTypePill(type: String, selected: Boolean, onClick: () -> Unit) {176    val accent = LocalPeachAccent.current177    Box(178        modifier = Modifier179            .clip(RoundedCornerShape(PeachRadius.TypeTab))180            .border(181                1.dp,182                if (selected) accent.copy(alpha = 0.5f) else PeachColors.T3,183                RoundedCornerShape(PeachRadius.TypeTab),184            )185            .background(if (selected) accent.copy(alpha = 0.08f) else Color.Transparent)186            .clickable(onClick = onClick)187            .padding(horizontal = 12.dp, vertical = 5.dp),188    ) {189        Text(190            text = type.uppercase(),191            color = if (selected) accent else PeachColors.T2,192            fontFamily = PeachType.Mono,193            fontSize = 9.sp,194            letterSpacing = 0.08.em,195        )196    }197}198199@Composable200private fun Field(201    label: String,202    value: String,203    onChange: (String) -> Unit,204    singleLine: Boolean = true,205    transformation: VisualTransformation = VisualTransformation.None,206    trailing: (@Composable () -> Unit)? = null,207) {208    val accent = LocalPeachAccent.current209    Column(Modifier.fillMaxWidth().padding(bottom = 14.dp)) {210        Text(label.uppercase(), color = PeachColors.T1, fontFamily = PeachType.Mono, fontSize = 8.sp)211        Spacer(Modifier.height(6.dp))212        Row(verticalAlignment = Alignment.CenterVertically) {213            Box(214                Modifier.weight(1f)215                    .border(1.dp, PeachColors.T3, RoundedCornerShape(10.dp))216                    .padding(horizontal = 12.dp, vertical = 12.dp),217            ) {218                BasicTextField(219                    value = value,220                    onValueChange = onChange,221                    singleLine = singleLine,222                    textStyle = TextStyle(color = PeachColors.T0, fontFamily = PeachType.Mono, fontSize = 13.sp),223                    cursorBrush = SolidColor(accent),224                    visualTransformation = transformation,225                    modifier = Modifier.fillMaxWidth(),226                )227            }228            if (trailing != null) { Spacer(Modifier.width(8.dp)); trailing() }229        }230    }231}232233@Composable234private fun PasswordField(235    value: String,236    show: Boolean,237    onChange: (String) -> Unit,238    onToggleShow: () -> Unit,239    onToggleGenerator: () -> Unit,240) {241    Field(242        label = "password",243        value = value,244        onChange = onChange,245        transformation = if (show) VisualTransformation.None else PasswordVisualTransformation(),246        trailing = {247            Text(if (show) "hide" else "show", color = LocalPeachAccent.current,248                fontFamily = PeachType.Mono, fontSize = 9.sp,249                modifier = Modifier.clip(RoundedCornerShape(6.dp)).clickable(onClick = onToggleShow).padding(6.dp))250            Text("⟳", color = LocalPeachAccent.current, fontSize = 16.sp,251                modifier = Modifier.clip(RoundedCornerShape(6.dp)).clickable(onClick = onToggleGenerator).padding(6.dp))252        },253    )254}255256@Composable257private fun GeneratorDrawer(258    open: Boolean,259    length: Int,260    upper: Boolean, lower: Boolean, numbers: Boolean, symbols: Boolean,261    password: String,262    copied: Boolean,263    onLength: (Int) -> Unit,264    onUpper: (Boolean) -> Unit, onLower: (Boolean) -> Unit,265    onNumbers: (Boolean) -> Unit, onSymbols: (Boolean) -> Unit,266    onGenerate: () -> Unit,267    onCopy: () -> Unit,268) {269    val height by animateDpAsState(270        targetValue = if (open) 234.dp else 0.dp,271        animationSpec = tween(320, easing = PeachMotion.Spring),272        label = "gen-drawer",273    )274    Box(275        Modifier.fillMaxWidth().height(height).clipToBounds()276            .clip(RoundedCornerShape(12.dp)).background(PeachColors.S2),277    ) {278        GeneratorBody(279            length = length,280            upper = upper, lower = lower, numbers = numbers, symbols = symbols,281            password = password, copied = copied,282            onLength = onLength,283            onUpper = onUpper, onLower = onLower, onNumbers = onNumbers, onSymbols = onSymbols,284            onGenerate = onGenerate, onCopy = onCopy,285        )286    }287    if (open) Spacer(Modifier.height(14.dp))288}289290/**291 * Shared generator body — used by the inline drawer here and the detail292 * ModalBottomSheet (D4). Stateless; the generate row carries the [Copy] drain.293 */294@Composable295internal fun GeneratorBody(296    length: Int,297    upper: Boolean, lower: Boolean, numbers: Boolean, symbols: Boolean,298    password: String,299    copied: Boolean,300    onLength: (Int) -> Unit,301    onUpper: (Boolean) -> Unit, onLower: (Boolean) -> Unit,302    onNumbers: (Boolean) -> Unit, onSymbols: (Boolean) -> Unit,303    onGenerate: () -> Unit,304    onCopy: () -> Unit,305) {306    val accent = LocalPeachAccent.current307    Column(Modifier.fillMaxWidth().padding(16.dp)) {308        Row(Modifier.fillMaxWidth(), horizontalArrangement = Arrangement.SpaceBetween) {309            Text("GENERATOR", color = PeachColors.T1, fontFamily = PeachType.Mono, fontSize = 8.sp)310            Text("length $length", color = accent, fontFamily = PeachType.Mono, fontSize = 9.sp)311        }312        Slider(313            value = length.toFloat(),314            onValueChange = { onLength(it.toInt()) },315            valueRange = 8f..128f,316            colors = SliderDefaults.colors(317                thumbColor = accent, activeTrackColor = accent, inactiveTrackColor = PeachColors.T3,318            ),319        )320        Row(horizontalArrangement = Arrangement.spacedBy(8.dp)) {321            TogglePill("A-Z", upper, onUpper)322            TogglePill("a-z", lower, onLower)323            TogglePill("0-9", numbers, onNumbers)324            TogglePill("!@#", symbols, onSymbols)325        }326        Spacer(Modifier.height(12.dp))327        Row(verticalAlignment = Alignment.CenterVertically) {328            Box(329                Modifier.weight(1f).clip(RoundedCornerShape(10.dp))330                    .border(1.dp, accent.copy(alpha = 0.5f), RoundedCornerShape(10.dp))331                    .background(accent.copy(alpha = 0.10f))332                    .clickable(onClick = onGenerate).padding(vertical = 12.dp),333                contentAlignment = Alignment.Center,334            ) {335                Text("generate", color = accent, fontFamily = PeachType.Mono, fontSize = 12.sp)336            }337            Spacer(Modifier.width(8.dp))338            CopyButton(copied = copied, onCopy = { if (password.isNotEmpty()) onCopy() })339        }340    }341}342343@Composable344private fun TogglePill(label: String, on: Boolean, onToggle: (Boolean) -> Unit) {345    val accent = LocalPeachAccent.current346    Box(347        Modifier.clip(RoundedCornerShape(PeachRadius.TypeTab))348            .border(1.dp, if (on) accent.copy(alpha = 0.5f) else PeachColors.T3, RoundedCornerShape(PeachRadius.TypeTab))349            .background(if (on) accent.copy(alpha = 0.10f) else Color.Transparent)350            .clickable { onToggle(!on) }351            .padding(horizontal = 10.dp, vertical = 6.dp),352    ) {353        Text(label, color = if (on) accent else PeachColors.T2, fontFamily = PeachType.Mono, fontSize = 9.sp)354    }355}356357// PasswordHistorySection / HistoryRow / HistoryAction / timeSince were extracted358// to ui/vault/PasswordHistorySection.kt (D7) so EntryDetailScreen can reuse them.