Meshtastic-Apple/Meshtastic/Views/Nodes/PositionLog.swift

183 lines
5.1 KiB
Swift
Raw Normal View History

//
// LocationHistory.swift
// Meshtastic
//
// Copyright(c) Garth Vander Houwen 7/5/22.
//
import SwiftUI
struct PositionLog: View {
2023-03-06 10:33:18 -08:00
@Environment(\.managedObjectContext) var context
@EnvironmentObject var bleManager: BLEManager
2023-03-06 10:33:18 -08:00
2022-07-15 15:01:42 -07:00
@State var isExporting = false
@State var exportString = ""
2023-03-06 10:33:18 -08:00
var node: NodeInfoEntity
2023-03-06 10:33:18 -08:00
@State private var isPresentingClearLogConfirm = false
var body: some View {
2023-03-06 10:33:18 -08:00
2022-09-13 09:56:03 -07:00
NavigationStack {
2022-12-31 00:26:59 -08:00
let localeDateFormat = DateFormatter.dateFormat(fromTemplate: "yyMMddjmma", options: 0, locale: Locale.current)
2023-01-12 07:57:24 -08:00
let dateFormatString = (localeDateFormat ?? "MM/dd/YY j:mma").replacingOccurrences(of: ",", with: "")
if UIDevice.current.userInterfaceIdiom == .pad || UIDevice.current.userInterfaceIdiom == .mac {
2023-03-06 10:33:18 -08:00
// Add a table for mac and ipad
2023-03-14 14:33:27 -07:00
Table(node.positions?.reversed() as? [PositionEntity] ?? []) {
2022-11-05 08:26:27 -07:00
TableColumn("SeqNo") { position in
Text(String(position.seqNo))
}
TableColumn("Latitude") { position in
Text(String(format: "%.5f", position.latitude ?? 0))
2022-11-05 08:26:27 -07:00
}
TableColumn("Longitude") { position in
Text(String(format: "%.5f", position.longitude ?? 0))
2022-11-05 08:26:27 -07:00
}
TableColumn("Altitude") { position in
2023-04-08 01:14:17 -07:00
let altitude = Measurement(value: Double(position.altitude), unit: UnitLength.meters)
Text(String(altitude.formatted()))
2022-11-05 08:26:27 -07:00
}
TableColumn("Sats") { position in
Text(String(position.satsInView))
}
TableColumn("Speed") { position in
2023-04-08 01:14:17 -07:00
let speed = Measurement(value: Double(position.speed), unit: UnitSpeed.kilometersPerHour)
Text(speed.formatted())
2022-11-05 08:26:27 -07:00
}
TableColumn("Heading") { position in
2023-04-08 01:14:17 -07:00
Text("\(position.heading)°")
2022-11-05 08:26:27 -07:00
}
TableColumn("SNR") { position in
2022-11-11 11:04:52 -08:00
Text("\(String(format: "%.2f", position.snr)) dB")
}
2022-11-05 08:26:27 -07:00
TableColumn("Time Stamp") { position in
2022-12-31 00:26:59 -08:00
Text(position.time?.formattedDate(format: dateFormatString) ?? NSLocalizedString("unknown.age", comment: ""))
2022-09-13 21:06:54 -07:00
}
}
2023-03-06 10:33:18 -08:00
} else {
2023-03-06 10:33:18 -08:00
ScrollView {
// Use a grid on iOS as a table only shows a single column
let columns = [
GridItem(spacing: 0.1),
GridItem(spacing: 0.1),
GridItem(.flexible(minimum: 35, maximum: 40), spacing: 0.1),
2023-04-08 01:14:17 -07:00
GridItem(.flexible(minimum: 45, maximum: 50), spacing: 0.1),
GridItem(spacing: 0)
]
LazyVGrid(columns: columns, alignment: .leading, spacing: 1) {
2023-03-06 10:33:18 -08:00
2022-09-13 21:06:54 -07:00
GridRow {
2023-03-06 10:33:18 -08:00
Text("Latitude")
2022-09-30 20:25:41 -07:00
.font(.caption2)
.fontWeight(.bold)
Text("Longitude")
2022-09-30 20:25:41 -07:00
.font(.caption2)
.fontWeight(.bold)
Text("Sats")
2022-09-30 20:25:41 -07:00
.font(.caption2)
.fontWeight(.bold)
Text("Alt")
2022-09-30 20:25:41 -07:00
.font(.caption2)
.fontWeight(.bold)
2022-12-31 02:23:21 -08:00
Text("timestamp")
2022-09-30 20:25:41 -07:00
.font(.caption2)
.fontWeight(.bold)
}
ForEach(node.positions!.reversed() as! [PositionEntity], id: \.self) { (mappin: PositionEntity) in
2023-04-08 01:14:17 -07:00
let altitude = Measurement(value: Double(mappin.altitude), unit: UnitLength.meters)
GridRow {
Text(String(format: "%.5f", mappin.latitude ?? 0))
.font(.caption2)
Text(String(format: "%.5f", mappin.longitude ?? 0))
.font(.caption2)
Text(String(mappin.satsInView))
.font(.caption2)
2023-04-08 01:14:17 -07:00
Text(altitude.formatted())
.font(.caption2)
2023-01-12 07:57:24 -08:00
Text(mappin.time?.formattedDate(format: dateFormatString) ?? "Unknown time")
.font(.caption2)
}
}
}
}
.padding(.leading)
}
HStack {
Button(role: .destructive) {
isPresentingClearLogConfirm = true
} label: {
Label("Clear Log", systemImage: "trash.fill")
}
.buttonStyle(.bordered)
.buttonBorderShape(.capsule)
.controlSize(.large)
.padding()
.confirmationDialog(
2022-12-13 08:47:14 -08:00
"are.you.sure",
isPresented: $isPresentingClearLogConfirm,
titleVisibility: .visible
) {
Button("Delete all positions?", role: .destructive) {
if clearPositions(destNum: node.num, context: context) {
2022-10-04 18:47:12 -07:00
print("Successfully Cleared Position Log")
2023-03-06 10:33:18 -08:00
} else {
2022-10-04 18:47:12 -07:00
print("Clear Position Log Failed")
}
}
}
2023-03-06 10:33:18 -08:00
Button {
2023-03-06 10:33:18 -08:00
2023-03-06 15:30:10 -08:00
exportString = positionToCsvFile(positions: node.positions!.array as? [PositionEntity] ?? [])
isExporting = true
2023-03-06 10:33:18 -08:00
} label: {
2023-03-06 10:33:18 -08:00
2022-12-12 20:35:38 -08:00
Label("save", systemImage: "square.and.arrow.down")
}
.buttonStyle(.bordered)
.buttonBorderShape(.capsule)
.controlSize(.large)
.padding()
2022-07-15 15:01:42 -07:00
}
.fileExporter(
isPresented: $isExporting,
document: CsvDocument(emptyCsv: exportString),
contentType: .commaSeparatedText,
defaultFilename: String("\(node.user?.longName ?? "Node") Position Log"),
onCompletion: { result in
if case .success = result {
2023-03-06 10:33:18 -08:00
print("Position log download succeeded.")
self.isExporting = false
2023-03-06 10:33:18 -08:00
} else {
2023-03-06 10:33:18 -08:00
print("Position log download failed: \(result).")
}
}
)
}
.navigationTitle("Position Log \(node.positions?.count ?? 0) Points")
.navigationBarItems(trailing:
ZStack {
ConnectedDevice(bluetoothOn: bleManager.isSwitchedOn, deviceConnected: bleManager.connectedPeripheral != nil, name: (bleManager.connectedPeripheral != nil) ? bleManager.connectedPeripheral.shortName : "????")
})
.onAppear {
self.bleManager.context = context
}
}
}