328 lines · 11.8 KB · SwiftRaw
// Story's people, places and drafts, from the core// (`crates/cotton-ffi/src/phone/story_ink.rs`, `story_drafts.rs`;// assets/design/story/CONTRACT.md §3–7): a character's page, every place// and one place, a name changed everywhere, notes kept on a character or a// place, and drafts marked, read and gone back to. Decoded exactly as the// core writes them.import CottonKitimport CottonValuesimport Foundationenum StoryShelfMethod { static let person = "story.person" static let places = "story.places" static let place = "story.place" static let rename = "story.rename" static let draftsList = "story.drafts.list" static let draftsMark = "story.drafts.mark" static let draftsRead = "story.drafts.read" static let draftsRestore = "story.drafts.restore" static let draftsChanges = "story.drafts.changes" static let personLines = "story.person.lines" static let merge = "story.merge" static let pictureAdd = "story.picture.add" static let pictureMove = "story.picture.move" static let pictureRemove = "story.picture.remove" static let bringIn = "story.bring_in" static let fdxWrite = "story.fdx.write" static let fountainWrite = "story.fountain.write"}extension StoryTable { static let character = "story_character"}/// One picture of a character, a place or a story day (`story.pictures`):/// its media key, and the day it is of, if any.struct StoryPicture: Decodable, Equatable, Identifiable { let id: String /// The media key. let picture: String let day: Int? let caption: String? enum CodingKeys: String, CodingKey { case id, picture, day, caption } init(from decoder: Decoder) throws { let values = try decoder.container(keyedBy: CodingKeys.self) id = try values.decode(String.self, forKey: .id) picture = try values.decodeIfPresent(String.self, forKey: .picture) ?? "" caption = try values.decodeIfPresent(String.self, forKey: .caption) // A day comes as a JSON number, whole or not. day = (try? values.decodeIfPresent(Double.self, forKey: .day)).flatMap { $0 }.map { Int($0) } }}/// `story.person { id }`: a character as the script holds them.struct StoryPerson: Decodable, Equatable { /// A story day they are in, `Day 2`. struct Day: Decodable, Equatable, Identifiable { let day: Int let label: String var id: Int { day } } let id: String let name: String let initial: String? let picture: String? let lines: Int let about: String? let scenes: Int let sceneIds: [String] /// How much they say in each of their scenes, `8 lines`, by scene id. let linesIn: [String: String]? let days: [Day]? let pictures: [StoryPicture]? enum CodingKeys: String, CodingKey { case id, name, initial, picture, lines, about, scenes, days, pictures case sceneIds = "scene_ids" case linesIn = "lines_in" }}/// One place (`story.places`, `story.place`).struct StoryPlace: Decodable, Equatable, Identifiable { /// A time of day the scenes here are at, and how many. struct Time: Decodable, Equatable { let time: String let scenes: Int } let id: String let name: String let about: String? let picture: String? let scenes: Int let sceneIds: [String] /// The quiet line under its name, the core's. let line: String? // A place's own page. let pictures: [StoryPicture]? let times: [Time]? /// Who speaks in each scene here, by scene id. let people: [String: String]? enum CodingKeys: String, CodingKey { case id, name, about, picture, scenes, line, pictures, times, people case sceneIds = "scene_ids" }}/// `story.person.lines { id }`: everything one character says, in order.struct StoryPersonLines: Decodable, Equatable { struct Line: Decodable, Equatable, Identifiable { let scene: String let line: String let number: Int let title: String let text: String let aside: String? var id: String { line } } let id: String let name: String let lines: [Line]}/// `story.bring_in`'s answer: the new story, how many scenes, and the line/// asking the person to read a printed script through.struct StoryBroughtIn: Decodable { let story: String let scenes: Int let check: String?}/// `story.fdx.write` and `story.fountain.write`: a file's words and name.struct StoryFileOut: Decodable { let text: String let name: String}/// `story.drafts.changes { draft }`: what changed since a draft, scene by scene.struct StoryChanges: Decodable, Equatable { struct Lines: Decodable, Equatable { let added: Int let removed: Int let changed: Int } struct Scene: Decodable, Equatable, Identifiable { let scene: String let numberThen: Int? let numberNow: Int? /// `added`, `removed`, `moved`, `changed` or `same`. let change: String let headingThen: String? let headingNow: String? let lines: Lines var id: String { scene } enum CodingKeys: String, CodingKey { case scene, change, lines case numberThen = "number_then" case numberNow = "number_now" case headingThen = "heading_then" case headingNow = "heading_now" } } struct Since: Decodable, Equatable { let scenesAdded: Int let scenesRemoved: Int let scenesChanged: Int let linesAdded: Int let linesRemoved: Int let linesChanged: Int /// The scenes counted as changed on the working copy's row. var scenes: Int { scenesAdded + scenesRemoved + scenesChanged } enum CodingKeys: String, CodingKey { case scenesAdded = "scenes_added" case scenesRemoved = "scenes_removed" case scenesChanged = "scenes_changed" case linesAdded = "lines_added" case linesRemoved = "lines_removed" case linesChanged = "lines_changed" } } let scenes: [Scene] let since: Since}/// One marked draft (`story.drafts.list`), newest first.struct StoryDraftMark: Decodable, Equatable, Identifiable { let id: String let name: String /// ISO 8601, when it was marked. let madeAt: String let scenes: Int let pages: Double enum CodingKeys: String, CodingKey { case id, name, scenes, pages case madeAt = "made_at" }}/// `story.drafts.read { draft }`: a draft's pages and its Fountain.struct StoryDraftRead: Decodable { let draft: StoryDraftMark let pages: [StoryScript.Page] let fountainText: String let fountainName: String enum CodingKeys: String, CodingKey { case draft, pages case fountainText = "fountain_text" case fountainName = "fountain_name" }}/// The door to the people, places and drafts.@MainActorstruct StoryShelf { let core: any CottonCalling private struct Id: Encodable { let id: String } private struct Story: Encodable { let story: String? } private struct Rename: Encodable { let table: String; let id: String; let name: String } private struct Mark: Encodable { let story: String?; let name: String } private struct Draft: Encodable { let draft: String } private struct Places: Decodable { let places: [StoryPlace] } private struct Merge: Encodable { let table: String; let id: String; let into: String } private struct PictureAdd: Encodable { let picture: String; let character: String?; let place: String?; let day: Int? } private struct PictureMove: Encodable { let id: String; let after: String } private struct BringIn: Encodable { let path: String; let name: String } private struct Drafts: Decodable { let drafts: [StoryDraftMark] } func person(_ id: String) async throws -> StoryPerson { try await core.call(StoryShelfMethod.person, Id(id: id), as: StoryPerson.self) } func places(story: String) async throws -> [StoryPlace] { try await core.call(StoryShelfMethod.places, Story(story: story), as: Places.self).places } func place(_ id: String) async throws -> StoryPlace { try await core.call(StoryShelfMethod.place, Id(id: id), as: StoryPlace.self) } /// A character's or a place's name, changed in every scene that says it. func rename(table: String, id: String, name: String) async throws { _ = try await core.call(StoryShelfMethod.rename, Rename(table: table, id: id, name: name), as: JSONValue.self) } /// The notes kept on a character or a place. func writeAbout(table: String, id: String, about: String) async throws { _ = try await core.callJSON(StoryMethod.recordsUpdate, [ "table": RecordValue.text(table), "id": .text(id), "patch": .object(["about": .text(about)]), ]) } func drafts(story: String) async throws -> [StoryDraftMark] { try await core.call(StoryShelfMethod.draftsList, Story(story: story), as: Drafts.self).drafts } func markDraft(story: String, name: String) async throws { _ = try await core.call(StoryShelfMethod.draftsMark, Mark(story: story, name: name), as: JSONValue.self) } func readDraft(_ draft: String) async throws -> StoryDraftRead { try await core.call(StoryShelfMethod.draftsRead, Draft(draft: draft), as: StoryDraftRead.self) } /// What changed since a draft, scene by scene. func changes(_ draft: String) async throws -> StoryChanges { try await core.call(StoryShelfMethod.draftsChanges, Draft(draft: draft), as: StoryChanges.self) } /// Everything one character says, in order. func lines(of character: String) async throws -> StoryPersonLines { try await core.call(StoryShelfMethod.personLines, Id(id: character), as: StoryPersonLines.self) } /// Two characters or two places become one, `into`. func merge(table: String, id: String, into: String) async throws { _ = try await core.call(StoryShelfMethod.merge, Merge(table: table, id: id, into: into), as: JSONValue.self) } /// A picture kept in the store (`capture.media`) added to a character or a place, on a story day or not. func addPicture(_ key: String, character: String? = nil, place: String? = nil, day: Int? = nil) async throws { _ = try await core.call(StoryShelfMethod.pictureAdd, PictureAdd(picture: key, character: character, place: place, day: day), as: JSONValue.self) } /// A picture among its own, after another (`start` for the first place, which is the face). func movePicture(_ id: String, after: String) async throws { _ = try await core.call(StoryShelfMethod.pictureMove, PictureMove(id: id, after: after), as: JSONValue.self) } func removePicture(_ id: String) async throws { _ = try await core.call(StoryShelfMethod.pictureRemove, Id(id: id), as: JSONValue.self) } /// A script file the person picked (Fountain, Final Draft, PDF, text) read into a new story. func bringIn(path: String, name: String) async throws -> StoryBroughtIn { try await core.call(StoryShelfMethod.bringIn, BringIn(path: path, name: name), as: StoryBroughtIn.self) } /// The script as a Final Draft file. func fdx(story: String) async throws -> StoryFileOut { try await core.call(StoryShelfMethod.fdxWrite, Story(story: story), as: StoryFileOut.self) } /// The script as a Fountain file. func fountain(story: String) async throws -> StoryFileOut { try await core.call(StoryShelfMethod.fountainWrite, Story(story: story), as: StoryFileOut.self) } /// The script becomes this draft again; scenes written since go to Deleted. func restoreDraft(_ draft: String) async throws { _ = try await core.call(StoryShelfMethod.draftsRestore, Draft(draft: draft), as: JSONValue.self) }}