// Keys a page reads so the facts about someone read as they always did: the// ways to reach them (`reach`, on a text column holding several values —// `multiple`), their clock (`zone`), the day that comes round every year// (`yearly`), who wrote a long text and when (`provenance`), which way an// amount runs ("Example owes you", `signs`), and the `{first}` token that// names the record in a heading or a placeholder ("Is this Example?"). Each// is drawn once, for any table that names it (People was the first: the// contact card's phones and emails, "Their time 7:30 pm · tomorrow", "Turns// 34 on Mon 17 Aug", the summary's byline, "Example owes you").//// The reading lives here, pure, so the words are settled by tests;// `SlipFace` and `ReachLines` only draw them.importCottonKitimportCottonValuesimportFoundation/// Which kind of contact point a `reach` column holds.publictypealiasReachKind = DefinitionReachKind/// A `reach` column: what its values are, and — for phones — the column/// holding the channel the owner's conversation with them runs over/// ("whatsapp"), which sends the page's Message to WhatsApp. "phone", or/// {"kind", "channel"}; either form round-trips as it was written.publictypealiasColumnReach = DefinitionColumnReachextensionDefinitionColumnReach {publicinit(kind: ReachKind, channel: String? = nil) {guardlet channel else { self = .word(kind); return }var detail = DefinitionReachDetail(_generated: (), kind: kind) detail.channel = channelself = .detailed(detail) }publicvar kind: ReachKind {switchself {case .word(let kind): kindcase .detailed(let detail): detail.kind } }publicvar channel: String? {switchself {case .word: nilcase .detailed(let detail): detail.channel } }}/// Who wrote a long text (`by`: "user" for the owner, else the assistant's/// own label), when (`at`), and what has arrived since (`since`: words the/// engine answers, "3 notes since").publictypealiasColumnProvenance = DefinitionColumnProvenanceextensionDefinitionColumnProvenance {publicinit(by: String, at: String, since: String? = nil) {self.init(_generated: (), by: by, at: at)self.since = since }}/// An amount's words for the way it runs, each read with the figure/// unsigned: above nothing (`positive`), below (`negative`), and nothing at/// all (`zero`, a sentence of its own).publictypealiasAmountSigns = DefinitionAmountSignsextensionDefinitionAmountSigns {publicinit(positive: String, negative: String, zero: String? = nil) {self.init(_generated: (), positive: positive, negative: negative)self.zero = zero }}extensionTableDefinition {/// `text` with the `{first}` token read as the first word of the/// record's title ("Is this {first}?" → "Is this Example?"), or "them"/// when the record has no title — a heading never says "{first}"; and/// the `{title}` token as the words the record's lists are named by/// (`headingWords`: "No movements in Everyday yet"), or "this <name>"/// while it has none. Nil for nil.publicfunc words(_ text: String?, for record: TableRecord?) -> String? {guardvar text else { returnnil }if text.contains("{first}") {let first = record?.said?.slip.name.map(CardPerson.firstName) text = text.replacingOccurrences(of: "{first}", with: first ?? "them") }if text.contains("{title}") {let heading = record?.said?.slip.heading text = text.replacingOccurrences(of: "{title}", with: heading ?? "this \(self.name.lowercased())") }return text }/// The first phone column marked `reach`: where the page's quiet Call and/// Message stand.publicvar firstPhoneReach: ColumnDefinition? { columns.first { $0.reach?.kind == .phone } }}extensionFaceText {/// A several-value text column's values, in the order written: a list,/// or the JSON array an engine writes into text; a plain value is one./// Blank values are left out.publicstaticfunc values(_ value: RecordValue) -> [String] { choices(value).map { $0.trimmingCharacters(in: .whitespacesAndNewlines) }.filter { !$0.isEmpty } }/// "1992-08-17" → (1992, 8, 17); "--08-17" → (nil, 8, 17). Nil for/// anything else, or a month or day out of range.staticfunc yearlyParts(_ text: String) -> (year: Int?, month: Int, day: Int)? {let trimmed = text.trimmingCharacters(in: .whitespacesAndNewlines)let numbers: [Int]let year: Int?if trimmed.hasPrefix("--") { numbers = trimmed.dropFirst(2).split(separator: "-").compactMap { Int($0) }guard numbers.count == 2else { returnnil } year = nilreturn (1...12).contains(numbers[0]) && (1...31).contains(numbers[1]) ? (year, numbers[0], numbers[1]) : nil } numbers = trimmed.prefix(10).split(separator: "-").compactMap { Int($0) }guard numbers.count == 3, (1...12).contains(numbers[1]), (1...31).contains(numbers[2]) else { returnnil }return (numbers[0], numbers[1], numbers[2]) }/// How many days a month has in a year — asked of the calendar.staticfunc daysInMonth(year: Int, month: Int, calendar: Calendar) -> Int {var parts = DateComponents() (parts.year, parts.month, parts.day) = (year, month, 1)guardlet first = calendar.date(from: parts), let range = calendar.range(of: .day, in: .month, for: first)else { return31 }return range.count }/// The local midnight the day falls on in `year`, the day CLAMPED into/// the month — the whole 29 February rule, and a 31st in a 30-day month/// stays in its month rather than rolling into the next.staticfunc yearlyDay(month: Int, day: Int, year: Int, calendar: Calendar) -> Date? {var parts = DateComponents() (parts.year, parts.month) = (year, month) parts.day = min(day, daysInMonth(year: year, month: month, calendar: calendar))return calendar.date(from: parts) }}