Cotton code
72 lines · 2.7 KB · SwiftRaw
// COMMS' EXPORT ON IPHONE (chat CONTRACT.md "Export"; owner, 23 Nov 2026, promised on the website for launch).// The core writes the file (`chat.export`: one chat, or every chat, as a text file with names and times and the// files this phone holds, in a zip); the phone only offers it and hands it to the share sheet, as Words' export// does. Every chat from You → Export; one chat from Export chat on its own page (a person's, a group's).#if os(iOS)import CottonKitimport SwiftUIenum ChatExportMethod {    /// `{ thread? }` → `{ path, file_name, mime, count, files, missing }`; refused `refused` with nothing to export.    static let export = "chat.export"}/// The zip the core wrote.struct ChatExported: Decodable, Equatable, Sendable {    let path: String    let fileName: String    enum CodingKeys: String, CodingKey {        case path        case fileName = "file_name"    }}enum ChatExport {    private struct Ask: Encodable, Sendable {        var thread: String?    }    /// One chat (`thread`) or every chat (nil), written by the core and handed to the share sheet; a refusal is said    /// in the core's words.    @MainActor static func share(_ core: (any CottonCalling)?, thread: String?, said: (String) -> Void) async {        guard let core else { return }        do {            let made = try await core.call(ChatExportMethod.export, Ask(thread: thread), as: ChatExported.self)            ShareSheet.present(URL(fileURLWithPath: made.path))        } catch {            said(ActionRefused.words(for: error))        }    }}/// You → Export: what it makes, then Export all chats.struct ChatYouExportPage: View {    let core: (any CottonCalling)?    let back: () -> Void    @Environment(\.chatLook) private var look    @Environment(ToastCenter.self) private var toasts: ToastCenter?    @State private var working = false    var body: some View {        ChatYouPageFrame(title: ChatWords.line("export.title"), id: "you-export-page", back: back) {            VStack(alignment: .leading, spacing: 18) {                Text(verbatim: ChatWords.line("export.hint"))                    .font(look.font(16)).foregroundStyle(look.colours.dim)                    .fixedSize(horizontal: false, vertical: true)                WarmPill(words: ChatWords.line("export.all"), id: "you-export-all", accent: true) {                    guard !working else { return }                    working = true                    Task {                        await ChatExport.share(core, thread: nil) { toasts?.show($0, tone: .error) }                        working = false                    }                }                .opacity(working ? 0.5 : 1)            }            .padding(.top, 8)        }    }}#endif