SPB Git

Repository Public

WidgetLock.swift 2.0 KB 42 lines swift

1// SP Notes iOS2// File: WidgetShared/WidgetLock.swift3// Description: The app-lock setting as the widget extension can read it. The lock itself lives in the app (`Core/AppLock.swift`,4//              standard defaults since 1.0); its "enabled" flag is mirrored into the App Group defaults so that widgets, their5//              note picker and the interactive intents show nothing private while the lock is on.6//7// Author: Simon-Pierre Boucher8// Contact: contact@spboucher.ai9import Foundation1011/// Reads and writes the mirrored flag. A value type naming its defaults suite, so tests inject a throw-away suite.12struct WidgetLock: Sendable, Equatable {13    /// Same key as the app's own defaults (1.0).14    static let enabledKey = "lock.enabled"15    /// The App Group suite; nil = no mirror (the flag then reads false).16    var suiteName: String? = AppGroup.identifier1718    static let shared = WidgetLock()1920    private var defaults: UserDefaults? { suiteName.flatMap { UserDefaults(suiteName: $0) } }2122    /// True when the owner turned the app lock on: every widget then shows its redacted state.23    var isEnabled: Bool { defaults?.bool(forKey: Self.enabledKey) ?? false }2425    /// Writes the mirror. Returns true when the stored value changed (the widgets must then be reloaded).26    @discardableResult27    func set(_ enabled: Bool) -> Bool {28        guard let defaults else { return false }29        let known = defaults.object(forKey: Self.enabledKey) as? Bool30        guard known != enabled else { return false }31        defaults.set(enabled, forKey: Self.enabledKey)32        // An upgrade from 1.0 writes `false` over "nothing": nothing changes on screen.33        return (known ?? false) != enabled34    }3536    /// True in the widget and share extensions (their bundle is an `.appex`).37    static var isExtensionProcess: Bool { Bundle.main.bundlePath.hasSuffix(".appex") }3839    /// The message of every redacted widget.40    static let message: LocalizedStringResource = "Verrouillé — ouvre SP Notes"41}42
Files main
Menu

Theme