Cotton code
101 lines · 4.7 KB · SwiftRaw
// A refusal, said — and the one environment value a screen or a test swaps the// OS out through.//// The mechanism is `CottonKit/Capabilities/`: what may be asked, what the// answer was, and the one file allowed to ask. This is the half that has a// screen — the sentence, the button, and the way a preview or a test hands a// different authority to everything under it.//// **Never a retry.** A denied capability gets a sentence and, when Settings can// actually fix it, a button to Settings. It never gets a spinner and it never// gets a second prompt: iOS grants one per install, and an app that keeps// asking is an app that reads as broken.import CottonKitimport Foundationimport SwiftUI#if canImport(UIKit)import UIKit#endif/// What to say about a capability that blocked, and whether anything can be/// done about it.public enum CapabilityRefusal {    /// The sentence a person is owed — `nil` for the one state that owes none.    ///    /// Total over both enums, so a new capability or a new state cannot ship    /// silent. `CapabilityGateTests` walks every pair.    public static func sentence(        _ capability: DeviceCapability, _ access: CapabilityAccess    ) -> String? {        let noun = Self.noun(capability)        switch access {        case .authorized: return nil        case .denied: return ShellLines.line("refusal", "capability.cotton_doesn_t_have", ["noun": "\(noun)"])        case .notDetermined: return ShellLines.line("refusal", "capability.cotton_needs_access_to", ["noun": "\(noun)"])        case .restricted: return ShellLines.line("refusal", "capability.access_to_isn_t", ["noun": "\(noun)"])        case .unavailable: return ShellLines.line("refusal", "capability.cotton_can_t_reach", ["noun": "\(noun)"])        }    }    /// The thing itself, lower case and article-carrying, because every    /// sentence above puts it mid-phrase: "access to your microphone", never    /// "access to Microphone". One noun per capability rather than the four    /// sentences written out per capability — the SHAPE of what has to be said    /// is the same every time, and a hand-written sentence per pair is how one    /// of them goes missing.    private static func noun(_ capability: DeviceCapability) -> String {        switch capability {        case .microphone: return ShellLines.line("refusal", "capability.your_microphone")        case .speech: return ShellLines.line("refusal", "capability.speech_recognition")        case .camera: return ShellLines.line("refusal", "capability.the_camera")        case .contacts: return ShellLines.line("refusal", "capability.your_contacts")        case .notifications: return "notifications"        case .photoLibrary, .photoLibraryAdd: return ShellLines.line("refusal", "capability.your_photo_library")        case .localNetwork: return ShellLines.line("refusal", "capability.your_mac_on_this")        case .appleHealth: return ShellLines.line("refusal", "capability.apple_health")        }    }    /// The same, for whatever a gate answered. `nil` when nothing blocked.    public static func sentence(for result: CapabilityGateResult) -> String? {        switch result {        case .ready: return nil        case .blocked(let capability, let access): return sentence(capability, access)        }    }    /// Is there a switch in Settings behind this refusal? Only `denied` has    /// one; a `restricted` capability has no row to turn on, and offering the    /// button anyway sends somebody looking for a switch that is not there.    public static func offersSettings(_ access: CapabilityAccess) -> Bool {        access.settingsWouldHelp    }    /// Cotton's own page in Settings.    ///    /// `UIApplication.openSettingsURLString` is documented to be a valid URL,    /// but it is still a string being parsed — so this answers an optional and    /// every caller has to say what it does with nothing, rather than a    /// force-unwrap in a path a person reaches by being refused.    public static var settingsURL: URL? {        URL(string: UIApplication.openSettingsURLString)    }}// MARK: - The authority, in the environment/// The default is the REAL one, so a screen that forgets to take it still asks/// the OS properly rather than silently believing it was refused. A test or a/// preview hands down a `ScriptedCapabilityAuthority` instead.private struct CottonCapabilitiesKey: EnvironmentKey {    static let defaultValue: any CapabilityAuthority = SystemCapabilityAuthority()}extension EnvironmentValues {    public var cottonCapabilities: any CapabilityAuthority {        get { self[CottonCapabilitiesKey.self] }        set { self[CottonCapabilitiesKey.self] = newValue }    }}