Cotton code
428 lines · 16.5 KB · SwiftRaw
// ALBUMS' CALLS (assets/design/albums/DECISIONS.md "SHELL BRIEF"): every screen asks the core `albums.*` and draws what// comes back. The keys here are the contract. Never a count, a size, a word, a set or a pick made in the shell: they are// the core's (ALBUMS CORE 1). Until it lands, `AlbumsFixture` answers these calls on the phone, from what the library// and the pass hold, with the same keys; switching is one line (`AlbumsBook.live`).#if os(iOS)import CottonKitimport Foundation/// One value of a request.enum AlbumsValue: Encodable, Sendable, Equatable {    case text(String), flag(Bool), number(Int), list([String]), rows([[String: AlbumsValue]]), none    func encode(to encoder: Encoder) throws {        var c = encoder.singleValueContainer()        switch self {        case .text(let s): try c.encode(s)        case .flag(let b): try c.encode(b)        case .number(let n): try c.encode(n)        case .list(let l): try c.encode(l)        case .rows(let r): try c.encode(r)        case .none: try c.encodeNil()        }    }}// MARK: - Answers/// The pass's lines at the top of Albums and Tidy: each may be null.struct AlbumsPassLines: Decodable, Equatable, Sendable {    var head: String?    var detail: String?    var note: String?    var cloud: String?    var fraction: Double?    var paused: Bool?}struct AlbumsStart: Decodable, Equatable, Sendable {    var screen: String    var firstRun: FirstRun?    var noPermission: NoPermission?    var limited: Limited?    struct FirstRun: Decodable, Equatable, Sendable {        var title: String        var lead: String        var promise: String        var nextHead: String        var nextLine: String        var faces: Faces?        var start: String        enum CodingKeys: String, CodingKey { case title, lead, promise, faces, start, nextHead = "next_head", nextLine = "next_line" }    }    /// Faces are on by default: one plain line and a switch (the coordinator, 27 Nov).    struct Faces: Decodable, Equatable, Sendable { var label: String; var line: String; var on: Bool }    struct NoPermission: Decodable, Equatable, Sendable { var title: String; var line: String; var act: String }    struct Limited: Decodable, Equatable, Sendable { var line: String; var act: String }    enum CodingKeys: String, CodingKey { case screen, limited, firstRun = "first_run", noPermission = "no_permission" }}struct AlbumsHome: Decodable, Equatable, Sendable {    var title: String    var make: String    var progress: AlbumsPassLines?    var limited: AlbumsStart.Limited?    var rows: [Row]    var fromPhotos: FromPhotos?    var allPhotos: AllPhotos    var empty: String?    /// The albums Albums will make itself (people, places, trips), shown as coming until it makes them.    var coming: FromPhotos?    struct Row: Decodable, Equatable, Sendable, Identifiable {        var id: String        var title: String        var line: String?        var count: String?        var cover: String?        var kind: String?    }    struct FromPhotos: Decodable, Equatable, Sendable { var heading: String; var rows: [Row] }    struct AllPhotos: Decodable, Equatable, Sendable { var title: String; var count: String }    enum CodingKeys: String, CodingKey { case title, make, progress, limited, rows, empty, coming, fromPhotos = "from_photos", allPhotos = "all_photos" }}/// An album, or All photos: its photos by day (or month), each heading with its count.struct AlbumsAlbum: Decodable, Equatable, Sendable {    var title: String    var line: String?    var mine: Bool    var groups: [Group]    var empty: String?    var menu: Menu?    struct Group: Decodable, Equatable, Sendable, Identifiable {        var heading: String        var count: String?        var ids: [String]        var id: String { heading + (ids.first ?? "") }    }    struct Menu: Decodable, Equatable, Sendable {        var rename: String        var delete: String        var deleteAsk: String        var deleteLine: String        enum CodingKeys: String, CodingKey { case rename, delete, deleteAsk = "delete_ask", deleteLine = "delete_line" }    }}struct AlbumsTidy: Decodable, Equatable, Sendable {    var title: String    var progress: AlbumsPassLines?    var bin: Bin?    var couldFree: String?    var heading: String?    var lists: [List]    var empty: Empty?    struct Bin: Decodable, Equatable, Sendable { var line: String; var act: String }    struct List: Decodable, Equatable, Sendable, Identifiable {        var list: String        var name: String        var count: String        var size: String?        var cover: String?        var still: Bool?        var id: String { list }    }    enum CodingKeys: String, CodingKey { case title, progress, bin, heading, lists, empty, couldFree = "could_free" }}struct AlbumsEmptyWords: Decodable, Equatable, Sendable { var title: String; var line: String }typealias Empty = AlbumsEmptyWordsstruct AlbumsSets: Decodable, Equatable, Sendable {    var title: String    var back: String    var head: String?    var rows: [Row]    var nextFrom: Int?    var empty: Empty?    struct Row: Decodable, Equatable, Sendable, Identifiable {        var set: String        var count: String        var when: String        var ask: String        var keep: String        var strip: [String]        /// How many photos the Set holds: what Keep on the row carries as its count.        var photos: Int        var id: String { self.set }    }    enum CodingKeys: String, CodingKey { case title, back, head, rows, empty, nextFrom = "next_from" }}struct AlbumsSet: Decodable, Equatable, Sendable {    var set: String    var back: String    var position: String?    var title: String    var when: String?    var members: [Member]    var line: String    var button: String    var keepAll: String    var gone: Bool?    struct Member: Decodable, Equatable, Sendable, Identifiable { var id: String; var keep: Bool }    enum CodingKeys: String, CodingKey { case set, back, position, title, when, members, line, button, gone, keepAll = "keep_all" }}/// What a keep, a move to the Bin or a Bring back answers.struct AlbumsDone: Decodable, Equatable, Sendable {    var title: String?    var after: String?    var afterLine: String?    var said: String?    var undo: Undo?    var kept: [String]?    var next: Next?    var stays: String?    var platform: AlbumsPlatform?    /// What was on screen is no longer what the core holds: nothing was done; the screen reads again and redraws.    var changed: Bool?    struct Undo: Decodable, Equatable, Sendable { var label: String; var token: String }    struct Next: Decodable, Equatable, Sendable { var label: String; var set: String? }    enum CodingKeys: String, CodingKey { case title, after, said, undo, kept, next, stays, platform, changed, afterLine = "after_line" }}/// The system acts once per part, in order (iPhone: a delete), then tells the core what is still there.struct AlbumsPlatform: Decodable, Equatable, Sendable {    var act: String    var parts: [[String]]}struct AlbumsList: Decodable, Equatable, Sendable {    var list: String    var title: String    var back: String    var head: String?    var groups: [Group]    var select: String    var done: String    var binSelected: String    var keepSelected: String?    var binAll: BinAll?    var nextFrom: Int?    var empty: Empty?    struct Group: Decodable, Equatable, Sendable, Identifiable {        var heading: String?        var items: [Item]        var id: String { (heading ?? "") + (items.first?.id ?? "") }    }    struct Item: Decodable, Equatable, Sendable, Identifiable {        var id: String        var size: String?        var when: String?        var video: Bool?        var duration: String?        var original: String?    }    struct BinAll: Decodable, Equatable, Sendable {        var label: String        var confirm: Confirm    }    struct Confirm: Decodable, Equatable, Sendable { var question: String; var line: String; var yes: String; var no: String }    enum CodingKeys: String, CodingKey {        case list, title, back, head, groups, select, done, empty        case binSelected = "bin_selected", keepSelected = "keep_selected", binAll = "bin_all", nextFrom = "next_from"    }}struct AlbumsBin: Decodable, Equatable, Sendable {    var title: String    var back: String    var head: String?    var note: String?    var groups: [Group]    var act: Act?    var empty: Empty?    struct Group: Decodable, Equatable, Sendable, Identifiable {        var heading: String        var bringBack: String        var ids: [String]        var id: String { heading + (ids.first ?? "") }        enum CodingKeys: String, CodingKey { case heading, ids, bringBack = "bring_back" }    }    struct Act: Decodable, Equatable, Sendable {        var label: String        var question: String        var line: String        var noWayBack: String        var note: String?        var yes: String        var no: String        enum CodingKeys: String, CodingKey { case label, question, line, note, yes, no, noWayBack = "no_way_back" }    }}struct AlbumsPeople: Decodable, Equatable, Sendable {    var title: String    var head: String?    var off: Off?    var named: [Person]    var who: Who?    var looking: String?    struct Off: Decodable, Equatable, Sendable {        var lead: String        var points: [String]        var notNow: String        var turnOn: String        enum CodingKeys: String, CodingKey { case lead, points, notNow = "not_now", turnOn = "turn_on" }    }    struct Person: Decodable, Equatable, Sendable, Identifiable { var id: String; var name: String; var count: String; var cover: String? }    struct Who: Decodable, Equatable, Sendable { var heading: String; var groups: [Group] }    struct Group: Decodable, Equatable, Sendable, Identifiable {        var id: String        var line: String        var covers: [String]        var name: String    }}struct AlbumsYou: Decodable, Equatable, Sendable {    var rows: [Row]    var privacy: String?    var facesOff: Ask?    /// What the first look found and how fast, as plain counts (the look-only build's "This first look").    var firstLook: Section?    struct Section: Decodable, Equatable, Sendable { var heading: String; var rows: [Row] }    struct Row: Decodable, Equatable, Sendable, Identifiable {        var row: String        var label: String        var value: String?        var id: String { row }    }    struct Ask: Decodable, Equatable, Sendable { var question: String; var line: String; var yes: String; var no: String }    enum CodingKeys: String, CodingKey { case rows, privacy, facesOff = "faces_off", firstLook = "first_look" }}struct AlbumsSaid: Decodable, Equatable, Sendable { var said: String? }/// A method error's words: `gone` goes back a screen quietly; anything else says the family's failure line.struct AlbumsRefusal: Error, Equatable {    let code: String    var gone: Bool { code == "gone" }}// MARK: - The callsstruct AlbumsBook: Sendable {    /// The real core answers `albums.*` once ALBUMS CORE 1 lands; until then the phone's fixture does.    static let live = false    let core: any CottonCalling    init(core: any CottonCalling) {        self.core = Self.live ? core : AlbumsFixture.shared    }    /// The first TestFlight build only looks (the owner, 27 Nov): no Keep, Bin, Delete or Bring back is drawn anywhere,    /// the Bin cannot be reached, and one quiet line says why. It reads the build itself: an app built with    /// `photoWrites: false` (COTTON_NO_PHOTO_WRITES) installs no photo writer, so `AlbumsLibrary.canWrite` is false.    static var lookOnly: Bool { !AlbumsLibrary.canWrite }    /// For tests: a given core, as it is.    init(answering core: any CottonCalling) { self.core = core }    func call<A: Decodable & Sendable>(_ method: String, _ fields: [String: AlbumsValue] = [:], as: A.Type) async throws -> A {        do { return try await core.call(method, fields, as: A.self) } catch let error as CottonCallError {            throw AlbumsRefusal(code: error.code)        }    }    func start(permission: String, chosen: Int?) async throws -> AlbumsStart {        var fields: [String: AlbumsValue] = ["platform": .text("ios"), "permission": .text(permission),                                             "offset_s": .number(TimeZone.current.secondsFromGMT())]        if let chosen { fields["chosen"] = .number(chosen) }        return try await call("albums.start", fields, as: AlbumsStart.self)    }    func home(system: [LibraryAlbum]) async throws -> AlbumsHome {        let rows: [[String: AlbumsValue]] = system.map { ["id": .text($0.id), "title": .text($0.title), "count": .number($0.count), "kind": .text($0.kind.rawValue)] }        return try await call("albums.home", ["system": .rows(rows)], as: AlbumsHome.self)    }    func album(_ id: String) async throws -> AlbumsAlbum { try await call("albums.album", ["id": .text(id)], as: AlbumsAlbum.self) }    func allPhotos() async throws -> AlbumsAlbum { try await call("albums.all", as: AlbumsAlbum.self) }    struct Lines: Decodable, Sendable { var progress: AlbumsPassLines? }    func passLines() async throws -> Lines { try await call("albums.pass.lines", as: Lines.self) }    func tidy() async throws -> AlbumsTidy { try await call("albums.tidy", as: AlbumsTidy.self) }    func sets(from: Int = 0) async throws -> AlbumsSets { try await call("albums.sets", ["from": .number(from)], as: AlbumsSets.self) }    func set(_ id: String) async throws -> AlbumsSet { try await call("albums.set", ["set": .text(id)], as: AlbumsSet.self) }    /// Every act carries the ids that were on screen and their count (the core review): the core acts only on what he    /// saw, or answers `changed`.    private func shown(_ ids: [String], _ fields: [String: AlbumsValue]) -> [String: AlbumsValue] {        var out = fields        out["shown"] = .list(ids)        out["count"] = .number(ids.count)        return out    }    /// A Set kept: from its row (`keep` nil: the core's picks; `shown` the row's strip, `count` its photos) or from the    /// Set itself (`keep` his choice, `shown` every member).    func keepSet(_ id: String, keep: [String]? = nil, shown ids: [String], count: Int) async throws -> AlbumsDone {        var fields: [String: AlbumsValue] = ["set": .text(id), "shown": .list(ids), "count": .number(count)]        if let keep { fields["keep"] = .list(keep) }        return try await call("albums.set.keep", fields, as: AlbumsDone.self)    }    func list(_ list: String, from: Int = 0) async throws -> AlbumsList {        try await call("albums.list", ["list": .text(list), "from": .number(from)], as: AlbumsList.self)    }    func binMove(ids: [String], shown on: [String]) async throws -> AlbumsDone {        try await call("albums.bin.move", shown(on, ["ids": .list(ids)]), as: AlbumsDone.self)    }    func binMove(list: String, shown on: [String]) async throws -> AlbumsDone {        try await call("albums.bin.move", shown(on, ["list": .text(list)]), as: AlbumsDone.self)    }    func keep(ids: [String], shown on: [String]) async throws -> AlbumsDone {        try await call("albums.keep", shown(on, ["ids": .list(ids)]), as: AlbumsDone.self)    }    func undo(_ token: String) async throws -> AlbumsDone { try await call("albums.undo", ["token": .text(token)], as: AlbumsDone.self) }    func bin() async throws -> AlbumsBin { try await call("albums.bin", as: AlbumsBin.self) }    func bringBack(_ ids: [String], shown on: [String]) async throws -> AlbumsDone {        try await call("albums.bin.bring_back", shown(on, ["ids": .list(ids)]), as: AlbumsDone.self)    }    /// The Bin's Delete, at his tap, with exactly the ids the Bin showed.    func binDelete(shown on: [String]) async throws -> AlbumsDone { try await call("albums.bin.delete", shown(on, [:]), as: AlbumsDone.self) }    func platformDone(act: String, asked: [String], stillHere: [String]) async throws -> AlbumsDone {        try await call("albums.platform.done", ["act": .text(act), "asked": .list(asked), "still_here": .list(stillHere)], as: AlbumsDone.self)    }    func people() async throws -> AlbumsPeople { try await call("albums.people", as: AlbumsPeople.self) }    func faces(on: Bool) async throws -> AlbumsSaid { try await call("albums.faces", ["on": .flag(on)], as: AlbumsSaid.self) }    func name(_ group: String, _ name: String) async throws -> AlbumsSaid {        try await call("albums.people.name", ["group": .text(group), "name": .text(name)], as: AlbumsSaid.self)    }    func you() async throws -> AlbumsYou { try await call("albums.you", as: AlbumsYou.self) }}#endif