Cotton code
60 lines · 2.7 KB · SwiftRaw
// The alarm row, kept TYPED — the one shape in this lane that is arithmetic// before it is a screen.//// `AlarmPlan.swift` reads an hour, a minute and a list of ISO weekdays against// an injected calendar to say when an alarm next rings, and the reconciler// diffs that against what the operating system already holds. Those are pure// functions tested at 7:29 and 7:31 without a device; handing them a bag of// keys would put the spelling of every column inside the clock arithmetic.//// NOT `Decodable`: the boundary answers a `JSONValue` and this is BUILT from// one, field by field, through the same accessors every other screen reads// with — so a column the read does not carry takes the column's own default// rather than throwing away the whole row.import CottonKit/// One row of `cotton_alarms`, disabled ones included.////// **`id` is also the AlarmKit alarm id**: the reconciler diffs the operating/// system's alarms against these directly, which is why the read answers the/// stored id rather than an index.struct Alarm: Equatable, Identifiable, Sendable {    var id: String = ""    /// `""` is what "the owner named nothing" looks like — the column's own    /// default. The screen decides what that reads as; the core never invents    /// a word for it.    var label: String = ""    var hour: Int = 0    var minute: Int = 0    /// ISO weekdays, 1–7. Empty is a ONE-SHOT, which is also how a row whose    /// column will not parse reads: the whole screen must not go dark for one    /// malformed row.    var repeatDays: [Int] = []    var enabled: Bool = false    /// Saved and carried, and **not read by AlarmKit**: an AlarmKit alarm rings    /// by construction and its own alert sound wants an API this app does not    /// call. The switch is still offered because the row travels to a phone    /// that falls back to a notification, where it does decide.    var sound: Bool = true    var createdAt: String?    var updatedAt: String?    init() {}    init(_ json: JSONValue) {        id = json.text("id") ?? ""        // Exactly as stored: the editor seeds its name field from this and        // writes it back, and the row's own subtitle is what trims.        label = json.untrimmed("label") ?? ""        hour = json.number("hour") ?? 0        minute = json.number("minute") ?? 0        // A stray entry the crate could not read is already gone; anything left        // that is not a whole number is dropped here for the same reason.        repeatDays = json.list("repeatDays").compactMap(\.int)        enabled = json.flag("enabled")        sound = json.flag("sound")        createdAt = json.text("createdAt")        updatedAt = json.text("updatedAt")    }}