Skip to content

Commit

Permalink
basic reconnect and log export
Browse files Browse the repository at this point in the history
  • Loading branch information
JJTech0130 committed Mar 27, 2024
1 parent fab0df8 commit 5376a11
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 6 deletions.
29 changes: 23 additions & 6 deletions ValidationRelay/LogView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ struct LogView: View {
HStack {
Text(item.message)
.foregroundColor(item.isError ? .red : .primary)
.lineLimit(4)
Spacer()
Text(item.date, style: .time)
.font(.caption)
Expand All @@ -47,15 +48,31 @@ struct LogView: View {
logItems.items = []
}
}
// Export log button
ToolbarItem(placement: .navigationBarTrailing) {
// Pop up a share sheet, use share sheet icon for button
Button(action: {
// Create a string of the log items
let logString = logItems.items.map { item in
"\(item.date): \(item.message)"
}.joined(separator: "\n")
let av = UIActivityViewController(activityItems: [logString], applicationActivities: nil)
UIApplication.shared.windows.first?.rootViewController?.present(av, animated: true, completion: nil)
}) {
Image(systemName: "square.and.arrow.up")
}
}
}
}
}

#Preview {
LogView(logItems: {
let testLog = LogItems()
testLog.log("Test message")
testLog.log("Test error", isError: true)
return testLog
}())
NavigationView {
LogView(logItems: {
let testLog = LogItems()
testLog.log("Test message")
testLog.log("Test error", isError: true)
return testLog
}())
}
}
6 changes: 6 additions & 0 deletions ValidationRelay/Relay.swift
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ class RelayConnectionManager: WebSocketConnectionDelegate, ObservableObject {
if currentURL?.absoluteString == savedRegistrationURL {
print("Using saved registration code")
logItems.log("Using saved registration code \(savedRegistrationCode)")
logItems.log("Using saved registration secret \(savedRegistrationSecret)")
registerCommand["data"] = ["code": savedRegistrationCode, "secret": savedRegistrationSecret]
}
let data = try! JSONSerialization.data(withJSONObject: registerCommand)
Expand All @@ -88,6 +89,10 @@ class RelayConnectionManager: WebSocketConnectionDelegate, ObservableObject {
if !connectionStatusMessage.contains("Error") {
connectionStatusMessage = ""
}
if currentURL != nil {
logItems.log("Was not a clean disconnect, reconnecting")
connect(currentURL!)
}
}

func webSocketViabilityDidChange(connection: WebSocketConnection, isViable: Bool) {
Expand Down Expand Up @@ -124,6 +129,7 @@ class RelayConnectionManager: WebSocketConnectionDelegate, ObservableObject {
registrationCode = code
if savedRegistrationCode != code {
logItems.log("New registration code \(code)")
logItems.log("secret \(data["secret"] as? String ?? "")")
}
savedRegistrationCode = code
savedRegistrationSecret = data["secret"] as? String ?? ""
Expand Down

0 comments on commit 5376a11

Please sign in to comment.