2021-10-20 00:31:22 -07:00
|
|
|
import SwiftUI
|
|
|
|
|
import Foundation
|
2021-10-23 10:27:10 -07:00
|
|
|
import UniformTypeIdentifiers
|
2021-10-20 00:31:22 -07:00
|
|
|
|
|
|
|
|
struct MeshLog: View {
|
2021-11-16 10:14:20 -08:00
|
|
|
let logFile = MeshLogger.logFile
|
2021-10-20 00:31:22 -07:00
|
|
|
var text = ""
|
|
|
|
|
@State private var logs = [String]()
|
2021-10-23 10:27:10 -07:00
|
|
|
@State private var isExporting: Bool = false
|
2021-10-24 01:26:04 -07:00
|
|
|
@State private var document: LogDocument = LogDocument(logFile: "MESHTASTIC MESH ACTIVITY LOG\n")
|
2021-11-29 15:59:06 -08:00
|
|
|
|
2021-10-20 00:31:22 -07:00
|
|
|
var body: some View {
|
|
|
|
|
|
|
|
|
|
List(logs, id: \.self, rowContent: Text.init)
|
|
|
|
|
.task {
|
|
|
|
|
do {
|
2021-11-29 15:59:06 -08:00
|
|
|
|
2021-10-20 00:31:22 -07:00
|
|
|
let url = logFile!
|
|
|
|
|
logs.removeAll()
|
|
|
|
|
for try await log in url.lines {
|
|
|
|
|
logs.append(log)
|
2021-10-24 01:26:04 -07:00
|
|
|
document.logFile.append("\(log) \n")
|
2021-10-20 00:31:22 -07:00
|
|
|
}
|
|
|
|
|
logs.reverse()
|
|
|
|
|
} catch {
|
|
|
|
|
// Stop adding logs when an error is thrown
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-10-24 01:26:04 -07:00
|
|
|
.fileExporter(
|
|
|
|
|
isPresented: $isExporting,
|
|
|
|
|
document: document,
|
|
|
|
|
contentType: UTType.plainText,
|
|
|
|
|
defaultFilename: "mesh-activity-log",
|
2021-11-29 17:09:27 -08:00
|
|
|
onCompletion: { result in
|
2021-11-29 15:59:06 -08:00
|
|
|
|
2021-10-24 01:26:04 -07:00
|
|
|
if case .success = result {
|
|
|
|
|
print("Mesh activity log download: success.")
|
|
|
|
|
} else {
|
|
|
|
|
print("Mesh activity log download \(result).")
|
|
|
|
|
}
|
2021-10-23 10:27:10 -07:00
|
|
|
}
|
2021-10-24 01:26:04 -07:00
|
|
|
)
|
2021-11-29 15:59:06 -08:00
|
|
|
|
2021-10-20 00:31:22 -07:00
|
|
|
.textSelection(.enabled)
|
|
|
|
|
.font(.caption2)
|
2021-11-29 15:59:06 -08:00
|
|
|
|
|
|
|
|
HStack(alignment: .center) {
|
2021-10-20 00:31:22 -07:00
|
|
|
Spacer()
|
|
|
|
|
Button(action: {
|
|
|
|
|
let text = ""
|
|
|
|
|
do {
|
|
|
|
|
try text.write(to: logFile!, atomically: false, encoding: .utf8)
|
|
|
|
|
logs.removeAll()
|
|
|
|
|
} catch {
|
|
|
|
|
print(error)
|
|
|
|
|
}
|
2021-11-29 15:59:06 -08:00
|
|
|
|
2021-10-20 00:31:22 -07:00
|
|
|
}) {
|
2022-01-11 20:12:07 -08:00
|
|
|
Image(systemName: "trash.circle.fill")
|
|
|
|
|
.symbolRenderingMode(.hierarchical)
|
|
|
|
|
.imageScale(.large).foregroundColor(.accentColor)
|
2021-10-20 00:31:22 -07:00
|
|
|
Text("Clear Log").font(.caption)
|
|
|
|
|
.font(.caption)
|
2022-01-11 20:12:07 -08:00
|
|
|
.foregroundColor(.accentColor)
|
2021-10-20 00:31:22 -07:00
|
|
|
}
|
|
|
|
|
.padding()
|
|
|
|
|
.background(Color(.systemGray6))
|
|
|
|
|
.clipShape(Capsule())
|
2021-11-29 15:59:06 -08:00
|
|
|
|
2021-10-20 00:31:22 -07:00
|
|
|
Spacer()
|
2021-11-29 15:59:06 -08:00
|
|
|
|
2021-10-20 00:31:22 -07:00
|
|
|
Button(action: {
|
2021-10-23 10:27:10 -07:00
|
|
|
isExporting = true
|
2021-10-20 00:31:22 -07:00
|
|
|
}) {
|
2022-01-11 20:12:07 -08:00
|
|
|
Image(systemName: "arrow.down.circle.fill")
|
|
|
|
|
.symbolRenderingMode(.hierarchical)
|
|
|
|
|
.imageScale(.large).foregroundColor(.accentColor)
|
2021-10-20 00:31:22 -07:00
|
|
|
Text("Download Log")
|
|
|
|
|
.font(.caption)
|
2022-01-11 20:12:07 -08:00
|
|
|
.foregroundColor(.accentColor)
|
2021-10-20 00:31:22 -07:00
|
|
|
}
|
|
|
|
|
.padding()
|
|
|
|
|
.background(Color(.systemGray6))
|
|
|
|
|
.clipShape(Capsule())
|
2021-11-29 15:59:06 -08:00
|
|
|
|
2021-10-20 00:31:22 -07:00
|
|
|
Spacer()
|
2021-11-29 15:59:06 -08:00
|
|
|
|
2021-10-20 00:31:22 -07:00
|
|
|
}
|
|
|
|
|
.padding(.bottom, 10)
|
|
|
|
|
.navigationTitle("Mesh Activity Log")
|
|
|
|
|
}
|
|
|
|
|
}
|