Cotton code
283 lines · 11.5 KB · SwiftRaw
// The answers of Post's core calls (`mail.*`, crates/cotton-ffi/src/phone/chat_mail_post*.rs; assets/design/mail// CONTRACT.md "The core calls each screen needs"), as the screens read them. Every word in them is the core's; the// phone only places them.#if os(iOS)import CottonKitimport Foundation/// The `post` line set: the few words a screen reads directly (tabs, titles, buttons); everything else arrives in a/// call's answer already in words.enum PostWords {    static func line(_ key: String, _ fills: [String: String] = [:]) -> String { ShellLines.line("post", key, fills) }    /// A counted line (`one`, `other`, and `zero` where the set has it), its `{n}` filled.    static func count(_ key: String, _ n: Int) -> String {        let form = n == 0 && ShellLines.shipped("post", "\(key).zero") != nil ? "zero" : n == 1 ? "one" : "other"        return line("\(key).\(form)", ["n": n.formatted()])    }}/// A row's sender: a person's round face or a machine's square mark, with the initials it carries.struct PostFrom: Decodable, Equatable, Sendable {    struct Picture: Decodable, Equatable, Sendable { var initials: String? }    var name: String    var address: String?    var face: String?    var initials: String?    var picture: Picture?    /// A tap on the sender opens their page.    var sender: String?    var isPerson: Bool { face != "machine" }    var shownInitials: String {        if let initials = initials ?? picture?.initials, !initials.isEmpty { return initials }        return String(name.split(separator: " ").prefix(2).compactMap(\.first)).uppercased()    }}/// `MailRow` (CONTRACT "Shared shapes"): every list's row. A draft carries `draft` and its mark.struct PostRow: Decodable, Equatable, Sendable, Identifiable {    struct Account: Decodable, Equatable, Sendable { var tag: String }    var thread: String?    var email: String?    var draft: String?    var from: PostFrom    var subject: String    var line: String?    var when: String?    var unread: Bool = false    var count: Int = 1    var attachments: Int?    var account: Account?    var a11y: [String]?    /// Waiting on you: Reply or Open.    var act: String?    /// A draft's mark ("Draft").    var mark: String?    var id: String { draft ?? thread ?? email ?? subject }    enum CodingKeys: String, CodingKey { case thread, email, draft, from, subject, line, when, unread, count, attachments, account, a11y, act, mark }    init(from decoder: Decoder) throws {        let c = try decoder.container(keyedBy: CodingKeys.self)        thread = try c.decodeIfPresent(String.self, forKey: .thread).flatMap { $0.isEmpty ? nil : $0 }        email = try c.decodeIfPresent(String.self, forKey: .email)        draft = try c.decodeIfPresent(String.self, forKey: .draft)        from = try c.decode(PostFrom.self, forKey: .from)        subject = try c.decodeIfPresent(String.self, forKey: .subject) ?? ""        line = try c.decodeIfPresent(String.self, forKey: .line)        when = try c.decodeIfPresent(String.self, forKey: .when)        unread = try c.decodeIfPresent(Bool.self, forKey: .unread) ?? false        count = try c.decodeIfPresent(Int.self, forKey: .count) ?? 1        attachments = try c.decodeIfPresent(Int.self, forKey: .attachments)        account = try c.decodeIfPresent(Account.self, forKey: .account)        a11y = try c.decodeIfPresent([String].self, forKey: .a11y)        act = try c.decodeIfPresent(String.self, forKey: .act)        mark = try c.decodeIfPresent(String.self, forKey: .mark)    }}struct PostStatus: Decodable, Equatable, Sendable {    var line: String    var state: String?}struct PostHistory: Decodable, Equatable, Sendable {    var line: String    var paused: String?}/// `mail.home`: People, Waiting on you, From machines, the status line and the history./// FIRST SYNC STATES (`phone/first_sync.rs`): while his mail is still coming, an answer says so in place of empty or/// zero: `line` ("Loading your data…") and `seen`, Account's count ("Syncing 1,200 of 3,108", its percent).struct PostFirstSync: Decodable, Equatable, Sendable {    struct Seen: Decodable, Equatable, Sendable { var state: String?; var line: String?; var percent: Int? }    var state: String    var line: String    var seen: Seen?    /// A slow or missed answer mid-sync: the loading words to keep standing, and the small line under the count.    var bringing: String?    var trying: String?}struct PostHome: Decodable, Equatable, Sendable {    var firstSync: PostFirstSync?    struct Search: Decodable, Equatable, Sendable { var hint: String }    struct More: Decodable, Equatable, Sendable { var place: String; var label: String }    struct People: Decodable, Equatable, Sendable { var title: String; var line: String?; var rows: [PostRow]; var more: More? }    struct Waiting: Decodable, Equatable, Sendable { var title: String; var rows: [PostRow] }    struct Group: Decodable, Equatable, Sendable, Identifiable { var id: String; var title: String; var line: String?; var count: Int; var inbox: Int? }    struct Machines: Decodable, Equatable, Sendable { var title: String; var line: String?; var groups: [Group] }    /// Mail's chips (owner, 26 Nov 2026): All, People, Unread and the kinds, each the `mail.list` place it opens and its    /// unread count when above zero.    struct Chip: Decodable, Equatable, Sendable, Identifiable { var id: String; var label: String; var place: String; var count: String? }    var chips: [Chip]?    var title: String    var search: Search    var people: People    var waiting: Waiting?    var machines: Machines?    var status: PostStatus?    var history: PostHistory?    var write: String    var empty: String?}/// `mail.list {place}`: a group, the Inbox, or a place on the side page.struct PostList: Decodable, Equatable, Sendable {    var firstSync: PostFirstSync?    struct Why: Decodable, Equatable, Sendable { var line: String; var rules: String }    struct Bulk: Decodable, Equatable, Sendable, Identifiable { var act: String; var label: String; var count: Int; var id: String { act } }    struct Bin: Decodable, Equatable, Sendable { var line: String }    var title: String    var line: String?    var rows: [PostRow]    var why: Why?    var bulk: [Bulk]?    var after: String?    var empty: String?    var bin: Bin?}/// `mail.side`: inboxes, places and Gmail's labels.struct PostSide: Decodable, Equatable, Sendable {    struct Row: Decodable, Equatable, Sendable, Identifiable { var id: String; var title: String; var count: Int?; var tag: String? }    struct Band: Decodable, Equatable, Sendable { var title: String; var rows: [Row] }    var open: String    var close: String    var inboxes: Band    var places: Band    var labels: Band?    var you: String}/// `mail.thread`: a conversation of mail.struct PostThread: Decodable, Equatable, Sendable {    struct With: Decodable, Equatable, Sendable { var name: String; var address: String? }    struct Link: Decodable, Equatable, Sendable { var at: [Int]; var url: String }    struct Attachment: Decodable, Equatable, Sendable, Identifiable {        var index: Int        var name: String        var mime: String?        var size: String?        var can: Bool = false        var kept: Bool = false        var note: String?        var opens: Bool?        var line: String?        var id: Int { index }    }    struct Message: Decodable, Equatable, Sendable, Identifiable {        var email: String        var mine: Bool = false        var from: PostFrom        var to: String?        var cc: String?        var when: String        var subject: String?        var text: String        var links: [Link]?        var unread: Bool?        var attachments: [Attachment]        /// The quoted tail (On … wrote:, a forward's header, a closing run of >), folded under its own label.        var quoted: Quoted?        var id: String { email }    }    struct Quoted: Decodable, Equatable, Sendable { var label: String; var text: String; var links: [Link]? }    struct Act: Decodable, Equatable, Sendable { var mode: String; var label: String }    struct Acts: Decodable, Equatable, Sendable { var reply: Act; var reply_all: Act?; var forward: Act }    /// `on`: this sender's pictures load without asking (mail.pictures.always); `sender` is whose they are.    struct Pictures: Decodable, Equatable, Sendable { var line: String; var load: String; var always: String; var on: Bool?; var sender: String? }    struct Page: Decodable, Equatable, Sendable { var label: String; var pictures: Pictures }    var thread: String    var subject: String    var with: With    var who: String?    var messages: [Message]    var acts: Acts    var page: Page?    var quoted: String?}/// `mail.page`: the designed page landed on the phone, or that it is coming.struct PostPage: Decodable, Equatable, Sendable {    var path: String?    var pictures: String?    var coming: String?}/// `mail.file`: the attachment landed on the phone.struct PostOpened: Decodable, Equatable, Sendable {    var path: String    var mime: String?    var name: String?}/// One row of Files and of Search's files.struct PostFile: Decodable, Equatable, Sendable, Identifiable {    var email: String    var index: Int    var name: String    var kind: String    var size: String?    var from: String?    var when: String?    var line: String?    var can: Bool = false    var id: String { "\(email)/\(index)" }}/// `mail.files`.struct PostFiles: Decodable, Equatable, Sendable {    var firstSync: PostFirstSync?    struct Kind: Decodable, Equatable, Sendable, Identifiable { var id: String; var label: String; var on: Bool }    var title: String    var kinds: [Kind]    var rows: [PostFile]    var after: String?    var empty: String?}/// `mail.search`.struct PostSearch: Decodable, Equatable, Sendable {    struct Band<Row: Decodable & Equatable & Sendable>: Decodable, Equatable, Sendable { var title: String; var rows: [Row] }    var hint: String?    var people: Band<PostRow>?    var mail: Band<PostRow>?    var files: Band<PostFile>?    var empty: String?    enum CodingKeys: String, CodingKey { case hint, people, mail, files, empty }    init(from decoder: Decoder) throws {        let c = try decoder.container(keyedBy: CodingKeys.self)        hint = try c.decodeIfPresent(String.self, forKey: .hint)        // An empty search answers bare lists; a search with words answers titled bands.        people = try? c.decodeIfPresent(Band<PostRow>.self, forKey: .people)        mail = try? c.decodeIfPresent(Band<PostRow>.self, forKey: .mail)        files = try? c.decodeIfPresent(Band<PostFile>.self, forKey: .files)        empty = try c.decodeIfPresent(String.self, forKey: .empty)    }}/// `After` (CONTRACT "Shared shapes"): what an act really did, with Undo and the mailbox's line. It stays on screen/// until he leaves the page.struct PostAfter: Decodable, Equatable, Sendable, Identifiable {    struct Mailbox: Decodable, Equatable, Sendable { var state: String; var line: String; var retry: String? }    var after: String    var act: String?    var count: Int?    var line: String    var mailbox: Mailbox?    var undo: String?    var id: String { after }}/// A Post read: the answer, or the core's words for why it could not answer, said in the screen's own place.enum PostRead<Answer: Sendable>: Sendable {    case read(Answer)    case refused(String)    var answer: Answer? { if case .read(let answer) = self { answer } else { nil } }    var refusal: String? { if case .refused(let line) = self { line } else { nil } }}#endif