AdditionalUrlsDetail.kt

10.1 KB · raw

1package com.peachpasswords.ui.vault23import android.content.ActivityNotFoundException4import android.content.Context5import android.content.Intent6import android.net.Uri7import android.widget.Toast8import androidx.compose.animation.core.LinearEasing9import androidx.compose.animation.core.animateFloatAsState10import androidx.compose.animation.core.tween11import androidx.compose.foundation.background12import androidx.compose.foundation.clickable13import androidx.compose.foundation.layout.Arrangement14import androidx.compose.foundation.layout.Box15import androidx.compose.foundation.layout.Column16import androidx.compose.foundation.layout.Row17import androidx.compose.foundation.layout.Spacer18import androidx.compose.foundation.layout.fillMaxWidth19import androidx.compose.foundation.layout.height20import androidx.compose.foundation.layout.padding21import androidx.compose.foundation.layout.size22import androidx.compose.foundation.layout.width23import androidx.compose.foundation.shape.RoundedCornerShape24import androidx.compose.foundation.text.BasicTextField25import androidx.compose.material.icons.Icons26import androidx.compose.material.icons.automirrored.rounded.OpenInNew27import androidx.compose.material.icons.rounded.Check28import androidx.compose.material.icons.rounded.ContentCopy29import androidx.compose.material3.Icon30import androidx.compose.material3.Text31import androidx.compose.runtime.Composable32import androidx.compose.runtime.getValue33import androidx.compose.runtime.mutableStateOf34import androidx.compose.ui.Alignment35import androidx.compose.ui.Modifier36import androidx.compose.ui.draw.clip37import androidx.compose.ui.draw.drawBehind38import androidx.compose.ui.geometry.Offset39import androidx.compose.ui.geometry.Size40import androidx.compose.ui.graphics.Brush41import androidx.compose.ui.graphics.Color42import androidx.compose.ui.graphics.SolidColor43import androidx.compose.ui.platform.LocalContext44import androidx.compose.ui.text.TextStyle45import androidx.compose.ui.unit.dp46import androidx.compose.ui.unit.em47import androidx.compose.ui.unit.sp48import androidx.compose.runtime.remember49import androidx.compose.runtime.setValue50import com.peachpasswords.ui.theme.LocalPeachAccent51import com.peachpasswords.ui.theme.LocalReduceMotion52import com.peachpasswords.ui.theme.peachColors53import com.peachpasswords.ui.theme.PeachType5455/**56 * E3 — additional-URL rows for EntryDetailScreen. Self-contained: it carries its57 * own field shell, copy button, and open-in-browser control so it can be inserted58 * into the detail field stack without touching the screen's private `FieldShell`59 * / `CopyButton`. Each row shows "URL N", the value, copy, and open-in-browser.60 *61 * `copiedKeyFor(index)` is the per-row clipboard latch key the caller passes to62 * `vm.copy(key, value)`; `copiedField` is the screen's current latch.63 */64@Composable65fun AdditionalUrlRows(66    urls: List<String>,67    copiedField: String?,68    onCopy: (key: String, value: String) -> Unit,69    onSave: (index: Int, value: String) -> Unit = { _, _ -> },70) {71    val context = LocalContext.current72    urls.forEachIndexed { index, url ->73        if (url.isNotBlank()) {74            val key = additionalUrlCopyKey(index)75            var editing by remember(url) { mutableStateOf(false) }76            var draft by remember(url) { mutableStateOf(url) }77            AdditionalUrlFieldShell(label = "URL ${index + 2}") {78                if (editing) {79                    Column(Modifier.weight(1f)) {80                        AdditionalUrlInlineField(81                            draft = draft,82                            onDraftChange = { draft = it },83                            modifier = Modifier.fillMaxWidth(),84                        )85                        Spacer(Modifier.height(8.dp))86                        AdditionalUrlSaveCancelRow(87                            onSave = { onSave(index, draft); editing = false },88                            onCancel = { draft = url; editing = false },89                        )90                    }91                } else {92                    Text(93                        url,94                        color = peachColors.T0,95                        style = PeachType.bodyMono,96                        modifier = Modifier.weight(1f).clickable { draft = url; editing = true },97                    )98                    OpenInBrowserButton(onClick = { openUrlInBrowser(context, url) })99                    Spacer(Modifier.width(4.dp))100                    AdditionalUrlCopyButton(copied = copiedField == key, onCopy = { onCopy(key, url) })101                }102            }103        }104    }105}106107@Composable108private fun AdditionalUrlInlineField(109    draft: String,110    onDraftChange: (String) -> Unit,111    modifier: Modifier = Modifier,112) {113    val accent = LocalPeachAccent.current114    BasicTextField(115        value = draft,116        onValueChange = onDraftChange,117        singleLine = true,118        textStyle = PeachType.bodyMono.copy(color = peachColors.T0),119        cursorBrush = SolidColor(accent),120        modifier = modifier121            .padding(bottom = 4.dp)122            .drawBehind {123                drawLine(124                    color = accent,125                    start = Offset(0f, size.height),126                    end = Offset(size.width, size.height),127                    strokeWidth = 1.dp.toPx(),128                )129            },130    )131}132133@Composable134private fun AdditionalUrlSaveCancelRow(onSave: () -> Unit, onCancel: () -> Unit) {135    val accent = LocalPeachAccent.current136    Row(horizontalArrangement = Arrangement.spacedBy(16.dp), verticalAlignment = Alignment.CenterVertically) {137        Text(138            "✓ save", color = accent, style = PeachType.labelMono,139            modifier = Modifier.clip(RoundedCornerShape(6.dp)).clickable(onClick = onSave).padding(vertical = 4.dp),140        )141        Text(142            "✗", color = peachColors.T2, style = PeachType.labelMono,143            modifier = Modifier.clip(RoundedCornerShape(6.dp)).clickable(onClick = onCancel).padding(horizontal = 6.dp, vertical = 4.dp),144        )145    }146}147148/** Per-row clipboard latch key; keeps each URL's copy feedback independent. */149fun additionalUrlCopyKey(index: Int): String = "additionalUrl:$index"150151/**152 * Launches ACTION_VIEW for the URL, prefixing a scheme when the stored value is153 * bare (so "example.com" still resolves). Falls back to a toast if no browser154 * handles the intent.155 */156fun openUrlInBrowser(context: Context, raw: String) {157    val normalized = if (raw.matches(Regex("^[a-zA-Z][a-zA-Z0-9+.-]*://.*"))) raw else "https://$raw"158    try {159        context.startActivity(160            Intent(Intent.ACTION_VIEW, Uri.parse(normalized)).apply {161                addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)162            },163        )164    } catch (_: ActivityNotFoundException) {165        Toast.makeText(context, "No app can open this link", Toast.LENGTH_SHORT).show()166    }167}168169/** Mirrors EntryDetailScreen's private FieldShell: mono label over a value/action row with a t3 hairline. */170@Composable171private fun AdditionalUrlFieldShell(172    label: String,173    content: @Composable androidx.compose.foundation.layout.RowScope.() -> Unit,174) {175    val hairline = peachColors.T3176    Column(177        Modifier.fillMaxWidth().padding(vertical = 10.dp).drawBehind {178            drawRect(179                color = hairline,180                topLeft = Offset(0f, size.height - 1.dp.toPx()),181                size = Size(size.width, 1.dp.toPx()),182            )183        },184    ) {185        Text(label.uppercase(), color = peachColors.T2, style = PeachType.labelMono.copy(letterSpacing = 0.08.em))186        Spacer(Modifier.height(6.dp))187        Row(verticalAlignment = Alignment.CenterVertically, content = content)188    }189}190191@Composable192private fun OpenInBrowserButton(onClick: () -> Unit) {193    Box(194        Modifier195            .size(36.dp)196            .clip(RoundedCornerShape(6.dp))197            .background(peachColors.S2)198            .clickable(onClick = onClick),199        contentAlignment = Alignment.Center,200    ) {201        Icon(202            Icons.AutoMirrored.Rounded.OpenInNew,203            contentDescription = "open in browser",204            tint = peachColors.T2,205            modifier = Modifier.size(15.dp),206        )207    }208}209210/** Mirrors EntryDetailScreen's private CopyButton (sheen sweep + drain bar, spec §4.4). */211@Composable212private fun AdditionalUrlCopyButton(copied: Boolean, onCopy: () -> Unit) {213    val accent = LocalPeachAccent.current214    val reduceMotion = LocalReduceMotion.current215    val animate = copied && !reduceMotion216    val drain by animateFloatAsState(217        targetValue = if (copied) 1f else 0f,218        animationSpec = if (animate) tween(1400, easing = LinearEasing) else tween(0),219        label = "url-copy-drain",220    )221    val sheen by animateFloatAsState(222        targetValue = if (copied) 1f else 0f,223        animationSpec = if (animate) tween(600, easing = LinearEasing) else tween(0),224        label = "url-copy-sheen",225    )226    Box(227        Modifier228            .size(36.dp)229            .clip(RoundedCornerShape(6.dp))230            .background(peachColors.S2)231            .clickable(onClick = onCopy)232            .drawBehind {233                if (animate && sheen < 1f) {234                    val sweepX = (sheen * 2f - 0.5f) * size.width235                    val band = size.width * 0.5f236                    drawRect(237                        brush = Brush.linearGradient(238                            colors = listOf(Color.Transparent, accent.copy(alpha = 0.35f), Color.Transparent),239                            start = Offset(sweepX - band, 0f),240                            end = Offset(sweepX + band, size.height),241                        ),242                    )243                }244                if (drain > 0f) {245                    val h = 2.dp.toPx()246                    drawRect(247                        color = accent,248                        topLeft = Offset(0f, size.height - h),249                        size = Size(size.width * drain, h),250                    )251                }252            },253        contentAlignment = Alignment.Center,254    ) {255        Icon(256            if (copied) Icons.Rounded.Check else Icons.Rounded.ContentCopy,257            contentDescription = "copy",258            tint = if (copied) accent else peachColors.T1,259            modifier = Modifier.size(15.dp),260        )261    }262}