// THE CORE'S ANSWERS, READ (assets/design/days/CONTRACT.md). Days decides// nothing: each type here is one shape the contract names, read as it comes,// so the screens draw what the core said and nothing they worked out// themselves. The same shapes as Android's `days/book/Answers.kt`.importFoundation/// A row's mark (contract §1): never empty.enumDaysMark: Equatable, Hashable, Sendable {/// The pen circle to tick, on every to-do.case tick(done: Bool)/// A drawn mark from the catalogue, by its id.case drawn(String)/// The small quiet dot: a row with no mark.case dot}/// What tapping a row opens (contract §1 `opens`).enumDaysOpens: Equatable, Hashable, Sendable {case event(id: String, occurrenceAt: String?)case todo(id: String)case person(String)case nothing}enumDaysWhose: String, Equatable, Sendable {case yours, google, birthdayinit(_ said: String?) { self = DaysWhose(rawValue: said ?? "") ?? .yours }}enumDaysKind: String, Equatable, Sendable {case event, todo, birthdayinit(_ said: String?) { self = DaysKind(rawValue: said ?? "") ?? .event }}/// One row on a day or in Sometime (contract §1).structDaysRow: Equatable, Hashable, Identifiable, Sendable {var key: Stringvar kind: DaysKindvar time: String?var end: String?var mark: DaysMark/// A to-do's drawn mark, beside its tick (the contract's open point): drawn by nothing yet.var drawnMark: String?var words: Stringvar suffix: String?var done: Boolvar whose: DaysWhosevar opens: DaysOpensvar id: String { key }staticfunc == (a: DaysRow, b: DaysRow) -> Bool { a.key == b.key && a.done == b.done && a.words == b.words && a.mark == b.mark && a.suffix == b.suffix && a.time == b.time }func hash(into hasher: inoutHasher) { hasher.combine(key) }/// The row's table, for `days.done`, `days.move`, `days.change`; none for a birthday.var table: String? {switch opens {case .event: "event"case .todo: "todo"default: nil } }/// The row's record id; none for a birthday.var recordId: String? {switch opens {case .event(let id, _): idcase .todo(let id): iddefault: nil } }/// Whether its day is the person's to change: their own (a Google event's day is Google's).var movable: Bool { whose == .yours && recordId != nil }var occurrenceAt: String? {ifcase .event(_, let at) = opens { return at }returnnil }}/// One entry of `days.span`: a day with its rows, or a run of quiet days folded into one line.enumDaysSpan: Equatable, Identifiable, Sendable {case day(DaysDay)case fold(DaysFold)var id: String {switchself {case .day(let day): day.daycase .fold(let fold): "fold:\(fold.from)" } }/// Whether [day] is this entry: its own, or folded into it.func holds(_ day: String) -> Bool {switchself {case .day(let one): one.day == daycase .fold(let fold): day >= fold.from && day <= fold.to } }}structDaysDay: Equatable, Sendable {var day: Stringvar number: Stringvar weekday: Stringvar month: Stringvar today: Boolvar rows: [DaysRow]}structDaysFold: Equatable, Sendable {var from: Stringvar to: Stringvar count: Intvar words: String}/// `days.span`'s answer.structDaysSpanAnswer: Equatable, Sendable {var today: Stringvar days: [DaysSpan]/// Nothing lies after the last day answered (contract §2, The end): the/// list's closing line, and no read further ahead.var end: DaysEnd?/// Nothing at all lies before this day (contract §2, The beginning): no read further back.var begins: String?}/// The end of the list (`end`): the last day with a row (or today), and the closing line.structDaysEnd: Equatable, Sendable {var after: Stringvar words: String}/// A to-do the person ticked or unticked since the list was opened (contract/// §2, Keep): it stays on the day its row stood on (nil: Sometime), done or/// not as it now is, until the list is next opened.structDaysKept: Equatable, Sendable {var id: Stringvar day: String?var done: Bool/// As `keep` sends it: `{ id, day }`.var json: [String: Any] { ["id": id, "day": day.map { $0 asAny } ?? NSNull()] }}/// One group of Sometime (contract §5): every to-do of it, no "first few".structDaysGroup: Equatable, Identifiable, Sendable {var groupId: String?var name: Stringvar count: Intvar rows: [DaysRow]var id: String { groupId ?? "no-group" }}/// `days.groups` (contract §5b): the filter's choices, same-named groups as one.structDaysGroups: Equatable, Sendable {/// The filter's word with no group chosen ("All").var all: String/// Every open to-do.var count: Intvar groups: [DaysGroupCount]}structDaysGroupCount: Equatable, Identifiable, Sendable {var id: Stringvar name: Stringvar count: Int}/// `days.month` (contract §6).structDaysMonth: Equatable, Sendable {var month: Stringvar title: Stringvar weekStart: Int/// Blank cells before the 1st in a week starting on `weekStart` (the core's; nil from an older core).var lead: Int?/// Always six.var weeks: Int = 6var days: [DaysMonthDay]}structDaysMonthDay: Equatable, Hashable, Sendable {var day: Stringvar marks: [String]var other: Boolvar today: Bool}/// A stretch of the words read as the day or the time (`said`), for underlining.structDaysSaid: Equatable, Sendable {var from: Intvar to: Intvar what: String}/// `days.read_line` (contract §7): what the words say, for the pill.structDaysReadLine: Equatable, Sendable {var title: Stringvar day: String?var time: String?var end: String?var kind: DaysKindvar pill: String?var marks: [String]var said: [DaysSaid]staticlet empty = DaysReadLine(title: "", day: nil, time: nil, end: nil, kind: .todo, pill: nil, marks: [], said: [])}/// What may be changed on an item's sheet (`canChange`).structDaysCanChange: Equatable, Sendable {var title = false, time = false, mark = false, note = false, group = false, alert = false}/// `days.item` (contract §3): the one sheet's facts.structDaysItem: Equatable, Sendable {var table: Stringvar id: Stringvar whose: DaysWhosevar words: Stringvar day: String?var time: String?var end: String?var mark: String?var note: String?/// A to-do's group (the first that holds it), read through `groups.list`.var group: String?/// An event's alert choices (`alerts`), the one that stands `on`; empty for a to-do.var alerts: [DaysAlertChoice] = []var done: Boolvar repeats: Boolvar canChange: DaysCanChangevar says: String?/// The person's to-do groups to choose from on the sheet, in their order.var groups: [DaysGroupChoice] = []/// The quick day row (`choices`, contract §3): Today, Tomorrow, Next/// week, Sometime (a to-do), each labelled by its line key `move.<key>`.var choices: [DaysDayChoice] = []/// A timed event's end chips (`ends`, contract §14); nil on an all-day event, a to-do, or a core without them.var ends: DaysEnds?/// An event's people (`people`, contract §15); nil on a to-do or a core without them.var people: DaysPeople?/// An invitation received (`invitation`, contract §17): who sent it and the three answers.var invitation: DaysInvitation?/// Who Delete tells (`deleteTells`, contract §15): "Priya and Akash are told it is off."var deleteTells: String?}/// An event's alert as the core answers and takes it (contract §3): No alert/// (`"none"`, which never reminds) or minutes before it starts.enumDaysAlert: Hashable, Sendable {case offcase minutes(Int)init?(_ value: Any?) {iflet word = value as? String {guard word == "none"else { returnnil }self = .off } elseiflet number = value as? NSNumber {self = .minutes(number.intValue) } else {returnnil } }/// As `days.change` takes it.var json: Any {switchself {case .off: return"none"case .minutes(let n): return n } }}/// One alert the sheet offers (`days.item` alerts): the alert, and whether it is the one that stands.structDaysAlertChoice: Equatable, Sendable {var alert: DaysAlertvar on: Bool}/// One quick day choice on the sheet: its key and the day it moves to (nil: Sometime).structDaysDayChoice: Equatable, Sendable {var key: Stringvar day: String?}/// A to-do group offered on the sheet (`groups.list`).structDaysGroupChoice: Equatable, Sendable {var id: Stringvar name: String}/// What `days.add` made: its table and id, and the day and time it landed on./// Why Days asks leave to notify, before iOS asks (`leave` beside `request_notifications`; lines `days.json`).structDaysLeave: Equatable, Sendable, Identifiable {var title: String?var body: Stringvar go: Stringvar notNow: String?var id: String { body }}/// The leave asked, held for the screen: the book is made before the screen it speaks to.@MainActor @ObservablefinalclassDaysLeaveAsking {var asking: DaysLeave?}structDaysAdded: Equatable, Sendable {var table: Stringvar id: Stringvar day: String?var time: String?}/// One mark of the catalogue (`days.marks`): its id, its words per language,/// and its drawing as pen strokes (SVG paths on a 24 by 24 square).structDaysCatalogueMark: Equatable, Sendable {var id: Stringvar words: [String: [String]]var strokes: [String] = []}/// What `days.change` is given: only the fields that changed; a `nil` inside clears that field.structDaysChange: Equatable, Sendable {var title: String??var day: String??var time: String??var mark: String??var note: String??var group: String??/// The alert chosen; the sheet never clears it (`"none"` is No alert).var alert: DaysAlert?/// The end as a length after the start, or No end (contract §14).var length: DaysLength?/// The end picked as a clock time (contract §14: at or before the start is the next day).var end: String?/// The whole new list of people, the chips as they stand (contract §15).var people: [DaysPerson]?var isEmpty: Bool { title == nil && day == nil && time == nil && mark == nil && note == nil && group == nil && alert == nil && length == nil && end == nil && people == nil }/// The fields as the contract sends them, an explicit null where one is cleared.var json: [String: Any] {var out: [String: Any] = [:]func put<T>(_ key: String, _ value: T??) {guardlet value else { return } out[key] = value.map { $0 asAny } ?? NSNull() } put("title", title); put("day", day); put("time", time); put("mark", mark) put("note", note)iflet alert { out["alert"] = alert.json }iflet length { out["length"] = length.json }iflet end { out["end"] = end }iflet people { out["people"] = people.map(\.json) }return out }}/// A row of a view or of search (contract §11, §12): a row with its day.structDaysListed: Equatable, Identifiable, Sendable {var row: DaysRow/// `YYYY-MM-DD`, or nil: no day (Sometime).var day: String?/// The day in words ("Sun 25 Oct", "Sometime").var dayWords: String/// A repeating item's rhythm in words ("Every Monday"), in Repeating.var rhythm: String?var dropped: Bool/// Where a tap on a search result lands.var lands: DaysLands?var id: String { row.key }}/// Where a search result takes the person (contract §12 `lands`).enumDaysLands: Equatable, Sendable {case days(String)case sometimecase view(String)}/// `days.show` (contract §11): one of the four views, read a page at a time.structDaysShown: Equatable, Sendable {var view: Stringvar title: Stringvar rows: [DaysListed]var empty: String?/// The cursor for the page after; nil at the end.var next: String?}/// `days.find` (contract §12): Coming, then Before.structDaysFound: Equatable, Sendable {var coming: [DaysListed]var before: [DaysListed]/// "Nothing found for “words”.", or nil (nothing typed yet, or something found).var empty: String?staticlet none = DaysFound(coming: [], before: [], empty: nil)}// MARK: - Reading the core's JSON/// The contract's shapes read from a decoded JSON object, as Android reads them.enumDaysRead {typealiasObject = [String: Any]staticfunc s(_ o: Object, _ key: String) -> String? { o[key] as? String }staticfunc i(_ o: Object, _ key: String) -> Int? { (o[key] as? NSNumber)?.intValue }staticfunc b(_ o: Object, _ key: String) -> Bool { (o[key] as? Bool) ?? ((o[key] as? NSNumber)?.boolValue ?? false) }staticfunc o(_ o: Object, _ key: String) -> Object? { o[key] as? Object }staticfunc objects(_ o: Object, _ key: String) -> [Object] { (o[key] as? [Any])?.compactMap { $0 as? Object } ?? [] }staticfunc strings(_ o: Object, _ key: String) -> [String] { (o[key] as? [Any])?.compactMap { $0 as? String } ?? [] }staticfunc mark(_ m: Object?) -> DaysMark {switch m.flatMap({ s($0, "kind") }) {case"tick": .tick(done: b(m!, "done"))case"drawn": s(m!, "id").map(DaysMark.drawn) ?? .dotdefault: .dot } }staticfunc opens(_ x: Object?) -> DaysOpens {guardlet x else { return .nothing }iflet person = s(x, "person") { return .person(person) }switch s(x, "sheet") {case"event": return s(x, "id").map { .event(id: $0, occurrenceAt: s(x, "occurrenceAt")) } ?? .nothingcase"todo": return s(x, "id").map { .todo(id: $0) } ?? .nothingdefault: return .nothing } }staticfunc row(_ x: Object) -> DaysRow {DaysRow( key: s(x, "key") ?? "", kind: DaysKind(s(x, "kind")), time: s(x, "time"), end: s(x, "end"), mark: mark(o(x, "mark")), drawnMark: s(x, "drawnMark"), words: s(x, "words") ?? "", suffix: s(x, "suffix"), done: b(x, "done"), whose: DaysWhose(s(x, "whose")), opens: opens(o(x, "opens"))) }staticfunc span(_ x: Object) -> DaysSpanAnswer {let days: [DaysSpan] = objects(x, "days").compactMap { entry iniflet fold = o(entry, "fold") {guardlet from = s(fold, "from"), let to = s(fold, "to") else { returnnil }return .fold(DaysFold(from: from, to: to, count: i(fold, "count") ?? 0, words: s(fold, "words") ?? "")) }guardlet day = s(entry, "day") else { returnnil }return .day(DaysDay(day: day, number: s(entry, "number") ?? "", weekday: s(entry, "weekday") ?? "", month: s(entry, "month") ?? "", today: b(entry, "today"), rows: objects(entry, "rows").map(row))) }let end = o(x, "end").flatMap { e in s(e, "after").map { DaysEnd(after: $0, words: s(e, "words") ?? "") } }returnDaysSpanAnswer(today: s(x, "today") ?? "", days: days, end: end, begins: s(x, "begins")) }/// The groups in the core's order: the answer itself a list, or under `groups`.staticfunc groups(_ any: Any) -> [DaysGroup] {let list = (any as? [Any]) ?? ((any as? Object)?["groups"] as? [Any]) ?? []return list.compactMap { $0 as? Object }.map { g inDaysGroup(groupId: s(g, "id"), name: s(g, "name") ?? "", count: i(g, "count") ?? 0, rows: objects(g, "rows").map(row)) } }staticfunc groupCounts(_ x: Object) -> DaysGroups {DaysGroups(all: s(x, "all") ?? "", count: i(x, "count") ?? 0, groups: objects(x, "groups").compactMap { g inguardlet id = s(g, "id") else { returnnil }returnDaysGroupCount(id: id, name: s(g, "name") ?? "", count: i(g, "count") ?? 0) }) }staticfunc month(_ x: Object) -> DaysMonth {DaysMonth(month: s(x, "month") ?? "", title: s(x, "title") ?? "", weekStart: i(x, "weekStart") ?? 1, lead: i(x, "lead"), weeks: i(x, "weeks") ?? 6, days: objects(x, "days").map { DaysMonthDay(day: s($0, "day") ?? "", marks: strings($0, "marks"), other: b($0, "other"), today: b($0, "today")) }) }staticfunc readLine(_ x: Object) -> DaysReadLine {DaysReadLine( title: s(x, "title") ?? "", day: s(x, "day"), time: s(x, "time"), end: s(x, "end"), kind: DaysKind(s(x, "kind")), pill: s(x, "pill"), marks: strings(x, "marks"), said: objects(x, "said").compactMap { one inguardlet from = i(one, "from"), let to = i(one, "to") else { returnnil }returnDaysSaid(from: from, to: to, what: s(one, "what") ?? "") }) }staticfunc item(table: String, id: String, _ x: Object) -> DaysItem {let can = o(x, "canChange") ?? [:]returnDaysItem( table: table, id: id, whose: DaysWhose(s(x, "whose")), words: s(x, "words") ?? "", day: s(x, "day"), time: s(x, "time"), end: s(x, "end"), mark: s(x, "mark"), note: s(x, "note"),// `group` is `{ id, name }` (an older core answered the id alone). group: s(x, "group") ?? o(x, "group").flatMap { s($0, "id") }, alerts: objects(x, "alerts").compactMap { c inDaysAlert(c["alert"]).map { DaysAlertChoice(alert: $0, on: b(c, "on")) } }, done: b(x, "done"), repeats: b(x, "repeats"),// A to-do's group is changed through `groups.*` until `days.change` takes it (CONTRACT §3). canChange: DaysCanChange(title: b(can, "title"), time: b(can, "time"), mark: b(can, "mark"), note: b(can, "note"), group: table == "todo", alert: b(can, "alert")), says: s(x, "says"), choices: objects(x, "choices").compactMap { c in s(c, "key").map { DaysDayChoice(key: $0, day: s(c, "day")) } }, ends: ends(o(x, "ends")), people: people(o(x, "people")), invitation: invitation(o(x, "invitation")), deleteTells: s(x, "deleteTells")) }staticfunc added(_ x: Object) -> DaysAdded? {guardlet table = s(x, "table"), let id = s(x, "id") else { returnnil }returnDaysAdded(table: table, id: id, day: s(x, "day"), time: s(x, "time")) }staticfunc listed(_ x: Object) -> DaysListed {var lands: DaysLands?iflet l = o(x, "lands") {switch s(l, "place") {case"days": lands = s(l, "day").map(DaysLands.days)case"sometime": lands = .sometimecase"show": lands = s(l, "view").map(DaysLands.view)default: lands = nil } }returnDaysListed(row: row(x), day: s(x, "day"), dayWords: s(x, "dayWords") ?? "", rhythm: s(x, "rhythm"), dropped: b(x, "dropped"), lands: lands) }staticfunc shown(_ x: Object) -> DaysShown {DaysShown(view: s(x, "view") ?? "", title: s(x, "title") ?? "", rows: objects(x, "rows").map(listed), empty: s(x, "empty"), next: s(x, "next")) }staticfunc found(_ x: Object) -> DaysFound {DaysFound(coming: objects(x, "coming").map(listed), before: objects(x, "before").map(listed), empty: s(x, "empty")) }staticfunc catalogue(_ any: Any) -> [DaysCatalogueMark] {let list = (any as? [Any]) ?? ((any as? Object)?["marks"] as? [Any]) ?? []return list.compactMap { $0 as? Object }.compactMap { m inguardlet id = s(m, "id") else { returnnil }let words = (o(m, "words") ?? [:]).mapValues { ($0 as? [Any])?.compactMap { $0 as? String } ?? [] }returnDaysCatalogueMark(id: id, words: words, strokes: strings(m, "strokes")) } }}