// 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.importCottonKitimportFoundationimportSwiftUI#if canImport(UIKit)import UIKit#endif/// What to say about a capability that blocked, and whether anything can be/// done about it.publicenumCapabilityRefusal {/// 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.publicstaticfunc sentence( _ capability: DeviceCapability, _ access: CapabilityAccess ) -> String? {let noun = Self.noun(capability)switch access {case .authorized: returnnilcase .denied: returnShellLines.line("refusal", "capability.cotton_doesn_t_have", ["noun": "\(noun)"])case .notDetermined: returnShellLines.line("refusal", "capability.cotton_needs_access_to", ["noun": "\(noun)"])case .restricted: returnShellLines.line("refusal", "capability.access_to_isn_t", ["noun": "\(noun)"])case .unavailable: returnShellLines.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.privatestaticfunc noun(_ capability: DeviceCapability) -> String {switch capability {case .microphone: returnShellLines.line("refusal", "capability.your_microphone")case .speech: returnShellLines.line("refusal", "capability.speech_recognition")case .camera: returnShellLines.line("refusal", "capability.the_camera")case .contacts: returnShellLines.line("refusal", "capability.your_contacts")case .notifications: return"notifications"case .photoLibrary, .photoLibraryAdd: returnShellLines.line("refusal", "capability.your_photo_library")case .localNetwork: returnShellLines.line("refusal", "capability.your_mac_on_this")case .appleHealth: returnShellLines.line("refusal", "capability.apple_health") } }/// The same, for whatever a gate answered. `nil` when nothing blocked.publicstaticfunc sentence(for result: CapabilityGateResult) -> String? {switch result {case .ready: returnnilcase .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.publicstaticfunc 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.publicstaticvar 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.privatestructCottonCapabilitiesKey: EnvironmentKey {staticlet defaultValue: any CapabilityAuthority = SystemCapabilityAuthority()}extensionEnvironmentValues {publicvar cottonCapabilities: any CapabilityAuthority { get { self[CottonCapabilitiesKey.self] } set { self[CottonCapabilitiesKey.self] = newValue } }}