Sources/OrgstarMobile/EmacsImportScreen.swift
cmc iOS: archive, publishing, agenda calendars, HTML and open, rich text copy, idle time, agenda folder import
2197a7a1d9 5 days ago99 lines · 4435 bytes
1#if os(iOS)
2import OrgApp
3import OrgCore
4import SwiftUI
5
6/// Import from Emacs: a configuration folder (a synced `~/.config/doom` or `~/.emacs.d`), read
7/// and picked from before anything changes. Settings go into the configuration
8/// folder's config.toml when there is one. Agenda folders name the Mac's paths, so each is
9/// added as the folder chosen for it on this device.
10struct EmacsImportScreen: View {
11 let model: MobileModel
12 @State private var location: URL?
13 @State private var result: EmacsImport?
14 @State private var selected: Set<Int> = []
15 @State private var choosing = false
16 @State private var summary: String?
17 @State private var importing = false
18 /// The folders chosen on this device, by the Mac path they stand for.
19 @State private var folders: [String: URL] = [:]
20 /// The Mac path the folder picker is choosing for.
21 @State private var folderFor: String?
22 @State private var choosingFolder = false
23
24 var body: some View {
25 List {
26 Section {
27 Button(location == nil ? "Choose Folder…" : "Choose Another Folder…") { choosing = true }
28 } footer: {
29 Text(location.map { "Reading \($0.lastPathComponent)" + ((result?.isDoom ?? false) ? ", a Doom configuration" : "") + "." }
30 ?? "The folder holding init.el, config.el or config.org.")
31 }
32 if let summary {
33 Section { Text(summary) }
34 } else if let result {
35 EmacsImportSections(result: result, groups: result.groups(folders: true), selected: $selected,
36 font: .system(.callout, design: .monospaced))
37 let paths = EmacsImport.foldersToAdd(result.items.filter { selected.contains($0.id) }, workspace: model.workspace)
38 if !paths.isEmpty {
39 Section {
40 ForEach(paths, id: \.self) { path in
41 Button {
42 folderFor = path
43 choosingFolder = true
44 } label: {
45 LabeledContent(path, value: folders[path]?.lastPathComponent ?? "Choose…")
46 }
47 }
48 } header: {
49 Text("Folders on this device")
50 } footer: {
51 Text("Choose the folder on this device for each one. Folders not chosen aren't added.")
52 }
53 }
54 }
55 }
56 .navigationTitle("Import from Emacs")
57 .toolbar {
58 if result != nil, summary == nil {
59 Button("Import") { runImport() }.disabled(selected.isEmpty || importing)
60 }
61 }
62 .fileImporter(isPresented: $choosing, allowedContentTypes: [.folder]) { chosen in
63 if case .success(let url) = chosen { read(url) }
64 }
65 .background {
66 // A second importer on the same view would replace the first.
67 Color.clear.fileImporter(isPresented: $choosingFolder, allowedContentTypes: [.folder]) { chosen in
68 guard let path = folderFor, case .success(let url) = chosen else { return }
69 // Kept for the bookmark the import makes; access stays on for the session.
70 _ = url.startAccessingSecurityScopedResource()
71 folders[path] = url
72 }
73 }
74 }
75
76 private func read(_ url: URL) {
77 let accessing = url.startAccessingSecurityScopedResource()
78 defer { if accessing { url.stopAccessingSecurityScopedResource() } }
79 location = url
80 let scanned = EmacsImport.scan(EmacsImport.files(in: url))
81 result = scanned
82 selected = Set(scanned.groups(folders: true).flatMap(\.1).map(\.id))
83 folders = [:]
84 summary = nil
85 }
86
87 private func runImport() {
88 guard let result else { return }
89 importing = true
90 let chosen = result.items.filter { selected.contains($0.id) }
91 Task {
92 summary = await EmacsImport.apply(chosen, workspace: model.workspace, directory: model.configFolder ?? UserConfig.directory,
93 writeConfig: model.configFolder != nil, chosenFolders: folders)
94 model.reloadConfig()
95 importing = false
96 }
97 }
98}
99#endif