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 85 if (state.created) onNavigate(PeachNav.RecoverySetup) else onUnlocked()86 }87 }8889 90 LaunchedEffect(canBiometric) {91 if (canBiometric && activity != null && !state.unlocked) {92 kotlinx.coroutines.delay(300)93 vm.biometricUnlock(activity)94 }95 }9697 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 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 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 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 }217 }218219 if (!state.hasVault) {220 Spacer(Modifier.height(24.dp))221 RestoreTabs(onNavigate = onNavigate)222 }223 }224 }225}226227@Composable228private fun FieldLabel(text: String) {229 Text(text.uppercase(), color = PeachColors.T2, style = PeachType.labelMono.copy(letterSpacing = 0.08.em))230}231232233private fun mono18Spaced() = TextStyle(fontFamily = PeachType.Mono, fontSize = 18.sp, letterSpacing = 0.18.em)234235@Composable236private fun GlassPanel(modifier: Modifier = Modifier, content: @Composable androidx.compose.foundation.layout.ColumnScope.() -> Unit) {237 Column(238 modifier239 .fillMaxWidth()240 .clip(RoundedCornerShape(18.dp))241 .background(Color(0xE0131109))242 .border(1.dp, PeachColors.T3, RoundedCornerShape(18.dp))243 .padding(20.dp),244 content = content,245 )246}247248@Composable249private fun UnderlineField(250 value: String,251 onChange: (String) -> Unit,252 placeholder: String,253 textStyle: TextStyle,254 imeAction: ImeAction,255 onImeAction: () -> Unit,256) {257 val accent = LocalPeachAccent.current258 val interaction = remember { MutableInteractionSource() }259 val focused by interaction.collectIsFocusedAsState()260 BasicTextField(261 value = value,262 onValueChange = onChange,263 singleLine = true,264 textStyle = textStyle.copy(color = PeachColors.T0, textAlign = TextAlign.Center),265 cursorBrush = SolidColor(accent),266 visualTransformation = PasswordVisualTransformation(),267 keyboardOptions = KeyboardOptions(keyboardType = KeyboardType.Password, imeAction = imeAction),268 keyboardActions = KeyboardActions(onGo = { onImeAction() }, onNext = { onImeAction() }),269 interactionSource = interaction,270 modifier = Modifier271 .fillMaxWidth()272 .drawBehind {273 drawRect(274 color = if (focused) accent else PeachColors.T3,275 topLeft = Offset(0f, size.height - 1.dp.toPx()),276 size = Size(size.width, 1.dp.toPx()),277 )278 }279 .padding(vertical = 8.dp),280 decorationBox = { inner ->281 Box(contentAlignment = Alignment.Center) {282 if (value.isEmpty()) {283 Text(placeholder, style = textStyle.copy(color = PeachColors.T2, textAlign = TextAlign.Center))284 }285 inner()286 }287 },288 )289}290291@Composable292private fun AccentButton(label: String, enabled: Boolean, onClick: () -> Unit) {293 val accent = LocalPeachAccent.current294 Box(295 modifier = Modifier296 .fillMaxWidth()297 .alpha(if (enabled) 1f else 0.5f)298 .clip(RoundedCornerShape(10.dp))299 .background(accent)300 .clickable(enabled = enabled, onClick = onClick)301 .padding(vertical = 13.dp),302 contentAlignment = Alignment.Center,303 ) {304 Text(305 label,306 color = Color.White,307 style = TextStyle(fontFamily = PeachType.Body, fontSize = 13.sp, letterSpacing = 0.06.em),308 )309 }310}311312@Composable313private fun RestoreTabs(onNavigate: (PeachNav) -> Unit) {314 val tabs = listOf(315 "PeachScript" to PeachNav.Import,316 "CSV" to PeachNav.Import,317 "From Phone" to PeachNav.ChirpPair,318 "Codex" to PeachNav.CodexRestore,319 )320 Column(horizontalAlignment = Alignment.CenterHorizontally) {321 Text("restore instead", color = PeachColors.T1, style = PeachType.labelMono)322 Spacer(Modifier.height(10.dp))323 Row(horizontalArrangement = Arrangement.spacedBy(6.dp)) {324 tabs.forEach { (label, dest) ->325 Box(326 modifier = Modifier327 .border(1.dp, PeachColors.T3, RoundedCornerShape(8.dp))328 .clickable { onNavigate(dest) }329 .padding(horizontal = 10.dp, vertical = 8.dp),330 ) {331 Text(label, color = PeachColors.T1, style = PeachType.sortPill)332 }333 }334 }335 }336}