// 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)importCottonWritingimportSwiftUIimportUniformTypeIdentifiers@MainActor@ObservablefinalclassNotesImport {staticlet shared = NotesImport()/// Markdown and text, TextBundle (a folder) and TextPack (its zip).staticlet 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 = falseprivate(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) {guardcase .success(let picked) = result, !picked.isEmpty, !running else { return } running = true report = nilTask {let staged = awaitTask.detached { Self.stage(picked) }.valuedo { report = tryawait book.bringIn(staged).said } catch { report = ActionRefused.words(for: error) } running = false showing = trueawait 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 privatestaticfunc 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 inlet scoped = url.startAccessingSecurityScopedResource()defer { if scoped { url.stopAccessingSecurityScopedResource() } }let target = root.appendingPathComponent(url.lastPathComponent)do {tryFileManager.default.copyItem(at: url, to: target)returnNoteImportFile(path: target.path, name: url.lastPathComponent) } catch {returnnil } } }}/// While notes come in, a spinner; then the core's sentence for what came in/// and what could not be read.structNotesImportProgress: View { @Bindable var importer: NotesImportlet look: WritingLooklet close: Stringprivatefunc ui(_ colour: UIColor) -> Color { Color(uiColor: colour) }var body: someView {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([]))) } } elseiflet 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