AdditionalUrlsDetail.kt

7.5 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.material.icons.Icons25import androidx.compose.material.icons.automirrored.rounded.OpenInNew26import androidx.compose.material.icons.rounded.Check27import androidx.compose.material.icons.rounded.ContentCopy28import androidx.compose.material3.Icon29import androidx.compose.material3.Text30import androidx.compose.runtime.Composable31import androidx.compose.ui.Alignment32import androidx.compose.ui.Modifier33import androidx.compose.ui.draw.clip34import androidx.compose.ui.draw.drawBehind35import androidx.compose.ui.geometry.Offset36import androidx.compose.ui.geometry.Size37import androidx.compose.ui.graphics.Brush38import androidx.compose.ui.graphics.Color39import androidx.compose.ui.platform.LocalContext40import androidx.compose.ui.text.TextStyle41import androidx.compose.ui.unit.dp42import androidx.compose.ui.unit.em43import androidx.compose.ui.unit.sp44import com.peachpasswords.ui.theme.LocalPeachAccent45import com.peachpasswords.ui.theme.LocalReduceMotion46import com.peachpasswords.ui.theme.PeachColors47import com.peachpasswords.ui.theme.PeachType4849/**50 * E3 — additional-URL rows for EntryDetailScreen. Self-contained: it carries its51 * own field shell, copy button, and open-in-browser control so it can be inserted52 * into the detail field stack without touching the screen's private `FieldShell`53 * / `CopyButton`. Each row shows "URL N", the value, copy, and open-in-browser.54 *55 * `copiedKeyFor(index)` is the per-row clipboard latch key the caller passes to56 * `vm.copy(key, value)`; `copiedField` is the screen's current latch.57 */58@Composable59fun AdditionalUrlRows(60    urls: List<String>,61    copiedField: String?,62    onCopy: (key: String, value: String) -> Unit,63) {64    val context = LocalContext.current65    urls.forEachIndexed { index, url ->66        if (url.isNotBlank()) {67            val key = additionalUrlCopyKey(index)68            AdditionalUrlFieldShell(label = "URL ${index + 2}") {69                Text(70                    url,71                    color = PeachColors.T0,72                    style = PeachType.bodyMono,73                    modifier = Modifier.weight(1f),74                )75                OpenInBrowserButton(onClick = { openUrlInBrowser(context, url) })76                Spacer(Modifier.width(4.dp))77                AdditionalUrlCopyButton(copied = copiedField == key, onCopy = { onCopy(key, url) })78            }79        }80    }81}8283/** Per-row clipboard latch key; keeps each URL's copy feedback independent. */84fun additionalUrlCopyKey(index: Int): String = "additionalUrl:$index"8586/**87 * Launches ACTION_VIEW for the URL, prefixing a scheme when the stored value is88 * bare (so "example.com" still resolves). Falls back to a toast if no browser89 * handles the intent.90 */91fun openUrlInBrowser(context: Context, raw: String) {92    val normalized = if (raw.matches(Regex("^[a-zA-Z][a-zA-Z0-9+.-]*://.*"))) raw else "https://$raw"93    try {94        context.startActivity(95            Intent(Intent.ACTION_VIEW, Uri.parse(normalized)).apply {96                addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)97            },98        )99    } catch (_: ActivityNotFoundException) {100        Toast.makeText(context, "No app can open this link", Toast.LENGTH_SHORT).show()101    }102}103104/** Mirrors EntryDetailScreen's private FieldShell: mono label over a value/action row with a t3 hairline. */105@Composable106private fun AdditionalUrlFieldShell(107    label: String,108    content: @Composable androidx.compose.foundation.layout.RowScope.() -> Unit,109) {110    Column(111        Modifier.fillMaxWidth().padding(vertical = 10.dp).drawBehind {112            drawRect(113                color = PeachColors.T3,114                topLeft = Offset(0f, size.height - 1.dp.toPx()),115                size = Size(size.width, 1.dp.toPx()),116            )117        },118    ) {119        Text(label.uppercase(), color = PeachColors.T2, style = PeachType.labelMono.copy(letterSpacing = 0.08.em))120        Spacer(Modifier.height(6.dp))121        Row(verticalAlignment = Alignment.CenterVertically, content = content)122    }123}124125@Composable126private fun OpenInBrowserButton(onClick: () -> Unit) {127    Box(128        Modifier129            .size(36.dp)130            .clip(RoundedCornerShape(6.dp))131            .background(PeachColors.S2)132            .clickable(onClick = onClick),133        contentAlignment = Alignment.Center,134    ) {135        Icon(136            Icons.AutoMirrored.Rounded.OpenInNew,137            contentDescription = "open in browser",138            tint = PeachColors.T2,139            modifier = Modifier.size(15.dp),140        )141    }142}143144/** Mirrors EntryDetailScreen's private CopyButton (sheen sweep + drain bar, spec §4.4). */145@Composable146private fun AdditionalUrlCopyButton(copied: Boolean, onCopy: () -> Unit) {147    val accent = LocalPeachAccent.current148    val reduceMotion = LocalReduceMotion.current149    val animate = copied && !reduceMotion150    val drain by animateFloatAsState(151        targetValue = if (copied) 1f else 0f,152        animationSpec = if (animate) tween(1400, easing = LinearEasing) else tween(0),153        label = "url-copy-drain",154    )155    val sheen by animateFloatAsState(156        targetValue = if (copied) 1f else 0f,157        animationSpec = if (animate) tween(600, easing = LinearEasing) else tween(0),158        label = "url-copy-sheen",159    )160    Box(161        Modifier162            .size(36.dp)163            .clip(RoundedCornerShape(6.dp))164            .background(PeachColors.S2)165            .clickable(onClick = onCopy)166            .drawBehind {167                if (animate && sheen < 1f) {168                    val sweepX = (sheen * 2f - 0.5f) * size.width169                    val band = size.width * 0.5f170                    drawRect(171                        brush = Brush.linearGradient(172                            colors = listOf(Color.Transparent, accent.copy(alpha = 0.35f), Color.Transparent),173                            start = Offset(sweepX - band, 0f),174                            end = Offset(sweepX + band, size.height),175                        ),176                    )177                }178                if (drain > 0f) {179                    val h = 2.dp.toPx()180                    drawRect(181                        color = accent,182                        topLeft = Offset(0f, size.height - h),183                        size = Size(size.width * drain, h),184                    )185                }186            },187        contentAlignment = Alignment.Center,188    ) {189        Icon(190            if (copied) Icons.Rounded.Check else Icons.Rounded.ContentCopy,191            contentDescription = "copy",192            tint = if (copied) accent else PeachColors.T1,193            modifier = Modifier.size(15.dp),194        )195    }196}