PasswordHistorySection.kt

6.4 KB · raw

1package com.peachpasswords.ui.vault23import androidx.compose.foundation.background4import androidx.compose.foundation.border5import androidx.compose.foundation.clickable6import androidx.compose.foundation.layout.Arrangement7import androidx.compose.foundation.layout.Box8import androidx.compose.foundation.layout.Column9import androidx.compose.foundation.layout.Row10import androidx.compose.foundation.layout.Spacer11import androidx.compose.foundation.layout.fillMaxWidth12import androidx.compose.foundation.layout.height13import androidx.compose.foundation.layout.padding14import androidx.compose.foundation.layout.width15import androidx.compose.foundation.shape.RoundedCornerShape16import androidx.compose.material3.Text17import androidx.compose.runtime.Composable18import androidx.compose.runtime.getValue19import androidx.compose.runtime.mutableStateOf20import androidx.compose.runtime.remember21import androidx.compose.runtime.setValue22import androidx.compose.ui.Alignment23import androidx.compose.ui.Modifier24import androidx.compose.ui.draw.clip25import androidx.compose.ui.text.font.FontStyle26import androidx.compose.ui.unit.dp27import androidx.compose.ui.unit.sp28import com.peachpasswords.ui.theme.LocalPeachAccent29import com.peachpasswords.ui.theme.PeachColors30import com.peachpasswords.ui.theme.PeachType31import com.peachpasswords.vault.PasswordHistoryItem3233/**34 * Shared past-password history UI, extracted from EntryEditScreen so both the35 * editor and EntryDetailScreen render the same rows. Behavior is unchanged from36 * the original private copy.37 *38 * - [PasswordHistorySection] is the original combined toggle + content block used39 *   by EntryEditScreen (toggle always present so the affordance is discoverable;40 *   empty state explains how it fills).41 * - [PasswordHistoryToggle] + [PasswordHistoryList] split the same pieces apart so42 *   EntryDetailScreen can drive expand/collapse with AnimatedVisibility itself.43 *44 * Rows are masked until revealed; "use" hands the value to [onRestore].45 */46@Composable47internal fun PasswordHistorySection(48    history: List<PasswordHistoryItem>,49    expanded: Boolean,50    onToggle: () -> Unit,51    onRestore: (String) -> Unit,52    onCopy: (String) -> Unit,53) {54    Column(Modifier.fillMaxWidth().padding(bottom = 14.dp)) {55        PasswordHistoryToggle(count = history.size, expanded = expanded, onToggle = onToggle)56        if (expanded) {57            Spacer(Modifier.height(8.dp))58            PasswordHistoryList(history = history, onRestore = onRestore, onCopy = onCopy)59        }60    }61}6263/**64 * The "↺ history (N)" affordance — Cormorant italic, accent-tinted. Shows a bare65 * "↺ history" when empty and "↺ hide history" when [expanded].66 */67@Composable68internal fun PasswordHistoryToggle(69    count: Int,70    expanded: Boolean,71    onToggle: () -> Unit,72) {73    val accent = LocalPeachAccent.current74    val label = if (count == 0) "↺ history" else "↺ history ($count)"75    Text(76        if (expanded) "↺ hide history" else label,77        color = accent.copy(alpha = 0.7f),78        fontFamily = PeachType.Display,79        fontStyle = FontStyle.Italic,80        fontSize = 15.sp,81        modifier = Modifier.clip(RoundedCornerShape(6.dp)).clickable(onClick = onToggle).padding(vertical = 4.dp),82    )83}8485/**86 * The expanded body: either the empty-state explainer or the list of past87 * passwords. Caller owns the expand/collapse (and any AnimatedVisibility wrap).88 */89@Composable90internal fun PasswordHistoryList(91    history: List<PasswordHistoryItem>,92    onRestore: (String) -> Unit,93    onCopy: (String) -> Unit,94) {95    val accent = LocalPeachAccent.current96    if (history.isEmpty()) {97        Box(98            Modifier.fillMaxWidth().clip(RoundedCornerShape(8.dp))99                .border(1.dp, accent.copy(alpha = 0.12f), RoundedCornerShape(8.dp))100                .background(accent.copy(alpha = 0.05f)).padding(10.dp),101        ) {102            Text(103                "No earlier passwords yet. When you change this password, the old one is saved here so you can copy or restore it.",104                color = PeachColors.T2, fontFamily = PeachType.Mono, fontSize = 10.sp,105            )106        }107    } else {108        Column(verticalArrangement = Arrangement.spacedBy(6.dp)) {109            history.forEach { item -> HistoryRow(item, onRestore, onCopy) }110        }111    }112}113114@Composable115private fun HistoryRow(116    item: PasswordHistoryItem,117    onRestore: (String) -> Unit,118    onCopy: (String) -> Unit,119) {120    val accent = LocalPeachAccent.current121    var revealed by remember { mutableStateOf(false) }122    Row(123        Modifier.fillMaxWidth().clip(RoundedCornerShape(8.dp))124            .border(1.dp, accent.copy(alpha = 0.12f), RoundedCornerShape(8.dp))125            .background(accent.copy(alpha = 0.06f)).padding(horizontal = 10.dp, vertical = 8.dp),126        verticalAlignment = Alignment.CenterVertically,127        horizontalArrangement = Arrangement.SpaceBetween,128    ) {129        Column(Modifier.weight(1f)) {130            Text(131                if (revealed) item.password else "••••••••••••",132                color = PeachColors.T0, fontFamily = PeachType.Mono, fontSize = 12.sp, maxLines = 1,133            )134            Text(135                timeSince(item.changedAt),136                color = accent.copy(alpha = 0.55f), fontFamily = PeachType.Mono, fontSize = 8.sp,137            )138        }139        Spacer(Modifier.width(6.dp))140        HistoryAction(if (revealed) "hide" else "show") { revealed = !revealed }141        HistoryAction("copy") { onCopy(item.password) }142        HistoryAction("use") { onRestore(item.password) }143    }144}145146@Composable147private fun HistoryAction(label: String, onClick: () -> Unit) {148    Text(149        label, color = LocalPeachAccent.current.copy(alpha = 0.7f),150        fontFamily = PeachType.Mono, fontSize = 9.sp,151        modifier = Modifier.clip(RoundedCornerShape(5.dp)).clickable(onClick = onClick).padding(horizontal = 5.dp, vertical = 3.dp),152    )153}154155/** Coarse "time since" label for the history list. Mirrors the browser helper. */156private fun timeSince(whenMs: Long): String {157    val secs = ((System.currentTimeMillis() - whenMs) / 1000).coerceAtLeast(0)158    if (secs < 60) return "just now"159    val mins = secs / 60160    if (mins < 60) return "${mins}m ago"161    val hours = mins / 60162    if (hours < 24) return "${hours}h ago"163    val days = hours / 24164    if (days < 30) return "${days}d ago"165    val months = days / 30166    if (months < 12) return "${months}mo ago"167    return "${months / 12}y ago"168}