EntryDetailScreen.kt

16.7 KB · raw

1package com.peachpasswords.ui.vault23import android.view.WindowManager4import androidx.compose.animation.AnimatedVisibility5import androidx.compose.animation.core.LinearEasing6import androidx.compose.animation.core.RepeatMode7import androidx.compose.animation.core.animateFloat8import androidx.compose.animation.core.animateFloatAsState9import androidx.compose.animation.core.infiniteRepeatable10import androidx.compose.animation.core.rememberInfiniteTransition11import androidx.compose.animation.core.tween12import androidx.compose.foundation.background13import androidx.compose.foundation.border14import androidx.compose.foundation.clickable15import androidx.compose.foundation.layout.Arrangement16import androidx.compose.foundation.layout.Box17import androidx.compose.foundation.layout.Column18import androidx.compose.foundation.layout.Row19import androidx.compose.foundation.layout.Spacer20import androidx.compose.foundation.layout.fillMaxSize21import androidx.compose.foundation.layout.fillMaxWidth22import androidx.compose.foundation.layout.height23import androidx.compose.foundation.layout.offset24import androidx.compose.foundation.layout.padding25import androidx.compose.foundation.layout.size26import androidx.compose.foundation.layout.statusBarsPadding27import androidx.compose.foundation.layout.width28import androidx.compose.foundation.rememberScrollState29import androidx.compose.foundation.shape.CircleShape30import androidx.compose.foundation.shape.RoundedCornerShape31import androidx.compose.material.icons.rounded.Check32import androidx.compose.material.icons.rounded.ContentCopy33import androidx.compose.foundation.verticalScroll34import androidx.compose.material.icons.Icons35import androidx.compose.material.icons.automirrored.rounded.ArrowBack36import androidx.compose.material.icons.outlined.DeleteOutline37import androidx.compose.material3.Icon38import androidx.compose.material3.Text39import androidx.compose.runtime.Composable40import androidx.compose.runtime.DisposableEffect41import androidx.compose.runtime.getValue42import androidx.compose.runtime.mutableStateOf43import androidx.compose.runtime.produceState44import androidx.compose.runtime.remember45import androidx.compose.runtime.setValue46import androidx.compose.ui.Alignment47import androidx.compose.ui.Modifier48import androidx.compose.ui.draw.clip49import androidx.compose.ui.draw.clipToBounds50import androidx.compose.ui.draw.drawBehind51import androidx.compose.ui.geometry.Offset52import androidx.compose.ui.geometry.Size53import androidx.compose.ui.graphics.Brush54import androidx.compose.ui.graphics.Color55import androidx.compose.ui.graphics.graphicsLayer56import androidx.compose.ui.platform.LocalContext57import androidx.compose.ui.text.TextStyle58import androidx.compose.ui.text.style.TextOverflow59import androidx.compose.ui.unit.dp60import androidx.compose.ui.unit.em61import androidx.compose.ui.unit.sp62import androidx.compose.runtime.collectAsState63import androidx.fragment.app.FragmentActivity64import androidx.hilt.navigation.compose.hiltViewModel65import com.peachpasswords.ui.PeachNav66import com.peachpasswords.ui.components.CharEnterText67import com.peachpasswords.ui.components.CopyButton68import com.peachpasswords.ui.components.FaviconInitials69import com.peachpasswords.ui.components.TOTPRing70import com.peachpasswords.ui.lock.Biometrics71import com.peachpasswords.ui.theme.LocalPeachAccent72import com.peachpasswords.ui.theme.LocalReduceMotion73import com.peachpasswords.ui.theme.peachColors74import com.peachpasswords.ui.theme.PeachType75import com.peachpasswords.ui.theme.ShellBackground76import com.peachpasswords.vault.TOTPGenerator77import com.peachpasswords.vault.hasTOTP78import com.peachpasswords.vault.isBreach79import com.peachpasswords.vault.isCard8081@Composable82fun EntryDetailScreen(83    id: String,84    onNavigate: (PeachNav) -> Unit,85    onBack: () -> Unit,86    vm: EntryDetailViewModel = hiltViewModel(),87) {88    val entry by vm.entry.collectAsState()89    val identitySuggestions by vm.identitySuggestions.collectAsState()90    val cvcRevealed by vm.cvcRevealed.collectAsState()91    var cardRevealed by remember { mutableStateOf(false) }92    val copiedField by vm.copiedField.collectAsState()93    val context = LocalContext.current94    val accent = LocalPeachAccent.current95    var confirmingDelete by remember { mutableStateOf(false) }96    var generatorOpen by remember { mutableStateOf(false) }97    var historyExpanded by remember { mutableStateOf(false) }9899    // FLAG_SECURE only while this screen is shown — blocks screenshots of secrets.100    DisposableEffect(Unit) {101        val window = (context as? FragmentActivity)?.window102        window?.setFlags(WindowManager.LayoutParams.FLAG_SECURE, WindowManager.LayoutParams.FLAG_SECURE)103        onDispose { window?.clearFlags(WindowManager.LayoutParams.FLAG_SECURE) }104    }105106    val e = entry107    ShellBackground {108        Column(Modifier.fillMaxSize().statusBarsPadding()) {109            // Header: back · favicon+domain (or delete confirm) · trash110            Row(111                Modifier.fillMaxWidth().height(56.dp).padding(horizontal = 8.dp),112                verticalAlignment = Alignment.CenterVertically,113            ) {114                Icon(115                    Icons.AutoMirrored.Rounded.ArrowBack, contentDescription = "back", tint = peachColors.T0,116                    modifier = Modifier.size(40.dp).clip(CircleShape).clickable(onClick = onBack).padding(8.dp),117                )118                Box(Modifier.weight(1f), contentAlignment = Alignment.Center) {119                    when {120                        e == null -> {}121                        confirmingDelete -> Text(122                            "delete “${e.title}”?",123                            color = peachColors.Breach,124                            style = PeachType.labelMono.copy(letterSpacing = 0.08.em),125                        )126                        else -> Row(127                            verticalAlignment = Alignment.CenterVertically,128                            horizontalArrangement = Arrangement.spacedBy(8.dp),129                        ) {130                            FaviconInitials(source = e.url ?: e.title, size = 16.dp)131                            val domain = remember(e.url, e.title) { e.displayDomain() }132                            if (domain.isNotEmpty()) {133                                Text(domain, color = peachColors.T2, style = TextStyle(fontFamily = PeachType.Mono, fontSize = 9.sp))134                            }135                        }136                    }137                }138                if (e != null) {139                    Icon(140                        Icons.Outlined.DeleteOutline, contentDescription = "delete",141                        tint = if (confirmingDelete) peachColors.Breach else peachColors.T1,142                        modifier = Modifier.size(40.dp).clip(CircleShape)143                            .clickable { if (confirmingDelete) vm.delete(onDone = onBack) else confirmingDelete = true }144                            .padding(8.dp),145                    )146                }147            }148149            if (e == null) {150                Box(Modifier.fillMaxSize(), contentAlignment = Alignment.Center) {151                    Text("entry not found", color = peachColors.T1, style = PeachType.bodyMono)152                }153                return@Column154            }155156            // D11 — breach banner strip when this entry's password is known-compromised.157            if (e.isBreach()) {158                Row(159                    Modifier.fillMaxWidth()160                        .background(peachColors.BreachBg)161                        .padding(horizontal = 16.dp, vertical = 8.dp),162                    verticalAlignment = Alignment.CenterVertically,163                ) {164                    Text("⚠", color = peachColors.Breach, style = PeachType.bodyMono)165                    Spacer(Modifier.width(10.dp))166                    Text(167                        "Password found in a data breach — change it now",168                        color = peachColors.Breach, style = PeachType.labelMono,169                        modifier = Modifier.weight(1f),170                    )171                    Text(172                        "Change password",173                        color = accent, style = PeachType.labelMono,174                        modifier = Modifier.clip(RoundedCornerShape(6.dp))175                            .clickable { onNavigate(PeachNav.EntryEdit(e.id)) }176                            .padding(horizontal = 6.dp, vertical = 4.dp),177                    )178                }179            }180181            Column(182                Modifier.fillMaxSize().verticalScroll(rememberScrollState())183                    .padding(horizontal = 24.dp),184            ) {185                // Hero — ghost PeachScript watermark behind the entry name. The full186                // title is drawn (not just its first letter); a long title overflows187                // and clips naturally, which is the intended watermark behavior.188                Box(Modifier.fillMaxWidth().clipToBounds()) {189                    Text(190                        text = ghostWatermarkText(e.title),191                        style = TextStyle(fontFamily = PeachType.Script, fontSize = 140.sp),192                        color = peachColors.T0.copy(alpha = 0.03f),193                        maxLines = 1,194                        softWrap = false,195                        overflow = TextOverflow.Clip,196                        modifier = Modifier.align(Alignment.TopCenter).offset(y = (-16).dp),197                    )198                    Column(Modifier.fillMaxWidth().padding(top = 18.dp)) {199                        CharEnterText(200                            text = e.title,201                            style = PeachType.entryNameHero.copy(color = peachColors.T0),202                        )203                        Spacer(Modifier.height(8.dp))204                        Text(205                            text = "edit",206                            color = accent,207                            style = PeachType.labelMono,208                            modifier = Modifier.clip(RoundedCornerShape(6.dp))209                                .clickable { onNavigate(PeachNav.EntryEdit(e.id)) }210                                .padding(vertical = 4.dp),211                        )212                    }213                }214                Spacer(Modifier.height(20.dp))215216                if (e.isCard()) {217                    CardDetailFields(218                        entry = e,219                        cardRevealed = cardRevealed,220                        cvcRevealed = cvcRevealed,221                        copiedField = copiedField,222                        onToggleCardReveal = { cardRevealed = !cardRevealed },223                        onRevealCvc = {224                            val activity = context as? FragmentActivity225                            if (activity != null && Biometrics.isAvailable(context)) {226                                Biometrics.authenticate(activity, "Unlock Peach", e.title, onSuccess = vm::revealCvc)227                            } else vm.revealCvc()228                        },229                        onMaskCvc = vm::maskCvc,230                        onCopy = vm::copy,231                        copyButton = { copied, onCopy -> CopyButton(copied, onCopy) },232                    )233                } else {234                    e.username?.takeIf { it.isNotBlank() }?.let { v ->235                        EditableFieldRow(236                            label = "username", field = "username", value = v,237                            copied = copiedField == "username",238                            onCopy = { vm.copy("username", v) },239                            onSave = { vm.updateField("username", it) },240                            suggestions = identitySuggestions,241                        )242                    }243                    e.email?.takeIf { it.isNotBlank() }?.let { v ->244                        EditableFieldRow(245                            label = "email", field = "email", value = v,246                            copied = copiedField == "email",247                            onCopy = { vm.copy("email", v) },248                            onSave = { vm.updateField("email", it) },249                            suggestions = identitySuggestions,250                        )251                    }252                    e.additionalUrls?.takeIf { it.any { u -> u.isNotBlank() } }?.let { urls ->253                        AdditionalUrlRows(254                            urls = urls,255                            copiedField = copiedField,256                            onCopy = { key, value -> vm.copy(key, value) },257                        )258                    }259                    e.password?.takeIf { it.isNotBlank() }?.let { pw ->260                        EditablePasswordRow(261                            value = pw,262                            copied = copiedField == "password",263                            entryTitle = e.title,264                            onCopy = { vm.copy("password", pw) },265                            onSave = { vm.updateField("password", it) },266                            onGenerate = { generatorOpen = true },267                        )268                    }269                    val history = e.passwordHistory.orEmpty()270                    if (history.isNotEmpty()) {271                        PasswordHistoryToggle(272                            count = history.size,273                            expanded = historyExpanded,274                            onToggle = { historyExpanded = !historyExpanded },275                        )276                        AnimatedVisibility(visible = historyExpanded) {277                            Column(Modifier.padding(top = 8.dp, bottom = 6.dp)) {278                                PasswordHistoryList(279                                    history = history,280                                    onRestore = { oldPw -> vm.restorePassword(e.id, oldPw) },281                                    onCopy = { oldPw -> vm.copy("password", oldPw) },282                                )283                            }284                        }285                    }286                    if (e.hasTOTP()) {287                        Spacer(Modifier.height(4.dp))288                        TotpBandEditable(289                            totpField = e.totp!!,290                            copied = copiedField == "totp",291                            onCopy = vm::copyTotp,292                            onSaveSecret = { newSecret -> vm.updateField("totp", newSecret) },293                        )294                    }295                }296                e.notes?.takeIf { it.isNotBlank() }?.let { v ->297                    EditableFieldRow(298                        label = "notes", field = "notes", value = v,299                        copied = copiedField == "notes",300                        onCopy = { vm.copy("notes", v) },301                        onSave = { vm.updateField("notes", it) },302                        mono = false, singleLine = false,303                    )304                }305                e.tags?.takeIf { it.isNotEmpty() }?.let { tags ->306                    DetailTagRow(307                        tags = tags,308                        onEditTags = { onNavigate(PeachNav.EntryEdit(e.id)) },309                    )310                }311312                Spacer(Modifier.height(48.dp))313            }314315            if (generatorOpen) {316                GeneratorSheet(317                    copied = copiedField == "genDraft",318                    onCopy = { draft -> vm.copy("genDraft", draft) },319                    onUse = { draft -> vm.applyGeneratedPassword(draft) { generatorOpen = false } },320                    onDismiss = { generatorOpen = false },321                )322            }323        }324    }325}326327/** A field row: uppercase mono label over a value/action row, with a t3 hairline beneath (spec §5.4). */328@Composable329internal fun FieldShell(330    label: String,331    content: @Composable androidx.compose.foundation.layout.RowScope.() -> Unit,332) {333    val hairline = peachColors.T3334    Column(335        Modifier.fillMaxWidth().padding(vertical = 10.dp).drawBehind {336            drawRect(337                color = hairline,338                topLeft = Offset(0f, size.height - 1.dp.toPx()),339                size = Size(size.width, 1.dp.toPx()),340            )341        },342    ) {343        Text(label.uppercase(), color = peachColors.T2, style = PeachType.labelMono.copy(letterSpacing = 0.08.em))344        Spacer(Modifier.height(6.dp))345        Row(verticalAlignment = Alignment.CenterVertically, content = content)346    }347}348349/**350 * Text for the ghost PeachScript watermark behind the entry name: the full title,351 * falling back to "P" for a blank title so the hero never renders empty.352 */353internal fun ghostWatermarkText(title: String): String = title.ifBlank { "P" }354355private fun com.peachpasswords.vault.VaultEntry.displayDomain(): String {356    val raw = url357    if (!raw.isNullOrBlank()) {358        val cleaned = raw.replace(Regex("^https?://"), "").removePrefix("www.").substringBefore('/')359        if (cleaned.isNotEmpty()) return cleaned360    }361    return ""362}