Cotton code
59 lines · 2.8 KB · SwiftRaw
// Keys that let a list ask its owner something and take the answer: the// words an empty list says (`empty`), a row standing for another record that a// tap opens (`opens`), a slip's typed answers (`answers` — "Close without an// answer"), and a slip's answer in the owner's own words (`reply` — "Answer",// a sheet with the question and a ruled field). Each is drawn once, for any// table that names it; the questions assistants asked (`comment`, the /decide// list) were the first to need all four together.//// The spellings are the shared format's, as the Rust definitions decode them// (`crates/cotton-core/src/definitions`): `empty` is `EmptyWords`, `opens` is// `RecordOpens`, `answers` is `RecordAnswers`. `reply` is new on this side// (FORMAT CHANGE, filed for another Mac). Only `reply` is defined here: `EmptyWords`// and `emptyWords` live with the definition (`TableDefinition`,// `TableDefinition+Reading`), `RecordOpens` and `opening` in `PictureKeys`,// `SlipAnswers` and `isAnswered` (which reads `reply` too) in `AnswerKeys` —// one copy each.//// The reading lives here, pure, so the rules are settled by tests;// `SlipFace`, `ReplySheet` and `RecordListSurface` only draw them.import CottonValuesimport Foundation/// A slip's answer in the owner's own words: the text column the words are/// written to, the verb's words (absent: "Answer") and the field's words./// The words are typed, or spoken through the keyboard's dictation; blank/// words are no answer and send nothing.public typealias SlipReply = DefinitionSlipReplyextension DefinitionSlipReply {    public init(column: String, label: String? = nil, placeholder: String? = nil) {        self.init(_generated: (), column: column)        self.label = label        self.placeholder = placeholder    }    public var verb: String { label ?? "Answer" }}extension TableDefinition {    /// The slip's reply, while it may be given: a text column it names, on a    /// record whose question is still open. Nil otherwise.    public func replyOffered(_ record: TableRecord) -> SlipReply? {        guard let reply = slip.reply, let column = column(reply.column),            column.type == .text || column.type == .longtext, !isAnswered(record)        else { return nil }        return reply    }    /// What a reply writes: the words trimmed into the reply's column. Nil    /// for words that are only whitespace — a blank answer is no answer, and    /// sending one would settle the question with nothing in it to read.    public func replyPatch(_ words: String) -> [String: RecordValue]? {        guard let reply = slip.reply else { return nil }        let said = words.trimmingCharacters(in: .whitespacesAndNewlines)        guard !said.isEmpty else { return nil }        return [reply.column: .text(said)]    }}