LockScreen.kt

9.3 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.layout.Arrangement9import androidx.compose.foundation.layout.Box10import androidx.compose.foundation.layout.Column11import androidx.compose.foundation.layout.Row12import androidx.compose.foundation.layout.Spacer13import androidx.compose.foundation.layout.fillMaxSize14import androidx.compose.foundation.layout.offset15import androidx.compose.foundation.layout.fillMaxWidth16import androidx.compose.foundation.layout.height17import androidx.compose.foundation.layout.padding18import androidx.compose.foundation.layout.systemBarsPadding19import androidx.compose.foundation.shape.RoundedCornerShape20import androidx.compose.foundation.text.KeyboardActions21import androidx.compose.foundation.text.KeyboardOptions22import androidx.compose.material3.LocalTextStyle23import androidx.compose.material3.Text24import androidx.compose.runtime.Composable25import androidx.compose.runtime.LaunchedEffect26import androidx.compose.runtime.getValue27import androidx.compose.runtime.mutableStateOf28import androidx.compose.runtime.remember29import androidx.compose.runtime.setValue30import androidx.compose.ui.Alignment31import androidx.compose.ui.Modifier32import androidx.compose.ui.draw.alpha33import androidx.compose.ui.graphics.Color34import androidx.compose.ui.text.input.ImeAction35import androidx.compose.ui.text.input.KeyboardType36import androidx.compose.ui.text.input.PasswordVisualTransformation37import androidx.compose.ui.text.font.FontStyle38import androidx.compose.ui.text.font.FontWeight39import androidx.compose.ui.text.style.TextAlign40import androidx.compose.ui.unit.IntOffset41import androidx.compose.ui.unit.dp42import androidx.compose.ui.unit.sp43import androidx.hilt.navigation.compose.hiltViewModel44import androidx.compose.runtime.collectAsState45import com.peachpasswords.ui.PeachNav46import com.peachpasswords.ui.components.LockRuneString47import com.peachpasswords.ui.components.PeachMark48import com.peachpasswords.ui.theme.LocalPeachAccent49import com.peachpasswords.ui.theme.PeachColors50import com.peachpasswords.ui.theme.PeachType51import com.peachpasswords.ui.theme.ShellBackground5253@Composable54fun LockScreen(55    onUnlocked: () -> Unit,56    onNavigate: (PeachNav) -> Unit,57    vm: LockViewModel = hiltViewModel(),58) {59    val state by vm.state.collectAsState()60    val accent = LocalPeachAccent.current6162    var password by remember { mutableStateOf("") }63    var confirm by remember { mutableStateOf("") }6465    LaunchedEffect(state.unlocked) { if (state.unlocked) onUnlocked() }6667    // Shake the password field on each failed unlock (matches the 300ms browser shake).68    val shake = remember { Animatable(0f) }69    LaunchedEffect(state.shakeNonce) {70        if (state.shakeNonce > 0) {71            shake.animateTo(72                targetValue = 0f,73                animationSpec = keyframes {74                    durationMillis = 30075                    8f at 50; -8f at 100; 8f at 150; -8f at 200; 4f at 250; 0f at 30076                },77            )78        }79    }8081    // 400ms biometric trigger — dormant until enrollment lands (canTryBiometric == false).82    LaunchedEffect(Unit) {83        if (state.hasVault && vm.canTryBiometric()) {84            kotlinx.coroutines.delay(400)85            // biometric prompt wired with Protection settings86        }87    }8889    ShellBackground {90        // Faint PeachMark art, top-right, 120dp at 12% opacity.91        PeachMark(92            modifier = Modifier93                .align(Alignment.TopEnd)94                .systemBarsPadding()95                .padding(end = 20.dp, top = 8.dp),96            size = 120.dp,97            alpha = 0.12f,98        )99100        Column(101            modifier = Modifier102                .fillMaxSize()103                .systemBarsPadding()104                .padding(horizontal = 32.dp),105            verticalArrangement = Arrangement.Center,106            horizontalAlignment = Alignment.CenterHorizontally,107        ) {108            Text(109                text = "peach",110                color = accent,111                fontFamily = PeachType.Display,112                fontStyle = FontStyle.Italic,113                fontWeight = FontWeight.Light,114                fontSize = 44.sp,115            )116            Spacer(Modifier.height(10.dp))117            LockRuneString()118            Spacer(Modifier.height(36.dp))119120            Box(modifier = Modifier.offset { IntOffset(shake.value.dp.roundToPx(), 0) }) {121                Column(horizontalAlignment = Alignment.CenterHorizontally) {122                    PasswordField(123                        value = password,124                        onChange = { password = it; vm.clearError() },125                        placeholder = if (state.hasVault) "master password" else "create a master password",126                        imeAction = if (state.hasVault) ImeAction.Go else ImeAction.Next,127                        onImeAction = { if (state.hasVault) vm.unlock(password) },128                    )129                    if (!state.hasVault) {130                        Spacer(Modifier.height(12.dp))131                        PasswordField(132                            value = confirm,133                            onChange = { confirm = it; vm.clearError() },134                            placeholder = "confirm password",135                            imeAction = ImeAction.Go,136                            onImeAction = { vm.create(password, confirm) },137                        )138                    }139                }140            }141142            Spacer(Modifier.height(8.dp))143            Text(144                text = state.error ?: " ",145                color = PeachColors.Breach,146                fontFamily = PeachType.Mono,147                fontSize = 11.sp,148                textAlign = TextAlign.Center,149                modifier = Modifier.height(20.dp),150            )151152            Spacer(Modifier.height(8.dp))153            PrimaryButton(154                label = if (state.hasVault) "unlock" else "create vault",155                enabled = !state.loading,156                onClick = { if (state.hasVault) vm.unlock(password) else vm.create(password, confirm) },157            )158159            if (!state.hasVault) {160                Spacer(Modifier.height(28.dp))161                RestoreTabs(onNavigate = onNavigate)162            }163        }164    }165}166167@Composable168private fun PasswordField(169    value: String,170    onChange: (String) -> Unit,171    placeholder: String,172    imeAction: ImeAction,173    onImeAction: () -> Unit,174) {175    androidx.compose.foundation.text.BasicTextField(176        value = value,177        onValueChange = onChange,178        singleLine = true,179        textStyle = LocalTextStyle.current.copy(180            color = PeachColors.T0,181            fontFamily = PeachType.Mono,182            fontSize = 15.sp,183            textAlign = TextAlign.Center,184        ),185        cursorBrush = androidx.compose.ui.graphics.SolidColor(LocalPeachAccent.current),186        visualTransformation = PasswordVisualTransformation(),187        keyboardOptions = KeyboardOptions(keyboardType = KeyboardType.Password, imeAction = imeAction),188        keyboardActions = KeyboardActions(onGo = { onImeAction() }, onNext = { onImeAction() }),189        modifier = Modifier190            .fillMaxWidth()191            .border(1.dp, PeachColors.T3, RoundedCornerShape(12.dp))192            .padding(vertical = 16.dp, horizontal = 16.dp),193        decorationBox = { inner ->194            Box(contentAlignment = Alignment.Center) {195                if (value.isEmpty()) {196                    Text(placeholder, color = PeachColors.T2, fontFamily = PeachType.Mono, fontSize = 14.sp)197                }198                inner()199            }200        },201    )202}203204@Composable205private fun PrimaryButton(label: String, enabled: Boolean, onClick: () -> Unit) {206    val accent = LocalPeachAccent.current207    Box(208        modifier = Modifier209            .fillMaxWidth()210            .alpha(if (enabled) 1f else 0.5f)211            .border(1.dp, accent.copy(alpha = 0.5f), RoundedCornerShape(12.dp))212            .background(accent.copy(alpha = 0.10f), RoundedCornerShape(12.dp))213            .clickable(enabled = enabled, onClick = onClick)214            .padding(vertical = 16.dp),215        contentAlignment = Alignment.Center,216    ) {217        Text(label, color = accent, fontFamily = PeachType.Mono, fontSize = 14.sp)218    }219}220221@Composable222private fun RestoreTabs(onNavigate: (PeachNav) -> Unit) {223    val tabs = listOf(224        "PeachScript" to PeachNav.Import,225        "CSV" to PeachNav.Import,226        "From Phone" to PeachNav.ChirpPair,227        "Codex" to PeachNav.CodexRestore,228    )229    Column(horizontalAlignment = Alignment.CenterHorizontally) {230        Text("restore instead", color = PeachColors.T1, fontFamily = PeachType.Mono, fontSize = 10.sp)231        Spacer(Modifier.height(10.dp))232        Row(horizontalArrangement = Arrangement.spacedBy(6.dp)) {233            tabs.forEach { (label, dest) ->234                Box(235                    modifier = Modifier236                        .border(1.dp, PeachColors.T3, RoundedCornerShape(8.dp))237                        .clickable { onNavigate(dest) }238                        .padding(horizontal = 10.dp, vertical = 8.dp),239                ) {240                    Text(label, color = PeachColors.T1, fontFamily = PeachType.Mono, fontSize = 9.sp)241                }242            }243        }244    }245}