82 lines · 3.8 KB · SwiftRaw
// `/documents/import`: how a document gets in. A document is its file — a// record typed on a blank page has nothing to read — so the list's "+" opens// the three places a file comes from (the files, the camera, the library),// as the old Documents "+" did. What is picked goes to `capture.media` as a// document; Cotton reads it on the Mac, and the page that opens is its own.import CottonKitimport SwiftUIstruct DocumentImportRoute: View { @Environment(\.cottonCore) private var core @Environment(\.theme) private var theme @Environment(Navigator.self) private var navigator: Navigator? @Environment(ToastCenter.self) private var toasts @State private var sources = AttachmentSourceState() @State private var adding = false /// The words the screen says, one place. enum Words { static var title: String { ShellLines.line("boot", "document_import.title") } static var lead: String { ShellLines.line("boot", "document_import.lead") } static var files: String { ShellLines.line("boot", "document_import.files") } static var camera: String { ShellLines.line("boot", "document_import.camera") } static var library: String { ShellLines.line("boot", "document_import.library") } static var adding: String { ShellLines.line("boot", "document_import.adding") } static var refused: String { ShellLines.line("boot", "document_import.refused") } static var noStore: String { ShellLines.line("boot", "document_import.no_store") } } var body: some View { PaperScreen(title: Words.title) { VStack(alignment: .leading, spacing: RecordListSurfaceKnobs.paperGap) { Text(adding ? Words.adding : Words.lead) .paperBody(15, ink: .pencil) .frame(maxWidth: .infinity, alignment: .leading) PaperTypedButton(Words.files) { sources.files = true } .accessibilityIdentifier("document-import-files") if Camera.isAvailable { PaperTypedButton(Words.camera) { sources.camera = true } .accessibilityIdentifier("document-import-camera") } PaperTypedButton(Words.library) { sources.library = true } .accessibilityIdentifier("document-import-library") } .disabled(adding) } .attachmentSources( $sources, photosOnly: false, onPicked: { source in Task { await add(source) } }, onRefused: { toasts.show($0, tone: .error) }) } /// Keeps what was picked as a document and opens its page in this /// screen's place, so Back from the page is the list. The staged copy is /// this screen's to delete either way. private func add(_ source: AttachmentSource) async { defer { try? FileManager.default.removeItem(at: source.url) } guard let core else { toasts.show(Words.noStore, tone: .error) return } adding = true defer { adding = false } do { let kept = try await CoreMediaCapture(core: core) .capture(file: source.url, mime: source.mime, kind: .document, filename: source.filename) guard let route = Self.opens(kept) else { navigator?.pop() return } navigator?.replace(with: route) } catch { // The core's own reason ("that file is larger than …") over ours. toasts.show((error as? CottonCallError)?.message ?? Words.refused, tone: .error) } } /// Where a kept document opens: its page, when the core named the record. static func opens(_ kept: MediaCaptured) -> Route? { kept.recordId.isEmpty ? nil : .documentsDocument(id: kept.recordId) }}