package com.peachpasswords.ui.vault import android.view.WindowManager import androidx.compose.animation.AnimatedVisibility 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.clipToBounds 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.text.font.FontStyle import androidx.compose.ui.text.style.TextOverflow 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.CopyButton 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 import com.peachpasswords.vault.isBreach import com.peachpasswords.vault.isCard @Composable fun EntryDetailScreen( id: String, onNavigate: (PeachNav) -> Unit, onBack: () -> Unit, vm: EntryDetailViewModel = hiltViewModel(), ) { val entry by vm.entry.collectAsState() val identitySuggestions by vm.identitySuggestions.collectAsState() val cvcRevealed by vm.cvcRevealed.collectAsState() var cardRevealed by remember { mutableStateOf(false) } val copiedField by vm.copiedField.collectAsState() val context = LocalContext.current val accent = LocalPeachAccent.current var confirmingDelete by remember { mutableStateOf(false) } var generatorOpen by remember { mutableStateOf(false) } var historyExpanded 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 } // D11 — breach banner strip when this entry's password is known-compromised. if (e.isBreach()) { Row( Modifier.fillMaxWidth() .background(peachColors.BreachBg) .padding(horizontal = 16.dp, vertical = 8.dp), verticalAlignment = Alignment.CenterVertically, ) { Text("⚠", color = peachColors.Breach, style = PeachType.bodyMono) Spacer(Modifier.width(10.dp)) Text( "Password found in a data breach — change it now", color = peachColors.Breach, style = PeachType.labelMono, modifier = Modifier.weight(1f), ) Text( "Change password", color = accent, style = PeachType.labelMono, modifier = Modifier.clip(RoundedCornerShape(6.dp)) .clickable { onNavigate(PeachNav.EntryEdit(e.id)) } .padding(horizontal = 6.dp, vertical = 4.dp), ) } } Column( Modifier.fillMaxSize().verticalScroll(rememberScrollState()) .padding(horizontal = 24.dp), ) { // Hero — ghost PeachScript watermark behind the entry name. The full // title is drawn (not just its first letter); a long title overflows // and clips naturally, which is the intended watermark behavior. Box(Modifier.fillMaxWidth().clipToBounds()) { Text( text = ghostWatermarkText(e.title), style = TextStyle(fontFamily = PeachType.Display, fontSize = 150.sp, fontStyle = FontStyle.Italic), color = peachColors.T0.copy(alpha = 0.03f), maxLines = 1, softWrap = false, overflow = TextOverflow.Clip, 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)) if (e.isCard()) { CardDetailFields( entry = e, cardRevealed = cardRevealed, cvcRevealed = cvcRevealed, copiedField = copiedField, onToggleCardReveal = { cardRevealed = !cardRevealed }, onRevealCvc = { val activity = context as? FragmentActivity if (activity != null && Biometrics.isAvailable(context)) { Biometrics.authenticate(activity, "Unlock Peach", e.title, onSuccess = vm::revealCvc) } else vm.revealCvc() }, onMaskCvc = vm::maskCvc, onCopy = vm::copy, copyButton = { copied, onCopy -> CopyButton(copied, onCopy) }, ) } else { e.username?.takeIf { it.isNotBlank() }?.let { v -> EditableFieldRow( label = "username", field = "username", value = v, copied = copiedField == "username", onCopy = { vm.copy("username", v) }, onSave = { vm.updateField("username", it) }, suggestions = identitySuggestions, ) } e.email?.takeIf { it.isNotBlank() }?.let { v -> EditableFieldRow( label = "email", field = "email", value = v, copied = copiedField == "email", onCopy = { vm.copy("email", v) }, onSave = { vm.updateField("email", it) }, suggestions = identitySuggestions, ) } e.additionalUrls?.takeIf { it.any { u -> u.isNotBlank() } }?.let { urls -> AdditionalUrlRows( urls = urls, copiedField = copiedField, onCopy = { key, value -> vm.copy(key, value) }, ) } e.password?.takeIf { it.isNotBlank() }?.let { pw -> EditablePasswordRow( value = pw, copied = copiedField == "password", entryTitle = e.title, onCopy = { vm.copy("password", pw) }, onSave = { vm.updateField("password", it) }, onGenerate = { generatorOpen = true }, ) } val history = e.passwordHistory.orEmpty() if (history.isNotEmpty()) { PasswordHistoryToggle( count = history.size, expanded = historyExpanded, onToggle = { historyExpanded = !historyExpanded }, ) AnimatedVisibility(visible = historyExpanded) { Column(Modifier.padding(top = 8.dp, bottom = 6.dp)) { PasswordHistoryList( history = history, onRestore = { oldPw -> vm.restorePassword(e.id, oldPw) }, onCopy = { oldPw -> vm.copy("password", oldPw) }, ) } } } if (e.hasTOTP()) { Spacer(Modifier.height(4.dp)) TotpBandEditable( totpField = e.totp!!, copied = copiedField == "totp", onCopy = vm::copyTotp, onSaveSecret = { newSecret -> vm.updateField("totp", newSecret) }, ) } } e.notes?.takeIf { it.isNotBlank() }?.let { v -> EditableFieldRow( label = "notes", field = "notes", value = v, copied = copiedField == "notes", onCopy = { vm.copy("notes", v) }, onSave = { vm.updateField("notes", it) }, mono = false, singleLine = false, ) } e.tags?.takeIf { it.isNotEmpty() }?.let { tags -> DetailTagRow( tags = tags, onEditTags = { onNavigate(PeachNav.EntryEdit(e.id)) }, ) } Spacer(Modifier.height(48.dp)) } if (generatorOpen) { GeneratorSheet( copied = copiedField == "genDraft", onCopy = { draft -> vm.copy("genDraft", draft) }, onUse = { draft -> vm.applyGeneratedPassword(draft) { generatorOpen = false } }, onDismiss = { generatorOpen = false }, ) } } } } /** A field row: uppercase mono label over a value/action row, with a t3 hairline beneath (spec §5.4). */ @Composable internal fun FieldShell( label: String, content: @Composable androidx.compose.foundation.layout.RowScope.() -> Unit, ) { val hairline = peachColors.T3 Column( Modifier.fillMaxWidth().padding(vertical = 10.dp).drawBehind { drawRect( color = hairline, 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) } } /** * Text for the ghost PeachScript watermark behind the entry name: the full title, * falling back to "P" for a blank title so the hero never renders empty. */ internal fun ghostWatermarkText(title: String): String = title.ifBlank { "P" } 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 "" }