Cotton code
83 lines · 3.4 KB · SwiftRaw
// Send (wireframes 6 and 7, from Read's ↗ and from About): the script handed// on as a file, through the system's share sheet. Three ways, each a word and// a quiet line saying what it is for: a PDF laid out by the core's pages// (`StoryPDF`), a Final Draft file (`story.fdx.write`) and a Fountain file// (`story.fountain.write`). A marked draft sends its own pages and Fountain// (`story.drafts.read`); the core writes no Final Draft file of a draft, so a// draft offers the other two.#if os(iOS)import CottonKitimport SwiftUIstruct InkSendSheet: View {    let story: InkStory    let script: String    /// The script's title, as the sheet's title says it.    let title: String    /// A marked draft to send instead of the script as it stands.    var draft: InkReadScreen.Draft? = nil    @Environment(\.closeCard) private var closeCard    var body: some View {        InkActsSheet(title: InkWords.line("ink.send_title", ["title": title]), acts: acts)    }    private var acts: [InkAct] {        var acts = [            InkAct(words: InkWords.line("ink.send_pdf_file"), detail: InkWords.line("ink.send_pdf_line")) {                run { try await InkSending.pdf(story: story, script: script, draft: draft) }            },        ]        if draft == nil {            acts.append(InkAct(words: InkWords.line("ink.send_fdx"), detail: InkWords.line("ink.send_fdx_line")) {                run {                    let file = try await story.shelf.fdx(story: script)                    try InkSending.share(file.text, named: file.name)                }            })        }        acts.append(InkAct(words: InkWords.line("ink.send_fountain_file"), detail: InkWords.line("ink.send_fountain_line")) {            run {                if let draft {                    let read = try await story.shelf.readDraft(draft.id)                    try InkSending.share(read.fountainText, named: read.fountainName)                } else {                    let file = try await story.shelf.fountain(story: script)                    try InkSending.share(file.text, named: file.name)                }            }        })        return acts    }    private func run(_ work: @escaping () async throws -> Void) {        closeCard?()        Task { await story.attempt { try await work() } }    }}/// The files Send hands to the share sheet.@MainActorenum InkSending {    /// The script's pages, or a draft's, as the PDF the core's layout draws.    static func pdf(story: InkStory, script: String, draft: InkReadScreen.Draft?) async throws {        let working = try await story.book.draft(story: script)        guard let draft else {            if let url = StoryPDF.write(working.script, named: working.title) { ShareSheet.present(url) }            return        }        let read = try await story.shelf.readDraft(draft.id)        let pages = StoryScript(paper: working.script.paper, titlePage: nil, pages: read.pages, scenes: [])        let title = [working.title, draft.name].filter { !$0.isEmpty }.joined(separator: " - ")        if let url = StoryPDF.write(pages, named: title) { ShareSheet.present(url) }    }    /// A file's words written under Story's own folder, then shared.    static func share(_ text: String, named name: String) throws {        let url = StoryFiles.folder().appendingPathComponent(StoryFiles.safe(name))        try Data(text.utf8).write(to: url, options: .atomic)        ShareSheet.present(url)    }}#endif