Skip to content

Commit 3a483bd

Browse files
committed
UI: add search function to DebugLogsView and make date more specific
1 parent b376f5a commit 3a483bd

File tree

1 file changed

+63
-47
lines changed

1 file changed

+63
-47
lines changed

InfiniLink/Core/Developer/DebugLogsView.swift

Lines changed: 63 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ struct DebugLogsView: View {
1212

1313
@AppStorage("logSelection") var logSelection = "ble"
1414

15+
@State private var searchText = ""
16+
1517
var body: some View {
1618
TabView(selection: $logSelection) {
1719
logs(for: .app)
@@ -34,57 +36,71 @@ struct DebugLogsView: View {
3436
}
3537

3638
func logs(for type: DebugLogTarget) -> some View {
37-
let logs = logManager.logs.filter({ $0.target == type })
39+
var logs: [DebugLog] {
40+
logManager.logs
41+
.filter({ $0.target == type })
42+
.filter({
43+
if !searchText.isEmpty {
44+
let query = searchText.lowercased()
45+
let body = $0.body.lowercased()
46+
let caller = ($0.caller ?? "").lowercased()
47+
48+
return body.contains(query) || caller.contains(query)
49+
}
50+
return true
51+
})
52+
}
3853

3954
return VStack {
40-
if logs.isEmpty {
41-
Text("No Logs")
42-
.foregroundStyle(.gray)
43-
.frame(maxHeight: .infinity)
44-
} else {
45-
List {
46-
ForEach(logs.sorted(by: { log1, log2 in
47-
return log1.date > log2.date
48-
})) { log in
49-
VStack(alignment: .leading, spacing: 8) {
50-
Group {
51-
if let caller = log.caller {
52-
Text(caller + "" + log.date.formatted())
53-
} else {
54-
Text(log.date.formatted())
55-
}
56-
}
57-
.font(.caption.weight(.semibold))
58-
.foregroundStyle(.gray)
59-
Text(log.body)
60-
if log.type == .error {
61-
let color = (log.type == .error ? Color.red : Color.orange)
62-
63-
Text(log.type.rawValue.uppercased())
64-
.font(.system(size: 10.5))
65-
.padding(4)
66-
.padding(.horizontal, 4)
67-
.foregroundStyle(color)
68-
.background {
69-
Capsule()
70-
.stroke(color, lineWidth: 2)
71-
}
72-
.clipShape(Capsule())
73-
}
55+
List(logs.sorted(by: { log1, log2 in
56+
return log1.date > log2.date
57+
})) { log in
58+
VStack(alignment: .leading, spacing: 8) {
59+
Group {
60+
let date = log.date.formatted(.dateTime.day().weekday().month().hour().minute().second())
61+
if let caller = log.caller {
62+
Text(caller + "" + date)
63+
} else {
64+
Text(date)
7465
}
75-
.listRowInsets(EdgeInsets(top: 0, leading: 0, bottom: 0, trailing: 0))
76-
.padding()
77-
.contextMenu {
78-
Button {
79-
UIPasteboard.general.string = {
80-
return "\(log.type.rawValue.capitalized) \(log.caller == nil ? "" : "from \(log.caller!)") at \(log.date.formatted()): \(log.body)"
81-
}()
82-
} label: {
83-
Label("Copy", systemImage: "doc.on.clipboard")
66+
}
67+
.font(.caption.weight(.semibold))
68+
.foregroundStyle(.gray)
69+
Text(log.body)
70+
if log.type == .error {
71+
let color = (log.type == .error ? Color.red : Color.orange)
72+
73+
Text(log.type.rawValue.uppercased())
74+
.font(.system(size: 10.5))
75+
.padding(4)
76+
.padding(.horizontal, 4)
77+
.foregroundStyle(color)
78+
.background {
79+
Capsule()
80+
.stroke(color, lineWidth: 2)
8481
}
85-
}
82+
.clipShape(Capsule())
8683
}
8784
}
85+
.listRowInsets(EdgeInsets(top: 0, leading: 0, bottom: 0, trailing: 0))
86+
.padding()
87+
.contextMenu {
88+
Button {
89+
UIPasteboard.general.string = {
90+
return "\(log.type.rawValue.capitalized) \(log.caller == nil ? "" : "from \(log.caller!)") at \(log.date.formatted()): \(log.body)"
91+
}()
92+
} label: {
93+
Label("Copy", systemImage: "doc.on.clipboard")
94+
}
95+
}
96+
}
97+
.searchable(text: $searchText)
98+
}
99+
.overlay {
100+
if logs.isEmpty {
101+
Text("No Logs")
102+
.foregroundStyle(.gray)
103+
.frame(maxHeight: .infinity)
88104
}
89105
}
90106
}
@@ -94,9 +110,9 @@ struct DebugLogsView: View {
94110
NavigationView {
95111
DebugLogsView()
96112
.onAppear {
97-
#if DEBUG
113+
#if DEBUG
98114
DebugLogManager.shared.logs.append(DebugLog(caller: "DebugLogsView", body: "This is a testing error, designed to be multiple lines long.", type: .error, target: .app, date: Date()))
99-
#endif
115+
#endif
100116
}
101117
}
102118
}

0 commit comments

Comments
 (0)