// 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. import CottonKit import CottonValues import Foundation /// Which kind of contact point a `reach` column holds. public typealias ReachKind = 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. public typealias ColumnReach = DefinitionColumnReach extension DefinitionColumnReach { public init(kind: ReachKind, channel: String? = nil) { guard let channel else { self = .word(kind); return } var detail = DefinitionReachDetail(_generated: (), kind: kind) detail.channel = channel self = .detailed(detail) } public var kind: ReachKind { switch self { case .word(let kind): kind case .detailed(let detail): detail.kind } } public var channel: String? { switch self { case .word: nil case .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"). public typealias ColumnProvenance = DefinitionColumnProvenance extension DefinitionColumnProvenance { public init(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). public typealias AmountSigns = DefinitionAmountSigns extension DefinitionAmountSigns { public init(positive: String, negative: String, zero: String? = nil) { self.init(_generated: (), positive: positive, negative: negative) self.zero = zero } } extension TableDefinition { /// `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 " /// while it has none. Nil for nil. public func words(_ text: String?, for record: TableRecord?) -> String? { guard var text else { return nil } 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. public var firstPhoneReach: ColumnDefinition? { columns.first { $0.reach?.kind == .phone } } } extension FaceText { /// 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. public static func 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. static func 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 == 2 else { return nil } year = nil return (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 { return nil } return (numbers[0], numbers[1], numbers[2]) } /// How many days a month has in a year — asked of the calendar. static func daysInMonth(year: Int, month: Int, calendar: Calendar) -> Int { var parts = DateComponents() (parts.year, parts.month, parts.day) = (year, month, 1) guard let first = calendar.date(from: parts), let range = calendar.range(of: .day, in: .month, for: first) else { return 31 } 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. static func 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) } }