AppLockLifecycleEffect.kt
composeApp/src/commonMain/kotlin/eu/friedcutlet/yokan/ui/screens/applock/AppLockLifecycleEffect.kt
1package eu.friedcutlet.yokan.ui.screens.applock23import androidx.compose.runtime.Composable4import androidx.compose.runtime.remember5import androidx.lifecycle.Lifecycle6import androidx.lifecycle.compose.LifecycleEventEffect7import eu.friedcutlet.yokan.data.SettingsStore8import eu.friedcutlet.yokan.database.appLockDue9import eu.friedcutlet.yokan.services.applock.AppLockController10import eu.friedcutlet.yokan.util.nowMillis1112/**13 * Leaving the foreground locks the app when the Security setting is on. Timeout 0 locks as soon as14 * the app leaves the foreground; longer timeouts are measured from ON_STOP and checked on ON_START.15 */16@Composable17fun AppLockLifecycleEffect(settings: SettingsStore, appLock: AppLockController) {18 val backgroundedAt = remember { longArrayOf(-1L) }19 LifecycleEventEffect(Lifecycle.Event.ON_STOP) {20 val current = settings.db.value21 if (!current.appLockEnabled) return@LifecycleEventEffect22 if (current.features.privacy.appLockTimeoutMinutes == 0) appLock.reset() else backgroundedAt[0] = nowMillis()23 }24 LifecycleEventEffect(Lifecycle.Event.ON_START) {25 val since = backgroundedAt[0].also { backgroundedAt[0] = -1L }26 val current = settings.db.value27 if (since >= 0 && current.appLockEnabled &&28 appLockDue(current.features.privacy.appLockTimeoutMinutes, since, nowMillis())29 ) appLock.reset()30 }31}32