// Keys a list reads so that work still to do reads as a day plan: the list// split by the day each record is next due, soonest first, with everything// owed from a day gone folded into Today (`upcoming`); the slip's foot saying// how late it is, the hour, or when a snooze brings it back; a small glyph on// the slip for a value worth seeing at a glance (`badge`: a todo that// repeats). Each is drawn once, for any table that names it (Todos was the// first: Today, Tomorrow, the later days, the undated last, then Done and// Dropped from its `group`).//// The order lives here, pure and over an injected clock; the words of a// slip's foot are the core's (`@slip_foot`, `@slip_strip`, resolved in// cotton-core's page_dates.rs) and `SlipFace` only draws them.importCottonKitimportCottonValuesimportFoundation/// A list split by the day each record is next due.publictypealiasListUpcoming = DefinitionListUpcomingextensionDefinitionListUpcoming {publicinit(dates: [String], undated: String? = nil, late: String? = nil, later: String? = nil, horizon: Int = 7, series: String? = nil) {self.init(_generated: ())self.dates = datesself.undated = undatedself.late = lateself.later = laterself.horizon = horizonself.series = series }}/// A glyph at a slip's foot while `column` holds a value: the todo's repeat/// mark. Spoken as `spoken`; the glyph itself is a plain system symbol.publictypealiasSlipBadge = DefinitionSlipBadgeextensionDefinitionSlipBadge {publicinit(column: String, symbol: String, spoken: String, values: [String]? = nil) {self.init(_generated: (), column: column, symbol: symbol, spoken: spoken)self.values = values }}extensionTableDefinition {/// The instant a record is next due: the latest of its `upcoming` dates/// that holds one, and which of them it was (0 is the record's own day)./// A later date (a snooze) counts only while it ends today or after: one/// that ended on a day gone is over, so the record is owed from its own/// day — late by that day, not by the snooze's — and a record with no/// day of its own is undated again, as the old board read a snooze./// Nil for a record with no day, or a table with no `upcoming`.publicfunc upcomingAt(_ record: TableRecord, now: Date = Date(), calendar: Calendar = .current) -> (at: Date, index: Int)? {guardlet upcoming else { returnnil }let today = calendar.startOfDay(for: now)var best: (at: Date, index: Int)?for (index, key) in upcoming.dates.enumerated() {guardlet iso = record[key].text, let at = CottonDate.parse(iso, calendar: calendar) else { continue }if index > 0, at < today { continue }iflet held = best, held.at >= at { continue } best = (at, index) }return best }/// The values a blank page for a new record starts with: every column's/// `default` (a new alarm is on), then today in the `create` key's/// `dated` column (a todo made from the list is due today).publicfunc createPreset(now: Date = Date(), calendar: Calendar = .current) -> [String: RecordValue] {var preset: [String: RecordValue] = [:]for column in columns {iflet value = column.defaultValue, !RecordValue.isBlank(value) { preset[column.key] = value } }iflet key = create?.dated, column(key)?.type == .date { preset[key] = .text(CottonDate.formatter("yyyy-MM-dd", calendar).string(from: now)) }return preset }}