Cotton code
99 lines · 4.2 KB · SwiftRaw
// The OS conversation `/settings#notifications` needs, which is 4j's plus one.//// Only the half the settings screen uses. `read` and `ask` are NOT redeclared// here: they are// `CottonKit.NotificationAuthority`'s, and this refines that protocol rather// than standing beside it, so there is exactly one answer in the app to "what// does iOS think?" and the settings screen cannot drift from the reminder// pass's reading of it.//// The one addition is `openSystemSettings()`. It belongs to this screen alone:// the way out of `denied` is the iOS Settings app, and the *only* place the app// offers that door is the blocked-permission card here. 4j never needed it —// nothing it does can be fixed by sending a person to Settings — which is why// the method is added by the screen that has the button rather than pushed down// into a protocol every reminder caller would then have to implement.import Foundation#if canImport(UIKit)    import UIKit#endifimport CottonKit/// `NotificationAuthority`, plus the door out of `denied`.public protocol NotificationSettingsAuthority: NotificationAuthority {    /// Open the app's page in the iOS Settings app. Nothing to report if it    /// refuses — the empty catch, kept.    @MainActor func openSystemSettings()}/// The real one. `read` / `ask` / `registerForRemoteNotifications` are 4j's/// `SystemNotificationAuthority` verbatim, held rather than reimplemented so a/// change to how the app reads iOS's answer lands in ONE place.public struct SystemNotificationSettingsAuthority: NotificationSettingsAuthority {    private let base = SystemNotificationAuthority()    public init() {}    public func read() async -> NotificationPermission { await base.read() }    public func ask() async -> NotificationPermission { await base.ask() }    public func registerForRemoteNotifications() async { await base.registerForRemoteNotifications() }    @MainActor public func openSystemSettings() {        #if canImport(UIKit)            guard let url = URL(string: UIApplication.openSettingsURLString) else { return }            UIApplication.shared.open(url)        #endif    }}/// The double. Answers what a test scripts, and counts the doors it was asked/// to open — `@unchecked Sendable` with a lock because a test drives it from/// the MainActor while the model reads it from a task.public final class ScriptedNotificationSettingsAuthority: NotificationSettingsAuthority, @unchecked Sendable {    private let lock = NSLock()    private var answers: [NotificationPermission]    private var asked = 0    private var opened = 0    /// The broadcast this double fires on `ask`, mirroring    /// `SystemNotificationSettingsAuthority`. Optional because most tests do    /// not care; passed when the test is ABOUT the broadcast, so "once per ask"    /// stays assertable without a real `UNUserNotificationCenter`.    private let changes: NotificationPermissionChanges?    /// `answers` is consumed one per `read`/`ask`; the last one repeats, so a    /// test that only cares about the steady state passes one.    public init(_ answers: [NotificationPermission], changes: NotificationPermissionChanges? = nil) {        self.answers = answers        self.changes = changes    }    private func next() -> NotificationPermission {        lock.withLock {            guard let head = answers.first else { return .undetermined }            if answers.count > 1 { answers.removeFirst() }            return head        }    }    public func read() async -> NotificationPermission { next() }    public func ask() async -> NotificationPermission {        lock.withLock { asked += 1 }        // Same shape as the real one: once per ask, whatever the answer.        defer { changes?.notify() }        return next()    }    public func registerForRemoteNotifications() async {}    @MainActor public func openSystemSettings() {        lock.withLock { opened += 1 }    }    /// How many times the prompt was spent. iOS grants one per install, so a    /// test that this screen never spends a second is worth having.    public var timesAsked: Int { lock.withLock { asked } }    public var timesOpenedSettings: Int { lock.withLock { opened } }}