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.PeachType4950515253545556575859@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}838485fun additionalUrlCopyKey(index: Int): String = "additionalUrl:$index"86878889909192fun 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}104105106@Composable107private fun AdditionalUrlFieldShell(108 label: String,109 content: @Composable androidx.compose.foundation.layout.RowScope.() -> Unit,110) {111 val hairline = peachColors.T3112 Column(113 Modifier.fillMaxWidth().padding(vertical = 10.dp).drawBehind {114 drawRect(115 color = hairline,116 topLeft = Offset(0f, size.height - 1.dp.toPx()),117 size = Size(size.width, 1.dp.toPx()),118 )119 },120 ) {121 Text(label.uppercase(), color = peachColors.T2, style = PeachType.labelMono.copy(letterSpacing = 0.08.em))122 Spacer(Modifier.height(6.dp))123 Row(verticalAlignment = Alignment.CenterVertically, content = content)124 }125}126127@Composable128private fun OpenInBrowserButton(onClick: () -> Unit) {129 Box(130 Modifier131 .size(36.dp)132 .clip(RoundedCornerShape(6.dp))133 .background(peachColors.S2)134 .clickable(onClick = onClick),135 contentAlignment = Alignment.Center,136 ) {137 Icon(138 Icons.AutoMirrored.Rounded.OpenInNew,139 contentDescription = "open in browser",140 tint = peachColors.T2,141 modifier = Modifier.size(15.dp),142 )143 }144}145146147@Composable148private fun AdditionalUrlCopyButton(copied: Boolean, onCopy: () -> Unit) {149 val accent = LocalPeachAccent.current150 val reduceMotion = LocalReduceMotion.current151 val animate = copied && !reduceMotion152 val drain by animateFloatAsState(153 targetValue = if (copied) 1f else 0f,154 animationSpec = if (animate) tween(1400, easing = LinearEasing) else tween(0),155 label = "url-copy-drain",156 )157 val sheen by animateFloatAsState(158 targetValue = if (copied) 1f else 0f,159 animationSpec = if (animate) tween(600, easing = LinearEasing) else tween(0),160 label = "url-copy-sheen",161 )162 Box(163 Modifier164 .size(36.dp)165 .clip(RoundedCornerShape(6.dp))166 .background(peachColors.S2)167 .clickable(onClick = onCopy)168 .drawBehind {169 if (animate && sheen < 1f) {170 val sweepX = (sheen * 2f - 0.5f) * size.width171 val band = size.width * 0.5f172 drawRect(173 brush = Brush.linearGradient(174 colors = listOf(Color.Transparent, accent.copy(alpha = 0.35f), Color.Transparent),175 start = Offset(sweepX - band, 0f),176 end = Offset(sweepX + band, size.height),177 ),178 )179 }180 if (drain > 0f) {181 val h = 2.dp.toPx()182 drawRect(183 color = accent,184 topLeft = Offset(0f, size.height - h),185 size = Size(size.width * drain, h),186 )187 }188 },189 contentAlignment = Alignment.Center,190 ) {191 Icon(192 if (copied) Icons.Rounded.Check else Icons.Rounded.ContentCopy,193 contentDescription = "copy",194 tint = if (copied) accent else peachColors.T1,195 modifier = Modifier.size(15.dp),196 )197 }198}