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