Repository Public
1// =============================================================================2// SP BOUCHER | SOFTWARE & RESEARCH3// ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING4// =============================================================================5//6// Project : QYVORA7// File : Sources/QyvoraWallet/AutoLockRule.swift8// Module : Qyvora Wallet9// Repository : https://git.spboucher.ai/qyvora10// Project Website : Not specified11// Created : 2026-12-0412// Last Modified : 2026-12-0413//14// Author : Simon-Pierre Boucher15// Position : Professor of Finance16// Institution : Université du Québec en Outaouais (UQO)17// Organization : SP Boucher18// Website : https://spboucher.ai19// Git : https://git.spboucher.ai20//21// Preferred Contact: contact@spboucher.ai22// Development : dev@spboucher.ai23// Support : support@spboucher.ai24// Security/Admin : admin@spboucher.ai25//26// Copyright (c) 202627// Simon-Pierre Boucher28// All rights reserved where applicable. This file is governed by the29// applicable project license, copyright notices and relevant law.30// Existing third-party rights and notices remain unaffected.31//32// License : None specified (no license file in this repository)33//34// Do not embed credentials, private keys, access tokens,35// certificates or confidential personal information.36//37// =============================================================================38// END OF SP BOUCHER SOURCE HEADER39// =============================================================================4041import Foundation42import QyvoraVault4344/// What the application does about the auto-lock at each event of its life cycle (QYV-APP-003).45///46/// The decisions live here, outside the views, so that they are tested with the package: the root view only maps47/// SwiftUI's scene phases to these events and carries out the answer.48///49/// - Leaving for the background locks at once under "immediately"; under a delay the vault is locked there only when50/// the delay has already elapsed (iOS suspends the process, no timer runs in the background).51/// - Coming back from the background always applies the policy before any content is shown: the time spent away52/// counts, since the last activity is not refreshed while the application is suspended.53/// - While the application is active and the vault unlocked, a timer applies the policy every `timerInterval`.54/// - Becoming merely inactive (the system's Face ID sheet, the app switcher) does nothing: locking there would undo55/// the unlock that the sheet performs.56public enum AutoLockRule {57 public enum Event: Sendable, Equatable, CaseIterable {58 /// The scene went to the background.59 case enteredBackground60 /// The scene is active again after having been in the background.61 case returnedFromBackground62 /// The repeating timer fired while the scene is active.63 case timerFired64 /// The scene became inactive without going to the background.65 case becameInactive66 }6768 public enum Action: Sendable, Equatable {69 case none70 /// Lock now, whatever the time elapsed.71 case lock72 /// Ask the service to lock if the policy says so (`WalletService.autoLockIfNeeded`).73 case applyPolicy74 }7576 /// How often the policy is applied while the application is active.77 public static let timerInterval: TimeInterval = 157879 public static func action(for event: Event, policy: AutoLockPolicy, isUnlocked: Bool) -> Action {80 guard isUnlocked else { return .none }81 switch event {82 case .becameInactive:83 return .none84 case .enteredBackground:85 return policy == .immediately ? .lock : .applyPolicy86 case .returnedFromBackground, .timerFired:87 return .applyPolicy88 }89 }9091 /// The timer runs for an unlocked vault while the scene is active. Under "immediately" it is not needed: the92 /// vault locks when the application leaves the foreground.93 public static func timerRuns(policy: AutoLockPolicy, isUnlocked: Bool, sceneIsActive: Bool) -> Bool {94 guard isUnlocked, sceneIsActive else { return false }95 if case .immediately = policy { return false }96 return true97 }9899 /// The content stays covered after a return from the background until the policy has been applied, so that an100 /// expired session never shows a frame of the wallet.101 public static func coversContent(sceneIsActive: Bool, applyingPolicy: Bool) -> Bool {102 !sceneIsActive || applyingPolicy103 }104}105