// APPLE HEALTH'S ASK, the one HealthKit permission call in Cotton (Cotton // Health, 13 Nov 2026). It lives in the one product only Cotton Health links, // so no other app's binary names HealthKit, and it is reached only through // `SystemCapabilityAuthority` (`DeviceCapability.appleHealth`), which every // ask goes through: `CapabilityGateSweepTests` and the capability gate sweep // hold it to this file. // // READ-ONLY: the prompt asks to read the reader's types and to share // (write) none. HealthKit never says whether a read was refused: once the // person has answered, `statusForAuthorizationRequest` says `unnecessary` // whatever they chose, and a type they turned off simply reads empty. So an // answered ask is `authorized`, and nothing here ever asks twice: a // request HealthKit completed without an error is the person's answer, // whatever the standing status says of a type the sheet folded away. // // A plain struct, so it inherits no actor isolation: HealthKit answers its // completions off the main queue. import CottonKit import HealthKit struct AppleHealthAccess: AppleHealthAsking { /// The types the reader reads, as HealthKit may be asked for them. let read: Set /// The app's one store: a store made for one call and dropped closes /// its connection to HealthKit under the call. static let store = HKHealthStore() func status() async -> CapabilityAccess { guard HKHealthStore.isHealthDataAvailable(), !read.isEmpty else { return .unavailable } let answer = try? await Self.store.statusForAuthorizationRequest(toShare: [], read: read) switch answer { case .unnecessary: return .authorized case .shouldRequest, .unknown, nil: return .notDetermined @unknown default: // A status this file has not met: asked as though never asked, // which HealthKit answers at once if it was. return .notDetermined } } func ask() async -> CapabilityAccess { guard HKHealthStore.isHealthDataAvailable(), !read.isEmpty else { return .unavailable } do { try await Self.store.requestAuthorization(toShare: [], read: read) } catch { return .denied } return .authorized } }