Sources/OrgApp/EmacsImportSections.swift
61 lines · 2966 bytes
1import SwiftUI
2
3extension EmacsImport {
4 /// The items by kind of change, empty kinds left out; agenda folders only when `folders`.
5 public func groups(folders: Bool) -> [(String, [Item])] {
6 [
7 ("Settings", items.filter { if case .setting = $0.change { true } else { false } }),
8 ("Folders", folders ? items.filter { if case .folder = $0.change { true } else { false } } : []),
9 ("Capture templates", items.filter { if case .capture = $0.change { true } else { false } }),
10 ("Key bindings", items.filter { if case .binding = $0.change { true } else { false } }),
11 ("Structure templates", items.filter { if case .structureTemplate = $0.change { true } else { false } }),
12 ("Denote templates", items.filter { if case .denoteTemplate = $0.change { true } else { false } }),
13 ("Agenda views", items.filter { if case .view = $0.change { true } else { false } }),
14 ].filter { !$0.1.isEmpty }
15 }
16}
17
18/// A section of toggles per group of an import, then what wasn't imported and why.
19public struct EmacsImportSections: View {
20 let result: EmacsImport
21 let groups: [(String, [EmacsImport.Item])]
22 @Binding var selected: Set<Int>
23 let font: Font
24
25 public init(result: EmacsImport, groups: [(String, [EmacsImport.Item])], selected: Binding<Set<Int>>, font: Font) {
26 self.result = result
27 self.groups = groups
28 _selected = selected
29 self.font = font
30 }
31
32 public var body: some View {
33 ForEach(groups, id: \.0) { title, items in
34 Section(title) {
35 ForEach(items) { item in
36 Toggle(isOn: Binding { selected.contains(item.id) } set: { on in
37 if on { selected.insert(item.id) } else { selected.remove(item.id) }
38 }) {
39 VStack(alignment: .leading, spacing: 1) {
40 Text(item.summary).font(font)
41 Text([item.name == item.summary ? nil : item.name, item.source.isEmpty ? nil : item.source].compactMap { $0 }.joined(separator: " · "))
42 .font(.caption).foregroundStyle(.secondary)
43 }
44 }
45 }
46 }
47 }
48 let notes = result.unsupported.map { ($0.id, $0.name, $0.reason, $0.source) } + result.unreadable.map { ($0.id, $0.name, $0.reason, $0.source) }
49 if !notes.isEmpty || !result.problems.isEmpty {
50 Section("Not imported") {
51 ForEach(result.problems, id: \.self) { Text($0).foregroundStyle(.red) }
52 ForEach(notes, id: \.0) { note in
53 VStack(alignment: .leading, spacing: 1) {
54 Text(note.1).font(font)
55 Text("\(note.2) · \(note.3)").font(.caption).foregroundStyle(.secondary)
56 }
57 }
58 }
59 }
60 }
61}