diff --git a/Meshtastic/Helpers/BLEManager.swift b/Meshtastic/Helpers/BLEManager.swift index a8aa55c5..c3c1da54 100644 --- a/Meshtastic/Helpers/BLEManager.swift +++ b/Meshtastic/Helpers/BLEManager.swift @@ -198,6 +198,7 @@ class BLEManager: NSObject, CBPeripheralDelegate, ObservableObject { func centralManager(_ central: CBCentralManager, didDisconnectPeripheral peripheral: CBPeripheral, error: Error?) { self.connectedPeripheral = nil self.isConnecting = false + self.isConnected = false self.isSubscribed = false if let e = error { // https://developer.apple.com/documentation/corebluetooth/cberror/code diff --git a/Meshtastic/Views/Settings/Config/BluetoothConfig.swift b/Meshtastic/Views/Settings/Config/BluetoothConfig.swift index 7eabda05..412282aa 100644 --- a/Meshtastic/Views/Settings/Config/BluetoothConfig.swift +++ b/Meshtastic/Views/Settings/Config/BluetoothConfig.swift @@ -11,6 +11,7 @@ struct BluetoothConfig: View { @Environment(\.managedObjectContext) var context @EnvironmentObject var bleManager: BLEManager + @Environment(\.dismiss) private var goBack var node: NodeInfoEntity? @@ -19,6 +20,8 @@ struct BluetoothConfig: View { @State var enabled = true @State var mode = 0 @State var fixedPin = "123456" + @State var shortPin = false + var pinLength: Int = 6 let numberFormatter: NumberFormatter = { @@ -57,77 +60,66 @@ struct BluetoothConfig: View { TextField("Fixed PIN", text: $fixedPin) .foregroundColor(.gray) .onChange(of: fixedPin, perform: { value in - - let digitCount = fixedPin.utf8.count - // Only mess with the value if it is too big - if digitCount > 6 || digitCount < 6 { - - fixedPin = "123456" - } - - if digitCount < 6 { - - fixedPin = "123456" + //Require that pin is no more than 6 numbers and no less than 6 numbers + if fixedPin.utf8.count == pinLength { + shortPin = false + } else if fixedPin.utf8.count > pinLength { + shortPin = false + fixedPin = String(fixedPin.prefix(pinLength)) + } else if fixedPin.utf8.count < pinLength { + shortPin = true } }) .foregroundColor(.gray) } .keyboardType(.decimalPad) + if shortPin { + + Text("BLE Pin must be 6 digits long.") + .font(.callout) + .foregroundColor(.red) + } } } } .disabled(bleManager.connectedPeripheral == nil) Button { - isPresentingSaveConfirm = true - } label: { - Label("Save", systemImage: "square.and.arrow.down") } - .disabled(bleManager.connectedPeripheral == nil || !hasChanges) + .disabled(bleManager.connectedPeripheral == nil || !hasChanges || shortPin) .buttonStyle(.bordered) .buttonBorderShape(.capsule) .controlSize(.large) .padding() .confirmationDialog( - "Are you sure you want to save?", isPresented: $isPresentingSaveConfirm, titleVisibility: .visible ) { Button("Save Config for \(bleManager.connectedPeripheral != nil ? bleManager.connectedPeripheral.longName : "Unknown")") { - var bc = Config.BluetoothConfig() bc.enabled = enabled bc.mode = BluetoothModes(rawValue: mode)?.protoEnumValue() ?? Config.BluetoothConfig.PairingMode.randomPin bc.fixedPin = UInt32(fixedPin) ?? 123456 - let adminMessageId = bleManager.saveBluetoothConfig(config: bc, fromUser: node!.user!, toUser: node!.user!) - if adminMessageId > 0 { - // Should show a saved successfully alert once I know that to be true // for now just disable the button after a successful save hasChanges = false - - } else { - + goBack() } } - } message: { - Text("After bluetooth config saves the node will reboot.") } } .navigationTitle("Bluetooth (BLE) Config") .navigationBarItems(trailing: - ZStack { - - ConnectedDevice(bluetoothOn: bleManager.isSwitchedOn, deviceConnected: bleManager.connectedPeripheral != nil, name: (bleManager.connectedPeripheral != nil) ? bleManager.connectedPeripheral.shortName : "????") + ConnectedDevice(bluetoothOn: bleManager.isSwitchedOn, deviceConnected: bleManager.connectedPeripheral != nil, name: (bleManager.connectedPeripheral != nil) ? bleManager.connectedPeripheral.shortName : "????") }) .onAppear { self.bleManager.context = context diff --git a/Meshtastic/Views/Settings/Config/DeviceConfig.swift b/Meshtastic/Views/Settings/Config/DeviceConfig.swift index 58be6096..6800a117 100644 --- a/Meshtastic/Views/Settings/Config/DeviceConfig.swift +++ b/Meshtastic/Views/Settings/Config/DeviceConfig.swift @@ -10,6 +10,7 @@ struct DeviceConfig: View { @Environment(\.managedObjectContext) var context @EnvironmentObject var bleManager: BLEManager + @Environment(\.dismiss) private var goBack var node: NodeInfoEntity? @@ -165,15 +166,11 @@ struct DeviceConfig: View { dc.buzzerGpio = UInt32(buzzerGPIO) let adminMessageId = bleManager.saveDeviceConfig(config: dc, fromUser: node!.user!, toUser: node!.user!) - if adminMessageId > 0 { - // Should show a saved successfully alert once I know that to be true // for now just disable the button after a successful save hasChanges = false - - } else { - + goBack() } } } diff --git a/Meshtastic/Views/Settings/Config/DisplayConfig.swift b/Meshtastic/Views/Settings/Config/DisplayConfig.swift index 99e8df47..61b420c2 100644 --- a/Meshtastic/Views/Settings/Config/DisplayConfig.swift +++ b/Meshtastic/Views/Settings/Config/DisplayConfig.swift @@ -11,6 +11,7 @@ struct DisplayConfig: View { @Environment(\.managedObjectContext) var context @EnvironmentObject var bleManager: BLEManager + @Environment(\.dismiss) private var goBack var node: NodeInfoEntity? @@ -115,9 +116,7 @@ struct DisplayConfig: View { // Should show a saved successfully alert once I know that to be true // for now just disable the button after a successful save hasChanges = false - - } else { - + goBack() } } } diff --git a/Meshtastic/Views/Settings/Config/LoRaConfig.swift b/Meshtastic/Views/Settings/Config/LoRaConfig.swift index 6288fb67..e5584f3d 100644 --- a/Meshtastic/Views/Settings/Config/LoRaConfig.swift +++ b/Meshtastic/Views/Settings/Config/LoRaConfig.swift @@ -11,6 +11,7 @@ struct LoRaConfig: View { @Environment(\.managedObjectContext) var context @EnvironmentObject var bleManager: BLEManager + @Environment(\.dismiss) private var goBack var node: NodeInfoEntity? @@ -83,16 +84,12 @@ struct LoRaConfig: View { lc.txEnabled = true let adminMessageId = bleManager.saveLoRaConfig(config: lc, fromUser: node!.user!, toUser: node!.user!) if adminMessageId > 0 { - // Should show a saved successfully alert once I know that to be true // for now just disable the button after a successful save hasChanges = false - - } else { - + goBack() } } - } message: { Text("After LoRa config saves the node will reboot.") } diff --git a/Meshtastic/Views/Settings/Config/Module/CannedMessagesConfig.swift b/Meshtastic/Views/Settings/Config/Module/CannedMessagesConfig.swift index 13f8a109..6d83625a 100644 --- a/Meshtastic/Views/Settings/Config/Module/CannedMessagesConfig.swift +++ b/Meshtastic/Views/Settings/Config/Module/CannedMessagesConfig.swift @@ -10,6 +10,7 @@ struct CannedMessagesConfig: View { @Environment(\.managedObjectContext) var context @EnvironmentObject var bleManager: BLEManager + @Environment(\.dismiss) private var goBack var node: NodeInfoEntity? @@ -251,6 +252,7 @@ struct CannedMessagesConfig: View { // Should show a saved successfully alert once I know that to be true // for now just disable the button after a successful save hasChanges = false + goBack() } } if hasMessagesChanges { diff --git a/Meshtastic/Views/Settings/Config/Module/ExternalNotificationConfig.swift b/Meshtastic/Views/Settings/Config/Module/ExternalNotificationConfig.swift index b1665c6e..5bceaec3 100644 --- a/Meshtastic/Views/Settings/Config/Module/ExternalNotificationConfig.swift +++ b/Meshtastic/Views/Settings/Config/Module/ExternalNotificationConfig.swift @@ -50,6 +50,7 @@ struct ExternalNotificationConfig: View { @Environment(\.managedObjectContext) var context @EnvironmentObject var bleManager: BLEManager + @Environment(\.dismiss) private var goBack var node: NodeInfoEntity? @@ -148,8 +149,7 @@ struct ExternalNotificationConfig: View { // Should show a saved successfully alert once I know that to be true // for now just disable the button after a successful save hasChanges = false - } else { - + goBack() } } } diff --git a/Meshtastic/Views/Settings/Config/Module/MQTTConfig.swift b/Meshtastic/Views/Settings/Config/Module/MQTTConfig.swift index 208f8d68..a8b135d9 100644 --- a/Meshtastic/Views/Settings/Config/Module/MQTTConfig.swift +++ b/Meshtastic/Views/Settings/Config/Module/MQTTConfig.swift @@ -10,6 +10,7 @@ struct MQTTConfig: View { @Environment(\.managedObjectContext) var context @EnvironmentObject var bleManager: BLEManager + @Environment(\.dismiss) private var goBack var node: NodeInfoEntity? @State private var isPresentingSaveConfirm: Bool = false @State var hasChanges: Bool = false @@ -103,8 +104,6 @@ struct MQTTConfig: View { } .keyboardType(.default) .scrollDismissesKeyboard(.interactively) - - HStack { Label("Password", systemImage: "wallet.pass") TextField("Server Password", text: $password) @@ -153,13 +152,11 @@ struct MQTTConfig: View { .controlSize(.large) .padding() .confirmationDialog( - "Are you sure?", isPresented: $isPresentingSaveConfirm, titleVisibility: .visible ) { Button("Save MQTT Config to \(bleManager.connectedPeripheral != nil ? bleManager.connectedPeripheral.longName : "Unknown")?") { - var mqtt = ModuleConfig.MQTTConfig() mqtt.enabled = self.enabled mqtt.address = self.address @@ -167,17 +164,12 @@ struct MQTTConfig: View { mqtt.password = self.password mqtt.encryptionEnabled = self.encryptionEnabled mqtt.jsonEnabled = self.jsonEnabled - let adminMessageId = bleManager.saveMQTTConfig(config: mqtt, fromUser: node!.user!, toUser: node!.user!) - if adminMessageId > 0 { - // Should show a saved successfully alert once I know that to be true // for now just disable the button after a successful save - self.hasChanges = false - - } else { - + hasChanges = false + goBack() } } } diff --git a/Meshtastic/Views/Settings/Config/Module/RangeTestConfig.swift b/Meshtastic/Views/Settings/Config/Module/RangeTestConfig.swift index f65a61b2..56ea350f 100644 --- a/Meshtastic/Views/Settings/Config/Module/RangeTestConfig.swift +++ b/Meshtastic/Views/Settings/Config/Module/RangeTestConfig.swift @@ -48,6 +48,7 @@ struct RangeTestConfig: View { @Environment(\.managedObjectContext) var context @EnvironmentObject var bleManager: BLEManager + @Environment(\.dismiss) private var goBack var node: NodeInfoEntity? @@ -109,8 +110,7 @@ struct RangeTestConfig: View { // Should show a saved successfully alert once I know that to be true // for now just disable the button after a successful save hasChanges = false - } else { - + goBack() } } } diff --git a/Meshtastic/Views/Settings/Config/Module/SerialConfig.swift b/Meshtastic/Views/Settings/Config/Module/SerialConfig.swift index df362039..16eea485 100644 --- a/Meshtastic/Views/Settings/Config/Module/SerialConfig.swift +++ b/Meshtastic/Views/Settings/Config/Module/SerialConfig.swift @@ -10,6 +10,7 @@ struct SerialConfig: View { @Environment(\.managedObjectContext) var context @EnvironmentObject var bleManager: BLEManager + @Environment(\.dismiss) private var goBack var node: NodeInfoEntity? @@ -142,9 +143,7 @@ struct SerialConfig: View { // Should show a saved successfully alert once I know that to be true // for now just disable the button after a successful save hasChanges = false - - } else { - + goBack() } } } diff --git a/Meshtastic/Views/Settings/Config/Module/TelemetryConfig.swift b/Meshtastic/Views/Settings/Config/Module/TelemetryConfig.swift index c8af1300..c944d987 100644 --- a/Meshtastic/Views/Settings/Config/Module/TelemetryConfig.swift +++ b/Meshtastic/Views/Settings/Config/Module/TelemetryConfig.swift @@ -70,6 +70,7 @@ struct TelemetryConfig: View { @Environment(\.managedObjectContext) var context @EnvironmentObject var bleManager: BLEManager + @Environment(\.dismiss) private var goBack var node: NodeInfoEntity? @@ -148,8 +149,7 @@ struct TelemetryConfig: View { // Should show a saved successfully alert once I know that to be true // for now just disable the button after a successful save hasChanges = false - } else { - + goBack() } } } diff --git a/Meshtastic/Views/Settings/Config/NetworkConfig.swift b/Meshtastic/Views/Settings/Config/NetworkConfig.swift index 8a488a97..3d3bafb9 100644 --- a/Meshtastic/Views/Settings/Config/NetworkConfig.swift +++ b/Meshtastic/Views/Settings/Config/NetworkConfig.swift @@ -11,6 +11,7 @@ struct NetworkConfig: View { @Environment(\.managedObjectContext) var context @EnvironmentObject var bleManager: BLEManager + @Environment(\.dismiss) private var goBack var node: NodeInfoEntity? @@ -119,10 +120,8 @@ struct NetworkConfig: View { if adminMessageId > 0 { // Should show a saved successfully alert once I know that to be true // for now just disable the button after a successful save - self.hasChanges = false - - } else { - + hasChanges = false + goBack() } } } message: { diff --git a/Meshtastic/Views/Settings/Config/PositionConfig.swift b/Meshtastic/Views/Settings/Config/PositionConfig.swift index 71db978d..361df32d 100644 --- a/Meshtastic/Views/Settings/Config/PositionConfig.swift +++ b/Meshtastic/Views/Settings/Config/PositionConfig.swift @@ -27,6 +27,7 @@ struct PositionConfig: View { @Environment(\.managedObjectContext) var context @EnvironmentObject var bleManager: BLEManager + @Environment(\.dismiss) private var goBack var node: NodeInfoEntity? @@ -234,6 +235,7 @@ struct PositionConfig: View { // Should show a saved successfully alert once I know that to be true // for now just disable the button after a successful save hasChanges = false + goBack() } } } diff --git a/Meshtastic/Views/Settings/UserConfig.swift b/Meshtastic/Views/Settings/UserConfig.swift index 87eefd16..c8fe32b4 100644 --- a/Meshtastic/Views/Settings/UserConfig.swift +++ b/Meshtastic/Views/Settings/UserConfig.swift @@ -10,6 +10,7 @@ struct UserConfig: View { @Environment(\.managedObjectContext) var context @EnvironmentObject var bleManager: BLEManager + @Environment(\.dismiss) private var goBack var node: NodeInfoEntity? @@ -90,6 +91,7 @@ struct UserConfig: View { let adminMessageId = bleManager.saveUser(config: u, fromUser: node!.user!, toUser: node!.user!) if adminMessageId > 0 { hasChanges = false + goBack() } } } message: {