Sources/OrgstarMobile/EmacsImportScreen.swift
102 lines · 4889 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, which name the Mac's paths, are left
9/// out.
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
19 var body: some View {
20 List {
21 Section {
22 Button(location == nil ? "Choose Folder…" : "Choose Another Folder…") { choosing = true }
23 } footer: {
24 Text(location.map { "Reading \($0.lastPathComponent)" + ((result?.isDoom ?? false) ? ", a Doom configuration" : "") + "." }
25 ?? "The folder holding init.el, config.el or config.org.")
26 }
27 if let summary {
28 Section { Text(summary) }
29 } else if let result {
30 ForEach(groups(result), id: \.0) { title, items in
31 Section(title) {
32 ForEach(items) { item in
33 Toggle(isOn: Binding { selected.contains(item.id) } set: { on in
34 if on { selected.insert(item.id) } else { selected.remove(item.id) }
35 }) {
36 VStack(alignment: .leading, spacing: 1) {
37 Text(item.summary).font(.system(.callout, design: .monospaced))
38 Text([item.name == item.summary ? nil : item.name, item.source.isEmpty ? nil : item.source].compactMap { $0 }.joined(separator: " · "))
39 .font(.caption).foregroundStyle(.secondary)
40 }
41 }
42 }
43 }
44 }
45 let notes = result.unsupported.map { ($0.id, $0.name, $0.reason, $0.source) } + result.unreadable.map { ($0.id, $0.name, $0.reason, $0.source) }
46 if !notes.isEmpty || !result.problems.isEmpty {
47 Section("Not imported") {
48 ForEach(result.problems, id: \.self) { Text($0).foregroundStyle(.red) }
49 ForEach(notes, id: \.0) { note in
50 VStack(alignment: .leading, spacing: 1) {
51 Text(note.1).font(.system(.callout, design: .monospaced))
52 Text("\(note.2) · \(note.3)").font(.caption).foregroundStyle(.secondary)
53 }
54 }
55 }
56 }
57 }
58 }
59 .navigationTitle("Import from Emacs")
60 .toolbar {
61 if result != nil, summary == nil {
62 Button("Import") { runImport() }.disabled(selected.isEmpty || importing)
63 }
64 }
65 .fileImporter(isPresented: $choosing, allowedContentTypes: [.folder]) { chosen in
66 if case .success(let url) = chosen { read(url) }
67 }
68 }
69
70 private func groups(_ result: EmacsImport) -> [(String, [EmacsImport.Item])] {
71 [
72 ("Settings", result.items.filter { if case .setting = $0.change { true } else { false } }),
73 ("Capture templates", result.items.filter { if case .capture = $0.change { true } else { false } }),
74 ("Key bindings", result.items.filter { if case .binding = $0.change { true } else { false } }),
75 ("Structure templates", result.items.filter { if case .structureTemplate = $0.change { true } else { false } }),
76 ("Agenda views", result.items.filter { if case .view = $0.change { true } else { false } }),
77 ].filter { !$0.1.isEmpty }
78 }
79
80 private func read(_ url: URL) {
81 let accessing = url.startAccessingSecurityScopedResource()
82 defer { if accessing { url.stopAccessingSecurityScopedResource() } }
83 location = url
84 let scanned = EmacsImport.scan(EmacsImport.files(in: url))
85 result = scanned
86 selected = Set(groups(scanned).flatMap(\.1).map(\.id))
87 summary = nil
88 }
89
90 private func runImport() {
91 guard let result else { return }
92 importing = true
93 let chosen = result.items.filter { selected.contains($0.id) }
94 Task {
95 summary = await EmacsImport.apply(chosen, workspace: nil, directory: model.configFolder ?? UserConfig.directory,
96 writeConfig: model.configFolder != nil)
97 model.reloadConfig()
98 importing = false
99 }
100 }
101}
102#endif