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 }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}245246247private 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 val hairline = peachColors.T3275 BasicTextField(276 value = value,277 onValueChange = onChange,278 singleLine = true,279 textStyle = textStyle.copy(color = peachColors.T0, textAlign = TextAlign.Center),280 cursorBrush = SolidColor(accent),281 visualTransformation = PasswordVisualTransformation(),282 keyboardOptions = KeyboardOptions(keyboardType = KeyboardType.Password, imeAction = imeAction),283 keyboardActions = KeyboardActions(onGo = { onImeAction() }, onNext = { onImeAction() }),284 interactionSource = interaction,285 modifier = Modifier286 .fillMaxWidth()287 .drawBehind {288 drawRect(289 color = if (focused) accent else hairline,290 topLeft = Offset(0f, size.height - 1.dp.toPx()),291 size = Size(size.width, 1.dp.toPx()),292 )293 }294 .padding(vertical = 8.dp),295 decorationBox = { inner ->296 Box(contentAlignment = Alignment.Center) {297 if (value.isEmpty()) {298 Text(placeholder, style = textStyle.copy(color = peachColors.T2, textAlign = TextAlign.Center))299 }300 inner()301 }302 },303 )304}305306@Composable307private fun AccentButton(label: String, enabled: Boolean, onClick: () -> Unit) {308 val accent = LocalPeachAccent.current309 Box(310 modifier = Modifier311 .fillMaxWidth()312 .alpha(if (enabled) 1f else 0.5f)313 .clip(RoundedCornerShape(10.dp))314 .background(accent)315 .clickable(enabled = enabled, onClick = onClick)316 .padding(vertical = 13.dp),317 contentAlignment = Alignment.Center,318 ) {319 Text(320 label,321 color = Color.White,322 style = TextStyle(fontFamily = PeachType.Body, fontSize = 13.sp, letterSpacing = 0.06.em),323 )324 }325}326327@Composable328private fun RestoreTabs(onNavigate: (PeachNav) -> Unit) {329 val tabs = listOf(330 "PeachScript" to PeachNav.Import,331 "CSV" to PeachNav.Import,332 "From Phone" to PeachNav.ChirpPair,333 "Codex" to PeachNav.CodexRestore,334 )335 Column(horizontalAlignment = Alignment.CenterHorizontally) {336 Text("restore instead", color = peachColors.T1, style = PeachType.labelMono)337 Spacer(Modifier.height(10.dp))338 Row(horizontalArrangement = Arrangement.spacedBy(6.dp)) {339 tabs.forEach { (label, dest) ->340 Box(341 modifier = Modifier342 .border(1.dp, peachColors.T3, RoundedCornerShape(8.dp))343 .clickable { onNavigate(dest) }344 .padding(horizontal = 10.dp, vertical = 8.dp),345 ) {346 Text(label, color = peachColors.T1, style = PeachType.sortPill)347 }348 }349 }350 }351}