LockScreen.kt

14.0 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 androidx.compose.ui.platform.LocalContext53import androidx.fragment.app.FragmentActivity54import com.peachpasswords.ui.PeachNav55import com.peachpasswords.ui.components.LockRuneString56import com.peachpasswords.ui.components.PeachMark57import com.peachpasswords.ui.theme.LocalPeachAccent58import com.peachpasswords.ui.theme.PeachColors59import com.peachpasswords.ui.security.StrengthBar60import com.peachpasswords.ui.security.scorePassword61import com.peachpasswords.ui.security.strengthColor62import com.peachpasswords.ui.security.strengthLabel63import com.peachpasswords.ui.theme.PeachType64import com.peachpasswords.ui.theme.ShellBackground6566@Composable67fun LockScreen(68    onUnlocked: () -> Unit,69    onNavigate: (PeachNav) -> Unit,70    vm: LockViewModel = hiltViewModel(),71) {72    val state by vm.state.collectAsState()73    val accent = LocalPeachAccent.current74    val context = LocalContext.current75    val activity = context as? FragmentActivity76    val canBiometric = remember(state.hasVault) { vm.canTryBiometric() }7778    var password by remember { mutableStateOf("") }79    var confirm by remember { mutableStateOf("") }80    val createScore = remember(password) { scorePassword(password) }8182    LaunchedEffect(state.unlocked) {83        if (state.unlocked) {84            // A freshly created vault routes through recovery-word setup before the list.85            if (state.created) onNavigate(PeachNav.RecoverySetup) else onUnlocked()86        }87    }8889    // Auto-offer biometric unlock shortly after the lock screen appears.90    LaunchedEffect(canBiometric) {91        if (canBiometric && activity != null && !state.unlocked) {92            kotlinx.coroutines.delay(300)93            vm.biometricUnlock(activity)94        }95    }9697    // Shake the panel on each failed unlock (brief Phase 4: 0→−12→10→−8→6→0 over ~400ms).98    val shake = remember { Animatable(0f) }99    LaunchedEffect(state.shakeNonce) {100        if (state.shakeNonce > 0) {101            shake.animateTo(102                targetValue = 0f,103                animationSpec = keyframes {104                    durationMillis = 400105                    -12f at 60; 10f at 140; -8f at 230; 6f at 320; 0f at 400106                },107            )108        }109    }110111    ShellBackground {112        // Wordmark, top-left.113        Text(114            text = "peach",115            color = accent,116            style = PeachType.wordmark,117            modifier = Modifier.align(Alignment.TopStart).systemBarsPadding().padding(start = 20.dp, top = 8.dp),118        )119120        Column(121            modifier = Modifier122                .fillMaxSize()123                .systemBarsPadding()124                .padding(horizontal = 28.dp),125            verticalArrangement = Arrangement.Center,126            horizontalAlignment = Alignment.CenterHorizontally,127        ) {128            // Hero: centered PeachMark · eyebrow · headline.129            PeachMark(size = 80.dp)130            Spacer(Modifier.height(18.dp))131            Text(132                text = if (state.hasVault) "VAULT LOCKED" else "NEW VAULT",133                color = PeachColors.T2,134                style = PeachType.lockEyebrow,135            )136            Spacer(Modifier.height(10.dp))137            Text(138                text = if (state.hasVault) "unlock\nyour vault." else "keep your\nsecrets.",139                color = PeachColors.T0,140                style = if (state.hasVault) PeachType.lockHeadline else PeachType.createHeadline,141                textAlign = TextAlign.Center,142            )143            Spacer(Modifier.height(14.dp))144            LockRuneString()145            Spacer(Modifier.height(24.dp))146147            // Glass panel holding the credential inputs.148            GlassPanel(149                modifier = Modifier.offset { IntOffset(shake.value.dp.roundToPx(), 0) },150            ) {151                FieldLabel(if (state.hasVault) "master password" else "create a master password")152                Spacer(Modifier.height(4.dp))153                UnderlineField(154                    value = password,155                    onChange = { password = it; vm.clearError() },156                    placeholder = "••••••••••",157                    textStyle = if (state.hasVault) PeachType.unlockInput else mono18Spaced(),158                    imeAction = if (state.hasVault) ImeAction.Go else ImeAction.Next,159                    onImeAction = { if (state.hasVault) vm.unlock(password) },160                )161162                if (!state.hasVault) {163                    Spacer(Modifier.height(16.dp))164                    FieldLabel("confirm password")165                    Spacer(Modifier.height(4.dp))166                    UnderlineField(167                        value = confirm,168                        onChange = { confirm = it; vm.clearError() },169                        placeholder = "••••••••••",170                        textStyle = mono18Spaced(),171                        imeAction = ImeAction.Go,172                        onImeAction = { vm.create(password, confirm) },173                    )174175                    if (password.isNotEmpty()) {176                        Spacer(Modifier.height(14.dp))177                        StrengthBar(score = createScore)178                        Spacer(Modifier.height(6.dp))179                        Text(180                            text = "strength: ${strengthLabel(createScore)}",181                            color = strengthColor(createScore),182                            style = PeachType.labelMono,183                            modifier = Modifier.fillMaxWidth(),184                            textAlign = TextAlign.Center,185                        )186                    }187                }188189                Text(190                    text = state.error ?: " ",191                    color = PeachColors.Breach,192                    style = PeachType.labelMono.copy(fontSize = 11.sp),193                    textAlign = TextAlign.Center,194                    modifier = Modifier.fillMaxWidth().padding(top = 12.dp).height(18.dp),195                )196197                Spacer(Modifier.height(4.dp))198                AccentButton(199                    label = if (state.hasVault) "unlock" else "create vault",200                    enabled = !state.loading && (state.hasVault || createScore >= MIN_CREATE_STRENGTH),201                    onClick = { if (state.hasVault) vm.unlock(password) else vm.create(password, confirm) },202                )203204                if (state.hasVault && canBiometric && activity != null) {205                    Spacer(Modifier.height(12.dp))206                    Text(207                        text = "Use biometrics",208                        color = accent,209                        style = PeachType.labelMono.copy(letterSpacing = 0.08.em),210                        textAlign = TextAlign.Center,211                        modifier = Modifier.fillMaxWidth()212                            .clip(RoundedCornerShape(8.dp))213                            .clickable { vm.biometricUnlock(activity) }214                            .padding(vertical = 8.dp),215                    )216                }217218                if (state.hasVault) {219                    Spacer(Modifier.height(8.dp))220                    Text(221                        text = "Forgot your password?",222                        color = PeachColors.T2,223                        style = PeachType.labelMono.copy(fontSize = 11.sp),224                        textAlign = TextAlign.Center,225                        modifier = Modifier.fillMaxWidth()226                            .clip(RoundedCornerShape(8.dp))227                            .clickable { onNavigate(PeachNav.RecoveryEntry) }228                            .padding(vertical = 8.dp),229                    )230                }231            }232233            if (!state.hasVault) {234                Spacer(Modifier.height(24.dp))235                RestoreTabs(onNavigate = onNavigate)236            }237        }238    }239}240241@Composable242private fun FieldLabel(text: String) {243    Text(text.uppercase(), color = PeachColors.T2, style = PeachType.labelMono.copy(letterSpacing = 0.08.em))244}245246/** IBM Plex Mono 18sp, wide tracking — the create-vault input style (spec §5.2). */247private fun mono18Spaced() = TextStyle(fontFamily = PeachType.Mono, fontSize = 18.sp, letterSpacing = 0.18.em)248249@Composable250private fun GlassPanel(modifier: Modifier = Modifier, content: @Composable androidx.compose.foundation.layout.ColumnScope.() -> Unit) {251    Column(252        modifier253            .fillMaxWidth()254            .clip(RoundedCornerShape(18.dp))255            .background(Color(0xE0131109))256            .border(1.dp, PeachColors.T3, RoundedCornerShape(18.dp))257            .padding(20.dp),258        content = content,259    )260}261262@Composable263private fun UnderlineField(264    value: String,265    onChange: (String) -> Unit,266    placeholder: String,267    textStyle: TextStyle,268    imeAction: ImeAction,269    onImeAction: () -> Unit,270) {271    val accent = LocalPeachAccent.current272    val interaction = remember { MutableInteractionSource() }273    val focused by interaction.collectIsFocusedAsState()274    BasicTextField(275        value = value,276        onValueChange = onChange,277        singleLine = true,278        textStyle = textStyle.copy(color = PeachColors.T0, textAlign = TextAlign.Center),279        cursorBrush = SolidColor(accent),280        visualTransformation = PasswordVisualTransformation(),281        keyboardOptions = KeyboardOptions(keyboardType = KeyboardType.Password, imeAction = imeAction),282        keyboardActions = KeyboardActions(onGo = { onImeAction() }, onNext = { onImeAction() }),283        interactionSource = interaction,284        modifier = Modifier285            .fillMaxWidth()286            .drawBehind {287                drawRect(288                    color = if (focused) accent else PeachColors.T3,289                    topLeft = Offset(0f, size.height - 1.dp.toPx()),290                    size = Size(size.width, 1.dp.toPx()),291                )292            }293            .padding(vertical = 8.dp),294        decorationBox = { inner ->295            Box(contentAlignment = Alignment.Center) {296                if (value.isEmpty()) {297                    Text(placeholder, style = textStyle.copy(color = PeachColors.T2, textAlign = TextAlign.Center))298                }299                inner()300            }301        },302    )303}304305@Composable306private fun AccentButton(label: String, enabled: Boolean, onClick: () -> Unit) {307    val accent = LocalPeachAccent.current308    Box(309        modifier = Modifier310            .fillMaxWidth()311            .alpha(if (enabled) 1f else 0.5f)312            .clip(RoundedCornerShape(10.dp))313            .background(accent)314            .clickable(enabled = enabled, onClick = onClick)315            .padding(vertical = 13.dp),316        contentAlignment = Alignment.Center,317    ) {318        Text(319            label,320            color = Color.White,321            style = TextStyle(fontFamily = PeachType.Body, fontSize = 13.sp, letterSpacing = 0.06.em),322        )323    }324}325326@Composable327private fun RestoreTabs(onNavigate: (PeachNav) -> Unit) {328    val tabs = listOf(329        "PeachScript" to PeachNav.Import,330        "CSV" to PeachNav.Import,331        "From Phone" to PeachNav.ChirpPair,332        "Codex" to PeachNav.CodexRestore,333    )334    Column(horizontalAlignment = Alignment.CenterHorizontally) {335        Text("restore instead", color = PeachColors.T1, style = PeachType.labelMono)336        Spacer(Modifier.height(10.dp))337        Row(horizontalArrangement = Arrangement.spacedBy(6.dp)) {338            tabs.forEach { (label, dest) ->339                Box(340                    modifier = Modifier341                        .border(1.dp, PeachColors.T3, RoundedCornerShape(8.dp))342                        .clickable { onNavigate(dest) }343                        .padding(horizontal = 10.dp, vertical = 8.dp),344                ) {345                    Text(label, color = PeachColors.T1, style = PeachType.sortPill)346                }347            }348        }349    }350}