mirror of
https://github.com/Novaloop-AG/element-x-ios.git
synced 2026-11-02 20:39:24 +00:00
Show the lock screen placeholder on willResignActive. (#2086)
This commit is contained in:
@@ -111,8 +111,8 @@ class AppLockService: AppLockServiceProtocol {
|
||||
timer.applicationDidEnterBackground()
|
||||
}
|
||||
|
||||
func computeNeedsUnlock(willEnterForegroundAt date: Date) -> Bool {
|
||||
timer.computeLockState(willEnterForegroundAt: date)
|
||||
func computeNeedsUnlock(didBecomeActiveAt date: Date) -> Bool {
|
||||
timer.computeLockState(didBecomeActiveAt: date)
|
||||
}
|
||||
|
||||
func unlock(with pinCode: String) -> Bool {
|
||||
|
||||
@@ -59,7 +59,7 @@ protocol AppLockServiceProtocol: AnyObject {
|
||||
/// Informs the service that the app has entered the background.
|
||||
func applicationDidEnterBackground()
|
||||
/// Decides whether the app should be unlocked with a PIN code/biometrics on foregrounding.
|
||||
func computeNeedsUnlock(willEnterForegroundAt date: Date) -> Bool
|
||||
func computeNeedsUnlock(didBecomeActiveAt date: Date) -> Bool
|
||||
|
||||
/// Attempt to unlock the app with the supplied PIN code.
|
||||
func unlock(with pinCode: String) -> Bool
|
||||
|
||||
@@ -21,13 +21,18 @@ class AppLockTimer {
|
||||
/// The amount of time the app should remain unlocked for whilst backgrounded.
|
||||
let gracePeriod: TimeInterval
|
||||
|
||||
/// Whether the timer considers the app to be locked or not.
|
||||
/// Whether the timer considers the app to be locked or not. Always starts with a locked app.
|
||||
///
|
||||
/// Internally this value may be incorrect, always call `needsUnlock` to get the correct value.
|
||||
private var isLocked = true
|
||||
/// The date when the app was last backgrounded whilst in an unlocked state.
|
||||
private var lastUnlockedBackground: Date?
|
||||
|
||||
/// Whether or not the app has moved to the background.
|
||||
///
|
||||
/// This allows us to distinguish between `willResignActive` and `didEnterBackground`.
|
||||
private var isInBackground = false
|
||||
|
||||
/// Creates a new timer.
|
||||
/// - Parameter gracePeriod: The amount of time the app should remain unlocked for whilst backgrounded.
|
||||
init(gracePeriod: TimeInterval) {
|
||||
@@ -36,13 +41,18 @@ class AppLockTimer {
|
||||
|
||||
/// Signals to the timer to track how long the app will be backgrounded for.
|
||||
func applicationDidEnterBackground(date: Date = .now) {
|
||||
isInBackground = true
|
||||
// Only update the last background date if the app is currently unlocked.
|
||||
guard !isLocked else { return }
|
||||
lastUnlockedBackground = date
|
||||
}
|
||||
|
||||
/// Asks the timer to recompute the lock state on foregrounding.
|
||||
func computeLockState(willEnterForegroundAt date: Date) -> Bool {
|
||||
/// Asks the timer to recompute the lock state when active. If ``applicationDidEnterBackground`` hasn't been
|
||||
/// called since the last computation, then this method won't recompute it, instead directly returning the previous state.
|
||||
func computeLockState(didBecomeActiveAt date: Date) -> Bool {
|
||||
guard isInBackground else { return isLocked }
|
||||
isInBackground = false
|
||||
|
||||
guard !isLocked, let lastUnlockedBackground else { return true }
|
||||
|
||||
isLocked = date.timeIntervalSince(lastUnlockedBackground) >= gracePeriod
|
||||
|
||||
Reference in New Issue
Block a user