-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
153a7b5
commit 837de24
Showing
4 changed files
with
139 additions
and
52 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
// | ||
// LogView.swift | ||
// ValidationRelay | ||
// | ||
// Created by James Gill on 3/26/24. | ||
// | ||
|
||
import SwiftUI | ||
|
||
struct LogItem: Identifiable, Hashable { | ||
var id = UUID() | ||
var message: String | ||
var isError: Bool | ||
var date: Date | ||
} | ||
|
||
class LogItems: ObservableObject { | ||
@Published var items: [LogItem] = [] | ||
|
||
func log(_ message: String, isError: Bool = false) { | ||
let item = LogItem(message: message, isError: isError, date: Date()) | ||
items.append(item) | ||
} | ||
} | ||
|
||
struct LogView: View { | ||
@ObservedObject var logItems: LogItems | ||
|
||
var body: some View { | ||
List { | ||
ForEach(logItems.items, id: \.self) { item in | ||
HStack { | ||
Text(item.message) | ||
.foregroundColor(item.isError ? .red : .black) | ||
Spacer() | ||
Text(item.date, style: .time) | ||
.font(.caption) | ||
.foregroundColor(.gray) | ||
} | ||
} | ||
}.listStyle(.grouped) | ||
.navigationTitle("Event Log") | ||
// Clear log button | ||
.toolbar { | ||
ToolbarItem(placement: .navigationBarTrailing) { | ||
Button("Clear") { | ||
logItems.items = [] | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
#Preview { | ||
LogView(logItems: { | ||
let testLog = LogItems() | ||
testLog.log("Test message") | ||
testLog.log("Test error", isError: true) | ||
return testLog | ||
}()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters