SPB Git

Repository Public License

SharedTypes.swift 3.3 KB 76 lines swift

1// ORIX Alarm2// File: Shared/SharedTypes.swift3// Description: Types compiled into both the app and the widget extension: app group, AlarmKit metadata and the widget snapshot file.4//5// Author: Simon-Pierre Boucher6// Contact: contact@spboucher.ai7import AlarmCore8import AlarmKit9import Foundation1011nonisolated enum AppGroup {12    static let identifier = "group.ai.spboucher.orix.alarm"13    /// Shared container, or Application Support when the group is unavailable (unit tests, unsigned builds).14    static var directory: URL {15        if let url = FileManager.default.containerURL(forSecurityApplicationGroupIdentifier: identifier) {16            return url.appendingPathComponent("ORIXAlarm", isDirectory: true)17        }18        let base = FileManager.default.urls(for: .applicationSupportDirectory, in: .userDomainMask)[0]19        return base.appendingPathComponent("ORIXAlarm", isDirectory: true)20    }21}2223/// Metadata attached to every system alarm scheduled by the app; rendered by the Live Activity.24nonisolated struct ORIXAlarmMetadata: AlarmMetadata {25    enum Role: String, Codable, Sendable { case alarm, snooze, focusTimer }26    /// Identifier of the alarm definition this system alarm belongs to.27    var alarmID: UUID28    var name: String29    var role: Role30    var hasMission: Bool31    var symbol: String32}3334/// Small read-only summary written by the app for the widgets (which cannot query AlarmKit state for the app).35nonisolated struct WidgetSnapshot: Codable, Sendable, Equatable {36    struct Entry: Codable, Sendable, Equatable, Identifiable {37        var id: UUID38        var name: String39        var hour: Int40        var minute: Int41        var weekdays: [Int]          // 1 = Sunday; empty = one-time42        var isEnabled: Bool43        var missionCount: Int44        var symbol: String45    }46    var generatedAt: Date = .now47    var alarms: [Entry] = []48    /// Start-of-day dates (last 7 days) on which a wake confirmation was recorded.49    var confirmedDays: [Date] = []50    var currentStreak: Int = 05152    static var fileURL: URL { AppGroup.directory.appendingPathComponent("widget-snapshot.json") }5354    static func load() -> WidgetSnapshot {55        guard let data = try? Data(contentsOf: fileURL) else { return WidgetSnapshot() }56        let d = JSONDecoder(); d.dateDecodingStrategy = .iso860157        return (try? d.decode(WidgetSnapshot.self, from: data)) ?? WidgetSnapshot()58    }5960    func save() {61        let e = JSONEncoder(); e.dateEncodingStrategy = .iso860162        guard let data = try? e.encode(self) else { return }63        try? FileManager.default.createDirectory(at: AppGroup.directory, withIntermediateDirectories: true)64        try? data.write(to: Self.fileURL, options: [.atomic])65    }6667    /// Enabled alarms with their next fire date after `date`, soonest first.68    func upcoming(after date: Date, calendar: Calendar = .current) -> [(entry: Entry, date: Date)] {69        alarms.filter(\.isEnabled).compactMap { e -> (entry: Entry, date: Date)? in70            let days = Set(e.weekdays.compactMap(Weekday.init(rawValue:)))71            guard let d = Recurrence.nextFireDate(time: ClockTime(hour: e.hour, minute: e.minute), weekdays: days, after: date, calendar: calendar) else { return nil }72            return (entry: e, date: d)73        }.sorted { $0.date < $1.date }74    }75}76
Files main
Menu

Theme