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.ExperimentalLayoutApi14import androidx.compose.foundation.layout.FlowRow15import androidx.compose.foundation.layout.Row16import androidx.compose.foundation.layout.Spacer17import androidx.compose.foundation.layout.fillMaxHeight18import androidx.compose.foundation.layout.fillMaxSize19import androidx.compose.foundation.layout.offset20import androidx.compose.foundation.layout.fillMaxWidth21import androidx.compose.foundation.layout.height22import androidx.compose.foundation.layout.padding23import androidx.compose.foundation.layout.systemBarsPadding24import androidx.compose.foundation.layout.widthIn25import androidx.compose.foundation.shape.RoundedCornerShape26import androidx.compose.foundation.text.BasicTextField27import androidx.compose.foundation.text.KeyboardActions28import androidx.compose.foundation.text.KeyboardOptions29import androidx.compose.material3.Text30import androidx.compose.runtime.Composable31import androidx.compose.runtime.LaunchedEffect32import androidx.compose.runtime.getValue33import androidx.compose.runtime.mutableStateOf34import androidx.compose.runtime.remember35import androidx.compose.runtime.setValue36import androidx.compose.ui.Alignment37import androidx.compose.ui.Modifier38import androidx.compose.ui.draw.alpha39import androidx.compose.ui.draw.clip40import androidx.compose.ui.draw.drawBehind41import androidx.compose.ui.geometry.Offset42import androidx.compose.ui.geometry.Size43import androidx.compose.ui.graphics.Color44import androidx.compose.ui.graphics.SolidColor45import androidx.compose.ui.text.TextStyle46import androidx.compose.ui.text.input.ImeAction47import androidx.compose.ui.text.input.KeyboardType48import androidx.compose.ui.text.input.PasswordVisualTransformation49import androidx.compose.ui.text.input.VisualTransformation50import androidx.compose.ui.text.style.TextAlign51import androidx.compose.ui.unit.IntOffset52import androidx.compose.ui.unit.dp53import androidx.compose.ui.unit.em54import androidx.compose.ui.unit.sp55import androidx.hilt.navigation.compose.hiltViewModel56import androidx.compose.runtime.collectAsState57import androidx.compose.ui.platform.LocalContext58import androidx.fragment.app.FragmentActivity59import com.peachpasswords.ui.PeachNav60import com.peachpasswords.ui.components.LockRuneString61import com.peachpasswords.ui.components.PeachMark62import com.peachpasswords.ui.components.PeachWordmark63import com.peachpasswords.ui.components.RuneGridBackground64import com.peachpasswords.ui.theme.LocalPeachAccent65import com.peachpasswords.ui.theme.PeachSpacing66import com.peachpasswords.ui.theme.peachColors67import com.peachpasswords.ui.security.StrengthBar68import com.peachpasswords.ui.security.scorePassword69import com.peachpasswords.ui.security.strengthColor70import com.peachpasswords.ui.security.strengthLabel71import com.peachpasswords.ui.theme.PeachType72import com.peachpasswords.ui.theme.ShellBackground7374757677787980818283internal sealed class UnlockRoute {84 85 data object RecoverySetup : UnlockRoute()86 87 data object Defer : UnlockRoute()88 89 data class PostUnlock(val destination: PeachNav) : UnlockRoute()90}919293949596979899100101internal fun routeOnUnlock(102 state: LockUiState,103 autofillPending: Boolean,104 postUnlock: () -> PeachNav,105): UnlockRoute = when {106 state.created -> UnlockRoute.RecoverySetup107 autofillPending -> UnlockRoute.Defer108 else -> UnlockRoute.PostUnlock(postUnlock())109}110111@Composable112fun LockScreen(113 onUnlocked: (PeachNav) -> Unit,114 onNavigate: (PeachNav) -> Unit,115 116117118119120121122123124 autofillPending: Boolean = false,125 vm: LockViewModel = hiltViewModel(),126) {127 val state by vm.state.collectAsState()128 val accent = LocalPeachAccent.current129 val context = LocalContext.current130 val activity = context as? FragmentActivity131 val canBiometric = remember(state.hasVault) { vm.canTryBiometric() }132133 var password by remember { mutableStateOf("") }134 var confirm by remember { mutableStateOf("") }135 var reveal by remember { mutableStateOf(false) }136 val createScore = remember(password) { scorePassword(password) }137 138 139 140 val createBlocker = if (state.hasVault || password.isEmpty()) null141 else createValidationError(password, confirm, createScore)142143 144 145 146 147 148 149 LaunchedEffect(state.unlocked) {150 if (!state.unlocked) return@LaunchedEffect151 when (val route = routeOnUnlock(state, autofillPending, vm::postUnlockDestination)) {152 UnlockRoute.RecoverySetup -> onNavigate(PeachNav.RecoverySetup)153 UnlockRoute.Defer -> Unit154 is UnlockRoute.PostUnlock -> onUnlocked(route.destination)155 }156 }157158 159 LaunchedEffect(canBiometric) {160 if (canBiometric && activity != null && !state.unlocked) {161 kotlinx.coroutines.delay(300)162 vm.biometricUnlock(activity)163 }164 }165166 167 val shake = remember { Animatable(0f) }168 LaunchedEffect(state.shakeNonce) {169 if (state.shakeNonce > 0) {170 shake.animateTo(171 targetValue = 0f,172 animationSpec = keyframes {173 durationMillis = 400174 -12f at 60; 10f at 140; -8f at 230; 6f at 320; 0f at 400175 },176 )177 }178 }179180 ShellBackground {181 RuneGridBackground(modifier = Modifier.fillMaxSize())182183 PeachWordmark(184 modifier = Modifier.align(Alignment.TopStart).systemBarsPadding().padding(start = 20.dp, top = 8.dp),185 )186187 Column(188 modifier = Modifier189 .align(Alignment.Center)190 .fillMaxHeight()191 .widthIn(max = com.peachpasswords.ui.theme.CONTENT_MAX_WIDTH)192 .fillMaxWidth()193 .systemBarsPadding()194 .padding(horizontal = 28.dp),195 verticalArrangement = Arrangement.Center,196 horizontalAlignment = Alignment.CenterHorizontally,197 ) {198 199 PeachMark(size = 80.dp)200 Spacer(Modifier.height(18.dp))201 Text(202 text = if (state.hasVault) "VAULT LOCKED" else "NEW VAULT",203 color = peachColors.T2,204 style = PeachType.lockEyebrow,205 )206 Spacer(Modifier.height(10.dp))207 Text(208 text = if (state.hasVault) "unlock\nyour vault." else "keep your\nsecrets.",209 color = peachColors.T0,210 style = if (state.hasVault) PeachType.lockHeadline else PeachType.createHeadline,211 textAlign = TextAlign.Center,212 )213 Spacer(Modifier.height(14.dp))214 LockRuneString()215 Spacer(Modifier.height(24.dp))216217 218 GlassPanel(219 modifier = Modifier.offset { IntOffset(shake.value.dp.roundToPx(), 0) },220 ) {221 Row(verticalAlignment = Alignment.CenterVertically) {222 FieldLabel(if (state.hasVault) "master password" else "create a master password")223 if (!state.hasVault) {224 Spacer(Modifier.weight(1f))225 Text(226 text = if (reveal) "hide" else "show",227 color = accent,228 style = PeachType.labelMono.copy(fontSize = 11.sp),229 modifier = Modifier230 .clip(RoundedCornerShape(6.dp))231 .clickable { reveal = !reveal }232 .padding(horizontal = 6.dp, vertical = 4.dp),233 )234 }235 }236 Spacer(Modifier.height(4.dp))237 UnderlineField(238 value = password,239 onChange = { password = it; vm.clearError() },240 placeholder = "••••••••••",241 textStyle = if (state.hasVault) PeachType.unlockInput else mono18Spaced(),242 imeAction = if (state.hasVault) ImeAction.Go else ImeAction.Next,243 onImeAction = { if (state.hasVault) vm.unlock(password) },244 masked = state.hasVault || !reveal,245 )246247 if (!state.hasVault) {248 Spacer(Modifier.height(16.dp))249 FieldLabel("confirm password")250 Spacer(Modifier.height(4.dp))251 UnderlineField(252 value = confirm,253 onChange = { confirm = it; vm.clearError() },254 placeholder = "••••••••••",255 textStyle = mono18Spaced(),256 imeAction = ImeAction.Go,257 onImeAction = { vm.create(password, confirm) },258 masked = !reveal,259 )260261 Spacer(Modifier.height(14.dp))262 if (password.isEmpty()) {263 Text(264 text = "At least $MIN_PASSWORD_LENGTH characters. The vault is created once both fields match and pass the strength check.",265 color = peachColors.T2,266 style = PeachType.hint,267 textAlign = TextAlign.Center,268 )269 } else {270 StrengthBar(score = createScore)271 Spacer(Modifier.height(6.dp))272 Text(273 text = "strength: ${strengthLabel(createScore)}",274 color = strengthColor(createScore),275 style = PeachType.labelMono,276 modifier = Modifier.fillMaxWidth(),277 textAlign = TextAlign.Center,278 )279 }280 }281282 Text(283 text = state.error ?: " ",284 color = peachColors.Breach,285 style = PeachType.labelMono.copy(fontSize = 11.sp),286 textAlign = TextAlign.Center,287 modifier = Modifier.fillMaxWidth().padding(top = 12.dp).height(18.dp),288 )289290 Spacer(Modifier.height(4.dp))291 AccentButton(292 label = when {293 state.hasVault -> "unlock"294 state.loading -> "creating..."295 createBlocker != null -> createBlocker296 else -> "create vault"297 },298 enabled = !state.loading && (state.hasVault || canSubmitCreateVault(password, confirm, createScore)),299 onClick = { if (state.hasVault) vm.unlock(password) else vm.create(password, confirm) },300 )301302 if (state.hasVault && canBiometric && activity != null) {303 Spacer(Modifier.height(12.dp))304 Text(305 text = "Use biometrics",306 color = accent,307 style = PeachType.labelMono.copy(letterSpacing = 0.08.em),308 textAlign = TextAlign.Center,309 modifier = Modifier.fillMaxWidth()310 .clip(RoundedCornerShape(8.dp))311 .clickable { vm.biometricUnlock(activity) }312 .padding(vertical = 8.dp),313 )314 }315316 if (state.hasVault) {317 Spacer(Modifier.height(8.dp))318 Text(319 text = "Forgot your password?",320 color = peachColors.T2,321 style = PeachType.labelMono.copy(fontSize = 11.sp),322 textAlign = TextAlign.Center,323 modifier = Modifier.fillMaxWidth()324 .clip(RoundedCornerShape(8.dp))325 .clickable { onNavigate(PeachNav.RecoveryEntry) }326 .padding(vertical = 8.dp),327 )328 }329 }330331 if (!state.hasVault) {332 Spacer(Modifier.height(24.dp))333 RestoreTabs(onNavigate = onNavigate)334 }335 }336 }337}338339@Composable340private fun FieldLabel(text: String) {341 Text(text.uppercase(), color = peachColors.T2, style = PeachType.labelMono.copy(letterSpacing = 0.08.em))342}343344345private fun mono18Spaced() = TextStyle(fontFamily = PeachType.Mono, fontSize = 18.sp, letterSpacing = 0.18.em)346347@Composable348private fun GlassPanel(modifier: Modifier = Modifier, content: @Composable androidx.compose.foundation.layout.ColumnScope.() -> Unit) {349 Column(350 modifier351 .fillMaxWidth()352 .clip(RoundedCornerShape(18.dp))353 .background(Color(0xE0131109))354 .border(1.dp, peachColors.T3, RoundedCornerShape(18.dp))355 .padding(20.dp),356 content = content,357 )358}359360@Composable361private fun UnderlineField(362 value: String,363 onChange: (String) -> Unit,364 placeholder: String,365 textStyle: TextStyle,366 imeAction: ImeAction,367 onImeAction: () -> Unit,368 masked: Boolean = true,369) {370 val accent = LocalPeachAccent.current371 val interaction = remember { MutableInteractionSource() }372 val focused by interaction.collectIsFocusedAsState()373 val hairline = peachColors.T3374 BasicTextField(375 value = value,376 onValueChange = onChange,377 singleLine = true,378 textStyle = textStyle.copy(color = peachColors.T0, textAlign = TextAlign.Center),379 cursorBrush = SolidColor(accent),380 visualTransformation = if (masked) PasswordVisualTransformation() else VisualTransformation.None,381 keyboardOptions = KeyboardOptions(keyboardType = KeyboardType.Password, imeAction = imeAction),382 keyboardActions = KeyboardActions(onGo = { onImeAction() }, onNext = { onImeAction() }),383 interactionSource = interaction,384 modifier = Modifier385 .fillMaxWidth()386 .drawBehind {387 drawRect(388 color = if (focused) accent else hairline,389 topLeft = Offset(0f, size.height - 1.dp.toPx()),390 size = Size(size.width, 1.dp.toPx()),391 )392 }393 .padding(vertical = 8.dp),394 decorationBox = { inner ->395 Box(contentAlignment = Alignment.Center) {396 if (value.isEmpty()) {397 Text(placeholder, style = textStyle.copy(color = peachColors.T2, textAlign = TextAlign.Center))398 }399 inner()400 }401 },402 )403}404405@Composable406private fun AccentButton(label: String, enabled: Boolean, onClick: () -> Unit) {407 val accent = LocalPeachAccent.current408 Box(409 modifier = Modifier410 .fillMaxWidth()411 .alpha(if (enabled) 1f else 0.5f)412 .clip(RoundedCornerShape(10.dp))413 .background(accent)414 .clickable(enabled = enabled, onClick = onClick)415 .padding(vertical = 13.dp),416 contentAlignment = Alignment.Center,417 ) {418 Text(419 label,420 color = Color.White,421 style = TextStyle(fontFamily = PeachType.Body, fontSize = 13.sp, letterSpacing = 0.06.em),422 )423 }424}425426@OptIn(ExperimentalLayoutApi::class)427@Composable428private fun RestoreTabs(onNavigate: (PeachNav) -> Unit) {429 val tabs = listOf(430 "PeachScript" to PeachNav.Import,431 "CSV" to PeachNav.Import,432 "Codex" to PeachNav.CodexRestore,433 )434 Column(horizontalAlignment = Alignment.CenterHorizontally) {435 Text("restore instead", color = peachColors.T1, style = PeachType.labelMono)436 Spacer(Modifier.height(10.dp))437 438 439 440 441 442 FlowRow(443 horizontalArrangement = Arrangement.spacedBy(6.dp, Alignment.CenterHorizontally),444 verticalArrangement = Arrangement.spacedBy(6.dp),445 ) {446 tabs.forEach { (label, dest) ->447 Box(448 modifier = Modifier449 .border(1.dp, peachColors.T3, RoundedCornerShape(8.dp))450 .clickable { onNavigate(dest) }451 .padding(horizontal = 10.dp, vertical = 8.dp),452 ) {453 Text(454 label,455 color = peachColors.T1,456 style = PeachType.sortPill,457 maxLines = 1,458 softWrap = false,459 )460 }461 }462 }463 }464}