// The shared composer's microphone over Cotton's own voice path: a// `VoiceField` (the core's `voice.*` session, live words then the second// pass, the person's edits never overwritten) as the `ComposerDictation` the// composer drives. Any app mounts it the same way: it names where the// microphone is (`VoicePlanRequest`) and the composer does the rest.importCottonComposerimportCottonKitimportCottonValuesimportCottonVoiceimportFoundationimportObservation#if canImport(UIKit)import UIKit#endif@MainActor@ObservablepublicfinalclassComposerVoice: ComposerDictation {publiclet field: VoiceField @ObservationIgnored privatelet request: () -> VoicePlanRequest/// - Parameter request: where the microphone is, asked at each start (the/// record may change between dictations). @ObservationIgnored privatelet capabilities: any CapabilityAuthoritypublicinit(core: any CottonCalling, capabilities: any CapabilityAuthority = SystemCapabilityAuthority(), request: @escaping () -> VoicePlanRequest) { field = VoiceField(session: VoiceSessions.make(core: core), inlineTentative: true)self.capabilities = capabilitiesself.request = request }/// A card the app shows before the phone is asked, or after it refused (store pass, 24 Nov 2026; voice lines/// `control.*`): `why` says what the microphone is for, with Continue and Not now, before the first prompt;/// `settings` says the phone refused it, with Open Settings and Not now. Nothing opens Settings unasked.publicenumAsk: Equatable, Sendable { case why, settings }publicvar ask: Ask?/// The start a card stands in front of. @ObservationIgnored privatevar waiting: (text: String, selection: NSRange?)?/// The card's words, in the voice line set.publicfunc askWords(_ ask: Ask) -> (line: String, go: String, notNow: String) {switch ask {case .why: (ShellLines.said(session.line("why"), as: ShellLines.speaker), session.line("go_on"), session.line("not_now"))case .settings: (ShellLines.said(session.line("no_permission"), as: ShellLines.speaker), session.line("open_settings"), session.line("not_now")) } }/// Continue (the phone asked now) or Open Settings, from the card that showed `asked` (the card may already have/// closed, and with it `ask`).publicfunc goOn(_ asked: Ask) async { ask = nilswitch asked {case .why:guardlet waiting else { return }self.waiting = nilawait begin(text: waiting.text, selection: waiting.selection)case .settings: waiting = nil #if canImport(UIKit)iflet settings = URL(string: UIApplication.openSettingsURLString) { await UIApplication.shared.open(settings) } #endif } }/// Not now (or the card closed): nothing is asked of the phone.publicfunc notNow() { ask = nil }privatevar session: VoiceSession { field.session }publicvar phase: ComposerDictationPhase {switch session.phase {case .idle: .idlecase .preparing: .preparingcase .listening: .listeningcase .settling: .settlingcase .failed(let failure): .failed(session.line(failure.lineKey)) } }publicvar level: Double { session.level }publicvar shown: String? { field.text }publicvar tentativeRange: NSRange? { field.tentativeRange }publicvar caret: Int { field.caret }/// The microphone was refused at the last start: the next tap opens this/// app's page in Settings, where it is turned back on, rather than doing/// nothing (launch-ios walk, 22 Nov 2026: Story's mic went dead after/// Don't Allow, with no word).publicprivate(set) var refused = false/// Why the last start did not run, in this app's name ("Cotton Story/// needs the microphone to hear you."), or nil when it ran.publicprivate(set) var refusal: String?publicfunc start(text: String, selection: NSRange?) async {let microphone = await capabilities.status(.microphone), speech = await capabilities.status(.speech)if microphone == .denied || microphone == .restricted || speech == .denied || speech == .restricted {// iOS asks once per install: after a refusal only Settings turns it back on, so the card says so first. refused = true waiting = (text, selection) ask = .settingsreturn }if microphone == .notDetermined || speech == .notDetermined { waiting = (text, selection) ask = .whyreturn }await begin(text: text, selection: selection) }privatefunc begin(text: String, selection: NSRange?) async { session.dismiss()await field.start(text: text, selection: selection, request: request()) noteHowItStarted(session.phase) }/// What a start ended in, kept past the session's own dismiss.func noteHowItStarted(_ phase: VoiceSession.Phase) {ifcase .failed(let failure) = phase { refusal = ShellLines.said(session.line(failure.lineKey), as: ShellLines.speaker) refused = failure == .denied } else { refusal = nil refused = false } }publicfunc stop() async -> String { await field.stop() }publicfunc cancel() -> String { field.cancel() }publicfunc didWrite(_ text: String) { field.didWrite(text) }publicfunc fieldChanged(_ text: String) { field.fieldChanged(text) }publicfunc selectionChanged(_ range: NSRange) { field.selectionChanged(range) }/// The composer's voice words, from the voice line set; the hint and/// send are the app's.publicfunc lines(hint: String, send: String) -> ComposerLines {ComposerLines( hint: hint, send: send, mic: session.line("idle"), stop: session.line("stop"), cancel: session.line("cancel"), listening: session.line("listening") ) }}