EntryEditScreen.kt

15.6 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.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.text.TextStyle37import androidx.compose.ui.text.font.FontStyle38import androidx.compose.ui.text.font.FontWeight39import androidx.compose.ui.text.input.PasswordVisualTransformation40import androidx.compose.ui.text.input.VisualTransformation41import androidx.compose.ui.unit.dp42import androidx.compose.ui.unit.em43import androidx.compose.ui.unit.sp44import androidx.compose.runtime.collectAsState45import androidx.hilt.navigation.compose.hiltViewModel46import com.peachpasswords.ui.components.CopyButton47import 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.ShellBackground5354@Composable55fun EntryEditScreen(56    id: String?,57    onBack: () -> Unit,58    onOpenNote: (String) -> Unit = {},59    vm: EntryEditViewModel = hiltViewModel(),60) {61    val f by vm.form.collectAsState()62    val usernameSuggestions by vm.usernameSuggestions.collectAsState()63    val accent = LocalPeachAccent.current6465    // Ghost watermark fades in (0 → 0.03α over 300ms) once the title has any text.66    val watermarkAlpha by androidx.compose.animation.core.animateFloatAsState(67        targetValue = editGhostWatermarkTarget(f.title),68        animationSpec = tween(300),69        label = "edit-watermark",70    )7172    ShellBackground {73        Column(Modifier.fillMaxSize().statusBarsPadding()) {74            Box(Modifier.fillMaxWidth().height(56.dp).padding(horizontal = 18.dp)) {75                Text("cancel", color = peachColors.T2, fontFamily = PeachType.Mono, fontSize = 11.sp,76                    modifier = Modifier.align(Alignment.CenterStart)77                        .clip(RoundedCornerShape(8.dp)).clickable(onClick = onBack).padding(8.dp))78                Text(editTitleBarLabel(vm.isNew, f.type), color = peachColors.T1,79                    fontFamily = PeachType.Display, fontStyle = FontStyle.Italic, fontSize = 18.sp,80                    modifier = Modifier.align(Alignment.Center))81                Text("save", color = accent, fontFamily = PeachType.Mono, fontSize = 11.sp,82                    modifier = Modifier.align(Alignment.CenterEnd)83                        .clip(RoundedCornerShape(8.dp)).clickable { vm.save(onBack) }.padding(8.dp))84            }8586            Box(Modifier.fillMaxSize().clipToBounds()) {87                // Ghost PeachScript watermark behind the form (matches the detail hero):88                // the full title, fading in as the user types.89                if (watermarkAlpha > 0f) {90                    Text(91                        text = ghostWatermarkText(f.title),92                        style = TextStyle(fontFamily = PeachType.Script, fontSize = 140.sp),93                        color = peachColors.T0.copy(alpha = watermarkAlpha),94                        maxLines = 1,95                        softWrap = false,96                        overflow = androidx.compose.ui.text.style.TextOverflow.Clip,97                        modifier = Modifier.align(Alignment.TopEnd).offset(x = (-16).dp, y = 8.dp),98                    )99                }100                Column(101                    Modifier.fillMaxSize().verticalScroll(rememberScrollState()).padding(horizontal = 22.dp),102                ) {103                Row(horizontalArrangement = Arrangement.spacedBy(8.dp)) {104                    listOf("login", "note", "card").forEach { t ->105                        EntryTypePill(t, f.type == t) {106                            // A brand-new note skips the form entirely and drops straight107                            // into the full-screen notes editor (brief Fix 6 item 7).108                            if (shouldOpenNotesMode(vm.isNew, t)) vm.createNoteAndOpen(onOpenNote)109                            else vm.setType(t)110                        }111                    }112                }113                Spacer(Modifier.height(18.dp))114115                Field("title", f.title, vm::setTitle)116                if (f.type == "login") {117                    Field("url", f.url, vm::setUrl)118                    AdditionalUrlsEditor(119                        urls = f.additionalUrls,120                        onChange = vm::setAdditionalUrl,121                        onAdd = vm::addAdditionalUrl,122                        onRemove = vm::removeAdditionalUrl,123                    )124                    IdentitySuggestField("username", f.username, usernameSuggestions, vm::setUsername)125                    IdentitySuggestField("email", f.email, usernameSuggestions, vm::setEmail)126                    PasswordField(127                        value = f.password,128                        show = f.showPassword,129                        onChange = vm::setPassword,130                        onToggleShow = vm::toggleShowPassword,131                        onToggleGenerator = vm::toggleGenerator,132                    )133                    GeneratorDrawer(134                        open = f.generatorOpen,135                        length = f.genLength,136                        upper = f.genUpper, lower = f.genLower, numbers = f.genNumbers, symbols = f.genSymbols,137                        password = f.password,138                        copied = f.genCopied,139                        onLength = vm::setGenLength,140                        onUpper = vm::setGenUpper, onLower = vm::setGenLower,141                        onNumbers = vm::setGenNumbers, onSymbols = vm::setGenSymbols,142                        onGenerate = vm::generate,143                        onCopy = vm::copyGenerated,144                    )145                    PasswordHistorySection(146                        history = f.history,147                        expanded = f.showHistory,148                        onToggle = vm::toggleHistory,149                        onRestore = vm::restorePassword,150                        onCopy = vm::copyHistoryPassword,151                    )152                    Field("2FA secret (otpauth:// or base32)", f.totp, vm::setTotp)153                }154                if (f.type == "card") {155                    CardEditFields(156                        cardNumber = f.cardNumber,157                        cardholderName = f.cardholderName,158                        expiry = f.expiry,159                        cvc = f.cvc,160                        showCvc = f.showCvc,161                        network = f.cardNetwork,162                        onCardNumber = vm::setCardNumber,163                        onCardholder = vm::setCardholderName,164                        onExpiry = vm::setExpiry,165                        onCvc = vm::setCvc,166                        onToggleShowCvc = vm::toggleShowCvc,167                    )168                }169                Field("notes", f.notes, vm::setNotes, singleLine = false)170                TagChipEditor(171                    tags = f.tags,172                    allTags = vm.allTags,173                    onAdd = vm::addTag,174                    onRemove = vm::removeTag,175                )176177                f.error?.let {178                    Spacer(Modifier.height(8.dp))179                    Text(it, color = peachColors.Breach, fontFamily = PeachType.Mono, fontSize = 11.sp)180                }181                Spacer(Modifier.height(40.dp))182                }183            }184        }185    }186}187188@Composable189private fun EntryTypePill(type: String, selected: Boolean, onClick: () -> Unit) {190    val accent = LocalPeachAccent.current191    Box(192        modifier = Modifier193            .clip(RoundedCornerShape(PeachRadius.TypeTab))194            .border(195                1.dp,196                if (selected) accent.copy(alpha = 0.5f) else peachColors.T3,197                RoundedCornerShape(PeachRadius.TypeTab),198            )199            .background(if (selected) accent.copy(alpha = 0.08f) else Color.Transparent)200            .clickable(onClick = onClick)201            .padding(horizontal = 12.dp, vertical = 5.dp),202    ) {203        Text(204            text = type.uppercase(),205            color = if (selected) accent else peachColors.T2,206            fontFamily = PeachType.Mono,207            fontSize = 9.sp,208            letterSpacing = 0.08.em,209        )210    }211}212213/** Centered title-bar label: "new entry" / "new note" / "new card", or "edit". */214internal fun editTitleBarLabel(isNew: Boolean, type: String): String = when {215    !isNew -> "edit"216    type == "note" -> "new note"217    type == "card" -> "new card"218    else -> "new entry"219}220221/** A brand-new note opens the full-screen notes editor instead of the form. */222internal fun shouldOpenNotesMode(isNew: Boolean, type: String): Boolean = isNew && type == "note"223224/** Target alpha for the edit-screen ghost watermark — visible only once a title exists. */225internal fun editGhostWatermarkTarget(title: String): Float = if (title.isNotBlank()) 0.03f else 0f226227@Composable228private fun Field(229    label: String,230    value: String,231    onChange: (String) -> Unit,232    singleLine: Boolean = true,233    transformation: VisualTransformation = VisualTransformation.None,234    trailing: (@Composable () -> Unit)? = null,235) {236    PeachField(237        label = label,238        value = value,239        onChange = onChange,240        singleLine = singleLine,241        mono = true,242        visualTransformation = transformation,243        trailing = trailing,244    )245}246247@Composable248private fun PasswordField(249    value: String,250    show: Boolean,251    onChange: (String) -> Unit,252    onToggleShow: () -> Unit,253    onToggleGenerator: () -> Unit,254) {255    Field(256        label = "password",257        value = value,258        onChange = onChange,259        transformation = if (show) VisualTransformation.None else PasswordVisualTransformation(),260        trailing = {261            Text(if (show) "hide" else "show", color = LocalPeachAccent.current,262                fontFamily = PeachType.Mono, fontSize = 9.sp,263                modifier = Modifier.clip(RoundedCornerShape(6.dp)).clickable(onClick = onToggleShow).padding(6.dp))264            Text("⟳", color = LocalPeachAccent.current, fontSize = 16.sp,265                modifier = Modifier.clip(RoundedCornerShape(6.dp)).clickable(onClick = onToggleGenerator).padding(6.dp))266        },267    )268}269270@Composable271private fun GeneratorDrawer(272    open: Boolean,273    length: Int,274    upper: Boolean, lower: Boolean, numbers: Boolean, symbols: Boolean,275    password: String,276    copied: Boolean,277    onLength: (Int) -> Unit,278    onUpper: (Boolean) -> Unit, onLower: (Boolean) -> Unit,279    onNumbers: (Boolean) -> Unit, onSymbols: (Boolean) -> Unit,280    onGenerate: () -> Unit,281    onCopy: () -> Unit,282) {283    val height by animateDpAsState(284        targetValue = if (open) 234.dp else 0.dp,285        animationSpec = tween(320, easing = PeachMotion.Spring),286        label = "gen-drawer",287    )288    Box(289        Modifier.fillMaxWidth().height(height).clipToBounds()290            .clip(RoundedCornerShape(12.dp)).background(peachColors.S2),291    ) {292        GeneratorBody(293            length = length,294            upper = upper, lower = lower, numbers = numbers, symbols = symbols,295            password = password, copied = copied,296            onLength = onLength,297            onUpper = onUpper, onLower = onLower, onNumbers = onNumbers, onSymbols = onSymbols,298            onGenerate = onGenerate, onCopy = onCopy,299        )300    }301    if (open) Spacer(Modifier.height(14.dp))302}303304/**305 * Shared generator body — used by the inline drawer here and the detail306 * ModalBottomSheet (D4). Stateless; the generate row carries the [Copy] drain.307 */308@Composable309internal fun GeneratorBody(310    length: Int,311    upper: Boolean, lower: Boolean, numbers: Boolean, symbols: Boolean,312    password: String,313    copied: Boolean,314    onLength: (Int) -> Unit,315    onUpper: (Boolean) -> Unit, onLower: (Boolean) -> Unit,316    onNumbers: (Boolean) -> Unit, onSymbols: (Boolean) -> Unit,317    onGenerate: () -> Unit,318    onCopy: () -> Unit,319) {320    val accent = LocalPeachAccent.current321    Column(Modifier.fillMaxWidth().padding(16.dp)) {322        Row(Modifier.fillMaxWidth(), horizontalArrangement = Arrangement.SpaceBetween) {323            Text("GENERATOR", color = peachColors.T1, fontFamily = PeachType.Mono, fontSize = 8.sp)324            Text("length $length", color = accent, fontFamily = PeachType.Mono, fontSize = 9.sp)325        }326        Slider(327            value = length.toFloat(),328            onValueChange = { onLength(it.toInt()) },329            valueRange = 8f..128f,330            colors = SliderDefaults.colors(331                thumbColor = accent, activeTrackColor = accent, inactiveTrackColor = peachColors.T3,332            ),333        )334        Row(horizontalArrangement = Arrangement.spacedBy(8.dp)) {335            TogglePill("A-Z", upper, onUpper)336            TogglePill("a-z", lower, onLower)337            TogglePill("0-9", numbers, onNumbers)338            TogglePill("!@#", symbols, onSymbols)339        }340        Spacer(Modifier.height(12.dp))341        Row(verticalAlignment = Alignment.CenterVertically) {342            Box(343                Modifier.weight(1f).clip(RoundedCornerShape(10.dp))344                    .border(1.dp, accent.copy(alpha = 0.5f), RoundedCornerShape(10.dp))345                    .background(accent.copy(alpha = 0.10f))346                    .clickable(onClick = onGenerate).padding(vertical = 12.dp),347                contentAlignment = Alignment.Center,348            ) {349                Text("generate", color = accent, fontFamily = PeachType.Mono, fontSize = 12.sp)350            }351            Spacer(Modifier.width(8.dp))352            CopyButton(copied = copied, onCopy = { if (password.isNotEmpty()) onCopy() })353        }354    }355}356357@Composable358private fun TogglePill(label: String, on: Boolean, onToggle: (Boolean) -> Unit) {359    val accent = LocalPeachAccent.current360    Box(361        Modifier.clip(RoundedCornerShape(PeachRadius.TypeTab))362            .border(1.dp, if (on) accent.copy(alpha = 0.5f) else peachColors.T3, RoundedCornerShape(PeachRadius.TypeTab))363            .background(if (on) accent.copy(alpha = 0.10f) else Color.Transparent)364            .clickable { onToggle(!on) }365            .padding(horizontal = 10.dp, vertical = 6.dp),366    ) {367        Text(label, color = if (on) accent else peachColors.T2, fontFamily = PeachType.Mono, fontSize = 9.sp)368    }369}370371// PasswordHistorySection / HistoryRow / HistoryAction / timeSince were extracted372// to ui/vault/PasswordHistorySection.kt (D7) so EntryDetailScreen can reuse them.