Repository Public
1// User preferences of this device (UserDefaults; nothing secret). Keys start with "spv.".2import Foundation3import Observation4import SharedVaultKit56@MainActor7@Observable8final class Preferences {9 @ObservationIgnored private let d: UserDefaults1011 /// Seconds without activity before the vault locks itself. 0 = never (not recommended).12 var autoLockSeconds: Int { didSet { d.set(autoLockSeconds, forKey: "spv.autoLockSeconds") } }13 /// macOS: lock when the Mac sleeps, the screen locks or the screensaver starts.14 var lockOnSleep: Bool { didSet { d.set(lockOnSleep, forKey: "spv.lockOnSleep") } }15 /// iOS: seconds in the background before the vault locks. 0 = immediately.16 var lockInBackgroundSeconds: Int { didSet { d.set(lockInBackgroundSeconds, forKey: "spv.lockInBackgroundSeconds") } }17 /// Seconds before a copied secret is removed from the clipboard. 0 = never.18 var clipboardClearSeconds: Int { didSet { d.set(clipboardClearSeconds, forKey: "spv.clipboardClearSeconds") } }19 /// macOS: exclude the windows from screenshots, screen recording and screen sharing; blur when the app is not active.20 var hideFromCapture: Bool { didSet { d.set(hideFromCapture, forKey: "spv.hideFromCapture") } }21 /// Hand site names and usernames (never secrets) to the system so that it can suggest them in the keyboard.22 var autofillSuggestions: Bool { didSet { d.set(autofillSuggestions, forKey: "spv.autofillSuggestions") } }23 /// macOS, opt-in: index item titles (only titles) in Spotlight.24 var spotlightTitles: Bool { didSet { d.set(spotlightTitles, forKey: "spv.spotlightTitles") } }25 /// Content-free notifications for pairing requests and agent approvals.26 var notifications: Bool { didSet { d.set(notifications, forKey: "spv.notifications") } }27 /// Opt-in breach check (k-anonymity) in the Security Center.28 var breachCheck: Bool { didSet { d.set(breachCheck, forKey: "spv.breachCheck") } }2930 var generatorPassphrase: Bool { didSet { d.set(generatorPassphrase, forKey: "spv.gen.passphrase") } }31 var passwordOptions: PasswordOptions { didSet { save(passwordOptions, "spv.gen.password") } }32 var passphraseOptions: PassphraseOptions { didSet { save(passphraseOptions, "spv.gen.words") } }3334 static let autoLockChoices: [(label: String, seconds: Int)] = [("1 minute", 60), ("5 minutes", 300), ("15 minutes", 900), ("1 hour", 3600), ("Never", 0)]35 static let backgroundChoices: [(label: String, seconds: Int)] = [("Immediately", 0), ("After 30 seconds", 30), ("After 1 minute", 60), ("After 5 minutes", 300)]36 static let clipboardChoices: [(label: String, seconds: Int)] = [("10 seconds", 10), ("30 seconds", 30), ("1 minute", 60), ("2 minutes", 120), ("Never", 0)]3738 init(defaults: UserDefaults = .standard) {39 d = defaults40 func int(_ k: String, _ def: Int) -> Int { defaults.object(forKey: k) == nil ? def : defaults.integer(forKey: k) }41 func bool(_ k: String, _ def: Bool) -> Bool { defaults.object(forKey: k) == nil ? def : defaults.bool(forKey: k) }42 func load<T: Decodable>(_ k: String, _ def: T) -> T { defaults.data(forKey: k).flatMap { try? JSONDecoder().decode(T.self, from: $0) } ?? def }43 autoLockSeconds = int("spv.autoLockSeconds", 300)44 lockOnSleep = bool("spv.lockOnSleep", true)45 lockInBackgroundSeconds = int("spv.lockInBackgroundSeconds", 60)46 clipboardClearSeconds = int("spv.clipboardClearSeconds", 30)47 hideFromCapture = bool("spv.hideFromCapture", true)48 autofillSuggestions = bool("spv.autofillSuggestions", true)49 spotlightTitles = bool("spv.spotlightTitles", false)50 notifications = bool("spv.notifications", true)51 breachCheck = bool("spv.breachCheck", false)52 generatorPassphrase = bool("spv.gen.passphrase", false)53 passwordOptions = load("spv.gen.password", PasswordOptions.default)54 passphraseOptions = load("spv.gen.words", PassphraseOptions.default)55 }5657 private func save<T: Encodable>(_ v: T, _ key: String) { if let data = try? JSONEncoder().encode(v) { d.set(data, forKey: key) } }58}59