// Bringing a library in: Markdown files, folders of them, TextBundles and // TextPacks picked in Files, copied where the core can read them and handed // to `notes.import` in one run. What came in, what was here already and what // could not be read is the core's to count and say; run again with the same // files, nothing comes in twice, so a run cut short is simply run again. #if os(iOS) import CottonWriting import SwiftUI import UniformTypeIdentifiers @MainActor @Observable final class NotesImport { static let shared = NotesImport() /// Markdown and text, TextBundle (a folder) and TextPack (its zip). static let types: [UTType] = [ .plainText, .text, UTType(filenameExtension: "md") ?? .plainText, UTType(filenameExtension: "markdown") ?? .plainText, UTType(importedAs: "org.textbundle.package", conformingTo: .package), UTType(importedAs: "org.textbundle.compressed", conformingTo: .zip), .folder, ] private(set) var running = false private(set) var report: String? var showing = false /// Hands every picked file to the core in one run, so its one sentence /// counts the whole library; a run cut short is run again, and what came /// in the first time is not brought in twice (the core's to know). func start(_ result: Result<[URL], Error>, book: NotesBook, finished: @escaping () async -> Void) { guard case .success(let picked) = result, !picked.isEmpty, !running else { return } running = true report = nil Task { let staged = await Task.detached { Self.stage(picked) }.value do { report = try await book.bringIn(staged).said } catch { report = ActionRefused.words(for: error) } running = false showing = true await finished() } } /// The picked files and folders copied into the app's own folder (Files /// lends them only while picked); the core reads a folder's files itself. nonisolated private static func stage(_ picked: [URL]) -> [NoteImportFile] { let root = FileManager.default.temporaryDirectory.appendingPathComponent("notes-import", isDirectory: true) try? FileManager.default.removeItem(at: root) try? FileManager.default.createDirectory(at: root, withIntermediateDirectories: true) return picked.compactMap { url in let scoped = url.startAccessingSecurityScopedResource() defer { if scoped { url.stopAccessingSecurityScopedResource() } } let target = root.appendingPathComponent(url.lastPathComponent) do { try FileManager.default.copyItem(at: url, to: target) return NoteImportFile(path: target.path, name: url.lastPathComponent) } catch { return nil } } } } /// While notes come in, a spinner; then the core's sentence for what came in /// and what could not be read. struct NotesImportProgress: View { @Bindable var importer: NotesImport let look: WritingLook let close: String private func ui(_ colour: UIColor) -> Color { Color(uiColor: colour) } var body: some View { if importer.running || (importer.showing && importer.report != nil) { VStack(alignment: .leading, spacing: 12) { if importer.running { HStack(spacing: 12) { ProgressView().tint(ui(look.theme.accent)) Text(verbatim: notesLine("import.working")).font(Font(look.font([]))) } } else if let report = importer.report { Text(verbatim: report).font(Font(look.font([]))) .accessibilityIdentifier("notes-import-report") Button { importer.showing = false } label: { Text(verbatim: close).font(Font(look.font(.bold))).foregroundStyle(ui(look.theme.accent)) } .frame(maxWidth: .infinity, alignment: .trailing) } } .foregroundStyle(ui(look.theme.ink)) .padding(20) .background(RoundedRectangle(cornerRadius: 18).fill(ui(look.theme.sheet)).shadow(color: ui(look.theme.ink).opacity(0.15), radius: 16, y: 6)) .padding(24) .accessibilityElement(children: .contain) } } } #endif