EditableFieldRow.kt

10.8 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.lock.Biometrics56import com.peachpasswords.ui.theme.LocalPeachAccent57import com.peachpasswords.ui.theme.PeachColors58import com.peachpasswords.ui.theme.PeachType5960/**61 * A field row that toggles between read-only display and inline edit.62 *63 * View mode: value text + copy button (unchanged look from the existing FieldRow).64 * Edit mode: an underlined BasicTextField with ✓ save / ✗ cancel beneath it, and65 * — when [suggestions] is non-empty — a DropdownMenu of prior values to tap-fill.66 *67 * @param field   logical field key passed to [onSave] (e.g. "username", "email", "notes").68 * @param onSave  called with the trimmed draft when the user taps ✓; the caller69 *                (EntryDetailViewModel.updateField) persists it.70 * @param suggestions identity-suggest values for USERNAME/EMAIL; empty disables the dropdown.71 * @param mono    mono body style for the value (false = serif body, used by notes).72 */73@Composable74fun EditableFieldRow(75    label: String,76    field: String,77    value: String,78    copied: Boolean,79    onCopy: () -> Unit,80    onSave: (String) -> Unit,81    suggestions: List<String> = emptyList(),82    mono: Boolean = true,83    singleLine: Boolean = true,84) {85    var editing by remember(value) { mutableStateOf(false) }86    var draft by remember(value) { mutableStateOf(value) }8788    FieldShell(label) {89        if (!editing) {90            Text(91                value,92                color = PeachColors.T0,93                style = if (mono) PeachType.bodyMono else PeachType.body,94                modifier = Modifier95                    .weight(1f)96                    .clickable { draft = value; editing = true },97            )98            CopyButton(copied, onCopy)99        } else {100            InlineEditor(101                draft = draft,102                onDraftChange = { draft = it },103                onSave = { onSave(draft.trim()); editing = false },104                onCancel = { draft = value; editing = false },105                suggestions = suggestions,106                onPickSuggestion = { draft = it },107                singleLine = singleLine,108                modifier = Modifier.weight(1f),109            )110        }111    }112}113114/**115 * Password variant of [EditableFieldRow]. Entering edit mode is gated behind a116 * biometric (or device-credential) prompt, mirroring the existing reveal flow in117 * PasswordRow. The draft starts masked; a show/hide toggle controls visibility.118 *119 * The gate runs only on the view→edit transition. Once authenticated the field is120 * editable with the cleartext draft available.121 */122@Composable123fun EditablePasswordRow(124    value: String,125    copied: Boolean,126    entryTitle: String,127    onCopy: () -> Unit,128    onSave: (String) -> Unit,129) {130    val accent = LocalPeachAccent.current131    val context = LocalContext.current132    var editing by remember(value) { mutableStateOf(false) }133    var draft by remember(value) { mutableStateOf(value) }134    var show by remember { mutableStateOf(false) }135136    fun beginEdit() {137        val activity = context as? FragmentActivity138        if (activity != null && Biometrics.isAvailable(context)) {139            Biometrics.authenticate(140                activity,141                title = "Unlock Peach",142                subtitle = entryTitle,143                onSuccess = { draft = value; show = false; editing = true },144            )145        } else {146            // No biometrics: fall through (same fallback the reveal flow uses).147            draft = value; show = false; editing = true148        }149    }150151    FieldShell("password") {152        if (!editing) {153            Text(154                text = "•".repeat(value.length.coerceIn(8, 16)),155                color = PeachColors.T0,156                style = PeachType.bodyMono,157                modifier = Modifier.weight(1f).clickable { beginEdit() },158            )159            Text(160                "edit", color = accent, style = PeachType.labelMono,161                modifier = Modifier.clip(RoundedCornerShape(6.dp)).clickable { beginEdit() }.padding(8.dp),162            )163            CopyButton(copied, onCopy)164        } else {165            Column(Modifier.weight(1f)) {166                Row(verticalAlignment = Alignment.CenterVertically) {167                    UnderlinedField(168                        draft = draft,169                        onDraftChange = { draft = it },170                        transformation = if (show) VisualTransformation.None else PasswordVisualTransformation(),171                        singleLine = true,172                        modifier = Modifier.weight(1f),173                    )174                    Text(175                        if (show) "hide" else "show", color = accent, style = PeachType.labelMono,176                        modifier = Modifier.clip(RoundedCornerShape(6.dp)).clickable { show = !show }.padding(6.dp),177                    )178                }179                Spacer(Modifier.height(8.dp))180                SaveCancelRow(181                    onSave = { onSave(draft); editing = false },182                    onCancel = { draft = value; editing = false },183                )184            }185        }186    }187}188189/** Underlined editor + suggestion dropdown + save/cancel — the shared edit body. */190@Composable191private fun InlineEditor(192    draft: String,193    onDraftChange: (String) -> Unit,194    onSave: () -> Unit,195    onCancel: () -> Unit,196    suggestions: List<String>,197    onPickSuggestion: (String) -> Unit,198    singleLine: Boolean,199    modifier: Modifier = Modifier,200) {201    var dropdownOpen by remember { mutableStateOf(suggestions.isNotEmpty()) }202203    Column(modifier) {204        Box {205            UnderlinedField(206                draft = draft,207                onDraftChange = onDraftChange,208                singleLine = singleLine,209                modifier = Modifier.fillMaxWidth(),210            )211            if (suggestions.isNotEmpty()) {212                DropdownMenu(213                    expanded = dropdownOpen,214                    onDismissRequest = { dropdownOpen = false },215                ) {216                    suggestions.take(4).forEach { suggestion ->217                        DropdownMenuItem(218                            text = {219                                Text(220                                    suggestion,221                                    color = PeachColors.T1,222                                    style = PeachType.labelMono.copy(letterSpacing = 0.06.em),223                                )224                            },225                            onClick = { onPickSuggestion(suggestion); dropdownOpen = false },226                        )227                    }228                }229            }230        }231        Spacer(Modifier.height(8.dp))232        SaveCancelRow(onSave = onSave, onCancel = onCancel)233    }234}235236/** A BasicTextField with an accent underline drawn beneath it (spec §5 inline edit). */237@Composable238private fun UnderlinedField(239    draft: String,240    onDraftChange: (String) -> Unit,241    transformation: VisualTransformation = VisualTransformation.None,242    singleLine: Boolean = true,243    modifier: Modifier = Modifier,244) {245    val accent = LocalPeachAccent.current246    BasicTextField(247        value = draft,248        onValueChange = onDraftChange,249        singleLine = singleLine,250        textStyle = PeachType.bodyMono.copy(color = PeachColors.T0),251        cursorBrush = SolidColor(accent),252        visualTransformation = transformation,253        modifier = modifier254            .padding(bottom = 4.dp)255            .drawBehind {256                drawLine(257                    color = accent,258                    start = Offset(0f, size.height),259                    end = Offset(size.width, size.height),260                    strokeWidth = 1.dp.toPx(),261                )262            },263    )264}265266@Composable267private fun SaveCancelRow(onSave: () -> Unit, onCancel: () -> Unit) {268    val accent = LocalPeachAccent.current269    Row(horizontalArrangement = Arrangement.spacedBy(16.dp), verticalAlignment = Alignment.CenterVertically) {270        Text(271            "✓ save", color = accent, style = PeachType.labelMono,272            modifier = Modifier.clip(RoundedCornerShape(6.dp)).clickable(onClick = onSave).padding(vertical = 4.dp),273        )274        Text(275            "✗", color = PeachColors.T2, style = PeachType.labelMono,276            modifier = Modifier.clip(RoundedCornerShape(6.dp)).clickable(onClick = onCancel).padding(horizontal = 6.dp, vertical = 4.dp),277        )278        Spacer(Modifier.width(1.dp))279    }280}