package eu.friedcutlet.yokan.ui.screens.applock import androidx.compose.runtime.Composable import androidx.compose.runtime.remember import androidx.lifecycle.Lifecycle import androidx.lifecycle.compose.LifecycleEventEffect import eu.friedcutlet.yokan.data.SettingsStore import eu.friedcutlet.yokan.database.appLockDue import eu.friedcutlet.yokan.services.applock.AppLockController import eu.friedcutlet.yokan.util.nowMillis /** * Leaving the foreground locks the app when the Security setting is on. Timeout 0 locks as soon as * the app leaves the foreground; longer timeouts are measured from ON_STOP and checked on ON_START. */ @Composable fun AppLockLifecycleEffect(settings: SettingsStore, appLock: AppLockController) { val backgroundedAt = remember { longArrayOf(-1L) } LifecycleEventEffect(Lifecycle.Event.ON_STOP) { val current = settings.db.value if (!current.appLockEnabled) return@LifecycleEventEffect if (current.features.privacy.appLockTimeoutMinutes == 0) appLock.reset() else backgroundedAt[0] = nowMillis() } LifecycleEventEffect(Lifecycle.Event.ON_START) { val since = backgroundedAt[0].also { backgroundedAt[0] = -1L } val current = settings.db.value if (since >= 0 && current.appLockEnabled && appLockDue(current.features.privacy.appLockTimeoutMinutes, since, nowMillis()) ) appLock.reset() } }