From 86449d9ff302bddd97725f7e87e020860d04e293 Mon Sep 17 00:00:00 2001 From: Baptiste Griva Date: Tue, 24 Sep 2024 10:42:07 +0200 Subject: [PATCH 1/7] feat: Lock app when device is locked --- .../Security/AppLockHelper.swift | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/Sources/InfomaniakCoreCommonUI/Security/AppLockHelper.swift b/Sources/InfomaniakCoreCommonUI/Security/AppLockHelper.swift index b228366..2bd248d 100644 --- a/Sources/InfomaniakCoreCommonUI/Security/AppLockHelper.swift +++ b/Sources/InfomaniakCoreCommonUI/Security/AppLockHelper.swift @@ -20,17 +20,22 @@ import LocalAuthentication public final class AppLockHelper { public static let lockAfterOneMinute: TimeInterval = 60 + public var deviceHasBeenLocked: Bool = false private let intervalToLockApp: TimeInterval private var timeSinceAppEnteredBackground = TimeInterval.zero public var isAppLocked: Bool { let shouldBeLocked = timeSinceAppEnteredBackground + intervalToLockApp < Date().timeIntervalSince1970 - return isAvailable() && shouldBeLocked + return isAvailable() && (shouldBeLocked || deviceHasBeenLocked) } public init(intervalToLockApp: TimeInterval = AppLockHelper.lockAfterOneMinute) { self.intervalToLockApp = intervalToLockApp + NotificationCenter.default.addObserver(self, + selector: #selector(updateDeviceStatus()), + name: NSNotification.Name.uiApplication.NSApplicationProtectedDataWillBecomeUnavailable, + object: nil) } public func isAvailable(_ context: LAContext? = nil) -> Bool { @@ -50,4 +55,10 @@ public final class AppLockHelper { return try await context.evaluatePolicy(.deviceOwnerAuthentication, localizedReason: reason) } + + @objc func updateDeviceStatus(){ + deviceHasBeenLocked = true + } + + } From 319408e3ccc85808e6ff71c96d13b1a0f3090332 Mon Sep 17 00:00:00 2001 From: Baptiste Griva Date: Tue, 24 Sep 2024 12:58:05 +0200 Subject: [PATCH 2/7] feat: Lock app when screen is locked --- Sources/InfomaniakCoreCommonUI/Security/AppLockHelper.swift | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/Sources/InfomaniakCoreCommonUI/Security/AppLockHelper.swift b/Sources/InfomaniakCoreCommonUI/Security/AppLockHelper.swift index 2bd248d..69a15af 100644 --- a/Sources/InfomaniakCoreCommonUI/Security/AppLockHelper.swift +++ b/Sources/InfomaniakCoreCommonUI/Security/AppLockHelper.swift @@ -17,6 +17,7 @@ */ import LocalAuthentication +import UIKit public final class AppLockHelper { public static let lockAfterOneMinute: TimeInterval = 60 @@ -33,8 +34,8 @@ public final class AppLockHelper { public init(intervalToLockApp: TimeInterval = AppLockHelper.lockAfterOneMinute) { self.intervalToLockApp = intervalToLockApp NotificationCenter.default.addObserver(self, - selector: #selector(updateDeviceStatus()), - name: NSNotification.Name.uiApplication.NSApplicationProtectedDataWillBecomeUnavailable, + selector: #selector(updateDeviceStatus), + name: UIApplication.protectedDataWillBecomeUnavailableNotification, object: nil) } From 254b15213f1c7664f0c7ce0da9ec24aeb8b3ab7d Mon Sep 17 00:00:00 2001 From: Baptiste Griva Date: Tue, 24 Sep 2024 13:36:33 +0200 Subject: [PATCH 3/7] fix: Review lock feature --- .../Security/AppLockHelper.swift | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/Sources/InfomaniakCoreCommonUI/Security/AppLockHelper.swift b/Sources/InfomaniakCoreCommonUI/Security/AppLockHelper.swift index 69a15af..348713d 100644 --- a/Sources/InfomaniakCoreCommonUI/Security/AppLockHelper.swift +++ b/Sources/InfomaniakCoreCommonUI/Security/AppLockHelper.swift @@ -21,22 +21,27 @@ import UIKit public final class AppLockHelper { public static let lockAfterOneMinute: TimeInterval = 60 - public var deviceHasBeenLocked: Bool = false + 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 || deviceHasBeenLocked) + let expiredTime = timeSinceAppEnteredBackground + intervalToLockApp < Date().timeIntervalSince1970 + let shouldBeLocked = expiredTime || deviceHasBeenLocked + return isAvailable() && shouldBeLocked } public init(intervalToLockApp: TimeInterval = AppLockHelper.lockAfterOneMinute) { self.intervalToLockApp = intervalToLockApp NotificationCenter.default.addObserver(self, - selector: #selector(updateDeviceStatus), + selector: #selector(deviceDidLock), name: UIApplication.protectedDataWillBecomeUnavailableNotification, object: nil) + NotificationCenter.default.addObserver(self, + selector: #selector(deviceDidUnlock), + name: UIApplication.protectedDataDidBecomeAvailableNotification, + object: nil) } public func isAvailable(_ context: LAContext? = nil) -> Bool { @@ -57,9 +62,13 @@ public final class AppLockHelper { return try await context.evaluatePolicy(.deviceOwnerAuthentication, localizedReason: reason) } - @objc func updateDeviceStatus(){ + @objc private func deviceDidLock(){ deviceHasBeenLocked = true } + @objc private func deviceDidUnlock(){ + deviceHasBeenLocked = false + } + } From 0c48f492d069b06b9db40dd62c918f7c924eaee0 Mon Sep 17 00:00:00 2001 From: Baptiste Griva Date: Tue, 24 Sep 2024 13:45:34 +0200 Subject: [PATCH 4/7] fix: Review lock feature --- .../Security/AppLockHelper.swift | 17 +++++++---------- 1 file changed, 7 insertions(+), 10 deletions(-) diff --git a/Sources/InfomaniakCoreCommonUI/Security/AppLockHelper.swift b/Sources/InfomaniakCoreCommonUI/Security/AppLockHelper.swift index 348713d..9221687 100644 --- a/Sources/InfomaniakCoreCommonUI/Security/AppLockHelper.swift +++ b/Sources/InfomaniakCoreCommonUI/Security/AppLockHelper.swift @@ -21,15 +21,14 @@ import UIKit public final class AppLockHelper { public static let lockAfterOneMinute: TimeInterval = 60 - private var deviceHasBeenLocked = false + private var deviceHasBeenLocked = false private let intervalToLockApp: TimeInterval private var timeSinceAppEnteredBackground = TimeInterval.zero public var isAppLocked: Bool { - let expiredTime = timeSinceAppEnteredBackground + intervalToLockApp < Date().timeIntervalSince1970 - let shouldBeLocked = expiredTime || deviceHasBeenLocked - return isAvailable() && shouldBeLocked + let timeHasExpired = timeSinceAppEnteredBackground + intervalToLockApp < Date().timeIntervalSince1970 + return isAvailable() && (expiredTime || deviceHasBeenLocked) } public init(intervalToLockApp: TimeInterval = AppLockHelper.lockAfterOneMinute) { @@ -61,14 +60,12 @@ public final class AppLockHelper { return try await context.evaluatePolicy(.deviceOwnerAuthentication, localizedReason: reason) } - - @objc private func deviceDidLock(){ + + @objc private func deviceDidLock() { deviceHasBeenLocked = true } - - @objc private func deviceDidUnlock(){ + + @objc private func deviceDidUnlock() { deviceHasBeenLocked = false } - - } From 18c64483273481699104de0d84eb8ce0ff324ae2 Mon Sep 17 00:00:00 2001 From: Baptiste Griva Date: Tue, 24 Sep 2024 13:51:02 +0200 Subject: [PATCH 5/7] fix: Variable name issue --- Sources/InfomaniakCoreCommonUI/Security/AppLockHelper.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Sources/InfomaniakCoreCommonUI/Security/AppLockHelper.swift b/Sources/InfomaniakCoreCommonUI/Security/AppLockHelper.swift index 9221687..dc49cf3 100644 --- a/Sources/InfomaniakCoreCommonUI/Security/AppLockHelper.swift +++ b/Sources/InfomaniakCoreCommonUI/Security/AppLockHelper.swift @@ -28,7 +28,7 @@ public final class AppLockHelper { public var isAppLocked: Bool { let timeHasExpired = timeSinceAppEnteredBackground + intervalToLockApp < Date().timeIntervalSince1970 - return isAvailable() && (expiredTime || deviceHasBeenLocked) + return isAvailable() && (timeHasExpired || deviceHasBeenLocked) } public init(intervalToLockApp: TimeInterval = AppLockHelper.lockAfterOneMinute) { From e21bc4aad49cf614a0a27e366a88081d15fefb1d Mon Sep 17 00:00:00 2001 From: Baptiste Griva Date: Mon, 30 Sep 2024 13:45:32 +0200 Subject: [PATCH 6/7] fix: Lock was working only first time --- .../InfomaniakCoreCommonUI/Security/AppLockHelper.swift | 8 -------- 1 file changed, 8 deletions(-) diff --git a/Sources/InfomaniakCoreCommonUI/Security/AppLockHelper.swift b/Sources/InfomaniakCoreCommonUI/Security/AppLockHelper.swift index dc49cf3..f0c7933 100644 --- a/Sources/InfomaniakCoreCommonUI/Security/AppLockHelper.swift +++ b/Sources/InfomaniakCoreCommonUI/Security/AppLockHelper.swift @@ -37,10 +37,6 @@ public final class AppLockHelper { selector: #selector(deviceDidLock), name: UIApplication.protectedDataWillBecomeUnavailableNotification, object: nil) - NotificationCenter.default.addObserver(self, - selector: #selector(deviceDidUnlock), - name: UIApplication.protectedDataDidBecomeAvailableNotification, - object: nil) } public func isAvailable(_ context: LAContext? = nil) -> Bool { @@ -64,8 +60,4 @@ public final class AppLockHelper { @objc private func deviceDidLock() { deviceHasBeenLocked = true } - - @objc private func deviceDidUnlock() { - deviceHasBeenLocked = false - } } From 6b1241044beb6ad1cfcb9839fc41257a0f8b4820 Mon Sep 17 00:00:00 2001 From: Baptiste Griva Date: Tue, 1 Oct 2024 13:56:15 +0200 Subject: [PATCH 7/7] feat: Don't lock when go background --- Sources/InfomaniakCoreCommonUI/Security/AppLockHelper.swift | 1 + 1 file changed, 1 insertion(+) diff --git a/Sources/InfomaniakCoreCommonUI/Security/AppLockHelper.swift b/Sources/InfomaniakCoreCommonUI/Security/AppLockHelper.swift index f0c7933..cb2c0c5 100644 --- a/Sources/InfomaniakCoreCommonUI/Security/AppLockHelper.swift +++ b/Sources/InfomaniakCoreCommonUI/Security/AppLockHelper.swift @@ -46,6 +46,7 @@ public final class AppLockHelper { public func setTime() { timeSinceAppEnteredBackground = Date().timeIntervalSince1970 + deviceHasBeenLocked = false } public func evaluatePolicy(reason: String) async throws -> Bool {