package com.peachpasswords.ui.vault import android.view.WindowManager import androidx.compose.foundation.background 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.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.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.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()) { TotpCard(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) } } @Composable private fun TotpCard(totpField: String, copied: Boolean, onCopy: () -> Unit) { val code by produceState(TOTPGenerator.generateCode(totpField) ?: "------") { while (true) { value = TOTPGenerator.generateCode(totpField) ?: "------" kotlinx.coroutines.delay(1000) } } FieldShell("one-time code") { val spaced = if (code.length == 6) "${code.substring(0, 3)} ${code.substring(3)}" else code Text(spaced, color = PeachColors.T0, style = PeachType.bodyMono, modifier = Modifier.weight(1f)) TOTPRing(size = 26.dp) Spacer(Modifier.width(8.dp)) CopyButton(copied, onCopy) } } @Composable private fun CopyButton(copied: Boolean, onCopy: () -> Unit) { Box( Modifier.size(36.dp).clip(RoundedCornerShape(8.dp)).clickable(onClick = onCopy), contentAlignment = Alignment.Center, ) { Text(if (copied) "✓" else "⧉", color = if (copied) PeachColors.SyncOk else PeachColors.T1, fontSize = 15.sp) } } 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 "" }