package com.peachpasswords.ui.lock import androidx.compose.animation.core.Animatable import androidx.compose.animation.core.keyframes import androidx.compose.foundation.background import androidx.compose.foundation.border import androidx.compose.foundation.clickable import androidx.compose.foundation.layout.Arrangement import androidx.compose.foundation.layout.Box import androidx.compose.foundation.layout.Column import androidx.compose.foundation.layout.Row import androidx.compose.foundation.layout.Spacer import androidx.compose.foundation.layout.fillMaxSize import androidx.compose.foundation.layout.offset import androidx.compose.foundation.layout.fillMaxWidth import androidx.compose.foundation.layout.height import androidx.compose.foundation.layout.padding import androidx.compose.foundation.layout.systemBarsPadding import androidx.compose.foundation.shape.RoundedCornerShape import androidx.compose.foundation.text.KeyboardActions import androidx.compose.foundation.text.KeyboardOptions import androidx.compose.material3.LocalTextStyle import androidx.compose.material3.Text import androidx.compose.runtime.Composable import androidx.compose.runtime.LaunchedEffect import androidx.compose.runtime.getValue import androidx.compose.runtime.mutableStateOf import androidx.compose.runtime.remember import androidx.compose.runtime.setValue import androidx.compose.ui.Alignment import androidx.compose.ui.Modifier import androidx.compose.ui.draw.alpha import androidx.compose.ui.graphics.Color import androidx.compose.ui.text.input.ImeAction import androidx.compose.ui.text.input.KeyboardType import androidx.compose.ui.text.input.PasswordVisualTransformation import androidx.compose.ui.text.font.FontStyle import androidx.compose.ui.text.font.FontWeight import androidx.compose.ui.text.style.TextAlign import androidx.compose.ui.unit.IntOffset import androidx.compose.ui.unit.dp import androidx.compose.ui.unit.sp import androidx.hilt.navigation.compose.hiltViewModel import androidx.compose.runtime.collectAsState import com.peachpasswords.ui.PeachNav import com.peachpasswords.ui.components.LockRuneString import com.peachpasswords.ui.components.PeachMark import com.peachpasswords.ui.theme.LocalPeachAccent import com.peachpasswords.ui.theme.PeachColors import com.peachpasswords.ui.theme.PeachType import com.peachpasswords.ui.theme.ShellBackground @Composable fun LockScreen( onUnlocked: () -> Unit, onNavigate: (PeachNav) -> Unit, vm: LockViewModel = hiltViewModel(), ) { val state by vm.state.collectAsState() val accent = LocalPeachAccent.current var password by remember { mutableStateOf("") } var confirm by remember { mutableStateOf("") } LaunchedEffect(state.unlocked) { if (state.unlocked) onUnlocked() } // Shake the password field on each failed unlock (matches the 300ms browser shake). val shake = remember { Animatable(0f) } LaunchedEffect(state.shakeNonce) { if (state.shakeNonce > 0) { shake.animateTo( targetValue = 0f, animationSpec = keyframes { durationMillis = 300 8f at 50; -8f at 100; 8f at 150; -8f at 200; 4f at 250; 0f at 300 }, ) } } // 400ms biometric trigger — dormant until enrollment lands (canTryBiometric == false). LaunchedEffect(Unit) { if (state.hasVault && vm.canTryBiometric()) { kotlinx.coroutines.delay(400) // biometric prompt wired with Protection settings } } ShellBackground { // Faint PeachMark art, top-right, 120dp at 12% opacity. PeachMark( modifier = Modifier .align(Alignment.TopEnd) .systemBarsPadding() .padding(end = 20.dp, top = 8.dp), size = 120.dp, alpha = 0.12f, ) Column( modifier = Modifier .fillMaxSize() .systemBarsPadding() .padding(horizontal = 32.dp), verticalArrangement = Arrangement.Center, horizontalAlignment = Alignment.CenterHorizontally, ) { Text( text = "peach", color = accent, fontFamily = PeachType.Display, fontStyle = FontStyle.Italic, fontWeight = FontWeight.Light, fontSize = 44.sp, ) Spacer(Modifier.height(10.dp)) LockRuneString() Spacer(Modifier.height(36.dp)) Box(modifier = Modifier.offset { IntOffset(shake.value.dp.roundToPx(), 0) }) { Column(horizontalAlignment = Alignment.CenterHorizontally) { PasswordField( value = password, onChange = { password = it; vm.clearError() }, placeholder = if (state.hasVault) "master password" else "create a master password", imeAction = if (state.hasVault) ImeAction.Go else ImeAction.Next, onImeAction = { if (state.hasVault) vm.unlock(password) }, ) if (!state.hasVault) { Spacer(Modifier.height(12.dp)) PasswordField( value = confirm, onChange = { confirm = it; vm.clearError() }, placeholder = "confirm password", imeAction = ImeAction.Go, onImeAction = { vm.create(password, confirm) }, ) } } } Spacer(Modifier.height(8.dp)) Text( text = state.error ?: " ", color = PeachColors.Breach, fontFamily = PeachType.Mono, fontSize = 11.sp, textAlign = TextAlign.Center, modifier = Modifier.height(20.dp), ) Spacer(Modifier.height(8.dp)) PrimaryButton( label = if (state.hasVault) "unlock" else "create vault", enabled = !state.loading, onClick = { if (state.hasVault) vm.unlock(password) else vm.create(password, confirm) }, ) if (!state.hasVault) { Spacer(Modifier.height(28.dp)) RestoreTabs(onNavigate = onNavigate) } } } } @Composable private fun PasswordField( value: String, onChange: (String) -> Unit, placeholder: String, imeAction: ImeAction, onImeAction: () -> Unit, ) { androidx.compose.foundation.text.BasicTextField( value = value, onValueChange = onChange, singleLine = true, textStyle = LocalTextStyle.current.copy( color = PeachColors.T0, fontFamily = PeachType.Mono, fontSize = 15.sp, textAlign = TextAlign.Center, ), cursorBrush = androidx.compose.ui.graphics.SolidColor(LocalPeachAccent.current), visualTransformation = PasswordVisualTransformation(), keyboardOptions = KeyboardOptions(keyboardType = KeyboardType.Password, imeAction = imeAction), keyboardActions = KeyboardActions(onGo = { onImeAction() }, onNext = { onImeAction() }), modifier = Modifier .fillMaxWidth() .border(1.dp, PeachColors.T3, RoundedCornerShape(12.dp)) .padding(vertical = 16.dp, horizontal = 16.dp), decorationBox = { inner -> Box(contentAlignment = Alignment.Center) { if (value.isEmpty()) { Text(placeholder, color = PeachColors.T2, fontFamily = PeachType.Mono, fontSize = 14.sp) } inner() } }, ) } @Composable private fun PrimaryButton(label: String, enabled: Boolean, onClick: () -> Unit) { val accent = LocalPeachAccent.current Box( modifier = Modifier .fillMaxWidth() .alpha(if (enabled) 1f else 0.5f) .border(1.dp, accent.copy(alpha = 0.5f), RoundedCornerShape(12.dp)) .background(accent.copy(alpha = 0.10f), RoundedCornerShape(12.dp)) .clickable(enabled = enabled, onClick = onClick) .padding(vertical = 16.dp), contentAlignment = Alignment.Center, ) { Text(label, color = accent, fontFamily = PeachType.Mono, fontSize = 14.sp) } } @Composable private fun RestoreTabs(onNavigate: (PeachNav) -> Unit) { val tabs = listOf( "PeachScript" to PeachNav.Import, "CSV" to PeachNav.Import, "From Phone" to PeachNav.ChirpPair, "Codex" to PeachNav.CodexRestore, ) Column(horizontalAlignment = Alignment.CenterHorizontally) { Text("restore instead", color = PeachColors.T1, fontFamily = PeachType.Mono, fontSize = 10.sp) Spacer(Modifier.height(10.dp)) Row(horizontalArrangement = Arrangement.spacedBy(6.dp)) { tabs.forEach { (label, dest) -> Box( modifier = Modifier .border(1.dp, PeachColors.T3, RoundedCornerShape(8.dp)) .clickable { onNavigate(dest) } .padding(horizontal = 10.dp, vertical = 8.dp), ) { Text(label, color = PeachColors.T1, fontFamily = PeachType.Mono, fontSize = 9.sp) } } } } }