EditableFieldRow.kt

10.9 KB · raw

1package com.peachpasswords.ui.vault23/*4 * D6 — Inline per-field editing for EntryDetailScreen.5 *6 * Self-contained drop-in composables (orchestrator merges these into7 * EntryDetailScreen.kt — D6/D7/E6 all touch that file, so this stays isolated8 * with anchors documented in INTEGRATION.md).9 *10 * Provides:11 *   - EditableFieldRow      : view <-> edit toggle, underlined BasicTextField,12 *                             ✓ save / ✗ cancel, optional identity-suggest dropdown.13 *   - EditablePasswordRow   : same, but gated behind a biometric/password unlock14 *                             before entering edit mode (mirrors the reveal flow).15 *16 * Both reuse the file's existing private helpers FieldShell, FieldRow-style17 * label/value layout, and CopyButton. Those are already present in18 * EntryDetailScreen.kt; this draft references them by name. If the orchestrator19 * places this in a separate file instead of inlining, FieldShell + CopyButton20 * must be made `internal` (see INTEGRATION.md).21 */2223import androidx.compose.foundation.clickable24import androidx.compose.foundation.layout.Arrangement25import androidx.compose.foundation.layout.Box26import androidx.compose.foundation.layout.Column27import androidx.compose.foundation.layout.Row28import androidx.compose.foundation.layout.Spacer29import androidx.compose.foundation.layout.fillMaxWidth30import androidx.compose.foundation.layout.height31import androidx.compose.foundation.layout.padding32import androidx.compose.foundation.layout.width33import androidx.compose.foundation.shape.RoundedCornerShape34import androidx.compose.foundation.text.BasicTextField35import androidx.compose.material3.DropdownMenu36import androidx.compose.material3.DropdownMenuItem37import androidx.compose.material3.Text38import androidx.compose.runtime.Composable39import androidx.compose.runtime.getValue40import androidx.compose.runtime.mutableStateOf41import androidx.compose.runtime.remember42import androidx.compose.runtime.setValue43import androidx.compose.ui.Alignment44import androidx.compose.ui.Modifier45import androidx.compose.ui.draw.clip46import androidx.compose.ui.draw.drawBehind47import androidx.compose.ui.geometry.Offset48import androidx.compose.ui.graphics.SolidColor49import androidx.compose.ui.platform.LocalContext50import androidx.compose.ui.text.input.PasswordVisualTransformation51import androidx.compose.ui.text.input.VisualTransformation52import androidx.compose.ui.unit.dp53import androidx.compose.ui.unit.em54import androidx.fragment.app.FragmentActivity55import com.peachpasswords.ui.components.CopyButton56import com.peachpasswords.ui.lock.Biometrics57import com.peachpasswords.ui.theme.LocalPeachAccent58import com.peachpasswords.ui.theme.PeachColors59import com.peachpasswords.ui.theme.PeachType6061/**62 * A field row that toggles between read-only display and inline edit.63 *64 * View mode: value text + copy button (unchanged look from the existing FieldRow).65 * Edit mode: an underlined BasicTextField with ✓ save / ✗ cancel beneath it, and66 * — when [suggestions] is non-empty — a DropdownMenu of prior values to tap-fill.67 *68 * @param field   logical field key passed to [onSave] (e.g. "username", "email", "notes").69 * @param onSave  called with the trimmed draft when the user taps ✓; the caller70 *                (EntryDetailViewModel.updateField) persists it.71 * @param suggestions identity-suggest values for USERNAME/EMAIL; empty disables the dropdown.72 * @param mono    mono body style for the value (false = serif body, used by notes).73 */74@Composable75fun EditableFieldRow(76    label: String,77    field: String,78    value: String,79    copied: Boolean,80    onCopy: () -> Unit,81    onSave: (String) -> Unit,82    suggestions: List<String> = emptyList(),83    mono: Boolean = true,84    singleLine: Boolean = true,85) {86    var editing by remember(value) { mutableStateOf(false) }87    var draft by remember(value) { mutableStateOf(value) }8889    FieldShell(label) {90        if (!editing) {91            Text(92                value,93                color = PeachColors.T0,94                style = if (mono) PeachType.bodyMono else PeachType.body,95                modifier = Modifier96                    .weight(1f)97                    .clickable { draft = value; editing = true },98            )99            CopyButton(copied, onCopy)100        } else {101            InlineEditor(102                draft = draft,103                onDraftChange = { draft = it },104                onSave = { onSave(draft.trim()); editing = false },105                onCancel = { draft = value; editing = false },106                suggestions = suggestions,107                onPickSuggestion = { draft = it },108                singleLine = singleLine,109                modifier = Modifier.weight(1f),110            )111        }112    }113}114115/**116 * Password variant of [EditableFieldRow]. Entering edit mode is gated behind a117 * biometric (or device-credential) prompt, mirroring the existing reveal flow in118 * PasswordRow. The draft starts masked; a show/hide toggle controls visibility.119 *120 * The gate runs only on the view→edit transition. Once authenticated the field is121 * editable with the cleartext draft available.122 */123@Composable124fun EditablePasswordRow(125    value: String,126    copied: Boolean,127    entryTitle: String,128    onCopy: () -> Unit,129    onSave: (String) -> Unit,130    onGenerate: () -> Unit,131) {132    val accent = LocalPeachAccent.current133    val context = LocalContext.current134    var editing by remember(value) { mutableStateOf(false) }135    var draft by remember(value) { mutableStateOf(value) }136    var show by remember { mutableStateOf(false) }137138    fun beginEdit() {139        val activity = context as? FragmentActivity140        if (activity != null && Biometrics.isAvailable(context)) {141            Biometrics.authenticate(142                activity,143                title = "Unlock Peach",144                subtitle = entryTitle,145                onSuccess = { draft = value; show = false; editing = true },146            )147        } else {148            // No biometrics: fall through (same fallback the reveal flow uses).149            draft = value; show = false; editing = true150        }151    }152153    FieldShell("password") {154        if (!editing) {155            Text(156                text = "•".repeat(value.length.coerceIn(8, 16)),157                color = PeachColors.T0,158                style = PeachType.bodyMono,159                modifier = Modifier.weight(1f).clickable { beginEdit() },160            )161            Text(162                "edit", color = accent, style = PeachType.labelMono,163                modifier = Modifier.clip(RoundedCornerShape(6.dp)).clickable { beginEdit() }.padding(8.dp),164            )165            CopyButton(copied, onCopy)166            WrenchButton(onClick = onGenerate)167        } else {168            Column(Modifier.weight(1f)) {169                Row(verticalAlignment = Alignment.CenterVertically) {170                    UnderlinedField(171                        draft = draft,172                        onDraftChange = { draft = it },173                        transformation = if (show) VisualTransformation.None else PasswordVisualTransformation(),174                        singleLine = true,175                        modifier = Modifier.weight(1f),176                    )177                    Text(178                        if (show) "hide" else "show", color = accent, style = PeachType.labelMono,179                        modifier = Modifier.clip(RoundedCornerShape(6.dp)).clickable { show = !show }.padding(6.dp),180                    )181                }182                Spacer(Modifier.height(8.dp))183                SaveCancelRow(184                    onSave = { onSave(draft); editing = false },185                    onCancel = { draft = value; editing = false },186                )187            }188        }189    }190}191192/** Underlined editor + suggestion dropdown + save/cancel — the shared edit body. */193@Composable194private fun InlineEditor(195    draft: String,196    onDraftChange: (String) -> Unit,197    onSave: () -> Unit,198    onCancel: () -> Unit,199    suggestions: List<String>,200    onPickSuggestion: (String) -> Unit,201    singleLine: Boolean,202    modifier: Modifier = Modifier,203) {204    var dropdownOpen by remember { mutableStateOf(suggestions.isNotEmpty()) }205206    Column(modifier) {207        Box {208            UnderlinedField(209                draft = draft,210                onDraftChange = onDraftChange,211                singleLine = singleLine,212                modifier = Modifier.fillMaxWidth(),213            )214            if (suggestions.isNotEmpty()) {215                DropdownMenu(216                    expanded = dropdownOpen,217                    onDismissRequest = { dropdownOpen = false },218                ) {219                    suggestions.take(4).forEach { suggestion ->220                        DropdownMenuItem(221                            text = {222                                Text(223                                    suggestion,224                                    color = PeachColors.T1,225                                    style = PeachType.labelMono.copy(letterSpacing = 0.06.em),226                                )227                            },228                            onClick = { onPickSuggestion(suggestion); dropdownOpen = false },229                        )230                    }231                }232            }233        }234        Spacer(Modifier.height(8.dp))235        SaveCancelRow(onSave = onSave, onCancel = onCancel)236    }237}238239/** A BasicTextField with an accent underline drawn beneath it (spec §5 inline edit). */240@Composable241private fun UnderlinedField(242    draft: String,243    onDraftChange: (String) -> Unit,244    transformation: VisualTransformation = VisualTransformation.None,245    singleLine: Boolean = true,246    modifier: Modifier = Modifier,247) {248    val accent = LocalPeachAccent.current249    BasicTextField(250        value = draft,251        onValueChange = onDraftChange,252        singleLine = singleLine,253        textStyle = PeachType.bodyMono.copy(color = PeachColors.T0),254        cursorBrush = SolidColor(accent),255        visualTransformation = transformation,256        modifier = modifier257            .padding(bottom = 4.dp)258            .drawBehind {259                drawLine(260                    color = accent,261                    start = Offset(0f, size.height),262                    end = Offset(size.width, size.height),263                    strokeWidth = 1.dp.toPx(),264                )265            },266    )267}268269@Composable270private fun SaveCancelRow(onSave: () -> Unit, onCancel: () -> Unit) {271    val accent = LocalPeachAccent.current272    Row(horizontalArrangement = Arrangement.spacedBy(16.dp), verticalAlignment = Alignment.CenterVertically) {273        Text(274            "✓ save", color = accent, style = PeachType.labelMono,275            modifier = Modifier.clip(RoundedCornerShape(6.dp)).clickable(onClick = onSave).padding(vertical = 4.dp),276        )277        Text(278            "✗", color = PeachColors.T2, style = PeachType.labelMono,279            modifier = Modifier.clip(RoundedCornerShape(6.dp)).clickable(onClick = onCancel).padding(horizontal = 6.dp, vertical = 4.dp),280        )281        Spacer(Modifier.width(1.dp))282    }283}