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.runtime.getValue32import androidx.compose.ui.Alignment33import androidx.compose.ui.Modifier34import androidx.compose.ui.draw.clip35import androidx.compose.ui.draw.drawBehind36import androidx.compose.ui.geometry.Offset37import androidx.compose.ui.geometry.Size38import androidx.compose.ui.graphics.Brush39import androidx.compose.ui.graphics.Color40import androidx.compose.ui.platform.LocalContext41import androidx.compose.ui.text.TextStyle42import androidx.compose.ui.unit.dp43import androidx.compose.ui.unit.em44import androidx.compose.ui.unit.sp45import com.peachpasswords.ui.theme.LocalPeachAccent46import com.peachpasswords.ui.theme.LocalReduceMotion47import com.peachpasswords.ui.theme.PeachColors48import com.peachpasswords.ui.theme.PeachType4950/**51 * E3 — additional-URL rows for EntryDetailScreen. Self-contained: it carries its52 * own field shell, copy button, and open-in-browser control so it can be inserted53 * into the detail field stack without touching the screen's private `FieldShell`54 * / `CopyButton`. Each row shows "URL N", the value, copy, and open-in-browser.55 *56 * `copiedKeyFor(index)` is the per-row clipboard latch key the caller passes to57 * `vm.copy(key, value)`; `copiedField` is the screen's current latch.58 */59@Composable60fun AdditionalUrlRows(61    urls: List<String>,62    copiedField: String?,63    onCopy: (key: String, value: String) -> Unit,64) {65    val context = LocalContext.current66    urls.forEachIndexed { index, url ->67        if (url.isNotBlank()) {68            val key = additionalUrlCopyKey(index)69            AdditionalUrlFieldShell(label = "URL ${index + 2}") {70                Text(71                    url,72                    color = PeachColors.T0,73                    style = PeachType.bodyMono,74                    modifier = Modifier.weight(1f),75                )76                OpenInBrowserButton(onClick = { openUrlInBrowser(context, url) })77                Spacer(Modifier.width(4.dp))78                AdditionalUrlCopyButton(copied = copiedField == key, onCopy = { onCopy(key, url) })79            }80        }81    }82}8384/** Per-row clipboard latch key; keeps each URL's copy feedback independent. */85fun additionalUrlCopyKey(index: Int): String = "additionalUrl:$index"8687/**88 * Launches ACTION_VIEW for the URL, prefixing a scheme when the stored value is89 * bare (so "example.com" still resolves). Falls back to a toast if no browser90 * handles the intent.91 */92fun openUrlInBrowser(context: Context, raw: String) {93    val normalized = if (raw.matches(Regex("^[a-zA-Z][a-zA-Z0-9+.-]*://.*"))) raw else "https://$raw"94    try {95        context.startActivity(96            Intent(Intent.ACTION_VIEW, Uri.parse(normalized)).apply {97                addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)98            },99        )100    } catch (_: ActivityNotFoundException) {101        Toast.makeText(context, "No app can open this link", Toast.LENGTH_SHORT).show()102    }103}104105/** Mirrors EntryDetailScreen's private FieldShell: mono label over a value/action row with a t3 hairline. */106@Composable107private fun AdditionalUrlFieldShell(108    label: String,109    content: @Composable androidx.compose.foundation.layout.RowScope.() -> Unit,110) {111    Column(112        Modifier.fillMaxWidth().padding(vertical = 10.dp).drawBehind {113            drawRect(114                color = PeachColors.T3,115                topLeft = Offset(0f, size.height - 1.dp.toPx()),116                size = Size(size.width, 1.dp.toPx()),117            )118        },119    ) {120        Text(label.uppercase(), color = PeachColors.T2, style = PeachType.labelMono.copy(letterSpacing = 0.08.em))121        Spacer(Modifier.height(6.dp))122        Row(verticalAlignment = Alignment.CenterVertically, content = content)123    }124}125126@Composable127private fun OpenInBrowserButton(onClick: () -> Unit) {128    Box(129        Modifier130            .size(36.dp)131            .clip(RoundedCornerShape(6.dp))132            .background(PeachColors.S2)133            .clickable(onClick = onClick),134        contentAlignment = Alignment.Center,135    ) {136        Icon(137            Icons.AutoMirrored.Rounded.OpenInNew,138            contentDescription = "open in browser",139            tint = PeachColors.T2,140            modifier = Modifier.size(15.dp),141        )142    }143}144145/** Mirrors EntryDetailScreen's private CopyButton (sheen sweep + drain bar, spec §4.4). */146@Composable147private fun AdditionalUrlCopyButton(copied: Boolean, onCopy: () -> Unit) {148    val accent = LocalPeachAccent.current149    val reduceMotion = LocalReduceMotion.current150    val animate = copied && !reduceMotion151    val drain by animateFloatAsState(152        targetValue = if (copied) 1f else 0f,153        animationSpec = if (animate) tween(1400, easing = LinearEasing) else tween(0),154        label = "url-copy-drain",155    )156    val sheen by animateFloatAsState(157        targetValue = if (copied) 1f else 0f,158        animationSpec = if (animate) tween(600, easing = LinearEasing) else tween(0),159        label = "url-copy-sheen",160    )161    Box(162        Modifier163            .size(36.dp)164            .clip(RoundedCornerShape(6.dp))165            .background(PeachColors.S2)166            .clickable(onClick = onCopy)167            .drawBehind {168                if (animate && sheen < 1f) {169                    val sweepX = (sheen * 2f - 0.5f) * size.width170                    val band = size.width * 0.5f171                    drawRect(172                        brush = Brush.linearGradient(173                            colors = listOf(Color.Transparent, accent.copy(alpha = 0.35f), Color.Transparent),174                            start = Offset(sweepX - band, 0f),175                            end = Offset(sweepX + band, size.height),176                        ),177                    )178                }179                if (drain > 0f) {180                    val h = 2.dp.toPx()181                    drawRect(182                        color = accent,183                        topLeft = Offset(0f, size.height - h),184                        size = Size(size.width * drain, h),185                    )186                }187            },188        contentAlignment = Alignment.Center,189    ) {190        Icon(191            if (copied) Icons.Rounded.Check else Icons.Rounded.ContentCopy,192            contentDescription = "copy",193            tint = if (copied) accent else PeachColors.T1,194            modifier = Modifier.size(15.dp),195        )196    }197}