From 7bb4eb94ab9c00f198af9ac35c76375db5d684ec Mon Sep 17 00:00:00 2001 From: Garth Vander Houwen Date: Sun, 19 Sep 2021 01:10:49 -0700 Subject: [PATCH] Add Connected indicator to navigationBarItems and display the same indicator on the messages page --- Meshtastic Client.xcodeproj/project.pbxproj | 4 --- MeshtasticClient/Helpers/BLEManager.swift | 2 +- MeshtasticClient/Model/ModelData.swift | 5 ++++ MeshtasticClient/Model/MyInfoModel.swift | 2 +- MeshtasticClient/Model/NodeInfoModel.swift | 11 ++++---- .../Views/Bluetooth/Connect.swift | 24 ++++++++++++----- MeshtasticClient/Views/ContentView.swift | 3 ++- .../Views/Helpers/CircleImage.swift | 27 ------------------- .../Views/Messages/MessageList.swift | 24 +++++++++++++++++ 9 files changed, 57 insertions(+), 45 deletions(-) delete mode 100644 MeshtasticClient/Views/Helpers/CircleImage.swift diff --git a/Meshtastic Client.xcodeproj/project.pbxproj b/Meshtastic Client.xcodeproj/project.pbxproj index c1ace865..da0285fc 100644 --- a/Meshtastic Client.xcodeproj/project.pbxproj +++ b/Meshtastic Client.xcodeproj/project.pbxproj @@ -39,7 +39,6 @@ DDC2E17A26CE248F0042C5E4 /* MeshtasticClientUITests.swift in Sources */ = {isa = PBXBuildFile; fileRef = DDC2E17926CE248F0042C5E4 /* MeshtasticClientUITests.swift */; }; DDC2E18F26CE25FE0042C5E4 /* ContentView.swift in Sources */ = {isa = PBXBuildFile; fileRef = DDC2E18E26CE25FE0042C5E4 /* ContentView.swift */; }; DDC2E19126CE26290042C5E4 /* Messages.swift in Sources */ = {isa = PBXBuildFile; fileRef = DDC2E19026CE26290042C5E4 /* Messages.swift */; }; - DDC2E19B26CE27150042C5E4 /* CircleImage.swift in Sources */ = {isa = PBXBuildFile; fileRef = DDC2E19A26CE27150042C5E4 /* CircleImage.swift */; }; DDC2E19D26CE27580042C5E4 /* ModelData.swift in Sources */ = {isa = PBXBuildFile; fileRef = DDC2E19C26CE27580042C5E4 /* ModelData.swift */; }; DDC2E1A726CEB3400042C5E4 /* LocationHelper.swift in Sources */ = {isa = PBXBuildFile; fileRef = DDC2E1A626CEB3400042C5E4 /* LocationHelper.swift */; }; DDC2E1AB26DD89EC0042C5E4 /* packets.json in Resources */ = {isa = PBXBuildFile; fileRef = DDC2E1AA26DD89EC0042C5E4 /* packets.json */; }; @@ -103,7 +102,6 @@ DDC2E17B26CE248F0042C5E4 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = ""; }; DDC2E18E26CE25FE0042C5E4 /* ContentView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ContentView.swift; sourceTree = ""; }; DDC2E19026CE26290042C5E4 /* Messages.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Messages.swift; sourceTree = ""; }; - DDC2E19A26CE27150042C5E4 /* CircleImage.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CircleImage.swift; sourceTree = ""; }; DDC2E19C26CE27580042C5E4 /* ModelData.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ModelData.swift; sourceTree = ""; }; DDC2E1A626CEB3400042C5E4 /* LocationHelper.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LocationHelper.swift; sourceTree = ""; }; DDC2E1AA26DD89EC0042C5E4 /* packets.json */ = {isa = PBXFileReference; lastKnownFileType = text.json; path = packets.json; sourceTree = ""; }; @@ -278,7 +276,6 @@ DDC2E18D26CE25CB0042C5E4 /* Helpers */ = { isa = PBXGroup; children = ( - DDC2E19A26CE27150042C5E4 /* CircleImage.swift */, DD47E3D526F17ED900029299 /* CircleText.swift */, DD47E3D826F3093800029299 /* MessageBubble.swift */, DD90860B26F684AF00DC5189 /* BatteryIcon.swift */, @@ -458,7 +455,6 @@ DDAF8C6326ED0A230058C060 /* admin.pb.swift in Sources */, DDAF8C5826ED07FD0058C060 /* mesh.pb.swift in Sources */, DD47E3D926F3093800029299 /* MessageBubble.swift in Sources */, - DDC2E19B26CE27150042C5E4 /* CircleImage.swift in Sources */, DDAF8C6726ED0C8C0058C060 /* remote_hardware.pb.swift in Sources */, DDAF8C6526ED0A490058C060 /* channel.pb.swift in Sources */, DD47E3DD26F390A000029299 /* MessageDetail.swift in Sources */, diff --git a/MeshtasticClient/Helpers/BLEManager.swift b/MeshtasticClient/Helpers/BLEManager.swift index e0977166..9f9ac5aa 100644 --- a/MeshtasticClient/Helpers/BLEManager.swift +++ b/MeshtasticClient/Helpers/BLEManager.swift @@ -117,7 +117,7 @@ class BLEManager: NSObject, ObservableObject, CBCentralManagerDelegate, CBPeriph } let newPeripheral = Peripheral(id: peripheral.identifier.uuidString, index: peripherals.count, name: peripheralName, rssi: RSSI.intValue) - print(newPeripheral) + //print(newPeripheral) peripherals.append(newPeripheral) } diff --git a/MeshtasticClient/Model/ModelData.swift b/MeshtasticClient/Model/ModelData.swift index cd60cd57..7ad2a671 100644 --- a/MeshtasticClient/Model/ModelData.swift +++ b/MeshtasticClient/Model/ModelData.swift @@ -5,10 +5,15 @@ Storage for model data. import Foundation import Combine +import CoreBluetooth +import CoreData final class ModelData: ObservableObject { @Published var nodes: [NodeInfoModel] = load("packets.json") + + // @ObservedObject var connectedPeripheral: CBPeripheral! + //@Published var connectedPeripheral: CBPeripheral! } func load(_ filename: String) -> T { diff --git a/MeshtasticClient/Model/MyInfoModel.swift b/MeshtasticClient/Model/MyInfoModel.swift index 0f707567..99331f20 100644 --- a/MeshtasticClient/Model/MyInfoModel.swift +++ b/MeshtasticClient/Model/MyInfoModel.swift @@ -7,7 +7,7 @@ import Foundation -struct MyInfoModel: Hashable, Codable, Identifiable { +struct MyInfoModel: Identifiable, Codable { let id = UUID() var myNodeNum: UInt32 diff --git a/MeshtasticClient/Model/NodeInfoModel.swift b/MeshtasticClient/Model/NodeInfoModel.swift index 2aab4941..0afcc0c7 100644 --- a/MeshtasticClient/Model/NodeInfoModel.swift +++ b/MeshtasticClient/Model/NodeInfoModel.swift @@ -8,14 +8,15 @@ import Foundation import SwiftUI import CoreLocation +import CoreData -struct NodeInfoModel: Hashable, Codable, Identifiable { +struct NodeInfoModel: Identifiable, Codable { let id = UUID() var num: UInt32 var user: User - struct User: Hashable, Codable, Identifiable { + struct User: Identifiable, Codable { var id: String var longName: String var shortName: String @@ -24,13 +25,13 @@ struct NodeInfoModel: Hashable, Codable, Identifiable { } var position: Position - struct Position: Hashable, Codable { + struct Position: Codable { var latitudeI: Int32? var latitude: Double? { if let unwrappedLat = latitudeI { let d = Double(unwrappedLat) - return d / 10000000 + return d / 1e7 } else { return nil @@ -41,7 +42,7 @@ struct NodeInfoModel: Hashable, Codable, Identifiable { if let unwrappedLong = longitudeI { let d = Double(unwrappedLong) - return d / 10000000 + return d / 1e7 } else { return nil diff --git a/MeshtasticClient/Views/Bluetooth/Connect.swift b/MeshtasticClient/Views/Bluetooth/Connect.swift index a5f62de3..57d2fafe 100644 --- a/MeshtasticClient/Views/Bluetooth/Connect.swift +++ b/MeshtasticClient/Views/Bluetooth/Connect.swift @@ -28,7 +28,7 @@ struct Connect: View { Section(header: Text("Connected Device").font(.largeTitle)) { if(bleManager.connectedPeripheral != nil){ HStack{ - Image(systemName: "dot.radiowaves.left.and.right").imageScale(.large).foregroundColor(.green) + Image(systemName: "antenna.radiowaves.left.and.right").imageScale(.large).foregroundColor(.green) Text((bleManager.connectedPeripheral.name != nil) ? bleManager.connectedPeripheral.name! : "Unknown").font(.title) } } @@ -94,11 +94,23 @@ struct Connect: View { .navigationTitle("Bluetooth Radios") .navigationBarItems(trailing: HStack { - Button(action: { - self.bleManager.startScanning() - }) { - Image(systemName: "arrow.clockwise.circle").imageScale(.large) - }} + VStack { + if bleManager.isSwitchedOn && bleManager.connectedPeripheral != nil { + Image(systemName: "antenna.radiowaves.left.and.right") + .imageScale(.large) + .foregroundColor(.green) + Text("CONNECTED").font(.caption2).foregroundColor(.gray) + } + else { + + Image(systemName: "antenna.radiowaves.left.and.right") + .imageScale(.large) + .foregroundColor(.red) + Text("DISCONNECTED").font(.caption).foregroundColor(.gray) + + } + } + }.offset(x: 10, y: -10) ) }.navigationViewStyle(StackNavigationViewStyle()) } diff --git a/MeshtasticClient/Views/ContentView.swift b/MeshtasticClient/Views/ContentView.swift index 850e0e60..e6506302 100644 --- a/MeshtasticClient/Views/ContentView.swift +++ b/MeshtasticClient/Views/ContentView.swift @@ -15,8 +15,9 @@ struct ContentView: View { } var body: some View { + TabView(selection: $selection) { - Messages() + MessageList() .tabItem { Label("Messages", systemImage: "message") } diff --git a/MeshtasticClient/Views/Helpers/CircleImage.swift b/MeshtasticClient/Views/Helpers/CircleImage.swift deleted file mode 100644 index 314a2a44..00000000 --- a/MeshtasticClient/Views/Helpers/CircleImage.swift +++ /dev/null @@ -1,27 +0,0 @@ -/* -See LICENSE folder for this sample’s licensing information. - -Abstract: -A view that clips an image to a circle and adds a stroke and shadow. -*/ - -import SwiftUI - -struct CircleImage: View { - var image: Image - - - var body: some View { - image - //.resizable() - .clipShape(/*@START_MENU_TOKEN@*/Circle()/*@END_MENU_TOKEN@*/) - .overlay(Circle().stroke(Color.white, lineWidth: 4)) - .shadow(radius: 7) - } -} - -struct CircleImage_Previews: PreviewProvider { - static var previews: some View { - CircleImage(image: Image("tbeam")) - } -} diff --git a/MeshtasticClient/Views/Messages/MessageList.swift b/MeshtasticClient/Views/Messages/MessageList.swift index 62303ff0..5ff8e262 100644 --- a/MeshtasticClient/Views/Messages/MessageList.swift +++ b/MeshtasticClient/Views/Messages/MessageList.swift @@ -5,6 +5,10 @@ struct MessageList: View { @State var typingMessage: String = "" + @ObservedObject var bleManager = BLEManager() + + @EnvironmentObject var modelData: ModelData + var body: some View { NavigationView { @@ -17,6 +21,26 @@ struct MessageList: View { } .navigationTitle("Channels") .navigationBarTitleDisplayMode(.inline) + .navigationBarItems(trailing: + HStack { + VStack { + if bleManager.isSwitchedOn { + Image(systemName: "antenna.radiowaves.left.and.right") + .imageScale(.large) + .foregroundColor(.green) + Text("CONNECTED").font(.caption2).foregroundColor(.gray) + } + else { + + Image(systemName: "antenna.radiowaves.left.and.right") + .imageScale(.large) + .foregroundColor(.red) + Text("DISCONNECTED").font(.caption).foregroundColor(.gray) + + } + } + }.offset(x: 10, y: -10) + ) } } }