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

191 lines
5.7 KiB
Swift
Raw Normal View History

//
// LocationHistory.swift
// Meshtastic
//
// Copyright(c) Garth Vander Houwen 7/5/22.
//
import SwiftUI
struct PositionLog: View {
@Environment(\.managedObjectContext) var context
@EnvironmentObject var bleManager: BLEManager
@Environment(\.verticalSizeClass) var verticalSizeClass: UserInterfaceSizeClass?
@Environment(\.horizontalSizeClass) var horizontalSizeClass: UserInterfaceSizeClass?
var useGrid: Bool {
let result = (verticalSizeClass == .regular || verticalSizeClass == .compact) && horizontalSizeClass == .compact
return result
}
2022-07-15 15:01:42 -07:00
@State var isExporting = false
@State var exportString = ""
var node: NodeInfoEntity
@State private var isPresentingClearLogConfirm = false
@State private var sortOrder = [KeyPathComparator(\PositionEntity.latitude)]
var body: some View {
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 && !useGrid || UIDevice.current.userInterfaceIdiom == .mac {
2023-03-06 10:33:18 -08:00
// Add a table for mac and ipad
var positions = node.positions?.reversed() as? [PositionEntity] ?? []
Table(positions) {
2022-11-05 08:26:27 -07:00
TableColumn("Latitude") { position in
Text(String(format: "%.5f", position.latitude ?? 0))
2022-11-05 08:26:27 -07:00
}
.width(min: 120)
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
}
.width(min: 120)
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
Text(position.time?.formattedDate(format: dateFormatString) ?? "unknown.age".localized)
2022-09-13 21:06:54 -07:00
}
.width(min: 180)
}
} else {
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) {
2022-09-13 21:06:54 -07:00
GridRow {
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(.bottom)
.padding(.trailing)
.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")
} else {
2022-10-04 18:47:12 -07:00
print("Clear Position Log Failed")
}
}
}
Button {
2023-03-06 15:30:10 -08:00
exportString = positionToCsvFile(positions: node.positions!.array as? [PositionEntity] ?? [])
isExporting = true
} label: {
Label("save", systemImage: "square.and.arrow.down")
2022-07-15 15:01:42 -07:00
}
.buttonStyle(.bordered)
.buttonBorderShape(.capsule)
.controlSize(.large)
.padding(.bottom)
.padding(.leading)
}
.fileExporter(
isPresented: $isExporting,
document: CsvDocument(emptyCsv: exportString),
contentType: .commaSeparatedText,
defaultFilename: String("\(node.user?.longName ?? "Node") Position Log"),
onCompletion: { result in
if case .success = result {
print("Position log download succeeded.")
self.isExporting = false
} else {
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
}
}
}