Cotton code
110 lines · 4.8 KB · SwiftRaw
// TABLE TWO — a LOCAL reminder's tap, `{kind, id}` through `RecordRoute`.//// ported. Its own header says why it is not the push table://// > Cotton's notifications are local and carry the record itself (`{kind, id}`),// > so the routing is `recordPath`, the same pure table every in-app link goes// > through. Adopting the platform hook would mean inventing push "types" for// > records the app is already holding, and maintaining a second link table// > beside `recordPath` that could drift from it.//// **The two tables are not rivals and they must keep rejecting each other's// payloads.** They used to be two hooks on one native response; in Swift// there is ONE `UNUserNotificationCenterDelegate` callback, so they become two// parsers behind one entry point — and the mutual rejection is what makes that// merge safe. `NotificationRoutes.route` answers `nil` for anything without a// mapped string `type`; ``ReminderTaps/parse(_:)`` answers `nil` for anything// that is not exactly `{kind: <one of five>, id: <non-empty>}`. Neither can// claim the other's tap, so the ORDER they are tried in does not matter — and// `NotificationTapTests` proves that rather than assuming it.//// **The parse is suspicious on purpose.** A notification sitting in// Notification Centre can outlive the build that scheduled it, so anything not// exactly this shape resolves to `nil` and routes nowhere, rather than// navigating somewhere wrong.//// **The local reminders never touch the server, and that is the ratified rule**// (2026-08-11, unchanged by the 2026-08-14 return of push): everything Cotton// reminds about already lives on the device that would be reminded, so a round// trip through a database, a push worker and APNs would add three failure modes// to deliver a fact the phone already holds. The accepted trade, stated there:// nothing here survives an uninstall or reaches a second device.import Foundation/// The payload every Cotton reminder carries.////// Deliberately two fields: the tap handler resolves the path from the KIND, so/// a route move never strands a notification already sitting in Notification/// Centre.public struct ReminderPayload: Equatable, Sendable {    public let kind: NotificationKind    public let id: String    public init(kind: NotificationKind, id: String) {        self.kind = kind        self.id = id    }}/// The local-reminder tap table.public enum ReminderTaps {    /// Read a tap's payload back — pure, and suspicious. See the header.    public static func parse(_ data: JSONValue) -> ReminderPayload? {        guard            case .object = data,            let kind = data["kind"]?.string,            let known = NotificationKind(rawValue: kind),            let id = data["id"]?.string,            !id.isEmpty        else { return nil }        return ReminderPayload(kind: known, id: id)    }    /// Where a tap lands when it lands on a RECORD.    ///    /// TOTAL over the four kinds, with no `default`, so a kind added without an    /// address does not compile. `birthday` opens the PERSON — a birthday is    /// not a record of its own — and `alarm` is not a record either: it has no    /// detail screen, only a tool, so it routes through ``toolRoute(for:)``.    public static func recordKind(for kind: NotificationKind) -> RecordKind? {        switch kind {        case .todo: return .todo        case .event: return .event        case .birthday: return .person        case .alarm: return nil        }    }    /// Where a tap lands when the kind has a TOOL but no record screen.    ///    /// Until 2026-08-14 an alarm tap deliberately routed nowhere — opening the    /// app was the whole response, because guessing at a route that did not    /// exist would have been worse. Both tools exist now, so the tap can say    /// where it came from.    ///    /// **Never overlaps ``recordKind(for:)``**, and the test says so: a kind    /// answering both would make the landing depend on the order two lookups    /// happen to be written in.    public static func toolRoute(for kind: NotificationKind) -> Route? {        switch kind {        case .alarm: return .alarms        case .todo, .event, .birthday: return nil        }    }    /// Where this reminder's tap lands, or `nil` when the kind leads nowhere    /// and opening the app is the whole response.    public static func route(for payload: ReminderPayload) -> Route? {        if let kind = recordKind(for: payload.kind) {            return .record(kind, payload.id)        }        return toolRoute(for: payload.kind)    }    /// Where a tap on this payload lands, or `nil` when it is not ours.    public static func route(for data: JSONValue) -> Route? {        guard let payload = parse(data) else { return nil }        return route(for: payload)    }}