Sources/OrgApp/EmacsImportSections.swift
60 lines · 2854 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 ("Agenda views", items.filter { if case .view = $0.change { true } else { false } }),
13 ].filter { !$0.1.isEmpty }
14 }
15}
16
17/// A section of toggles per group of an import, then what wasn't imported and why.
18public struct EmacsImportSections: View {
19 let result: EmacsImport
20 let groups: [(String, [EmacsImport.Item])]
21 @Binding var selected: Set<Int>
22 let font: Font
23
24 public init(result: EmacsImport, groups: [(String, [EmacsImport.Item])], selected: Binding<Set<Int>>, font: Font) {
25 self.result = result
26 self.groups = groups
27 _selected = selected
28 self.font = font
29 }
30
31 public var body: some View {
32 ForEach(groups, id: \.0) { title, items in
33 Section(title) {
34 ForEach(items) { item in
35 Toggle(isOn: Binding { selected.contains(item.id) } set: { on in
36 if on { selected.insert(item.id) } else { selected.remove(item.id) }
37 }) {
38 VStack(alignment: .leading, spacing: 1) {
39 Text(item.summary).font(font)
40 Text([item.name == item.summary ? nil : item.name, item.source.isEmpty ? nil : item.source].compactMap { $0 }.joined(separator: " · "))
41 .font(.caption).foregroundStyle(.secondary)
42 }
43 }
44 }
45 }
46 }
47 let notes = result.unsupported.map { ($0.id, $0.name, $0.reason, $0.source) } + result.unreadable.map { ($0.id, $0.name, $0.reason, $0.source) }
48 if !notes.isEmpty || !result.problems.isEmpty {
49 Section("Not imported") {
50 ForEach(result.problems, id: \.self) { Text($0).foregroundStyle(.red) }
51 ForEach(notes, id: \.0) { note in
52 VStack(alignment: .leading, spacing: 1) {
53 Text(note.1).font(font)
54 Text("\(note.2) · \(note.3)").font(.caption).foregroundStyle(.secondary)
55 }
56 }
57 }
58 }
59 }
60}