// 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. import CottonComposer import CottonKit import CottonValues import CottonVoice import Foundation import Observation #if canImport(UIKit) import UIKit #endif @MainActor @Observable public final class ComposerVoice: ComposerDictation { public let field: VoiceField @ObservationIgnored private let request: () -> VoicePlanRequest /// - Parameter request: where the microphone is, asked at each start (the /// record may change between dictations). @ObservationIgnored private let capabilities: any CapabilityAuthority public init(core: any CottonCalling, capabilities: any CapabilityAuthority = SystemCapabilityAuthority(), request: @escaping () -> VoicePlanRequest) { field = VoiceField(session: VoiceSessions.make(core: core), inlineTentative: true) self.capabilities = capabilities self.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. public enum Ask: Equatable, Sendable { case why, settings } public var ask: Ask? /// The start a card stands in front of. @ObservationIgnored private var waiting: (text: String, selection: NSRange?)? /// The card's words, in the voice line set. public func 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`). public func goOn(_ asked: Ask) async { ask = nil switch asked { case .why: guard let waiting else { return } self.waiting = nil await begin(text: waiting.text, selection: waiting.selection) case .settings: waiting = nil #if canImport(UIKit) if let settings = URL(string: UIApplication.openSettingsURLString) { await UIApplication.shared.open(settings) } #endif } } /// Not now (or the card closed): nothing is asked of the phone. public func notNow() { ask = nil } private var session: VoiceSession { field.session } public var phase: ComposerDictationPhase { switch session.phase { case .idle: .idle case .preparing: .preparing case .listening: .listening case .settling: .settling case .failed(let failure): .failed(session.line(failure.lineKey)) } } public var level: Double { session.level } public var shown: String? { field.text } public var tentativeRange: NSRange? { field.tentativeRange } public var 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). public private(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. public private(set) var refusal: String? public func 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 = .settings return } if microphone == .notDetermined || speech == .notDetermined { waiting = (text, selection) ask = .why return } await begin(text: text, selection: selection) } private func 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) { if case .failed(let failure) = phase { refusal = ShellLines.said(session.line(failure.lineKey), as: ShellLines.speaker) refused = failure == .denied } else { refusal = nil refused = false } } public func stop() async -> String { await field.stop() } public func cancel() -> String { field.cancel() } public func didWrite(_ text: String) { field.didWrite(text) } public func fieldChanged(_ text: String) { field.fieldChanged(text) } public func selectionChanged(_ range: NSRange) { field.selectionChanged(range) } /// The composer's voice words, from the voice line set; the hint and /// send are the app's. public func 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") ) } }