1package com.peachpasswords.ui.vault23import android.view.WindowManager4import androidx.compose.animation.core.LinearEasing5import androidx.compose.animation.core.RepeatMode6import androidx.compose.animation.core.animateFloat7import androidx.compose.animation.core.animateFloatAsState8import androidx.compose.animation.core.infiniteRepeatable9import androidx.compose.animation.core.rememberInfiniteTransition10import androidx.compose.animation.core.tween11import androidx.compose.foundation.background12import androidx.compose.foundation.border13import androidx.compose.foundation.clickable14import androidx.compose.foundation.layout.Arrangement15import androidx.compose.foundation.layout.Box16import androidx.compose.foundation.layout.Column17import androidx.compose.foundation.layout.Row18import androidx.compose.foundation.layout.Spacer19import androidx.compose.foundation.layout.fillMaxSize20import androidx.compose.foundation.layout.fillMaxWidth21import androidx.compose.foundation.layout.height22import androidx.compose.foundation.layout.offset23import androidx.compose.foundation.layout.padding24import androidx.compose.foundation.layout.size25import androidx.compose.foundation.layout.statusBarsPadding26import androidx.compose.foundation.layout.width27import androidx.compose.foundation.rememberScrollState28import androidx.compose.foundation.shape.CircleShape29import androidx.compose.foundation.shape.RoundedCornerShape30import androidx.compose.material.icons.rounded.Check31import androidx.compose.material.icons.rounded.ContentCopy32import androidx.compose.foundation.verticalScroll33import androidx.compose.material.icons.Icons34import androidx.compose.material.icons.automirrored.rounded.ArrowBack35import androidx.compose.material.icons.outlined.DeleteOutline36import androidx.compose.material3.Icon37import androidx.compose.material3.Text38import androidx.compose.runtime.Composable39import androidx.compose.runtime.DisposableEffect40import androidx.compose.runtime.getValue41import androidx.compose.runtime.mutableStateOf42import androidx.compose.runtime.produceState43import androidx.compose.runtime.remember44import androidx.compose.runtime.setValue45import androidx.compose.ui.Alignment46import androidx.compose.ui.Modifier47import androidx.compose.ui.draw.clip48import androidx.compose.ui.draw.drawBehind49import androidx.compose.ui.geometry.Offset50import androidx.compose.ui.geometry.Size51import androidx.compose.ui.graphics.Brush52import androidx.compose.ui.graphics.Color53import androidx.compose.ui.graphics.graphicsLayer54import androidx.compose.ui.platform.LocalContext55import androidx.compose.ui.text.TextStyle56import androidx.compose.ui.unit.dp57import androidx.compose.ui.unit.em58import androidx.compose.ui.unit.sp59import androidx.compose.runtime.collectAsState60import androidx.fragment.app.FragmentActivity61import androidx.hilt.navigation.compose.hiltViewModel62import com.peachpasswords.ui.PeachNav63import com.peachpasswords.ui.components.CharEnterText64import com.peachpasswords.ui.components.FaviconInitials65import com.peachpasswords.ui.components.TOTPRing66import com.peachpasswords.ui.lock.Biometrics67import com.peachpasswords.ui.theme.LocalPeachAccent68import com.peachpasswords.ui.theme.LocalReduceMotion69import com.peachpasswords.ui.theme.PeachColors70import com.peachpasswords.ui.theme.PeachType71import com.peachpasswords.ui.theme.ShellBackground72import com.peachpasswords.vault.TOTPGenerator73import com.peachpasswords.vault.hasTOTP74import com.peachpasswords.vault.isBreach75import com.peachpasswords.vault.isCard7677@Composable78fun EntryDetailScreen(79 id: String,80 onNavigate: (PeachNav) -> Unit,81 onBack: () -> Unit,82 vm: EntryDetailViewModel = hiltViewModel(),83) {84 val entry by vm.entry.collectAsState()85 val revealed by vm.revealed.collectAsState()86 val cvcRevealed by vm.cvcRevealed.collectAsState()87 var cardRevealed by remember { mutableStateOf(false) }88 val copiedField by vm.copiedField.collectAsState()89 val context = LocalContext.current90 val accent = LocalPeachAccent.current91 var confirmingDelete by remember { mutableStateOf(false) }9293 94 DisposableEffect(Unit) {95 val window = (context as? FragmentActivity)?.window96 window?.setFlags(WindowManager.LayoutParams.FLAG_SECURE, WindowManager.LayoutParams.FLAG_SECURE)97 onDispose { window?.clearFlags(WindowManager.LayoutParams.FLAG_SECURE) }98 }99100 val e = entry101 ShellBackground {102 Column(Modifier.fillMaxSize().statusBarsPadding()) {103 104 Row(105 Modifier.fillMaxWidth().height(56.dp).padding(horizontal = 8.dp),106 verticalAlignment = Alignment.CenterVertically,107 ) {108 Icon(109 Icons.AutoMirrored.Rounded.ArrowBack, contentDescription = "back", tint = PeachColors.T0,110 modifier = Modifier.size(40.dp).clip(CircleShape).clickable(onClick = onBack).padding(8.dp),111 )112 Box(Modifier.weight(1f), contentAlignment = Alignment.Center) {113 when {114 e == null -> {}115 confirmingDelete -> Text(116 "delete “${e.title}”?",117 color = PeachColors.Breach,118 style = PeachType.labelMono.copy(letterSpacing = 0.08.em),119 )120 else -> Row(121 verticalAlignment = Alignment.CenterVertically,122 horizontalArrangement = Arrangement.spacedBy(8.dp),123 ) {124 FaviconInitials(source = e.url ?: e.title, size = 16.dp)125 val domain = remember(e.url, e.title) { e.displayDomain() }126 if (domain.isNotEmpty()) {127 Text(domain, color = PeachColors.T2, style = TextStyle(fontFamily = PeachType.Mono, fontSize = 9.sp))128 }129 }130 }131 }132 if (e != null) {133 Icon(134 Icons.Outlined.DeleteOutline, contentDescription = "delete",135 tint = if (confirmingDelete) PeachColors.Breach else PeachColors.T1,136 modifier = Modifier.size(40.dp).clip(CircleShape)137 .clickable { if (confirmingDelete) vm.delete(onDone = onBack) else confirmingDelete = true }138 .padding(8.dp),139 )140 }141 }142143 if (e == null) {144 Box(Modifier.fillMaxSize(), contentAlignment = Alignment.Center) {145 Text("entry not found", color = PeachColors.T1, style = PeachType.bodyMono)146 }147 return@Column148 }149150 151 if (e.isBreach()) {152 Row(153 Modifier.fillMaxWidth()154 .background(PeachColors.BreachBg)155 .padding(horizontal = 16.dp, vertical = 8.dp),156 verticalAlignment = Alignment.CenterVertically,157 ) {158 Text("⚠", color = PeachColors.Breach, style = PeachType.bodyMono)159 Spacer(Modifier.width(10.dp))160 Text(161 "Password found in a data breach — change it now",162 color = PeachColors.Breach, style = PeachType.labelMono,163 modifier = Modifier.weight(1f),164 )165 Text(166 "Change password",167 color = accent, style = PeachType.labelMono,168 modifier = Modifier.clip(RoundedCornerShape(6.dp))169 .clickable { onNavigate(PeachNav.EntryEdit(e.id)) }170 .padding(horizontal = 6.dp, vertical = 4.dp),171 )172 }173 }174175 Column(176 Modifier.fillMaxSize().verticalScroll(rememberScrollState())177 .padding(horizontal = 24.dp),178 ) {179 180 Box(Modifier.fillMaxWidth()) {181 Text(182 text = e.title.firstOrNull()?.uppercase() ?: "P",183 style = TextStyle(fontFamily = PeachType.Script, fontSize = 140.sp),184 color = PeachColors.T0.copy(alpha = 0.03f),185 modifier = Modifier.align(Alignment.TopCenter).offset(y = (-16).dp),186 )187 Column(Modifier.fillMaxWidth().padding(top = 18.dp)) {188 CharEnterText(189 text = e.title,190 style = PeachType.entryNameHero.copy(color = PeachColors.T0),191 )192 Spacer(Modifier.height(8.dp))193 Text(194 text = "edit",195 color = accent,196 style = PeachType.labelMono,197 modifier = Modifier.clip(RoundedCornerShape(6.dp))198 .clickable { onNavigate(PeachNav.EntryEdit(e.id)) }199 .padding(vertical = 4.dp),200 )201 }202 }203 Spacer(Modifier.height(20.dp))204205 if (e.isCard()) {206 CardDetailFields(207 entry = e,208 cardRevealed = cardRevealed,209 cvcRevealed = cvcRevealed,210 copiedField = copiedField,211 onToggleCardReveal = { cardRevealed = !cardRevealed },212 onRevealCvc = {213 val activity = context as? FragmentActivity214 if (activity != null && Biometrics.isAvailable(context)) {215 Biometrics.authenticate(activity, "Unlock Peach", e.title, onSuccess = vm::revealCvc)216 } else vm.revealCvc()217 },218 onMaskCvc = vm::maskCvc,219 onCopy = vm::copy,220 copyButton = { copied, onCopy -> CopyButton(copied, onCopy) },221 )222 } else {223 e.username?.takeIf { it.isNotBlank() }?.let {224 FieldRow("username", it, copiedField == "username") { vm.copy("username", it) }225 }226 e.email?.takeIf { it.isNotBlank() }?.let {227 FieldRow("email", it, copiedField == "email") { vm.copy("email", it) }228 }229 e.password?.takeIf { it.isNotBlank() }?.let { pw ->230 PasswordRow(231 value = pw,232 revealed = revealed,233 copied = copiedField == "password",234 onReveal = {235 if (revealed) vm.mask()236 else {237 val activity = context as? FragmentActivity238 if (activity != null && Biometrics.isAvailable(context)) {239 Biometrics.authenticate(activity, "Unlock Peach", e.title, onSuccess = vm::reveal)240 } else vm.reveal()241 }242 },243 onCopy = { vm.copy("password", pw) },244 )245 }246 if (e.hasTOTP()) {247 Spacer(Modifier.height(4.dp))248 TotpBand(totpField = e.totp!!, copied = copiedField == "totp", onCopy = vm::copyTotp)249 }250 }251 e.notes?.takeIf { it.isNotBlank() }?.let {252 FieldRow("notes", it, copiedField == "notes", mono = false) { vm.copy("notes", it) }253 }254255 Spacer(Modifier.height(48.dp))256 }257 }258 }259}260261@Composable262private fun FieldRow(263 label: String,264 value: String,265 copied: Boolean,266 mono: Boolean = true,267 onCopy: () -> Unit,268) {269 FieldShell(label) {270 Text(271 value,272 color = PeachColors.T0,273 style = if (mono) PeachType.bodyMono else PeachType.body,274 modifier = Modifier.weight(1f),275 )276 CopyButton(copied, onCopy)277 }278}279280@Composable281private fun PasswordRow(282 value: String,283 revealed: Boolean,284 copied: Boolean,285 onReveal: () -> Unit,286 onCopy: () -> Unit,287) {288 val accent = LocalPeachAccent.current289 FieldShell("password") {290 Text(291 text = if (revealed) value else "•".repeat(value.length.coerceIn(8, 16)),292 color = PeachColors.T0,293 style = PeachType.bodyMono,294 modifier = Modifier.weight(1f),295 )296 Text(297 if (revealed) "hide" else "show", color = accent, style = PeachType.labelMono,298 modifier = Modifier.clip(RoundedCornerShape(6.dp)).clickable(onClick = onReveal).padding(8.dp),299 )300 CopyButton(copied, onCopy)301 }302}303304305@Composable306private fun FieldShell(307 label: String,308 content: @Composable androidx.compose.foundation.layout.RowScope.() -> Unit,309) {310 Column(311 Modifier.fillMaxWidth().padding(vertical = 10.dp).drawBehind {312 drawRect(313 color = PeachColors.T3,314 topLeft = Offset(0f, size.height - 1.dp.toPx()),315 size = Size(size.width, 1.dp.toPx()),316 )317 },318 ) {319 Text(label.uppercase(), color = PeachColors.T2, style = PeachType.labelMono.copy(letterSpacing = 0.08.em))320 Spacer(Modifier.height(6.dp))321 Row(verticalAlignment = Alignment.CenterVertically, content = content)322 }323}324325326@Composable327private fun TotpBand(totpField: String, copied: Boolean, onCopy: () -> Unit) {328 val accent = LocalPeachAccent.current329 val reduceMotion = LocalReduceMotion.current330 val code by produceState(TOTPGenerator.generateCode(totpField) ?: "------") {331 while (true) {332 value = TOTPGenerator.generateCode(totpField) ?: "------"333 kotlinx.coroutines.delay(1000)334 }335 }336 val remaining by produceState(TOTPGenerator.remainingSeconds()) {337 while (true) { value = TOTPGenerator.remainingSeconds(); kotlinx.coroutines.delay(500) }338 }339 val isHot = remaining <= 5340 341 val pulse = rememberInfiniteTransition(label = "totp-pulse")342 val pulseAlpha by pulse.animateFloat(343 initialValue = 0.55f, targetValue = 1f,344 animationSpec = infiniteRepeatable(tween(600), RepeatMode.Reverse), label = "totp-pulse-alpha",345 )346 val codeAlpha = if (isHot && !reduceMotion) pulseAlpha else 1f347348 Box(349 Modifier350 .fillMaxWidth()351 .padding(vertical = 10.dp)352 .clip(RoundedCornerShape(8.dp))353 .background(accent.copy(alpha = 0.08f))354 .border(1.dp, accent.copy(alpha = 0.20f), RoundedCornerShape(8.dp))355 .padding(horizontal = 14.dp, vertical = 10.dp),356 ) {357 Row(verticalAlignment = Alignment.CenterVertically) {358 Column(Modifier.weight(1f)) {359 Text(360 "ONE-TIME CODE",361 color = accent.copy(alpha = 0.70f),362 style = PeachType.labelMono.copy(letterSpacing = 0.08.em),363 )364 val spaced = if (code.length == 6) "${code.substring(0, 3)} ${code.substring(3)}" else code365 Text(366 spaced,367 color = if (isHot) PeachColors.Breach else accent,368 style = PeachType.totpCode,369 modifier = Modifier.graphicsLayer { alpha = codeAlpha },370 )371 }372 Text("${remaining}s", color = PeachColors.T2, style = PeachType.totpCountdown)373 Spacer(Modifier.width(12.dp))374 TOTPRing(size = 28.dp)375 Spacer(Modifier.width(8.dp))376 CopyButton(copied, onCopy)377 }378 }379}380381382383384385386387@Composable388private fun CopyButton(copied: Boolean, onCopy: () -> Unit) {389 val accent = LocalPeachAccent.current390 val reduceMotion = LocalReduceMotion.current391 val animate = copied && !reduceMotion392 val drain by animateFloatAsState(393 targetValue = if (copied) 1f else 0f,394 animationSpec = if (animate) tween(1400, easing = LinearEasing) else tween(0),395 label = "copy-drain",396 )397 val sheen by animateFloatAsState(398 targetValue = if (copied) 1f else 0f,399 animationSpec = if (animate) tween(600, easing = LinearEasing) else tween(0),400 label = "copy-sheen",401 )402 Box(403 Modifier404 .size(36.dp)405 .clip(RoundedCornerShape(6.dp))406 .background(PeachColors.S2)407 .clickable(onClick = onCopy)408 .drawBehind {409 if (animate && sheen < 1f) {410 val sweepX = (sheen * 2f - 0.5f) * size.width411 val band = size.width * 0.5f412 drawRect(413 brush = Brush.linearGradient(414 colors = listOf(Color.Transparent, accent.copy(alpha = 0.35f), Color.Transparent),415 start = Offset(sweepX - band, 0f),416 end = Offset(sweepX + band, size.height),417 ),418 )419 }420 if (drain > 0f) {421 val h = 2.dp.toPx()422 drawRect(423 color = accent,424 topLeft = Offset(0f, size.height - h),425 size = Size(size.width * drain, h),426 )427 }428 },429 contentAlignment = Alignment.Center,430 ) {431 Icon(432 if (copied) Icons.Rounded.Check else Icons.Rounded.ContentCopy,433 contentDescription = "copy",434 tint = if (copied) accent else PeachColors.T1,435 modifier = Modifier.size(15.dp),436 )437 }438}439440private fun com.peachpasswords.vault.VaultEntry.displayDomain(): String {441 val raw = url442 if (!raw.isNullOrBlank()) {443 val cleaned = raw.replace(Regex("^https?://"), "").removePrefix("www.").substringBefore('/')444 if (cleaned.isNotEmpty()) return cleaned445 }446 return ""447}