Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Forcelock the app when we lock the device #40

Merged
merged 7 commits into from
Oct 2, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 13 additions & 2 deletions Sources/InfomaniakCoreCommonUI/Security/AppLockHelper.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,20 +17,26 @@
*/

import LocalAuthentication
import UIKit

public final class AppLockHelper {
public static let lockAfterOneMinute: TimeInterval = 60

private var deviceHasBeenLocked = false
private let intervalToLockApp: TimeInterval
private var timeSinceAppEnteredBackground = TimeInterval.zero

public var isAppLocked: Bool {
let shouldBeLocked = timeSinceAppEnteredBackground + intervalToLockApp < Date().timeIntervalSince1970
return isAvailable() && shouldBeLocked
let timeHasExpired = timeSinceAppEnteredBackground + intervalToLockApp < Date().timeIntervalSince1970
return isAvailable() && (timeHasExpired || deviceHasBeenLocked)
}

public init(intervalToLockApp: TimeInterval = AppLockHelper.lockAfterOneMinute) {
self.intervalToLockApp = intervalToLockApp
NotificationCenter.default.addObserver(self,
selector: #selector(deviceDidLock),
name: UIApplication.protectedDataWillBecomeUnavailableNotification,
object: nil)
}

public func isAvailable(_ context: LAContext? = nil) -> Bool {
Expand All @@ -40,6 +46,7 @@ public final class AppLockHelper {

public func setTime() {
timeSinceAppEnteredBackground = Date().timeIntervalSince1970
deviceHasBeenLocked = false
}

public func evaluatePolicy(reason: String) async throws -> Bool {
Expand All @@ -50,4 +57,8 @@ public final class AppLockHelper {

return try await context.evaluatePolicy(.deviceOwnerAuthentication, localizedReason: reason)
}

@objc private func deviceDidLock() {
deviceHasBeenLocked = true
}
}
Loading