Cotton code
158 lines · 5.6 KB · SwiftRaw
// Cotton Meetings on the lock screen, in the Dynamic Island, on the Action// Button and in Control Centre (voice plan, 17 Nov 2026). Only the Meetings// targets and their widget extension link this: no other Cotton app offers// "record a meeting" to Siri, Shortcuts or the lock screen.//// The widget extension draws; it never records. Every intent here runs in// the APP's process (`AudioRecordingIntent` and `LiveActivityIntent` do), and// reaches the app's recorder through `MeetingsIntentBridge`, which the app// fills at launch. So this module carries no core, no line set and no// recorder: the words the Activity shows come in its content, resolved by// the app from the kit's lines.import ActivityKitimport AppIntentsimport Foundation/// The Live Activity's shape.public struct MeetingActivityAttributes: ActivityAttributes {    public struct ContentState: Codable, Hashable, Sendable {        public var memo: Bool        public var startedAt: Date        /// Whole spans that are over, in milliseconds.        public var bankedMs: Double        /// When the running span began; nil while paused.        public var runningSince: Date?        public var marks: Int        public var headline: String        public var pause: String        public var resume: String        public var stop: String        public var mark: String        public init(            memo: Bool, startedAt: Date, bankedMs: Double, runningSince: Date?, marks: Int, headline: String,            pause: String, resume: String, stop: String, mark: String        ) {            self.memo = memo            self.startedAt = startedAt            self.bankedMs = bankedMs            self.runningSince = runningSince            self.marks = marks            self.headline = headline            self.pause = pause            self.resume = resume            self.stop = stop            self.mark = mark        }        public var paused: Bool { runningSince == nil }        /// The moment the clock would read zero if it had never paused: what        /// a running timer text counts from.        public var clockZero: Date {            (runningSince ?? Date()).addingTimeInterval(-bankedMs / 1000)        }        /// The banked length as a clock, for a paused Activity.        public var bankedClock: String {            let total = max(0, Int(bankedMs / 1000))            let hours = total / 3600, minutes = (total % 3600) / 60, seconds = total % 60            return hours > 0                ? String(format: "%d:%02d:%02d", hours, minutes, seconds)                : String(format: "%d:%02d", minutes, seconds)        }    }    public init() {}}/// What an intent asks the app's recorder.public enum MeetingsLiveAct: Sendable, Equatable {    case startMeeting    case startMemo    case pause    case resume    case mark    case stop}/// What the recorder answered an intent.public enum MeetingsLiveAnswer: Sendable, Equatable {    case done    /// The microphone has not been allowed: the app must be opened once.    case needsApp    case refused(String)}/// The app's recorder, as the intents reach it. Filled by the app at launch.@MainActorpublic enum MeetingsIntentBridge {    public static var perform: (@MainActor (MeetingsLiveAct) async -> MeetingsLiveAnswer)?    static func run(_ act: MeetingsLiveAct) async throws {        guard let perform else { throw MeetingsIntentError.notReady }        switch await perform(act) {        case .done: return        case .needsApp: throw MeetingsIntentError.needsApp        case .refused(let why): throw MeetingsIntentError.refused(why)        }    }}enum MeetingsIntentError: Error, CustomLocalizedStringResourceConvertible {    case notReady    case needsApp    case refused(String)    var localizedStringResource: LocalizedStringResource {        switch self {        case .notReady, .needsApp: return LocalizedStringResource("intent.needs_app", table: "Meetings")        case .refused(let why): return LocalizedStringResource(stringLiteral: why)        }    }}/// The Live Activity, driven by the app's recorder.@MainActorpublic enum MeetingsLiveActivity {    private static var currentId: String?    /// Show, change or end the Activity. `nil` ends it.    public static func show(_ state: MeetingActivityAttributes.ContentState?) {        guard let state else {            end()            return        }        let standing = Activity<MeetingActivityAttributes>.activities.first {            ($0.id == currentId || currentId == nil) && ($0.activityState == .active || $0.activityState == .stale)        }        if let standing {            currentId = standing.id            let id = standing.id            Task { await update(id, state) }            return        }        guard ActivityAuthorizationInfo().areActivitiesEnabled else { return }        let made = try? Activity.request(            attributes: MeetingActivityAttributes(), content: ActivityContent(state: state, staleDate: nil), pushType: nil)        currentId = made?.id    }    /// End every Activity this app left standing, a killed process's too.    public static func end() {        currentId = nil        Task { await endAll() }    }    private nonisolated static func update(_ id: String, _ state: MeetingActivityAttributes.ContentState) async {        let found = Activity<MeetingActivityAttributes>.activities.first { $0.id == id }        await found?.update(ActivityContent(state: state, staleDate: nil))    }    private nonisolated static func endAll() async {        for one in Activity<MeetingActivityAttributes>.activities {            await one.end(nil, dismissalPolicy: .immediate)        }    }}