125 lines · 5.4 KB · SwiftRaw
// A `shell.today` answer for a scripted core, built from what a test means to// say about Home's page: the arrangement, the bands' rows, the notices, the// cards. Every other key the reader requires (`TodayAnswer`) is there, empty —// the core's shape (crates/cotton-ffi/src/methods/shell_today.rs), never a// subset the decoder would refuse.import CottonKitimport Foundationenum TodayAnswerJSON { /// The families `shell.today` names (`FAMILIES`): the tables `read.home` /// and the day read touch. static let families = ["core", "memory", "projects", "documents", "todos", "calendar", "people", "messages", "money", "mood"] /// What an empty Home says, as the board's lines word it. static let empty = "Nothing here yet." /// The whole answer, as JSON a `ScriptedCore.Reply.ok` carries. /// `arrangement` is the tools' arrangement block as raw JSON (the /// `read.home` shape: `{tiles, viewIds, tools, …}`), nil for none. static func answer( arrangement: String? = nil, asked: [[String: Any]] = [], askedTotal: Int? = nil, unreadable: Bool = false, done: [[String: Any]] = [], notices: [[String: Any]] = [], cards: [[String: Any]] = [], board: [[String: Any]] = [], acts: [String: Any] = [:], failed: [String: Any]? = nil, arriving: String? = nil, projects: [[String: Any]] = [], projectFactsMissing: Bool = false, families: [String] = TodayAnswerJSON.families, empty: String = TodayAnswerJSON.empty ) -> String { let total = askedTotal ?? asked.reduce(0) { $0 + ($1["count"] as? Int ?? 0) } var answer: [String: Any] = [ "cards": cards, "acts": acts, "count": cards.count, "parts": [ "asked": ["items": asked, "total": total, "unreadable": unreadable] as [String: Any], "done": done, "notices": notices, "board": board, ] as [String: Any], "projects": projects, "project_facts_missing": projectFactsMissing, "families": families, "empty": empty, "next": NSNull(), ] if let failed { answer["failed"] = failed } if let arriving { answer["arriving"] = arriving } if let arrangement, let block = try? JSONSerialization.jsonObject(with: Data(arrangement.utf8)) { answer["arrangement"] = block } return encoded(answer) } /// An `arrangement` block, as the core writes it: a tool's tile per key, /// in `order`. static func arrangement(_ order: [String], manual: Bool = true) -> String { let views = order.map { "tool/" + $0 } let tiles: [[String: Any]] = views.map { ["id": $0, "empty": false] } let block: [String: Any] = ["manual": manual, "tiles": tiles, "viewIds": views, "tools": order, "offers": [String]()] return encoded(block) } /// One Asked of you row, worded and addressed by the core. static func asked( _ key: String, count: Int, label: String, path: String, database: (id: String, name: String?)? = nil, tiles: [[String: Any]] = [] ) -> [String: Any] { var row: [String: Any] = ["key": key, "count": count, "label": label, "path": path, "tiles": tiles] if let database { let named: [String: Any] = ["id": database.id, "name": database.name.map { $0 as Any } ?? NSNull()] row["database"] = named } return row } /// One Done for you receipt, as the core words it. static func done( _ kind: String, id: String, label: String, text: String = "", when: String = "just now", action: String = "undo", path: String = "/" ) -> [String: Any] { ["kind": kind, "id": id, "label": label, "text": text, "when": when, "action": action, "path": path] } /// One notice that speaks now (`id` is the board's notice kind). static func notice(_ id: String, words: String, kind: String = "quiet", side: String? = nil) -> [String: Any] { var notice: [String: Any] = ["id": id, "words": words, "kind": kind] if let side { notice["side"] = side } return notice } /// One card of Home's deck: the deck card's required keys (`id`, `face`, /// `reach`, `record`) and Home's `kind`. static func card( id: String, kind: String, face: String, table: String? = nil, record: String? = nil, data: [String: Any] = [:], slots: [String: Any] = [:], tap: [String: String] = [:], acts: [String] = [], opens: [String: Any]? = nil ) -> [String: Any] { var wire: [String: Any] = ["id": record ?? id, "data": data] var card: [String: Any] = [ "id": id, "kind": kind, "face": face, "slots": slots, "tap": tap, "acts": acts, "reach": ["phones": [String](), "emails": [String](), "call": false, "message": false, "email": false] as [String: Any], ] if let table { card["table"] = table wire["table"] = table } card["record"] = wire if let opens { card["opens"] = opens } return card } private static func encoded(_ object: Any) -> String { let data = (try? JSONSerialization.data(withJSONObject: object, options: [.sortedKeys])) ?? Data("{}".utf8) return String(decoding: data, as: UTF8.self) }}