From 668fe30806494ec4e38286c341b89368deca0036 Mon Sep 17 00:00:00 2001 From: Garth Vander Houwen Date: Fri, 22 Dec 2023 06:31:27 -0800 Subject: [PATCH] Use locationshandler for GPS data on ios17 --- Meshtastic/Helpers/LocationsHandler.swift | 2 +- Meshtastic/Persistence/UpdateCoreData.swift | 10 ++- Meshtastic/Views/Settings/AppSettings.swift | 81 ++++++++++++++++----- 3 files changed, 70 insertions(+), 23 deletions(-) diff --git a/Meshtastic/Helpers/LocationsHandler.swift b/Meshtastic/Helpers/LocationsHandler.swift index ebe05903..d214f5ee 100644 --- a/Meshtastic/Helpers/LocationsHandler.swift +++ b/Meshtastic/Helpers/LocationsHandler.swift @@ -84,7 +84,7 @@ import CoreLocation func addLocation(_ location: CLLocation) -> Bool { let age = -location.timestamp.timeIntervalSinceNow if age > 10 { - print("Bad Location \(self.count): Too Old \(location)") + print("Bad Location \(self.count): Too Old \(age) seconds ago \(location)") return false } if location.horizontalAccuracy < 0 { diff --git a/Meshtastic/Persistence/UpdateCoreData.swift b/Meshtastic/Persistence/UpdateCoreData.swift index 2e6ed8a2..3a2cf34c 100644 --- a/Meshtastic/Persistence/UpdateCoreData.swift +++ b/Meshtastic/Persistence/UpdateCoreData.swift @@ -156,7 +156,9 @@ func upsertNodeInfoPacket (packet: MeshPacket, context: NSManagedObjectContext) // Update an existing node fetchedNode[0].id = Int64(packet.from) fetchedNode[0].num = Int64(packet.from) - fetchedNode[0].lastHeard = Date(timeIntervalSince1970: TimeInterval(Int64(packet.rxTime))) + if packet.rxTime > 0 { + fetchedNode[0].lastHeard = Date(timeIntervalSince1970: TimeInterval(Int64(packet.rxTime))) + } fetchedNode[0].snr = packet.rxSnr fetchedNode[0].rssi = packet.rxRssi @@ -268,7 +270,11 @@ func upsertPositionPacket (packet: MeshPacket, context: NSManagedObjectContext) mutablePositions.add(position) fetchedNode[0].id = Int64(packet.from) fetchedNode[0].num = Int64(packet.from) - fetchedNode[0].lastHeard = Date(timeIntervalSince1970: TimeInterval(Int64(positionMessage.time))) + if positionMessage.time > 0 { + fetchedNode[0].lastHeard = Date(timeIntervalSince1970: TimeInterval(Int64(positionMessage.time))) + } else if packet.rxTime > 0 { + fetchedNode[0].lastHeard = Date(timeIntervalSince1970: TimeInterval(Int64(packet.rxTime))) + } fetchedNode[0].snr = packet.rxSnr fetchedNode[0].rssi = packet.rxRssi fetchedNode[0].positions = mutablePositions.copy() as? NSOrderedSet diff --git a/Meshtastic/Views/Settings/AppSettings.swift b/Meshtastic/Views/Settings/AppSettings.swift index d1c18930..ffa51ba4 100644 --- a/Meshtastic/Views/Settings/AppSettings.swift +++ b/Meshtastic/Views/Settings/AppSettings.swift @@ -32,29 +32,70 @@ struct AppSettings: View { .toggleStyle(SwitchToggleStyle(tint: .accentColor)) } Section(header: Text("phone.gps")) { - let accuracy = Measurement(value: locationHelper.locationManager.location?.horizontalAccuracy ?? 300, unit: UnitLength.meters) - let altitiude = Measurement(value: locationHelper.locationManager.location?.altitude ?? 0, unit: UnitLength.meters) - let speed = Measurement(value: locationHelper.locationManager.location?.speed ?? 0, unit: UnitSpeed.kilometersPerHour) - HStack { - Label("Accuracy \(accuracy.formatted())", systemImage: "scope") + if #available(iOS 17.0, macOS 14.0, *) { + let horizontalAccuracy = Measurement(value: LocationsHandler.shared.lastLocation.horizontalAccuracy, unit: UnitLength.meters) + let verticalAccuracy = Measurement(value: LocationsHandler.shared.lastLocation.verticalAccuracy, unit: UnitLength.meters) + let altitiude = Measurement(value: LocationsHandler.shared.lastLocation.altitude, unit: UnitLength.meters) + let speed = Measurement(value: LocationsHandler.shared.lastLocation.speed, unit: UnitSpeed.kilometersPerHour) + let speedAccuracy = Measurement(value: LocationsHandler.shared.lastLocation.speedAccuracy, unit: UnitSpeed.metersPerSecond) + Label("Coordinate \(String(format: "%.5f", LocationsHandler.shared.lastLocation.coordinate.latitude)), \(String(format: "%.5f", LocationsHandler.shared.lastLocation.coordinate.longitude))", systemImage: "mappin") .font(.footnote) - Label("Sats \(LocationHelper.satsInView)", systemImage: "sparkles") + .textSelection(.enabled) + HStack { + Label("Accuracy \(horizontalAccuracy.formatted())", systemImage: "scope") + .font(.footnote) + Label("Sats \(LocationsHandler.satsInView)", systemImage: "sparkles") + .font(.footnote) + } + HStack { + if LocationsHandler.shared.lastLocation.verticalAccuracy > 0 { + Label("Altitude \(altitiude.formatted())", systemImage: "mountain.2") + .font(.footnote) + } + Label("Accuracy \(verticalAccuracy.formatted())", systemImage: "lines.measurement.vertical") + .font(.footnote) + } + if LocationsHandler.shared.lastLocation.courseAccuracy > 0 { + let degrees = Angle.degrees(Double(LocationsHandler.shared.lastLocation.course)) + Label { + let heading = Measurement(value: degrees.degrees, unit: UnitAngle.degrees) + Text("Heading: \(heading.formatted())") + } icon: { + Image(systemName: "location.north") + .symbolRenderingMode(.hierarchical) + .rotationEffect(degrees) + } .font(.footnote) - } - Label("Coordinate \(String(format: "%.5f", locationHelper.locationManager.location?.coordinate.latitude ?? 0)), \(String(format: "%.5f", locationHelper.locationManager.location?.coordinate.longitude ?? 0))", systemImage: "mappin") - .font(.footnote) - .textSelection(.enabled) - if locationHelper.locationManager.location?.verticalAccuracy ?? 0 > 0 { - Label("Altitude \(altitiude.formatted())", systemImage: "mountain.2") - .font(.footnote) - } - if locationHelper.locationManager.location?.courseAccuracy ?? 0 > 0 { - Label("Heading \(String(format: "%.2f", locationHelper.locationManager.location?.course ?? 0))°", systemImage: "location.circle") - .font(.footnote) - } - if locationHelper.locationManager.location?.speedAccuracy ?? 0 > 0 { - Label("Speed \(speed.formatted())", systemImage: "speedometer") + } + if LocationsHandler.shared.lastLocation.speedAccuracy > 0 { + Label("Speed \(speed.formatted())", systemImage: "speedometer") + .font(.footnote) + } + } else { + let accuracy = Measurement(value: locationHelper.locationManager.location?.horizontalAccuracy ?? 300, unit: UnitLength.meters) + let altitiude = Measurement(value: locationHelper.locationManager.location?.altitude ?? 0, unit: UnitLength.meters) + let speed = Measurement(value: locationHelper.locationManager.location?.speed ?? 0, unit: UnitSpeed.kilometersPerHour) + HStack { + Label("Accuracy \(accuracy.formatted())", systemImage: "scope") + .font(.footnote) + Label("Sats \(LocationHelper.satsInView)", systemImage: "sparkles") + .font(.footnote) + } + Label("Coordinate \(String(format: "%.5f", locationHelper.locationManager.location?.coordinate.latitude ?? 0)), \(String(format: "%.5f", locationHelper.locationManager.location?.coordinate.longitude ?? 0))", systemImage: "mappin") .font(.footnote) + .textSelection(.enabled) + if locationHelper.locationManager.location?.verticalAccuracy ?? 0 > 0 { + Label("Altitude \(altitiude.formatted())", systemImage: "mountain.2") + .font(.footnote) + } + if locationHelper.locationManager.location?.courseAccuracy ?? 0 > 0 { + Label("Heading \(String(format: "%.2f", locationHelper.locationManager.location?.course ?? 0))°", systemImage: "location.circle") + .font(.footnote) + } + if locationHelper.locationManager.location?.speedAccuracy ?? 0 > 0 { + Label("Speed \(speed.formatted())", systemImage: "speedometer") + .font(.footnote) + } } } Section(header: Text("Location Settings")) {