LockScreen.kt

11.1 KB · raw

1package com.peachpasswords.ui.lock23import androidx.compose.animation.core.Animatable4import androidx.compose.animation.core.keyframes5import androidx.compose.foundation.background6import androidx.compose.foundation.border7import androidx.compose.foundation.clickable8import androidx.compose.foundation.interaction.MutableInteractionSource9import androidx.compose.foundation.interaction.collectIsFocusedAsState10import androidx.compose.foundation.layout.Arrangement11import androidx.compose.foundation.layout.Box12import androidx.compose.foundation.layout.Column13import androidx.compose.foundation.layout.Row14import androidx.compose.foundation.layout.Spacer15import androidx.compose.foundation.layout.fillMaxSize16import androidx.compose.foundation.layout.offset17import androidx.compose.foundation.layout.fillMaxWidth18import androidx.compose.foundation.layout.height19import androidx.compose.foundation.layout.padding20import androidx.compose.foundation.layout.systemBarsPadding21import androidx.compose.foundation.shape.RoundedCornerShape22import androidx.compose.foundation.text.BasicTextField23import androidx.compose.foundation.text.KeyboardActions24import androidx.compose.foundation.text.KeyboardOptions25import androidx.compose.material3.Text26import androidx.compose.runtime.Composable27import androidx.compose.runtime.LaunchedEffect28import androidx.compose.runtime.getValue29import androidx.compose.runtime.mutableStateOf30import androidx.compose.runtime.remember31import androidx.compose.runtime.setValue32import androidx.compose.ui.Alignment33import androidx.compose.ui.Modifier34import androidx.compose.ui.draw.alpha35import androidx.compose.ui.draw.clip36import androidx.compose.ui.draw.drawBehind37import androidx.compose.ui.geometry.Offset38import androidx.compose.ui.geometry.Size39import androidx.compose.ui.graphics.Color40import androidx.compose.ui.graphics.SolidColor41import androidx.compose.ui.text.TextStyle42import androidx.compose.ui.text.input.ImeAction43import androidx.compose.ui.text.input.KeyboardType44import androidx.compose.ui.text.input.PasswordVisualTransformation45import androidx.compose.ui.text.style.TextAlign46import androidx.compose.ui.unit.IntOffset47import androidx.compose.ui.unit.dp48import androidx.compose.ui.unit.em49import androidx.compose.ui.unit.sp50import androidx.hilt.navigation.compose.hiltViewModel51import androidx.compose.runtime.collectAsState52import com.peachpasswords.ui.PeachNav53import com.peachpasswords.ui.components.LockRuneString54import com.peachpasswords.ui.components.PeachMark55import com.peachpasswords.ui.theme.LocalPeachAccent56import com.peachpasswords.ui.theme.PeachColors57import com.peachpasswords.ui.theme.PeachType58import com.peachpasswords.ui.theme.ShellBackground5960@Composable61fun LockScreen(62    onUnlocked: () -> Unit,63    onNavigate: (PeachNav) -> Unit,64    vm: LockViewModel = hiltViewModel(),65) {66    val state by vm.state.collectAsState()67    val accent = LocalPeachAccent.current6869    var password by remember { mutableStateOf("") }70    var confirm by remember { mutableStateOf("") }7172    LaunchedEffect(state.unlocked) { if (state.unlocked) onUnlocked() }7374    // Shake the panel on each failed unlock (brief Phase 4: 0→−12→10→−8→6→0 over ~400ms).75    val shake = remember { Animatable(0f) }76    LaunchedEffect(state.shakeNonce) {77        if (state.shakeNonce > 0) {78            shake.animateTo(79                targetValue = 0f,80                animationSpec = keyframes {81                    durationMillis = 40082                    -12f at 60; 10f at 140; -8f at 230; 6f at 320; 0f at 40083                },84            )85        }86    }8788    ShellBackground {89        // Wordmark, top-left.90        Text(91            text = "peach",92            color = accent,93            style = PeachType.wordmark,94            modifier = Modifier.align(Alignment.TopStart).systemBarsPadding().padding(start = 20.dp, top = 8.dp),95        )9697        Column(98            modifier = Modifier99                .fillMaxSize()100                .systemBarsPadding()101                .padding(horizontal = 28.dp),102            verticalArrangement = Arrangement.Center,103            horizontalAlignment = Alignment.CenterHorizontally,104        ) {105            // Hero: centered PeachMark · eyebrow · headline.106            PeachMark(size = 80.dp)107            Spacer(Modifier.height(18.dp))108            Text(109                text = if (state.hasVault) "VAULT LOCKED" else "NEW VAULT",110                color = PeachColors.T2,111                style = PeachType.lockEyebrow,112            )113            Spacer(Modifier.height(10.dp))114            Text(115                text = if (state.hasVault) "unlock\nyour vault." else "keep your\nsecrets.",116                color = PeachColors.T0,117                style = if (state.hasVault) PeachType.lockHeadline else PeachType.createHeadline,118                textAlign = TextAlign.Center,119            )120            Spacer(Modifier.height(14.dp))121            LockRuneString()122            Spacer(Modifier.height(24.dp))123124            // Glass panel holding the credential inputs.125            GlassPanel(126                modifier = Modifier.offset { IntOffset(shake.value.dp.roundToPx(), 0) },127            ) {128                FieldLabel(if (state.hasVault) "master password" else "create a master password")129                Spacer(Modifier.height(4.dp))130                UnderlineField(131                    value = password,132                    onChange = { password = it; vm.clearError() },133                    placeholder = "••••••••••",134                    textStyle = if (state.hasVault) PeachType.unlockInput else mono18Spaced(),135                    imeAction = if (state.hasVault) ImeAction.Go else ImeAction.Next,136                    onImeAction = { if (state.hasVault) vm.unlock(password) },137                )138139                if (!state.hasVault) {140                    Spacer(Modifier.height(16.dp))141                    FieldLabel("confirm password")142                    Spacer(Modifier.height(4.dp))143                    UnderlineField(144                        value = confirm,145                        onChange = { confirm = it; vm.clearError() },146                        placeholder = "••••••••••",147                        textStyle = mono18Spaced(),148                        imeAction = ImeAction.Go,149                        onImeAction = { vm.create(password, confirm) },150                    )151                }152153                Text(154                    text = state.error ?: " ",155                    color = PeachColors.Breach,156                    style = PeachType.labelMono.copy(fontSize = 11.sp),157                    textAlign = TextAlign.Center,158                    modifier = Modifier.fillMaxWidth().padding(top = 12.dp).height(18.dp),159                )160161                Spacer(Modifier.height(4.dp))162                AccentButton(163                    label = if (state.hasVault) "unlock" else "create vault",164                    enabled = !state.loading,165                    onClick = { if (state.hasVault) vm.unlock(password) else vm.create(password, confirm) },166                )167            }168169            if (!state.hasVault) {170                Spacer(Modifier.height(24.dp))171                RestoreTabs(onNavigate = onNavigate)172            }173        }174    }175}176177@Composable178private fun FieldLabel(text: String) {179    Text(text.uppercase(), color = PeachColors.T2, style = PeachType.labelMono.copy(letterSpacing = 0.08.em))180}181182/** IBM Plex Mono 18sp, wide tracking — the create-vault input style (spec §5.2). */183private fun mono18Spaced() = TextStyle(fontFamily = PeachType.Mono, fontSize = 18.sp, letterSpacing = 0.18.em)184185@Composable186private fun GlassPanel(modifier: Modifier = Modifier, content: @Composable androidx.compose.foundation.layout.ColumnScope.() -> Unit) {187    Column(188        modifier189            .fillMaxWidth()190            .clip(RoundedCornerShape(18.dp))191            .background(Color(0xE0131109))192            .border(1.dp, PeachColors.T3, RoundedCornerShape(18.dp))193            .padding(20.dp),194        content = content,195    )196}197198@Composable199private fun UnderlineField(200    value: String,201    onChange: (String) -> Unit,202    placeholder: String,203    textStyle: TextStyle,204    imeAction: ImeAction,205    onImeAction: () -> Unit,206) {207    val accent = LocalPeachAccent.current208    val interaction = remember { MutableInteractionSource() }209    val focused by interaction.collectIsFocusedAsState()210    BasicTextField(211        value = value,212        onValueChange = onChange,213        singleLine = true,214        textStyle = textStyle.copy(color = PeachColors.T0, textAlign = TextAlign.Center),215        cursorBrush = SolidColor(accent),216        visualTransformation = PasswordVisualTransformation(),217        keyboardOptions = KeyboardOptions(keyboardType = KeyboardType.Password, imeAction = imeAction),218        keyboardActions = KeyboardActions(onGo = { onImeAction() }, onNext = { onImeAction() }),219        interactionSource = interaction,220        modifier = Modifier221            .fillMaxWidth()222            .drawBehind {223                drawRect(224                    color = if (focused) accent else PeachColors.T3,225                    topLeft = Offset(0f, size.height - 1.dp.toPx()),226                    size = Size(size.width, 1.dp.toPx()),227                )228            }229            .padding(vertical = 8.dp),230        decorationBox = { inner ->231            Box(contentAlignment = Alignment.Center) {232                if (value.isEmpty()) {233                    Text(placeholder, style = textStyle.copy(color = PeachColors.T2, textAlign = TextAlign.Center))234                }235                inner()236            }237        },238    )239}240241@Composable242private fun AccentButton(label: String, enabled: Boolean, onClick: () -> Unit) {243    val accent = LocalPeachAccent.current244    Box(245        modifier = Modifier246            .fillMaxWidth()247            .alpha(if (enabled) 1f else 0.5f)248            .clip(RoundedCornerShape(10.dp))249            .background(accent)250            .clickable(enabled = enabled, onClick = onClick)251            .padding(vertical = 13.dp),252        contentAlignment = Alignment.Center,253    ) {254        Text(255            label,256            color = Color.White,257            style = TextStyle(fontFamily = PeachType.Body, fontSize = 13.sp, letterSpacing = 0.06.em),258        )259    }260}261262@Composable263private fun RestoreTabs(onNavigate: (PeachNav) -> Unit) {264    val tabs = listOf(265        "PeachScript" to PeachNav.Import,266        "CSV" to PeachNav.Import,267        "From Phone" to PeachNav.ChirpPair,268        "Codex" to PeachNav.CodexRestore,269    )270    Column(horizontalAlignment = Alignment.CenterHorizontally) {271        Text("restore instead", color = PeachColors.T1, style = PeachType.labelMono)272        Spacer(Modifier.height(10.dp))273        Row(horizontalArrangement = Arrangement.spacedBy(6.dp)) {274            tabs.forEach { (label, dest) ->275                Box(276                    modifier = Modifier277                        .border(1.dp, PeachColors.T3, RoundedCornerShape(8.dp))278                        .clickable { onNavigate(dest) }279                        .padding(horizontal = 10.dp, vertical = 8.dp),280                ) {281                    Text(label, color = PeachColors.T1, style = PeachType.sortPill)282                }283            }284        }285    }286}