package com.peachpasswords.ui.vault import android.view.WindowManager import androidx.compose.animation.core.LinearEasing import androidx.compose.animation.core.RepeatMode import androidx.compose.animation.core.animateFloat import androidx.compose.animation.core.animateFloatAsState import androidx.compose.animation.core.infiniteRepeatable import androidx.compose.animation.core.rememberInfiniteTransition import androidx.compose.animation.core.tween import androidx.compose.foundation.background import androidx.compose.foundation.border import androidx.compose.foundation.clickable import androidx.compose.foundation.layout.Arrangement import androidx.compose.foundation.layout.Box import androidx.compose.foundation.layout.Column import androidx.compose.foundation.layout.Row import androidx.compose.foundation.layout.Spacer import androidx.compose.foundation.layout.fillMaxSize import androidx.compose.foundation.layout.fillMaxWidth import androidx.compose.foundation.layout.height import androidx.compose.foundation.layout.offset import androidx.compose.foundation.layout.padding import androidx.compose.foundation.layout.size import androidx.compose.foundation.layout.statusBarsPadding import androidx.compose.foundation.layout.width import androidx.compose.foundation.rememberScrollState import androidx.compose.foundation.shape.CircleShape import androidx.compose.foundation.shape.RoundedCornerShape import androidx.compose.material.icons.rounded.Check import androidx.compose.material.icons.rounded.ContentCopy import androidx.compose.foundation.verticalScroll import androidx.compose.material.icons.Icons import androidx.compose.material.icons.automirrored.rounded.ArrowBack import androidx.compose.material.icons.outlined.DeleteOutline import androidx.compose.material3.Icon import androidx.compose.material3.Text import androidx.compose.runtime.Composable import androidx.compose.runtime.DisposableEffect import androidx.compose.runtime.getValue import androidx.compose.runtime.mutableStateOf import androidx.compose.runtime.produceState import androidx.compose.runtime.remember import androidx.compose.runtime.setValue import androidx.compose.ui.Alignment import androidx.compose.ui.Modifier import androidx.compose.ui.draw.clip import androidx.compose.ui.draw.drawBehind import androidx.compose.ui.geometry.Offset import androidx.compose.ui.geometry.Size import androidx.compose.ui.graphics.Brush import androidx.compose.ui.graphics.Color import androidx.compose.ui.graphics.graphicsLayer import androidx.compose.ui.platform.LocalContext import androidx.compose.ui.text.TextStyle import androidx.compose.ui.unit.dp import androidx.compose.ui.unit.em import androidx.compose.ui.unit.sp import androidx.compose.runtime.collectAsState import androidx.fragment.app.FragmentActivity import androidx.hilt.navigation.compose.hiltViewModel import com.peachpasswords.ui.PeachNav import com.peachpasswords.ui.components.CharEnterText import com.peachpasswords.ui.components.FaviconInitials import com.peachpasswords.ui.components.TOTPRing import com.peachpasswords.ui.lock.Biometrics import com.peachpasswords.ui.theme.LocalPeachAccent import com.peachpasswords.ui.theme.LocalReduceMotion import com.peachpasswords.ui.theme.PeachColors import com.peachpasswords.ui.theme.PeachType import com.peachpasswords.ui.theme.ShellBackground import com.peachpasswords.vault.TOTPGenerator import com.peachpasswords.vault.hasTOTP @Composable fun EntryDetailScreen( id: String, onNavigate: (PeachNav) -> Unit, onBack: () -> Unit, vm: EntryDetailViewModel = hiltViewModel(), ) { val entry by vm.entry.collectAsState() val revealed by vm.revealed.collectAsState() val copiedField by vm.copiedField.collectAsState() val context = LocalContext.current val accent = LocalPeachAccent.current var confirmingDelete by remember { mutableStateOf(false) } // FLAG_SECURE only while this screen is shown — blocks screenshots of secrets. DisposableEffect(Unit) { val window = (context as? FragmentActivity)?.window window?.setFlags(WindowManager.LayoutParams.FLAG_SECURE, WindowManager.LayoutParams.FLAG_SECURE) onDispose { window?.clearFlags(WindowManager.LayoutParams.FLAG_SECURE) } } val e = entry ShellBackground { Column(Modifier.fillMaxSize().statusBarsPadding()) { // Header: back · favicon+domain (or delete confirm) · trash Row( Modifier.fillMaxWidth().height(56.dp).padding(horizontal = 8.dp), verticalAlignment = Alignment.CenterVertically, ) { Icon( Icons.AutoMirrored.Rounded.ArrowBack, contentDescription = "back", tint = PeachColors.T0, modifier = Modifier.size(40.dp).clip(CircleShape).clickable(onClick = onBack).padding(8.dp), ) Box(Modifier.weight(1f), contentAlignment = Alignment.Center) { when { e == null -> {} confirmingDelete -> Text( "delete “${e.title}”?", color = PeachColors.Breach, style = PeachType.labelMono.copy(letterSpacing = 0.08.em), ) else -> Row( verticalAlignment = Alignment.CenterVertically, horizontalArrangement = Arrangement.spacedBy(8.dp), ) { FaviconInitials(source = e.url ?: e.title, size = 16.dp) val domain = remember(e.url, e.title) { e.displayDomain() } if (domain.isNotEmpty()) { Text(domain, color = PeachColors.T2, style = TextStyle(fontFamily = PeachType.Mono, fontSize = 9.sp)) } } } } if (e != null) { Icon( Icons.Outlined.DeleteOutline, contentDescription = "delete", tint = if (confirmingDelete) PeachColors.Breach else PeachColors.T1, modifier = Modifier.size(40.dp).clip(CircleShape) .clickable { if (confirmingDelete) vm.delete(onDone = onBack) else confirmingDelete = true } .padding(8.dp), ) } } if (e == null) { Box(Modifier.fillMaxSize(), contentAlignment = Alignment.Center) { Text("entry not found", color = PeachColors.T1, style = PeachType.bodyMono) } return@Column } Column( Modifier.fillMaxSize().verticalScroll(rememberScrollState()) .padding(horizontal = 24.dp), ) { // Hero — ghost PeachScript watermark behind the entry name. Box(Modifier.fillMaxWidth()) { Text( text = e.title.firstOrNull()?.uppercase() ?: "P", style = TextStyle(fontFamily = PeachType.Script, fontSize = 140.sp), color = PeachColors.T0.copy(alpha = 0.03f), modifier = Modifier.align(Alignment.TopCenter).offset(y = (-16).dp), ) Column(Modifier.fillMaxWidth().padding(top = 18.dp)) { CharEnterText( text = e.title, style = PeachType.entryNameHero.copy(color = PeachColors.T0), ) Spacer(Modifier.height(8.dp)) Text( text = "edit", color = accent, style = PeachType.labelMono, modifier = Modifier.clip(RoundedCornerShape(6.dp)) .clickable { onNavigate(PeachNav.EntryEdit(e.id)) } .padding(vertical = 4.dp), ) } } Spacer(Modifier.height(20.dp)) e.username?.takeIf { it.isNotBlank() }?.let { FieldRow("username", it, copiedField == "username") { vm.copy("username", it) } } e.email?.takeIf { it.isNotBlank() }?.let { FieldRow("email", it, copiedField == "email") { vm.copy("email", it) } } e.password?.takeIf { it.isNotBlank() }?.let { pw -> PasswordRow( value = pw, revealed = revealed, copied = copiedField == "password", onReveal = { if (revealed) vm.mask() else { val activity = context as? FragmentActivity if (activity != null && Biometrics.isAvailable(context)) { Biometrics.authenticate(activity, "Unlock Peach", e.title, onSuccess = vm::reveal) } else vm.reveal() } }, onCopy = { vm.copy("password", pw) }, ) } if (e.hasTOTP()) { Spacer(Modifier.height(4.dp)) TotpBand(totpField = e.totp!!, copied = copiedField == "totp", onCopy = vm::copyTotp) } e.notes?.takeIf { it.isNotBlank() }?.let { FieldRow("notes", it, copiedField == "notes", mono = false) { vm.copy("notes", it) } } Spacer(Modifier.height(48.dp)) } } } } @Composable private fun FieldRow( label: String, value: String, copied: Boolean, mono: Boolean = true, onCopy: () -> Unit, ) { FieldShell(label) { Text( value, color = PeachColors.T0, style = if (mono) PeachType.bodyMono else PeachType.body, modifier = Modifier.weight(1f), ) CopyButton(copied, onCopy) } } @Composable private fun PasswordRow( value: String, revealed: Boolean, copied: Boolean, onReveal: () -> Unit, onCopy: () -> Unit, ) { val accent = LocalPeachAccent.current FieldShell("password") { Text( text = if (revealed) value else "•".repeat(value.length.coerceIn(8, 16)), color = PeachColors.T0, style = PeachType.bodyMono, modifier = Modifier.weight(1f), ) Text( if (revealed) "hide" else "show", color = accent, style = PeachType.labelMono, modifier = Modifier.clip(RoundedCornerShape(6.dp)).clickable(onClick = onReveal).padding(8.dp), ) CopyButton(copied, onCopy) } } /** A field row: uppercase mono label over a value/action row, with a t3 hairline beneath (spec §5.4). */ @Composable private fun FieldShell( label: String, content: @Composable androidx.compose.foundation.layout.RowScope.() -> Unit, ) { Column( Modifier.fillMaxWidth().padding(vertical = 10.dp).drawBehind { drawRect( color = PeachColors.T3, topLeft = Offset(0f, size.height - 1.dp.toPx()), size = Size(size.width, 1.dp.toPx()), ) }, ) { Text(label.uppercase(), color = PeachColors.T2, style = PeachType.labelMono.copy(letterSpacing = 0.08.em)) Spacer(Modifier.height(6.dp)) Row(verticalAlignment = Alignment.CenterVertically, content = content) } } /** Accent-tinted TOTP band (spec §5.4): large Cormorant code, mono countdown, sweep ring. */ @Composable private fun TotpBand(totpField: String, copied: Boolean, onCopy: () -> Unit) { val accent = LocalPeachAccent.current val reduceMotion = LocalReduceMotion.current val code by produceState(TOTPGenerator.generateCode(totpField) ?: "------") { while (true) { value = TOTPGenerator.generateCode(totpField) ?: "------" kotlinx.coroutines.delay(1000) } } val remaining by produceState(TOTPGenerator.remainingSeconds()) { while (true) { value = TOTPGenerator.remainingSeconds(); kotlinx.coroutines.delay(500) } } val isHot = remaining <= 5 // Gentle pulse for the final 5s; the transition always runs but only applies when hot. val pulse = rememberInfiniteTransition(label = "totp-pulse") val pulseAlpha by pulse.animateFloat( initialValue = 0.55f, targetValue = 1f, animationSpec = infiniteRepeatable(tween(600), RepeatMode.Reverse), label = "totp-pulse-alpha", ) val codeAlpha = if (isHot && !reduceMotion) pulseAlpha else 1f Box( Modifier .fillMaxWidth() .padding(vertical = 10.dp) .clip(RoundedCornerShape(8.dp)) .background(accent.copy(alpha = 0.08f)) .border(1.dp, accent.copy(alpha = 0.20f), RoundedCornerShape(8.dp)) .padding(horizontal = 14.dp, vertical = 10.dp), ) { Row(verticalAlignment = Alignment.CenterVertically) { Column(Modifier.weight(1f)) { Text( "ONE-TIME CODE", color = accent.copy(alpha = 0.70f), style = PeachType.labelMono.copy(letterSpacing = 0.08.em), ) val spaced = if (code.length == 6) "${code.substring(0, 3)} ${code.substring(3)}" else code Text( spaced, color = if (isHot) PeachColors.Breach else accent, style = PeachType.totpCode, modifier = Modifier.graphicsLayer { alpha = codeAlpha }, ) } Text("${remaining}s", color = PeachColors.T2, style = PeachType.totpCountdown) Spacer(Modifier.width(12.dp)) TOTPRing(size = 28.dp) Spacer(Modifier.width(8.dp)) CopyButton(copied, onCopy) } } } /** * Copy button with the two-effect copy feedback (spec §4.4): a diagonal sheen * sweeps once over ~600ms and an accent drain bar fills 0→100% over 1.4s. Both * collapse instantly when the copied latch clears, and are skipped under * reduce-motion. */ @Composable private fun CopyButton(copied: Boolean, onCopy: () -> Unit) { val accent = LocalPeachAccent.current val reduceMotion = LocalReduceMotion.current val animate = copied && !reduceMotion val drain by animateFloatAsState( targetValue = if (copied) 1f else 0f, animationSpec = if (animate) tween(1400, easing = LinearEasing) else tween(0), label = "copy-drain", ) val sheen by animateFloatAsState( targetValue = if (copied) 1f else 0f, animationSpec = if (animate) tween(600, easing = LinearEasing) else tween(0), label = "copy-sheen", ) Box( Modifier .size(36.dp) .clip(RoundedCornerShape(6.dp)) .background(PeachColors.S2) .clickable(onClick = onCopy) .drawBehind { if (animate && sheen < 1f) { val sweepX = (sheen * 2f - 0.5f) * size.width val band = size.width * 0.5f drawRect( brush = Brush.linearGradient( colors = listOf(Color.Transparent, accent.copy(alpha = 0.35f), Color.Transparent), start = Offset(sweepX - band, 0f), end = Offset(sweepX + band, size.height), ), ) } if (drain > 0f) { val h = 2.dp.toPx() drawRect( color = accent, topLeft = Offset(0f, size.height - h), size = Size(size.width * drain, h), ) } }, contentAlignment = Alignment.Center, ) { Icon( if (copied) Icons.Rounded.Check else Icons.Rounded.ContentCopy, contentDescription = "copy", tint = if (copied) accent else PeachColors.T1, modifier = Modifier.size(15.dp), ) } } private fun com.peachpasswords.vault.VaultEntry.displayDomain(): String { val raw = url if (!raw.isNullOrBlank()) { val cleaned = raw.replace(Regex("^https?://"), "").removePrefix("www.").substringBefore('/') if (cleaned.isNotEmpty()) return cleaned } return "" }