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.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.RoundedCornerShape import androidx.compose.foundation.verticalScroll import androidx.compose.material3.Text import androidx.compose.runtime.Composable import androidx.compose.runtime.DisposableEffect import androidx.compose.runtime.getValue import androidx.compose.runtime.produceState import androidx.compose.ui.Alignment import androidx.compose.ui.Modifier import androidx.compose.ui.draw.clip import androidx.compose.ui.platform.LocalContext import androidx.compose.ui.text.TextStyle import androidx.compose.ui.text.font.FontStyle import androidx.compose.ui.text.font.FontWeight import androidx.compose.ui.unit.dp 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.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 // 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 Row( Modifier.fillMaxWidth().height(56.dp).padding(horizontal = 12.dp), verticalAlignment = Alignment.CenterVertically, horizontalArrangement = Arrangement.SpaceBetween, ) { Text("←", color = PeachColors.T0, fontSize = 22.sp, modifier = Modifier.clip(RoundedCornerShape(50)).clickable(onClick = onBack).padding(10.dp)) if (e != null) { Text("edit", color = accent, fontFamily = PeachType.Mono, fontSize = 12.sp, modifier = Modifier.clip(RoundedCornerShape(8.dp)) .clickable { onNavigate(PeachNav.EntryEdit(e.id)) }.padding(10.dp)) } } if (e == null) { Box(Modifier.fillMaxSize(), contentAlignment = Alignment.Center) { Text("entry not found", color = PeachColors.T1, fontFamily = PeachType.Mono, fontSize = 12.sp) } return@Column } Column( Modifier.fillMaxSize().verticalScroll(rememberScrollState()) .padding(horizontal = 24.dp), ) { Spacer(Modifier.height(8.dp)) CharEnterText( text = e.title, style = TextStyle( fontFamily = PeachType.Display, fontStyle = FontStyle.Italic, fontWeight = FontWeight.Light, fontSize = 34.sp, color = PeachColors.T0, ), ) e.url?.takeIf { it.isNotBlank() }?.let { Spacer(Modifier.height(4.dp)) Text(it, color = PeachColors.T1, fontFamily = PeachType.Mono, fontSize = 11.sp) } Spacer(Modifier.height(24.dp)) e.username?.takeIf { it.isNotBlank() }?.let { FieldCard("username", it, copiedField == "username") { vm.copy("username", it) } } e.email?.takeIf { it.isNotBlank() }?.let { FieldCard("email", it, copiedField == "email") { vm.copy("email", it) } } e.password?.takeIf { it.isNotBlank() }?.let { pw -> PasswordCard( 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 { FieldCard("notes", it, copiedField == "notes") { vm.copy("notes", it) } } Spacer(Modifier.height(28.dp)) Text( "delete entry", color = PeachColors.Breach, fontFamily = PeachType.Mono, fontSize = 11.sp, modifier = Modifier.clip(RoundedCornerShape(8.dp)) .clickable { vm.delete(onDone = onBack) }.padding(12.dp), ) Spacer(Modifier.height(40.dp)) } } } } @Composable private fun FieldCard(label: String, value: String, copied: Boolean, onCopy: () -> Unit) { Card { Text(label.uppercase(), color = PeachColors.T1, fontFamily = PeachType.Mono, fontSize = 8.sp) Spacer(Modifier.height(6.dp)) Row(verticalAlignment = Alignment.CenterVertically) { Text(value, color = PeachColors.T0, fontFamily = PeachType.Mono, fontSize = 14.sp, modifier = Modifier.weight(1f)) CopyButton(copied, onCopy) } } } @Composable private fun PasswordCard( value: String, revealed: Boolean, copied: Boolean, onReveal: () -> Unit, onCopy: () -> Unit, ) { Card { Text("PASSWORD", color = PeachColors.T1, fontFamily = PeachType.Mono, fontSize = 8.sp) Spacer(Modifier.height(6.dp)) Row(verticalAlignment = Alignment.CenterVertically) { Text( text = if (revealed) value else "••••••••••••••", color = PeachColors.T0, fontFamily = PeachType.Mono, fontSize = 14.sp, modifier = Modifier.weight(1f), ) Text(if (revealed) "hide" else "show", color = LocalPeachAccent.current, fontFamily = PeachType.Mono, fontSize = 10.sp, modifier = Modifier.clip(RoundedCornerShape(6.dp)).clickable(onClick = onReveal).padding(8.dp)) CopyButton(copied, onCopy) } } } @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) } } Card { Text("ONE-TIME CODE", color = PeachColors.T1, fontFamily = PeachType.Mono, fontSize = 8.sp) Spacer(Modifier.height(6.dp)) Row(verticalAlignment = Alignment.CenterVertically) { val spaced = if (code.length == 6) "${code.substring(0, 3)} ${code.substring(3)}" else code Text(spaced, color = PeachColors.T0, fontFamily = PeachType.Mono, fontSize = 20.sp, modifier = Modifier.weight(1f)) TOTPRing(size = 26.dp) Spacer(Modifier.width(8.dp)) CopyButton(copied, onCopy) } } } @Composable private fun Card(content: @Composable androidx.compose.foundation.layout.ColumnScope.() -> Unit) { Column( Modifier.fillMaxWidth().padding(bottom = 12.dp) .clip(RoundedCornerShape(14.dp)) .background(PeachColors.S1) .padding(16.dp), content = content, ) } @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) } }