From 7726914ad14459915532c5660ff552d9efb3fff5 Mon Sep 17 00:00:00 2001 From: Garth Vander Houwen Date: Wed, 22 Sep 2021 21:39:28 -0700 Subject: [PATCH] Finished all the tasks from issue #1, completing all the tasks for the BLE helper and Bluetooth Connect view --- Meshtastic Client.xcodeproj/project.pbxproj | 4 +- MeshtasticClient/Helpers/BLEManager.swift | 55 ++++++++++--------- .../Views/Bluetooth/Connect.swift | 22 +++++--- .../Views/Helpers/ConnectedDevice.swift | 2 +- MeshtasticClient/Views/Nodes/NodeDetail.swift | 2 +- 5 files changed, 48 insertions(+), 37 deletions(-) diff --git a/Meshtastic Client.xcodeproj/project.pbxproj b/Meshtastic Client.xcodeproj/project.pbxproj index a77cef84..7cf01a35 100644 --- a/Meshtastic Client.xcodeproj/project.pbxproj +++ b/Meshtastic Client.xcodeproj/project.pbxproj @@ -643,7 +643,7 @@ "$(inherited)", "@executable_path/Frameworks", ); - MARKETING_VERSION = 1.092; + MARKETING_VERSION = 1.093; PRODUCT_BUNDLE_IDENTIFIER = gvh.MeshtasticClient; PRODUCT_NAME = "$(TARGET_NAME)"; SUPPORTS_MACCATALYST = NO; @@ -669,7 +669,7 @@ "$(inherited)", "@executable_path/Frameworks", ); - MARKETING_VERSION = 1.092; + MARKETING_VERSION = 1.093; PRODUCT_BUNDLE_IDENTIFIER = gvh.MeshtasticClient; PRODUCT_NAME = "$(TARGET_NAME)"; SUPPORTS_MACCATALYST = NO; diff --git a/MeshtasticClient/Helpers/BLEManager.swift b/MeshtasticClient/Helpers/BLEManager.swift index dc757a7a..7a41aa6f 100644 --- a/MeshtasticClient/Helpers/BLEManager.swift +++ b/MeshtasticClient/Helpers/BLEManager.swift @@ -237,6 +237,7 @@ class BLEManager: NSObject, ObservableObject, CBCentralManagerDelegate, CBPeriph if decodedInfo.myInfo.myNodeNum != 0 { + meshData.load() print("Save a myInfo") do { print(try decodedInfo.myInfo.jsonString()) @@ -252,34 +253,35 @@ class BLEManager: NSObject, ObservableObject, CBCentralManagerDelegate, CBPeriph do { if meshData.nodes.contains(where: {$0.id == decodedInfo.nodeInfo.num}) { - // it exists, do something - } - else { - - meshData.nodes.append( - NodeInfoModel( - num: decodedInfo.nodeInfo.num, - user: NodeInfoModel.User( - id: decodedInfo.nodeInfo.user.id, - longName: decodedInfo.nodeInfo.user.longName, - shortName: decodedInfo.nodeInfo.user.shortName, - //macaddr: decodedInfo.nodeInfo.user.macaddr, - hwModel: String(describing: decodedInfo.nodeInfo.user.hwModel) - .uppercased()), - - position: NodeInfoModel.Position( - latitudeI: decodedInfo.nodeInfo.position.latitudeI, - longitudeI: decodedInfo.nodeInfo.position.longitudeI, - altitude: decodedInfo.nodeInfo.position.altitude, - batteryLevel: decodedInfo.nodeInfo.position.batteryLevel, - time: decodedInfo.nodeInfo.position.time), - - lastHeard: decodedInfo.nodeInfo.lastHeard, - snr: decodedInfo.nodeInfo.snr) - ) - meshData.save() + let nodeIndex = meshData.nodes.firstIndex(where: { $0.id == decodedInfo.nodeInfo.num }) + meshData.nodes.remove(at: nodeIndex!) + meshData.save() } + + meshData.nodes.append( + NodeInfoModel( + num: decodedInfo.nodeInfo.num, + user: NodeInfoModel.User( + id: decodedInfo.nodeInfo.user.id, + longName: decodedInfo.nodeInfo.user.longName, + shortName: decodedInfo.nodeInfo.user.shortName, + //macaddr: decodedInfo.nodeInfo.user.macaddr, + hwModel: String(describing: decodedInfo.nodeInfo.user.hwModel) + .uppercased()), + + position: NodeInfoModel.Position( + latitudeI: decodedInfo.nodeInfo.position.latitudeI, + longitudeI: decodedInfo.nodeInfo.position.longitudeI, + altitude: decodedInfo.nodeInfo.position.altitude, + batteryLevel: decodedInfo.nodeInfo.position.batteryLevel, + time: decodedInfo.nodeInfo.position.time), + + lastHeard: decodedInfo.nodeInfo.lastHeard, + snr: decodedInfo.nodeInfo.snr) + ) + meshData.save() + print(try decodedInfo.nodeInfo.jsonString()) } catch { fatalError("Failed to decode json") @@ -298,6 +300,7 @@ class BLEManager: NSObject, ObservableObject, CBCentralManagerDelegate, CBPeriph if decodedInfo.configCompleteID != 0 { print(decodedInfo) + meshData.load() } default: diff --git a/MeshtasticClient/Views/Bluetooth/Connect.swift b/MeshtasticClient/Views/Bluetooth/Connect.swift index 3738ce2b..ca34e285 100644 --- a/MeshtasticClient/Views/Bluetooth/Connect.swift +++ b/MeshtasticClient/Views/Bluetooth/Connect.swift @@ -32,7 +32,18 @@ struct Connect: View { .symbolRenderingMode(.hierarchical) .imageScale(.large).foregroundColor(.green) .padding(.trailing) - Text((bleManager.connectedPeripheral.name != nil) ? bleManager.connectedPeripheral.name! : "Unknown").font(.title3) + Text((bleManager.connectedPeripheral.name != nil) ? bleManager.connectedPeripheral.name! : "Unknown").font(.title2) + } + .padding() + .swipeActions { + Button { + bleManager.disconnectDevice() + } label: { + VStack { + Label("Disconnect", systemImage: "antenna.radiowaves.left.and.right.slash") + } + } + .tint(.red) } } else { @@ -43,11 +54,12 @@ struct Connect: View { .padding(.trailing) Text("No device connected").font(.title3) } + .padding() } }.textCase(nil) - Section(header: Text("New Devices").font(.title)) { + Section(header: Text("Available Devices").font(.title)) { ForEach(bleManager.peripherals.sorted(by: { $0.rssi > $1.rssi })) { peripheral in HStack { Image(systemName: "circle.fill") @@ -62,14 +74,10 @@ struct Connect: View { } Spacer() Text(String(peripheral.rssi) + " dB").font(.title3) - } + }.padding([.bottom,.top]) } }.textCase(nil) - Section(header: Text("Known Devices").font(.title)) { - - }.textCase(nil) - } HStack (alignment: .center) { diff --git a/MeshtasticClient/Views/Helpers/ConnectedDevice.swift b/MeshtasticClient/Views/Helpers/ConnectedDevice.swift index 52f08337..843b834e 100644 --- a/MeshtasticClient/Views/Helpers/ConnectedDevice.swift +++ b/MeshtasticClient/Views/Helpers/ConnectedDevice.swift @@ -37,7 +37,7 @@ struct ConnectedDevice: View { Text("Bluetooth Off").font(.caption).foregroundColor(.red) } } - }.offset(x: 10, y: -10) + }.offset(x: 5, y: -10) } } diff --git a/MeshtasticClient/Views/Nodes/NodeDetail.swift b/MeshtasticClient/Views/Nodes/NodeDetail.swift index 383db149..c9f7031a 100644 --- a/MeshtasticClient/Views/Nodes/NodeDetail.swift +++ b/MeshtasticClient/Views/Nodes/NodeDetail.swift @@ -106,7 +106,7 @@ struct NodeDetail: View { Text("ago").font(.title3) }.padding() Divider() - if node.position.latitudeI != nil { + if node.position.coordinate != nil { HStack(alignment: /*@START_MENU_TOKEN@*/.center/*@END_MENU_TOKEN@*/, spacing: 14) { Image(systemName: "mappin").font(.title).foregroundColor(.blue) VStack(alignment: .leading) {