Meshtastic-Apple/MeshtasticClient/Views/Settings/MeshLog.swift

93 lines
2 KiB
Swift
Raw Normal View History

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