Cotton code
75 lines · 3.7 KB · SwiftRaw
// A CARD'S ACTS, RUN AND SAID: what the deck's fan, a list's acts and the// viewer's Details all ask of the core when an act is tapped, and the receipt// each says. No drawing here, so it builds wherever CottonUI builds (the// guards' macOS stub too); the fan that taps them is iOS's (`EditingFan`).import CottonKitimport Foundation@MainActorenum DeckActs {    /// The words a tool's act answered, said: a receipt with its Undo    /// (`records.undo` of the change it names).    static func say(_ outcome: ToolActOutcome, toasts: ToastCenter?, core: any CottonCalling) {        switch outcome {        case .said(let words): _ = toasts?.show(words)        case .receipt(let words, let undo):            _ = toasts?.show(words, undo: {                // A refused undo is said, never swallowed.                Task { @MainActor in                    do { _ = try await core.callJSON(CottonMethod.recordsUndo, ["change_id": undo]) } catch {                        _ = toasts?.show(ActionRefused.words(for: error), tone: .error)                    }                }            })        default: break        }    }    /// What a tool's act came to, asked of the core (`tools.act`): a    /// connect's consent page, the flow an account act runs, the words a    /// Back up now says; nothing for an act naming no tool, which asks nothing.    static func ran(_ action: CardAction, words: String? = nil, core: any CottonCalling) async throws -> ToolActOutcome {        guard let tool = action.tool else { return .nothing }        let answer = try await core.call(CottonMethod.toolsAct, ToolActAsk(tool: tool, act: action.id, account: action.account, group: action.group,                                                                          record: action.record, words: words),                                         as: ToolActAnswer.self)        if let effect = answer.platform { return .platform(effect) }        if let page = answer.opens.flatMap(URL.init(string:)) { return .opens(page) }        if let document = answer.document { return .document(document, said: answer.said) }        if let said = answer.said, let undo = answer.undo { return .receipt(said, undo: undo) }        if let said = answer.said { return .said(said) }        return .nothing    }    /// The card put in the bin, and its receipt in the core's words, with the    /// Undo only where the core says the change can be taken back. A refused    /// Remove, or a refused Undo, is said in the core's line, never swallowed    /// (Books 1.0.0 (3) and (4) walks, 7 Nov 2026: an Undo after Remove on a    /// signed-in phone was refused and nothing said why).    static func binned(table: String, id: String, store: any Records, toasts: ToastCenter?) async {        let change: ChangeRecord        do { change = try await store.delete(table: table, id: id) } catch {            _ = toasts?.show(ActionRefused.words(for: error), tone: .error)            return        }        said(change, store: store, toasts: toasts)    }    /// A change's receipt in the core's words, with the Undo only where the    /// core says the change can be taken back; a refused Undo is said.    static func said(_ change: ChangeRecord, store: any Records, toasts: ToastCenter?) {        let words = change.said?.said ?? change.byline        guard change.undoable else {            _ = toasts?.show(words)            return        }        _ = toasts?.show(words, undo: {            Task { @MainActor in                do { _ = try await ChangeUndo.perform(change, through: store) } catch {                    _ = toasts?.show(ActionRefused.words(for: error), tone: .error)                }            }        })    }}