Cotton code
638 lines · 23.2 KB · SwiftRaw
// The answers of Cinemas' streaming calls (`cinemas.home`, `cinemas.row`, `cinemas.title`, `cinemas.season`,// `cinemas.search`; assets/design/cinemas/CONTRACT-2.md), as the screens read them. Every word in them is the core's;// the phone only places them.import CottonKitimport Foundation/// The `cinemas` line set: the few words a screen reads directly (the places, the bars, the empty states); everything/// else arrives in a call's answer already in words.enum CinemasWords {    static func line(_ key: String, _ fills: [String: String] = [:]) -> String { ShellLines.line("cinemas", key, fills) }}extension KeyedDecodingContainer {    /// A count or a minute the core writes as a JSON number, whole or not (`124` or `124.0`).    func wholeIfPresent(_ key: Key) throws -> Int? {        if let whole = try? decodeIfPresent(Int.self, forKey: key) { return whole }        return try decodeIfPresent(Double.self, forKey: key).map { Int($0.rounded()) }    }}/// One title as a poster (CONTRACT-2 "Item").struct CinemasItem: Decodable, Hashable, Sendable, Identifiable {    enum Kind: String, Decodable, Sendable { case film, series, clip, episode }    enum OnPhone: String, Decodable, Sendable { case kept, keeping }    var table: String    var id: String    var title: String    var year: String?    var kind: Kind    var poster: String?    var backdrop: String?    var caption: String?    var progress: Double?    var onPhone: OnPhone?    var preparing: Bool    var from: String?    var minutes: Int?    /// The core's drawn picture for it, worn when it has no picture (an episode wears its series').    var art: CinemasArt?    enum CodingKeys: String, CodingKey {        case table, id, title, year, kind, poster, backdrop, caption, progress, preparing, from, minutes, art        case onPhone = "on_phone"    }    init(table: String, id: String, title: String, year: String? = nil, kind: Kind, poster: String? = nil,         backdrop: String? = nil, caption: String? = nil, progress: Double? = nil, onPhone: OnPhone? = nil,         preparing: Bool = false, from: String? = nil, minutes: Int? = nil) {        self.table = table        self.id = id        self.title = title        self.year = year        self.kind = kind        self.poster = poster        self.backdrop = backdrop        self.caption = caption        self.progress = progress        self.onPhone = onPhone        self.preparing = preparing        self.from = from        self.minutes = minutes    }    init(from decoder: any Decoder) throws {        let c = try decoder.container(keyedBy: CodingKeys.self)        table = try c.decode(String.self, forKey: .table)        id = try c.decode(String.self, forKey: .id)        title = try c.decode(String.self, forKey: .title)        year = try c.decodeIfPresent(String.self, forKey: .year)        // A kind the phone does not know yet draws as a film, as the core's Films row reads it.        kind = (try? c.decode(Kind.self, forKey: .kind)) ?? .film        poster = try c.decodeIfPresent(String.self, forKey: .poster)        backdrop = try c.decodeIfPresent(String.self, forKey: .backdrop)        caption = try c.decodeIfPresent(String.self, forKey: .caption)        progress = try c.decodeIfPresent(Double.self, forKey: .progress)        onPhone = try? c.decodeIfPresent(OnPhone.self, forKey: .onPhone)        preparing = try c.decodeIfPresent(Bool.self, forKey: .preparing) ?? false        from = try c.decodeIfPresent(String.self, forKey: .from)        minutes = try c.wholeIfPresent(.minutes)        art = try? c.decodeIfPresent(CinemasArt.self, forKey: .art)    }    /// The record this poster stands for, as a key for the shared-element transition and the player.    var key: String { "\(table)/\(id)" }}/// `cinemas.home {}`.struct CinemasHome: Decodable, Equatable, Sendable {    struct Featured: Decodable, Equatable, Sendable {        var item: CinemasItem        var line: String?        var play: String        var tv: String    }    struct Row: Decodable, Equatable, Sendable, Identifiable {        var id: String        var title: String        var group: String?        var count: Int        var items: [CinemasItem]        var more: Bool        /// Inside Hidden, a hidden category the Bin holds (HIDE FROM SCREEN core 3): "In the Bin", with Restore.        var binned: String? = nil        var restore: CinemasNamedAct? = nil        /// A row that is a place rather than titles (inside Hidden, its Bin: `cinemas.bin {hidden: true}`, core 4).        var act: RowAct? = nil    }    struct RowAct: Decodable, Equatable, Sendable {        var method: String        var request: JSONValue?    }    struct Empty: Decodable, Equatable, Sendable {        var title: String        var line: String        var add: String    }    var featured: Featured?    var rows: [Row]    var empty: Empty?}/// `cinemas.row {id, after?}`: all of one row, a page at a time.struct CinemasRowPage: Decodable, Equatable, Sendable {    /// One of the row's filters (frame 2): All, Not watched, On this phone, A–Z, Newest; one is on.    struct Chip: Decodable, Equatable, Sendable, Identifiable {        var id: String        var label: String        var on: Bool    }    var title: String    var items: [CinemasItem]    var next: String?    /// How many titles the row holds under its filter; none from an older core.    var count: Int?    var chips: [Chip]?    var filter: String?}/// `cinemas.title {table, id}`.struct CinemasTitle: Decodable, Equatable, Sendable {    struct Play: Decodable, Equatable, Sendable {        var label: String        var fromStart: String?        enum CodingKeys: String, CodingKey {            case label            case fromStart = "from_start"        }    }    struct Act: Decodable, Equatable, Sendable, Identifiable {        var id: String        var label: String        /// The row's word ("Keep"), where the label is longer ("Keep on this phone").        var short: String?        /// `row` stands in the row of four; `more` waits behind More.        var place: String?        /// A second line under a row act: the size under Keep, the track under Subtitles.        var second: String?        init(id: String, label: String, short: String? = nil, place: String? = nil, second: String? = nil) {            self.id = id            self.label = label            self.short = short            self.place = place            self.second = second        }    }    struct Group: Decodable, Equatable, Sendable, Identifiable {        var id: String        var name: String        var `in`: Bool        /// Asked from inside Hidden (`hidden: true`): a hidden category says so. Outside, none is listed.        var hidden: Bool? = nil    }    struct Subtitles: Decodable, Equatable, Sendable {        var line: String?        var work: String?    }    struct Preparing: Decodable, Equatable, Sendable { var line: String }    struct Season: Decodable, Equatable, Sendable, Identifiable {        var id: String        var number: Int        var title: String        var episodes: Int?        var watched: Int?        enum CodingKeys: String, CodingKey { case id, number, title, episodes, watched }        init(id: String, number: Int, title: String?, episodes: Int? = nil, watched: Int? = nil) {            self.id = id            self.number = number            self.title = title ?? ShellLines.line("cinemas", "intake.season_title", ["number": "\(number)"])            self.episodes = episodes            self.watched = watched        }        init(from decoder: any Decoder) throws {            let c = try decoder.container(keyedBy: CodingKeys.self)            self.init(id: try c.decode(String.self, forKey: .id), number: try c.wholeIfPresent(.number) ?? 0,                      title: try c.decodeIfPresent(String.self, forKey: .title),                      episodes: try c.wholeIfPresent(.episodes), watched: try c.wholeIfPresent(.watched))        }    }    struct Source: Decodable, Equatable, Sendable {        var url: String        var by: String?    }    var item: CinemasItem    var synopsis: String?    var facts: [String]    var play: Play    var tv: String    var acts: [Act]    var groups: [Group]    var subtitles: Subtitles?    var preparing: Preparing?    var seasons: [Season]?    var source: Source?    /// "28 min watched · 1h 53m left" under a part-watched title's bar.    var watchedLine: String?    enum CodingKeys: String, CodingKey {        case item, synopsis, facts, play, tv, acts, groups, subtitles, preparing, seasons, source        case watchedLine = "watched_line"    }    init(item: CinemasItem, synopsis: String?, facts: [String], play: Play, tv: String, acts: [Act], groups: [Group],         subtitles: Subtitles?, preparing: Preparing?, seasons: [Season]?, source: Source?) {        self.item = item        self.synopsis = synopsis        self.facts = facts        self.play = play        self.tv = tv        self.acts = acts        self.groups = groups        self.subtitles = subtitles        self.preparing = preparing        self.seasons = seasons        self.source = source    }    init(from decoder: any Decoder) throws {        let c = try decoder.container(keyedBy: CodingKeys.self)        item = try c.decode(CinemasItem.self, forKey: .item)        synopsis = try c.decodeIfPresent(String.self, forKey: .synopsis)        facts = try c.decodeIfPresent([String].self, forKey: .facts) ?? []        play = try c.decode(Play.self, forKey: .play)        tv = try c.decode(String.self, forKey: .tv)        acts = try c.decodeIfPresent([Act].self, forKey: .acts) ?? []        groups = try c.decodeIfPresent([Group].self, forKey: .groups) ?? []        subtitles = try c.decodeIfPresent(Subtitles.self, forKey: .subtitles)        preparing = try c.decodeIfPresent(Preparing.self, forKey: .preparing)        seasons = try c.decodeIfPresent([Season].self, forKey: .seasons)        source = try c.decodeIfPresent(Source.self, forKey: .source)        watchedLine = try c.decodeIfPresent(String.self, forKey: .watchedLine)    }}/// `cinemas.season {id}`: a season's episodes, each a poster's facts plus its number, name, length and still.struct CinemasSeason: Decodable, Equatable, Sendable {    struct Episode: Decodable, Equatable, Sendable, Identifiable {        var item: CinemasItem        var number: Int        var title: String        var minutes: Int?        var still: String?        var synopsis: String?        /// "watched" or "23 min left", in the core's words.        var stateLine: String?        /// The core's name for the row ("2. Reeds"); none from an older core.        var label: String?        var id: String { item.id }        enum CodingKeys: String, CodingKey { case number, title, minutes, still, synopsis, label            case episodeTitle = "episode_title"            case stateLine = "state_line"        }        init(item: CinemasItem, number: Int, title: String, minutes: Int?, still: String?, synopsis: String? = nil) {            self.item = item            self.number = number            self.title = title            self.minutes = minutes            self.still = still            self.synopsis = synopsis        }        /// The contract writes an episode as an Item with its own fields beside it, in one object.        init(from decoder: any Decoder) throws {            item = try CinemasItem(from: decoder)            let c = try decoder.container(keyedBy: CodingKeys.self)            number = try c.wholeIfPresent(.number) ?? 0            // The core names an episode's own title `episode_title`; the item's title is the same words.            title = try c.decodeIfPresent(String.self, forKey: .episodeTitle) ?? item.title            minutes = item.minutes            still = try c.decodeIfPresent(String.self, forKey: .still)            stateLine = try c.decodeIfPresent(String.self, forKey: .stateLine)            synopsis = try c.decodeIfPresent(String.self, forKey: .synopsis)            label = try c.decodeIfPresent(String.self, forKey: .label)        }    }    struct Head: Decodable, Equatable, Sendable {        var id: String        var number: Int        var title: String?        enum CodingKeys: String, CodingKey { case id, number, title }        init(id: String, number: Int, title: String?) {            self.id = id            self.number = number            self.title = title        }        init(from decoder: any Decoder) throws {            let c = try decoder.container(keyedBy: CodingKeys.self)            id = try c.decode(String.self, forKey: .id)            number = try c.wholeIfPresent(.number) ?? 0            title = try c.decodeIfPresent(String.self, forKey: .title)        }    }    var season: Head    var episodes: [Episode]}/// `cinemas.search {q}`.struct CinemasFound: Decodable, Equatable, Sendable {    struct Group: Decodable, Equatable, Sendable, Identifiable {        var id: String        var name: String    }    /// A film's director or a video's channel, with how many of his titles are theirs.    struct Person: Decodable, Equatable, Sendable, Identifiable {        var name: String        var kind: String        var ids: [String]        var line: String?        var id: String { kind + "/" + name }    }    var items: [CinemasItem]    var groups: [Group]    /// The top result's line ("Film · 2021 · 1h 12m left"); none from an older core.    var topLine: String?    var people: [Person] = []    enum CodingKeys: String, CodingKey {        case items, groups, people        case topLine = "top_line"    }    init(items: [CinemasItem], groups: [Group], topLine: String? = nil, people: [Person] = []) {        self.items = items        self.groups = groups        self.topLine = topLine        self.people = people    }    init(from decoder: any Decoder) throws {        let c = try decoder.container(keyedBy: CodingKeys.self)        items = try c.decodeIfPresent([CinemasItem].self, forKey: .items) ?? []        groups = try c.decodeIfPresent([Group].self, forKey: .groups) ?? []        topLine = try c.decodeIfPresent(String.self, forKey: .topLine)        people = (try? c.decodeIfPresent([Person].self, forKey: .people)) ?? []    }}/// `groups.list {table: "film"}`: his groups, in his order.struct CinemasGroups: Decodable, Equatable, Sendable {    struct Group: Decodable, Equatable, Sendable, Identifiable {        var id: String        var name: String        var count: Int?        /// With `record` asked: whether that title is in this group.        var holds: Bool?        /// Asked from inside Hidden: a hidden category says so.        var hidden: Bool?        enum CodingKeys: String, CodingKey { case id, name, count, holds, hidden }        init(id: String, name: String, count: Int? = nil, holds: Bool? = nil, hidden: Bool? = nil) {            self.id = id            self.name = name            self.count = count            self.holds = holds            self.hidden = hidden        }        init(from decoder: any Decoder) throws {            let c = try decoder.container(keyedBy: CodingKeys.self)            id = try c.decode(String.self, forKey: .id)            name = try c.decodeIfPresent(String.self, forKey: .name) ?? ""            count = try c.wholeIfPresent(.count)            holds = try c.decodeIfPresent(Bool.self, forKey: .holds)            hidden = try c.decodeIfPresent(Bool.self, forKey: .hidden)        }    }    var groups: [Group]}/// `cinemas.hidden {unlocked, no_lock?}` (CONTRACT-2 HIDDEN): the hidden place, asked only once the phone's lock has/// passed. Its rows are Carry on (hidden titles only), then each hidden category in his order; locked, none.struct CinemasHiddenPlace: Decodable, Equatable, Sendable {    var title: String    var locked: Bool    var rows: [CinemasHome.Row]    var searchHint: String?    var empty: String?    /// With no screen lock: the honest line, and the act that opens the phone's settings.    var noLock: String?    var setLock: String?    /// Locked: the words of the button that opens it.    var unlock: String?    enum CodingKeys: String, CodingKey {        case title, locked, rows, empty, unlock        case searchHint = "search_hint"        case noLock = "no_lock"        case setLock = "set_lock"    }    init(title: String, locked: Bool, rows: [CinemasHome.Row], searchHint: String? = nil, empty: String? = nil,         noLock: String? = nil, setLock: String? = nil, unlock: String? = nil) {        self.unlock = unlock        self.title = title        self.locked = locked        self.rows = rows        self.searchHint = searchHint        self.empty = empty        self.noLock = noLock        self.setLock = setLock    }    init(from decoder: any Decoder) throws {        let c = try decoder.container(keyedBy: CodingKeys.self)        title = try c.decodeIfPresent(String.self, forKey: .title) ?? ""        locked = try c.decodeIfPresent(Bool.self, forKey: .locked) ?? false        rows = try c.decodeIfPresent([CinemasHome.Row].self, forKey: .rows) ?? []        searchHint = try c.decodeIfPresent(String.self, forKey: .searchHint)        empty = try c.decodeIfPresent(String.self, forKey: .empty)        noLock = try c.decodeIfPresent(String.self, forKey: .noLock)        setLock = try c.decodeIfPresent(String.self, forKey: .setLock)        unlock = try c.decodeIfPresent(String.self, forKey: .unlock)    }}/// `cinemas.hide {id, hidden}` → the category as it now stands and the core's words for it ("Rainy Sundays is hidden").struct CinemasHid: Decodable, Equatable, Sendable {    /// What Undo runs, exactly as the core handed it back (a Hide's carries a one-use token, so Stop hiding works once    /// without Hidden's unlock).    struct Undo: Decodable, Equatable, Sendable {        var method: String        var args: JSONValue    }    var id: String    var hidden: Bool    var line: String?    var undo: Undo? = nil    /// Asked for the state it already had: nothing changed, no Undo.    var unchanged: Bool? = nil}/// A read's outcome: the answer, or the core's refusal in its own words./// `cinemas.onPhone {free_bytes}`: what this phone keeps (frame 10).struct CinemasOnPhone: Decodable, Equatable, Sendable {    struct Flight: Decodable, Equatable, Sendable {        var line: String        var what: String    }    struct Kept: Decodable, Equatable, Sendable, Identifiable {        var item: CinemasItem        var line: String        var id: String { item.key }    }    var title: String    var space: String?    var flight: Flight?    var items: [Kept]    var empty: String?}/// `cinemas.preparing {table, id}`: what the Mac has done and is doing (frame 11).struct CinemasPreparing: Decodable, Equatable, Sendable {    struct Step: Decodable, Equatable, Sendable {        var label: String        var state: String    }    var head: String    var steps: [Step]    var queue: String?    var note: String?    var ready: Bool}/// `cinemas.remote {table, id, at_s, length_s, device}`: the remote's words (frame 8).struct CinemasRemoteWords: Decodable, Equatable, Sendable {    struct Next: Decodable, Equatable, Sendable {        var table: String        var id: String        var label: String    }    var playing: String    var title: String    var caption: String?    var at: String    var left: String?    var next: Next?    var volume: String?}enum CinemasRead<Answer: Sendable>: Sendable {    case read(Answer)    case refused(String)    var answer: Answer? {        if case .read(let answer) = self { return answer }        return nil    }}/// `cinemas.subtitles {table, id}`: a title's subtitle tracks, each saying where it came from, the chosen one ticked;/// what the Mac is doing about them; and the acts that ask it for more (PUNCH Both 4).struct CinemasSubtitles: Decodable, Equatable, Sendable {    struct Track: Decodable, Equatable, Sendable, Identifiable {        var language: String        var name: String        var from: String?        var chosen: Bool        var id: String { language + "/" + name }    }    struct Act: Decodable, Equatable, Sendable, Identifiable {        var id: String        var label: String    }    var title: String    var line: String    var tracks: [Track]    var working: String?    var acts: [Act]}/// A title sent to a friend (`send.make`): its id, for Undo, and the receipt's words.struct CinemasSent: Equatable, Sendable {    var id: String    var words: String?    var undo: String?    var undone: String?}/// `cinemas.bin {}`: the Bin, each title with its line; `empty` when nothing waits there.struct CinemasBin: Decodable, Equatable, Sendable {    var title: String    var items: [CinemasOnPhone.Kept]    var empty: String?    /// Core 4: which Bin this is (inside Hidden: the hidden titles alone), and its acts, named by the core.    var hidden: Bool? = nil    var count: Int? = nil    var restore: CinemasNamedAct? = nil    var erase: CinemasNamedAct? = nil}/// `cinemas.you {free_bytes}` (frame 12): Cinemas' sections under the account card, each row with its words, what it/// holds now and the act that changes or opens it: `switch` rows are turned by their act; `open` names a place the/// phone opens itself (`tv`: its TV picker; `help_ready`: the help line, shown in place).struct CinemasYou: Decodable, Equatable, Sendable {    struct Act: Decodable, Equatable, Sendable {        var method: String?        var request: JSONValue?        var open: String?    }    struct Row: Decodable, Equatable, Sendable, Identifiable {        var id: String        var label: String        var value: String?        var sub: String?        var `switch`: Bool?        var act: Act?    }    struct Section: Decodable, Equatable, Sendable, Identifiable {        var id: String        var title: String        var rows: [Row]    }    var sections: [Section]}/// An act the core names with its method and arguments (a hidden category's Restore: `cinemas.restore {id}`; the Bin's/// Restore and Delete forever, asked for one title with its `id` added).struct CinemasNamedAct: Decodable, Equatable, Sendable {    var label: String    var method: String    var args: [String: JSONValue]    /// One argument as text (a category's id), for an accessibility identifier.    func text(_ key: String) -> String? {        if case .string(let value)? = args[key] { value } else { nil }    }}/// A row of Home or Hidden: a place row (Hidden's Bin) carries no titles, so those default.extension CinemasHome.Row {    enum CodingKeys: String, CodingKey { case id, title, group, count, items, more, binned, restore, act }    init(from decoder: any Decoder) throws {        let c = try decoder.container(keyedBy: CodingKeys.self)        id = try c.decode(String.self, forKey: .id)        title = try c.decode(String.self, forKey: .title)        group = try c.decodeIfPresent(String.self, forKey: .group)        count = try c.decodeIfPresent(Int.self, forKey: .count) ?? 0        items = try c.decodeIfPresent([CinemasItem].self, forKey: .items) ?? []        more = try c.decodeIfPresent(Bool.self, forKey: .more) ?? false        binned = try c.decodeIfPresent(String.self, forKey: .binned)        restore = try c.decodeIfPresent(CinemasNamedAct.self, forKey: .restore)        act = try c.decodeIfPresent(CinemasHome.RowAct.self, forKey: .act)    }}