// 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. import CottonKit import CottonValues import Foundation /// A list split by the day each record is next due. public typealias ListUpcoming = DefinitionListUpcoming extension DefinitionListUpcoming { public init(dates: [String], undated: String? = nil, late: String? = nil, later: String? = nil, horizon: Int = 7, series: String? = nil) { self.init(_generated: ()) self.dates = dates self.undated = undated self.late = late self.later = later self.horizon = horizon self.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. public typealias SlipBadge = DefinitionSlipBadge extension DefinitionSlipBadge { public init(column: String, symbol: String, spoken: String, values: [String]? = nil) { self.init(_generated: (), column: column, symbol: symbol, spoken: spoken) self.values = values } } extension TableDefinition { /// 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`. public func upcomingAt(_ record: TableRecord, now: Date = Date(), calendar: Calendar = .current) -> (at: Date, index: Int)? { guard let upcoming else { return nil } let today = calendar.startOfDay(for: now) var best: (at: Date, index: Int)? for (index, key) in upcoming.dates.enumerated() { guard let iso = record[key].text, let at = CottonDate.parse(iso, calendar: calendar) else { continue } if index > 0, at < today { continue } if let 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). public func createPreset(now: Date = Date(), calendar: Calendar = .current) -> [String: RecordValue] { var preset: [String: RecordValue] = [:] for column in columns { if let value = column.defaultValue, !RecordValue.isBlank(value) { preset[column.key] = value } } if let key = create?.dated, column(key)?.type == .date { preset[key] = .text(CottonDate.formatter("yyyy-MM-dd", calendar).string(from: now)) } return preset } }