SPB Git

Repository Public License

AlarmIntents.swift 6.2 KB 142 lines swift

1// ORIX Alarm2// File: Shared/AlarmIntents.swift3// Description: Live Activity intents attached to system alarms (stop, stop and open the wake mission, snooze, open mission, pause/resume timer). They run in the app process.4//5// Author: Simon-Pierre Boucher6// Contact: contact@spboucher.ai7import AlarmKit8import AppIntents9import Foundation1011nonisolated enum AlarmIntentEvent: Sendable, Equatable {12    /// The user stopped the system alert of this system alarm identifier.13    case stopped(systemID: UUID, alarmID: UUID)14    case snoozeRequested(systemID: UUID, alarmID: UUID)15    case openMission(systemID: UUID, alarmID: UUID)16}1718/// Delivers intent events to the app model. Events that arrive before the app has installed its handler are queued.19@MainActor20final class IntentBridge {21    static let shared = IntentBridge()22    private var handler: ((AlarmIntentEvent) async -> Void)?23    private(set) var pending: [AlarmIntentEvent] = []2425    func install(_ handler: @escaping (AlarmIntentEvent) async -> Void) async {26        self.handler = handler27        let queued = pending; pending = []28        for event in queued { await handler(event) }29    }3031    func send(_ event: AlarmIntentEvent) async {32        if let handler { await handler(event) } else { pending.append(event) }33    }34}3536nonisolated private func uuid(_ s: String) -> UUID? { UUID(uuidString: s) }3738/// DEBUG simulator diagnostics: which process runs an alarm intent (see Scripts/probe-alarmkit.sh).39nonisolated func intentTrace(_ name: String) {40    #if DEBUG && targetEnvironment(simulator)41    let url = URL(fileURLWithPath: "/tmp/orix-alarm-probe/intents.log")42    guard FileManager.default.fileExists(atPath: "/tmp/orix-alarm-probe/enabled") else { return }43    let line = "\(Date.now.formatted(date: .omitted, time: .standard)) \(name) in \(ProcessInfo.processInfo.processName)\n"44    let text = ((try? String(contentsOf: url, encoding: .utf8)) ?? "") + line45    try? text.write(to: url, atomically: true, encoding: .utf8)46    #endif47}4849/// Stops the alert. Used when the alarm has no wake mission.50struct StopAlarmIntent: LiveActivityIntent {51    static let title: LocalizedStringResource = "Stop Alarm"52    static let isDiscoverable = false53    @Parameter(title: "System alarm") var systemID: String54    @Parameter(title: "Alarm") var alarmID: String55    init() {}56    init(systemID: UUID, alarmID: UUID) { self.systemID = systemID.uuidString; self.alarmID = alarmID.uuidString }57    func perform() async throws -> some IntentResult {58        intentTrace("StopAlarmIntent")59        guard let sid = uuid(systemID), let aid = uuid(alarmID) else { return .result() }60        try? AlarmManager.shared.stop(id: sid)61        await IntentBridge.shared.send(.stopped(systemID: sid, alarmID: aid))62        return .result()63    }64}6566/// Stops the alert and brings the app forward so the wake mission can start.67/// The system still lets the user stop the alarm without doing the mission: a mission never locks the alarm.68struct StopAndOpenMissionIntent: LiveActivityIntent {69    static let title: LocalizedStringResource = "Stop and Start Mission"70    static let isDiscoverable = false71    static let openAppWhenRun = true72    @Parameter(title: "System alarm") var systemID: String73    @Parameter(title: "Alarm") var alarmID: String74    init() {}75    init(systemID: UUID, alarmID: UUID) { self.systemID = systemID.uuidString; self.alarmID = alarmID.uuidString }76    func perform() async throws -> some IntentResult {77        intentTrace("StopAndOpenMissionIntent")78        guard let sid = uuid(systemID), let aid = uuid(alarmID) else { return .result() }79        try? AlarmManager.shared.stop(id: sid)80        await IntentBridge.shared.send(.stopped(systemID: sid, alarmID: aid))81        return .result()82    }83}8485/// Snoozes according to the alarm's snooze policy (the app schedules the follow-up alert).86struct SnoozeAlarmIntent: LiveActivityIntent {87    static let title: LocalizedStringResource = "Snooze Alarm"88    static let isDiscoverable = false89    @Parameter(title: "System alarm") var systemID: String90    @Parameter(title: "Alarm") var alarmID: String91    init() {}92    init(systemID: UUID, alarmID: UUID) { self.systemID = systemID.uuidString; self.alarmID = alarmID.uuidString }93    func perform() async throws -> some IntentResult {94        intentTrace("SnoozeAlarmIntent")95        guard let sid = uuid(systemID), let aid = uuid(alarmID) else { return .result() }96        await IntentBridge.shared.send(.snoozeRequested(systemID: sid, alarmID: aid))97        return .result()98    }99}100101/// Secondary button of an alarm without snooze: opens the app on the wake mission.102struct OpenMissionIntent: LiveActivityIntent {103    static let title: LocalizedStringResource = "Start Wake Mission"104    static let isDiscoverable = false105    static let openAppWhenRun = true106    @Parameter(title: "System alarm") var systemID: String107    @Parameter(title: "Alarm") var alarmID: String108    init() {}109    init(systemID: UUID, alarmID: UUID) { self.systemID = systemID.uuidString; self.alarmID = alarmID.uuidString }110    func perform() async throws -> some IntentResult {111        intentTrace("OpenMissionIntent")112        guard let sid = uuid(systemID), let aid = uuid(alarmID) else { return .result() }113        try? AlarmManager.shared.stop(id: sid)114        await IntentBridge.shared.send(.openMission(systemID: sid, alarmID: aid))115        return .result()116    }117}118119struct PauseTimerIntent: LiveActivityIntent {120    static let title: LocalizedStringResource = "Pause Timer"121    static let isDiscoverable = false122    @Parameter(title: "System alarm") var systemID: String123    init() {}124    init(systemID: UUID) { self.systemID = systemID.uuidString }125    func perform() async throws -> some IntentResult {126        if let sid = uuid(systemID) { try? AlarmManager.shared.pause(id: sid) }127        return .result()128    }129}130131struct ResumeTimerIntent: LiveActivityIntent {132    static let title: LocalizedStringResource = "Resume Timer"133    static let isDiscoverable = false134    @Parameter(title: "System alarm") var systemID: String135    init() {}136    init(systemID: UUID) { self.systemID = systemID.uuidString }137    func perform() async throws -> some IntentResult {138        if let sid = uuid(systemID) { try? AlarmManager.shared.resume(id: sid) }139        return .result()140    }141}142
Files main
Menu

Theme