AppLockOverlay.kt
composeApp/src/commonMain/kotlin/eu/friedcutlet/yokan/ui/screens/applock/AppLockOverlay.kt
1package eu.friedcutlet.yokan.ui.screens.applock23import androidx.compose.foundation.background4import androidx.compose.foundation.layout.Arrangement5import androidx.compose.foundation.layout.Box6import androidx.compose.foundation.layout.Column7import androidx.compose.foundation.layout.Spacer8import androidx.compose.foundation.layout.fillMaxSize9import androidx.compose.foundation.layout.padding10import androidx.compose.foundation.layout.size11import androidx.compose.foundation.layout.width12import androidx.compose.foundation.shape.RoundedCornerShape13import androidx.compose.material.icons.Icons14import androidx.compose.material.icons.filled.Lock15import androidx.compose.material3.Button16import androidx.compose.material3.Icon17import androidx.compose.material3.MaterialTheme18import androidx.compose.material3.Text19import androidx.compose.runtime.Composable20import androidx.compose.runtime.LaunchedEffect21import androidx.compose.runtime.getValue22import androidx.compose.runtime.mutableStateOf23import androidx.compose.runtime.remember24import androidx.compose.runtime.setValue25import androidx.compose.ui.Alignment26import androidx.compose.ui.Modifier27import androidx.compose.ui.text.font.FontWeight28import androidx.compose.ui.text.style.TextAlign29import androidx.compose.ui.unit.dp3031/**32 * Full-screen lock surface the app root stacks above everything while locked33 * (Mangayomi's AppLockScreen over the router). [onUnlock] starts an authentication34 * and reports whether it succeeded.35 */36@Composable37fun AppLockOverlay(38 unlocked: Boolean,39 supported: Boolean,40 onUnlock: (onResult: (Boolean) -> Unit) -> Unit,41) {42 var authenticating by remember { mutableStateOf(false) }43 var failed by remember { mutableStateOf(false) }44 fun unlock() {45 authenticating = true46 failed = false47 onUnlock { ok ->48 authenticating = false49 failed = !ok50 }51 }5253 LaunchedEffect(Unit) {54 if (!unlocked && !authenticating) unlock()55 }5657 // No AppBar — full-screen overlay. Back press is intentionally ignored58 // (PopScope(canPop:false) parity): the user must authenticate via the Unlock59 // button. We do not register a BackHandler, so the surrounding host can60 // decide whether back closes the screen; the screen itself takes no action.61 Box(62 modifier = Modifier63 .fillMaxSize()64 .background(MaterialTheme.colorScheme.surface),65 contentAlignment = Alignment.Center,66 ) {67 Column(68 modifier = Modifier.padding(32.dp),69 horizontalAlignment = Alignment.CenterHorizontally,70 verticalArrangement = Arrangement.spacedBy(20.dp),71 ) {72 Box(73 modifier = Modifier74 .size(96.dp)75 .background(76 MaterialTheme.colorScheme.primaryContainer.copy(alpha = 0.6f),77 RoundedCornerShape(24.dp),78 ),79 contentAlignment = Alignment.Center,80 ) {81 Icon(82 Icons.Filled.Lock,83 contentDescription = null,84 tint = MaterialTheme.colorScheme.primary,85 modifier = Modifier.size(56.dp),86 )87 }88 Text(89 if (unlocked) "Unlocked" else "App is locked",90 style = MaterialTheme.typography.headlineSmall,91 fontWeight = FontWeight.SemiBold,92 )93 Text(94 when {95 unlocked -> "You can now use the app."96 !supported -> "Biometric authentication is unavailable on this device."97 failed -> "Authentication failed. Tap Unlock to try again."98 authenticating -> "Authenticating…"99 else -> "Authenticate to unlock and continue using the application."100 },101 color = MaterialTheme.colorScheme.onSurfaceVariant,102 style = MaterialTheme.typography.bodyMedium,103 textAlign = TextAlign.Center,104 )105 if (!unlocked) {106 Button(107 onClick = { if (!authenticating) unlock() },108 enabled = !authenticating,109 ) {110 Icon(Icons.Filled.Lock, contentDescription = null)111 Spacer(Modifier.width(6.dp))112 Text(if (authenticating) "Authenticating…" else "Unlock")113 }114 }115 }116 }117}118