Reportage/Views/Settings/DiagnosticsSection.swift (2825B)
1import SwiftUI
2
3
4
5struct DiagnosticsSection: View {
6 var body: some View {
7 Section {
8 NavigationLink("Event Log") { EventLogView() }
9 .settingsRow()
10 } header: {
11 SettingsHeading("Diagnostics")
12 }
13 }
14}
15
16
17private struct EventLogView: View {
18 @State private var entries: [EventLog.Entry] = []
19 @State private var isConfirmingClear = false
20
21 var body: some View {
22 List {
23 if entries.isEmpty {
24 SettingsNote { Text("Nothing logged in the last day.") }
25 }
26 ForEach(entries.reversed()) { entry in
27 EventLogRow(entry: entry)
28 .settingsRow()
29 }
30 }
31 .listStyle(.plain)
32 .scrollContentBackground(.hidden)
33 .tabBarClearance()
34 .background(Theme.paper)
35 .foregroundStyle(Theme.ink)
36 .navigationTitle("Event Log")
37 .navigationBarTitleDisplayMode(.inline)
38 .toolbar {
39 ToolbarItemGroup(placement: .primaryAction) {
40 ShareLink(item: EventLog.shared.text()) {
41 Label("Share", systemImage: "square.and.arrow.up")
42 }
43 .disabled(entries.isEmpty)
44 Button("Clear", systemImage: "trash") { isConfirmingClear = true }
45 .disabled(entries.isEmpty)
46 }
47 }
48 .alert("Clear the Event Log?", isPresented: $isConfirmingClear) {
49 Button("Cancel", role: .cancel) {}
50 Button("Clear", role: .destructive) {
51 EventLog.shared.clear()
52 load()
53 }
54 }
55 .refreshable { load() }
56 .onAppear { load() }
57 }
58
59 private func load() {
60 entries = EventLog.shared.all()
61 }
62}
63
64
65struct EventLogRow: View {
66 let entry: EventLog.Entry
67
68 var body: some View {
69 VStack(alignment: .leading, spacing: 2) {
70 HStack {
71 Text(entry.date, format: .dateTime.weekday(.abbreviated).hour().minute().second())
72 .monospacedDigit()
73 Text(verbatim: entry.category)
74 Spacer()
75 if entry.level != .info {
76 Text(verbatim: entry.level.rawValue)
77 .foregroundStyle(entry.level == .error ? Color.red : Theme.accent)
78 }
79 }
80 .font(.caption)
81 .foregroundStyle(Theme.muted)
82 Text(verbatim: entry.message)
83 .font(.footnote)
84 .textSelection(.enabled)
85 }
86 }
87}