Sources/OrgstarMobile/EmacsImportScreen.swift
98 lines · 4338 bytes
3 symbols in this file
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 // Access starts when the import adds it.
70 folders[path] = url
71 }
72 }
73 }
74
75 private func read(_ url: URL) {
76 let accessing = url.startAccessingSecurityScopedResource()
77 defer { if accessing { url.stopAccessingSecurityScopedResource() } }
78 location = url
79 let scanned = EmacsImport.scan(EmacsImport.files(in: url))
80 result = scanned
81 selected = Set(scanned.groups(folders: true).flatMap(\.1).map(\.id))
82 folders = [:]
83 summary = nil
84 }
85
86 private func runImport() {
87 guard let result else { return }
88 importing = true
89 let chosen = result.items.filter { selected.contains($0.id) }
90 Task {
91 summary = await EmacsImport.apply(chosen, workspace: model.workspace, directory: model.configFolder ?? UserConfig.directory,
92 writeConfig: model.configFolder != nil, chosenFolders: folders)
93 model.reloadConfig()
94 importing = false
95 }
96 }
97}
98#endif