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.theme.PeachType60import com.peachpasswords.ui.theme.ShellBackground6162@Composable63fun LockScreen(64 onUnlocked: () -> Unit,65 onNavigate: (PeachNav) -> Unit,66 vm: LockViewModel = hiltViewModel(),67) {68 val state by vm.state.collectAsState()69 val accent = LocalPeachAccent.current70 val context = LocalContext.current71 val activity = context as? FragmentActivity72 val canBiometric = remember(state.hasVault) { vm.canTryBiometric() }7374 var password by remember { mutableStateOf("") }75 var confirm by remember { mutableStateOf("") }7677 LaunchedEffect(state.unlocked) {78 if (state.unlocked) {79 80 if (state.created) onNavigate(PeachNav.RecoverySetup) else onUnlocked()81 }82 }8384 85 LaunchedEffect(canBiometric) {86 if (canBiometric && activity != null && !state.unlocked) {87 kotlinx.coroutines.delay(300)88 vm.biometricUnlock(activity)89 }90 }9192 93 val shake = remember { Animatable(0f) }94 LaunchedEffect(state.shakeNonce) {95 if (state.shakeNonce > 0) {96 shake.animateTo(97 targetValue = 0f,98 animationSpec = keyframes {99 durationMillis = 400100 -12f at 60; 10f at 140; -8f at 230; 6f at 320; 0f at 400101 },102 )103 }104 }105106 ShellBackground {107 108 Text(109 text = "peach",110 color = accent,111 style = PeachType.wordmark,112 modifier = Modifier.align(Alignment.TopStart).systemBarsPadding().padding(start = 20.dp, top = 8.dp),113 )114115 Column(116 modifier = Modifier117 .fillMaxSize()118 .systemBarsPadding()119 .padding(horizontal = 28.dp),120 verticalArrangement = Arrangement.Center,121 horizontalAlignment = Alignment.CenterHorizontally,122 ) {123 124 PeachMark(size = 80.dp)125 Spacer(Modifier.height(18.dp))126 Text(127 text = if (state.hasVault) "VAULT LOCKED" else "NEW VAULT",128 color = PeachColors.T2,129 style = PeachType.lockEyebrow,130 )131 Spacer(Modifier.height(10.dp))132 Text(133 text = if (state.hasVault) "unlock\nyour vault." else "keep your\nsecrets.",134 color = PeachColors.T0,135 style = if (state.hasVault) PeachType.lockHeadline else PeachType.createHeadline,136 textAlign = TextAlign.Center,137 )138 Spacer(Modifier.height(14.dp))139 LockRuneString()140 Spacer(Modifier.height(24.dp))141142 143 GlassPanel(144 modifier = Modifier.offset { IntOffset(shake.value.dp.roundToPx(), 0) },145 ) {146 FieldLabel(if (state.hasVault) "master password" else "create a master password")147 Spacer(Modifier.height(4.dp))148 UnderlineField(149 value = password,150 onChange = { password = it; vm.clearError() },151 placeholder = "••••••••••",152 textStyle = if (state.hasVault) PeachType.unlockInput else mono18Spaced(),153 imeAction = if (state.hasVault) ImeAction.Go else ImeAction.Next,154 onImeAction = { if (state.hasVault) vm.unlock(password) },155 )156157 if (!state.hasVault) {158 Spacer(Modifier.height(16.dp))159 FieldLabel("confirm password")160 Spacer(Modifier.height(4.dp))161 UnderlineField(162 value = confirm,163 onChange = { confirm = it; vm.clearError() },164 placeholder = "••••••••••",165 textStyle = mono18Spaced(),166 imeAction = ImeAction.Go,167 onImeAction = { vm.create(password, confirm) },168 )169 }170171 Text(172 text = state.error ?: " ",173 color = PeachColors.Breach,174 style = PeachType.labelMono.copy(fontSize = 11.sp),175 textAlign = TextAlign.Center,176 modifier = Modifier.fillMaxWidth().padding(top = 12.dp).height(18.dp),177 )178179 Spacer(Modifier.height(4.dp))180 AccentButton(181 label = if (state.hasVault) "unlock" else "create vault",182 enabled = !state.loading,183 onClick = { if (state.hasVault) vm.unlock(password) else vm.create(password, confirm) },184 )185186 if (state.hasVault && canBiometric && activity != null) {187 Spacer(Modifier.height(12.dp))188 Text(189 text = "Use biometrics",190 color = accent,191 style = PeachType.labelMono.copy(letterSpacing = 0.08.em),192 textAlign = TextAlign.Center,193 modifier = Modifier.fillMaxWidth()194 .clip(RoundedCornerShape(8.dp))195 .clickable { vm.biometricUnlock(activity) }196 .padding(vertical = 8.dp),197 )198 }199 }200201 if (!state.hasVault) {202 Spacer(Modifier.height(24.dp))203 RestoreTabs(onNavigate = onNavigate)204 }205 }206 }207}208209@Composable210private fun FieldLabel(text: String) {211 Text(text.uppercase(), color = PeachColors.T2, style = PeachType.labelMono.copy(letterSpacing = 0.08.em))212}213214215private fun mono18Spaced() = TextStyle(fontFamily = PeachType.Mono, fontSize = 18.sp, letterSpacing = 0.18.em)216217@Composable218private fun GlassPanel(modifier: Modifier = Modifier, content: @Composable androidx.compose.foundation.layout.ColumnScope.() -> Unit) {219 Column(220 modifier221 .fillMaxWidth()222 .clip(RoundedCornerShape(18.dp))223 .background(Color(0xE0131109))224 .border(1.dp, PeachColors.T3, RoundedCornerShape(18.dp))225 .padding(20.dp),226 content = content,227 )228}229230@Composable231private fun UnderlineField(232 value: String,233 onChange: (String) -> Unit,234 placeholder: String,235 textStyle: TextStyle,236 imeAction: ImeAction,237 onImeAction: () -> Unit,238) {239 val accent = LocalPeachAccent.current240 val interaction = remember { MutableInteractionSource() }241 val focused by interaction.collectIsFocusedAsState()242 BasicTextField(243 value = value,244 onValueChange = onChange,245 singleLine = true,246 textStyle = textStyle.copy(color = PeachColors.T0, textAlign = TextAlign.Center),247 cursorBrush = SolidColor(accent),248 visualTransformation = PasswordVisualTransformation(),249 keyboardOptions = KeyboardOptions(keyboardType = KeyboardType.Password, imeAction = imeAction),250 keyboardActions = KeyboardActions(onGo = { onImeAction() }, onNext = { onImeAction() }),251 interactionSource = interaction,252 modifier = Modifier253 .fillMaxWidth()254 .drawBehind {255 drawRect(256 color = if (focused) accent else PeachColors.T3,257 topLeft = Offset(0f, size.height - 1.dp.toPx()),258 size = Size(size.width, 1.dp.toPx()),259 )260 }261 .padding(vertical = 8.dp),262 decorationBox = { inner ->263 Box(contentAlignment = Alignment.Center) {264 if (value.isEmpty()) {265 Text(placeholder, style = textStyle.copy(color = PeachColors.T2, textAlign = TextAlign.Center))266 }267 inner()268 }269 },270 )271}272273@Composable274private fun AccentButton(label: String, enabled: Boolean, onClick: () -> Unit) {275 val accent = LocalPeachAccent.current276 Box(277 modifier = Modifier278 .fillMaxWidth()279 .alpha(if (enabled) 1f else 0.5f)280 .clip(RoundedCornerShape(10.dp))281 .background(accent)282 .clickable(enabled = enabled, onClick = onClick)283 .padding(vertical = 13.dp),284 contentAlignment = Alignment.Center,285 ) {286 Text(287 label,288 color = Color.White,289 style = TextStyle(fontFamily = PeachType.Body, fontSize = 13.sp, letterSpacing = 0.06.em),290 )291 }292}293294@Composable295private fun RestoreTabs(onNavigate: (PeachNav) -> Unit) {296 val tabs = listOf(297 "PeachScript" to PeachNav.Import,298 "CSV" to PeachNav.Import,299 "From Phone" to PeachNav.ChirpPair,300 "Codex" to PeachNav.CodexRestore,301 )302 Column(horizontalAlignment = Alignment.CenterHorizontally) {303 Text("restore instead", color = PeachColors.T1, style = PeachType.labelMono)304 Spacer(Modifier.height(10.dp))305 Row(horizontalArrangement = Arrangement.spacedBy(6.dp)) {306 tabs.forEach { (label, dest) ->307 Box(308 modifier = Modifier309 .border(1.dp, PeachColors.T3, RoundedCornerShape(8.dp))310 .clickable { onNavigate(dest) }311 .padding(horizontal = 10.dp, vertical = 8.dp),312 ) {313 Text(label, color = PeachColors.T1, style = PeachType.sortPill)314 }315 }316 }317 }318}