LockScreen.kt

11.2 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) {73        if (state.unlocked) {74            // A freshly created vault routes through recovery-word setup before the list.75            if (state.created) onNavigate(PeachNav.RecoverySetup) else onUnlocked()76        }77    }7879    // Shake the panel on each failed unlock (brief Phase 4: 0→−12→10→−8→6→0 over ~400ms).80    val shake = remember { Animatable(0f) }81    LaunchedEffect(state.shakeNonce) {82        if (state.shakeNonce > 0) {83            shake.animateTo(84                targetValue = 0f,85                animationSpec = keyframes {86                    durationMillis = 40087                    -12f at 60; 10f at 140; -8f at 230; 6f at 320; 0f at 40088                },89            )90        }91    }9293    ShellBackground {94        // Wordmark, top-left.95        Text(96            text = "peach",97            color = accent,98            style = PeachType.wordmark,99            modifier = Modifier.align(Alignment.TopStart).systemBarsPadding().padding(start = 20.dp, top = 8.dp),100        )101102        Column(103            modifier = Modifier104                .fillMaxSize()105                .systemBarsPadding()106                .padding(horizontal = 28.dp),107            verticalArrangement = Arrangement.Center,108            horizontalAlignment = Alignment.CenterHorizontally,109        ) {110            // Hero: centered PeachMark · eyebrow · headline.111            PeachMark(size = 80.dp)112            Spacer(Modifier.height(18.dp))113            Text(114                text = if (state.hasVault) "VAULT LOCKED" else "NEW VAULT",115                color = PeachColors.T2,116                style = PeachType.lockEyebrow,117            )118            Spacer(Modifier.height(10.dp))119            Text(120                text = if (state.hasVault) "unlock\nyour vault." else "keep your\nsecrets.",121                color = PeachColors.T0,122                style = if (state.hasVault) PeachType.lockHeadline else PeachType.createHeadline,123                textAlign = TextAlign.Center,124            )125            Spacer(Modifier.height(14.dp))126            LockRuneString()127            Spacer(Modifier.height(24.dp))128129            // Glass panel holding the credential inputs.130            GlassPanel(131                modifier = Modifier.offset { IntOffset(shake.value.dp.roundToPx(), 0) },132            ) {133                FieldLabel(if (state.hasVault) "master password" else "create a master password")134                Spacer(Modifier.height(4.dp))135                UnderlineField(136                    value = password,137                    onChange = { password = it; vm.clearError() },138                    placeholder = "••••••••••",139                    textStyle = if (state.hasVault) PeachType.unlockInput else mono18Spaced(),140                    imeAction = if (state.hasVault) ImeAction.Go else ImeAction.Next,141                    onImeAction = { if (state.hasVault) vm.unlock(password) },142                )143144                if (!state.hasVault) {145                    Spacer(Modifier.height(16.dp))146                    FieldLabel("confirm password")147                    Spacer(Modifier.height(4.dp))148                    UnderlineField(149                        value = confirm,150                        onChange = { confirm = it; vm.clearError() },151                        placeholder = "••••••••••",152                        textStyle = mono18Spaced(),153                        imeAction = ImeAction.Go,154                        onImeAction = { vm.create(password, confirm) },155                    )156                }157158                Text(159                    text = state.error ?: " ",160                    color = PeachColors.Breach,161                    style = PeachType.labelMono.copy(fontSize = 11.sp),162                    textAlign = TextAlign.Center,163                    modifier = Modifier.fillMaxWidth().padding(top = 12.dp).height(18.dp),164                )165166                Spacer(Modifier.height(4.dp))167                AccentButton(168                    label = if (state.hasVault) "unlock" else "create vault",169                    enabled = !state.loading,170                    onClick = { if (state.hasVault) vm.unlock(password) else vm.create(password, confirm) },171                )172            }173174            if (!state.hasVault) {175                Spacer(Modifier.height(24.dp))176                RestoreTabs(onNavigate = onNavigate)177            }178        }179    }180}181182@Composable183private fun FieldLabel(text: String) {184    Text(text.uppercase(), color = PeachColors.T2, style = PeachType.labelMono.copy(letterSpacing = 0.08.em))185}186187/** IBM Plex Mono 18sp, wide tracking — the create-vault input style (spec §5.2). */188private fun mono18Spaced() = TextStyle(fontFamily = PeachType.Mono, fontSize = 18.sp, letterSpacing = 0.18.em)189190@Composable191private fun GlassPanel(modifier: Modifier = Modifier, content: @Composable androidx.compose.foundation.layout.ColumnScope.() -> Unit) {192    Column(193        modifier194            .fillMaxWidth()195            .clip(RoundedCornerShape(18.dp))196            .background(Color(0xE0131109))197            .border(1.dp, PeachColors.T3, RoundedCornerShape(18.dp))198            .padding(20.dp),199        content = content,200    )201}202203@Composable204private fun UnderlineField(205    value: String,206    onChange: (String) -> Unit,207    placeholder: String,208    textStyle: TextStyle,209    imeAction: ImeAction,210    onImeAction: () -> Unit,211) {212    val accent = LocalPeachAccent.current213    val interaction = remember { MutableInteractionSource() }214    val focused by interaction.collectIsFocusedAsState()215    BasicTextField(216        value = value,217        onValueChange = onChange,218        singleLine = true,219        textStyle = textStyle.copy(color = PeachColors.T0, textAlign = TextAlign.Center),220        cursorBrush = SolidColor(accent),221        visualTransformation = PasswordVisualTransformation(),222        keyboardOptions = KeyboardOptions(keyboardType = KeyboardType.Password, imeAction = imeAction),223        keyboardActions = KeyboardActions(onGo = { onImeAction() }, onNext = { onImeAction() }),224        interactionSource = interaction,225        modifier = Modifier226            .fillMaxWidth()227            .drawBehind {228                drawRect(229                    color = if (focused) accent else PeachColors.T3,230                    topLeft = Offset(0f, size.height - 1.dp.toPx()),231                    size = Size(size.width, 1.dp.toPx()),232                )233            }234            .padding(vertical = 8.dp),235        decorationBox = { inner ->236            Box(contentAlignment = Alignment.Center) {237                if (value.isEmpty()) {238                    Text(placeholder, style = textStyle.copy(color = PeachColors.T2, textAlign = TextAlign.Center))239                }240                inner()241            }242        },243    )244}245246@Composable247private fun AccentButton(label: String, enabled: Boolean, onClick: () -> Unit) {248    val accent = LocalPeachAccent.current249    Box(250        modifier = Modifier251            .fillMaxWidth()252            .alpha(if (enabled) 1f else 0.5f)253            .clip(RoundedCornerShape(10.dp))254            .background(accent)255            .clickable(enabled = enabled, onClick = onClick)256            .padding(vertical = 13.dp),257        contentAlignment = Alignment.Center,258    ) {259        Text(260            label,261            color = Color.White,262            style = TextStyle(fontFamily = PeachType.Body, fontSize = 13.sp, letterSpacing = 0.06.em),263        )264    }265}266267@Composable268private fun RestoreTabs(onNavigate: (PeachNav) -> Unit) {269    val tabs = listOf(270        "PeachScript" to PeachNav.Import,271        "CSV" to PeachNav.Import,272        "From Phone" to PeachNav.ChirpPair,273        "Codex" to PeachNav.CodexRestore,274    )275    Column(horizontalAlignment = Alignment.CenterHorizontally) {276        Text("restore instead", color = PeachColors.T1, style = PeachType.labelMono)277        Spacer(Modifier.height(10.dp))278        Row(horizontalArrangement = Arrangement.spacedBy(6.dp)) {279            tabs.forEach { (label, dest) ->280                Box(281                    modifier = Modifier282                        .border(1.dp, PeachColors.T3, RoundedCornerShape(8.dp))283                        .clickable { onNavigate(dest) }284                        .padding(horizontal = 10.dp, vertical = 8.dp),285                ) {286                    Text(label, color = PeachColors.T1, style = PeachType.sortPill)287                }288            }289        }290    }291}