From 277d2105326e8cf7d4a7318d3b97f080800659c8 Mon Sep 17 00:00:00 2001 From: Garth Vander Houwen Date: Tue, 21 Jun 2022 01:12:08 -0700 Subject: [PATCH] Add speed and heading to location manager --- MeshtasticApple/Helpers/BLEManager.swift | 1 + MeshtasticApple/Helpers/LocationHelper.swift | 20 ++++++++++++++++++- .../Views/Settings/LoRaConfig.swift | 5 +++-- .../Views/Settings/PositionConfig.swift | 18 ++++++++++------- MeshtasticApple/Views/Settings/Settings.swift | 15 ++++++++++---- 5 files changed, 45 insertions(+), 14 deletions(-) diff --git a/MeshtasticApple/Helpers/BLEManager.swift b/MeshtasticApple/Helpers/BLEManager.swift index d442b3a0..d815eb0c 100644 --- a/MeshtasticApple/Helpers/BLEManager.swift +++ b/MeshtasticApple/Helpers/BLEManager.swift @@ -688,6 +688,7 @@ class BLEManager: NSObject, ObservableObject, CBCentralManagerDelegate, CBPeriph positionPacket.longitudeI = Int32(LocationHelper.currentLocation.longitude * 1e7) positionPacket.time = UInt32(LocationHelper.currentTimestamp.timeIntervalSince1970) positionPacket.altitude = Int32(LocationHelper.currentAltitude) + var meshPacket = MeshPacket() meshPacket.to = UInt32(destNum) diff --git a/MeshtasticApple/Helpers/LocationHelper.swift b/MeshtasticApple/Helpers/LocationHelper.swift index b4ec565d..1137860a 100644 --- a/MeshtasticApple/Helpers/LocationHelper.swift +++ b/MeshtasticApple/Helpers/LocationHelper.swift @@ -8,10 +8,12 @@ class LocationHelper: NSObject, ObservableObject { static let DefaultLocation = CLLocationCoordinate2D(latitude: 37.3346, longitude: -122.0090) static let DefaultAltitude = CLLocationDistance(integerLiteral: 0) + static let DefaultSpeed = CLLocationSpeed(integerLiteral: 0) + static let DefaultHeading = CLLocationDirection(integerLiteral: 0) static var currentLocation: CLLocationCoordinate2D { - guard let location = shared.locationManager.location else { + guard let location = shared.locationManager.location else { return DefaultLocation } return location.coordinate @@ -25,6 +27,22 @@ class LocationHelper: NSObject, ObservableObject { return altitude } + static var currentSpeed: CLLocationSpeed { + + guard let speed = shared.locationManager.location?.speed else { + return DefaultSpeed + } + return speed + } + + static var currentHeading: CLLocationDirection { + + guard let speed = shared.locationManager.location?.course else { + return DefaultHeading + } + return speed + } + static var currentTimestamp: Date { guard let timestamp = shared.locationManager.location?.timestamp else { diff --git a/MeshtasticApple/Views/Settings/LoRaConfig.swift b/MeshtasticApple/Views/Settings/LoRaConfig.swift index 7bef9ffa..5395ccfa 100644 --- a/MeshtasticApple/Views/Settings/LoRaConfig.swift +++ b/MeshtasticApple/Views/Settings/LoRaConfig.swift @@ -189,11 +189,11 @@ struct LoRaConfig: View { @State private var isPresentingSaveConfirm: Bool = false @State var initialLoad: Bool = true + @State var hasChanges = false @State var region = 0 @State var modemPreset = 0 @State var hopLimit = 0 - @State var hasChanges = false var body: some View { @@ -242,6 +242,7 @@ struct LoRaConfig: View { isPresentingSaveConfirm = true } label: { + Label("Save", systemImage: "square.and.arrow.down") } .disabled(bleManager.connectedPeripheral == nil || !hasChanges) @@ -250,6 +251,7 @@ struct LoRaConfig: View { .controlSize(.large) .padding() .confirmationDialog( + "Are you sure?", isPresented: $isPresentingSaveConfirm ) { @@ -314,7 +316,6 @@ struct LoRaConfig: View { hasChanges = true } } - .navigationViewStyle(StackNavigationViewStyle()) } } diff --git a/MeshtasticApple/Views/Settings/PositionConfig.swift b/MeshtasticApple/Views/Settings/PositionConfig.swift index 05c27867..20e3fe13 100644 --- a/MeshtasticApple/Views/Settings/PositionConfig.swift +++ b/MeshtasticApple/Views/Settings/PositionConfig.swift @@ -111,6 +111,8 @@ struct PositionConfig: View { @Environment(\.managedObjectContext) var context @EnvironmentObject var bleManager: BLEManager + var node: NodeInfoEntity + @State var smartPositionEnabled = true @State var deviceGpsEnabled = true @State var fixedPosition = false @@ -181,6 +183,8 @@ struct PositionConfig: View { } } + .disabled(!(node.myInfo?.hasGps ?? true)) + Section(header: Text("Position Packet")) { Toggle(isOn: $smartPositionEnabled) { @@ -213,44 +217,44 @@ struct PositionConfig: View { Label("Altitude", systemImage: "arrow.up") } - .toggleStyle(DefaultToggleStyle()) + .toggleStyle(SwitchToggleStyle(tint: .accentColor)) .listRowSeparator(.visible) Toggle(isOn: $includePosSatsinview) { Label("Number of satellites", systemImage: "skew") } - .toggleStyle(DefaultToggleStyle()) + .toggleStyle(SwitchToggleStyle(tint: .accentColor)) + .disabled(!(node.myInfo?.hasGps ?? true)) .listRowSeparator(.visible) Toggle(isOn: $includePosSeqNos) { //64 Label("Sequence number", systemImage: "number") } - .toggleStyle(DefaultToggleStyle()) + .toggleStyle(SwitchToggleStyle(tint: .accentColor)) .listRowSeparator(.visible) Toggle(isOn: $includePosTimestamp) { //128 Label("Timestamp", systemImage: "clock") } - .toggleStyle(DefaultToggleStyle()) + .toggleStyle(SwitchToggleStyle(tint: .accentColor)) .listRowSeparator(.visible) Toggle(isOn: $includePosHeading) { //128 Label("Vehicle heading", systemImage: "location.circle") } - .toggleStyle(DefaultToggleStyle()) + .toggleStyle(SwitchToggleStyle(tint: .accentColor)) .listRowSeparator(.visible) Toggle(isOn: $includePosSpeed) { //128 Label("Vehicle speed", systemImage: "speedometer") } - .toggleStyle(DefaultToggleStyle()) + .toggleStyle(SwitchToggleStyle(tint: .accentColor)) .listRowSeparator(.visible) - } } } diff --git a/MeshtasticApple/Views/Settings/Settings.swift b/MeshtasticApple/Views/Settings/Settings.swift index c580f17e..d43f4982 100644 --- a/MeshtasticApple/Views/Settings/Settings.swift +++ b/MeshtasticApple/Views/Settings/Settings.swift @@ -25,6 +25,8 @@ struct Settings: View { NavigationView { List { + let connectedNodeNum = bleManager.connectedPeripheral != nil ? bleManager.connectedPeripheral.num : 0 + Section("General") { NavigationLink { AppSettings() @@ -43,7 +45,7 @@ struct Settings: View { } } - Section("Radio Configuration (Non-Functional Interaction Previews except for LoRa Config)") { + Section("Radio Configuration") { NavigationLink { DeviceConfig() @@ -53,6 +55,8 @@ struct Settings: View { .symbolRenderingMode(.hierarchical) Text("Device") } + .disabled(bleManager.connectedPeripheral == nil) + NavigationLink { DisplayConfig() } label: { @@ -61,8 +65,7 @@ struct Settings: View { .symbolRenderingMode(.hierarchical) Text("Display (Device Screen)") } - - let connectedNodeNum = bleManager.connectedPeripheral != nil ? bleManager.connectedPeripheral.num : 0 + .disabled(bleManager.connectedPeripheral == nil) NavigationLink() { @@ -77,7 +80,7 @@ struct Settings: View { .disabled(bleManager.connectedPeripheral == nil) NavigationLink { - PositionConfig() + PositionConfig(node: nodes.first(where: { $0.num == connectedNodeNum }) ?? NodeInfoEntity()) } label: { Image(systemName: "location") @@ -85,6 +88,7 @@ struct Settings: View { Text("Position") } + .disabled(bleManager.connectedPeripheral == nil) } Section("Module Configuration") { @@ -107,6 +111,8 @@ struct Settings: View { Text("Range Test") } + .disabled(!(nodes.first(where: { $0.num == connectedNodeNum })?.myInfo?.hasWifi ?? true) || bleManager.connectedPeripheral == nil) + NavigationLink { TelemetryConfig() } label: { @@ -116,6 +122,7 @@ struct Settings: View { Text("Telemetry (Sensors)") } + .disabled(true) } // Not Implemented: // External Notifications - Not Working