Meshtastic-Apple/MeshtasticClient/Views/Bluetooth/Connect.swift

236 lines
8 KiB
Swift
Raw Normal View History

2021-08-20 07:56:05 -07:00
//
2021-11-29 21:35:23 -08:00
// Connect.swift
2021-08-20 07:56:05 -07:00
// MeshtasticClient
//
// Created by Garth Vander Houwen on 8/18/21.
//
// Abstract:
// A view allowing you to interact with nearby meshtastic nodes
2021-08-20 07:56:05 -07:00
import SwiftUI
import MapKit
import CoreLocation
import CoreBluetooth
2021-08-20 07:56:05 -07:00
2021-09-18 17:10:22 -07:00
struct Connect: View {
2021-11-29 15:59:06 -08:00
2021-12-12 17:17:46 -08:00
@Environment(\.managedObjectContext) var context
@EnvironmentObject var bleManager: BLEManager
@EnvironmentObject var userSettings: UserSettings
2021-11-29 15:59:06 -08:00
2021-10-17 15:08:12 -07:00
@State var isPreferredRadio: Bool = false
2021-11-29 15:59:06 -08:00
2021-08-20 07:56:05 -07:00
var body: some View {
2021-11-29 15:59:06 -08:00
2021-10-17 15:08:12 -07:00
NavigationView {
2021-11-29 15:59:06 -08:00
2021-09-10 07:41:26 -07:00
VStack {
if bleManager.isSwitchedOn {
2021-11-29 15:59:06 -08:00
List {
if bleManager.lastConnectionError.count > 0 {
2021-11-29 15:59:06 -08:00
Section(header: Text("Connection Error").font(.title)) {
2021-11-29 15:59:06 -08:00
Text(bleManager.lastConnectionError).font(.title3).foregroundColor(.red)
}
.textCase(nil)
}
2021-11-29 15:59:06 -08:00
Section(header: Text("Connected Radio").font(.title)) {
2021-11-29 15:59:06 -08:00
if bleManager.connectedPeripheral != nil && bleManager.connectedPeripheral.peripheral.state == .connected {
HStack {
2021-11-29 15:59:06 -08:00
2021-09-21 20:49:12 -07:00
Image(systemName: "antenna.radiowaves.left.and.right")
.symbolRenderingMode(.hierarchical)
.imageScale(.large).foregroundColor(.green)
.padding(.trailing)
2021-11-29 15:59:06 -08:00
VStack(alignment: .leading) {
2021-12-15 23:53:45 -08:00
if bleManager.connectedPeripheral != nil {
2021-11-29 15:59:06 -08:00
2021-12-16 14:13:54 -08:00
Text(bleManager.connectedPeripheral.longName).font(.title2)
2021-12-25 23:48:12 -08:00
2021-11-29 15:59:06 -08:00
} else {
Text(String(bleManager.connectedPeripheral.peripheral.name ?? "Unknown")).font(.title2)
}
2021-12-16 14:13:54 -08:00
Text("BLE Name: ").font(.caption)+Text(bleManager.connectedPeripheral.name)
.font(.caption).foregroundColor(Color.gray)
2021-12-15 23:53:45 -08:00
if bleManager.connectedPeripheral != nil {
2021-12-16 14:13:54 -08:00
Text("FW Version: ").font(.caption)+Text(bleManager.connectedPeripheral.firmwareVersion)
.font(.caption).foregroundColor(Color.gray)
}
2021-11-17 07:34:37 -08:00
if bleManager.connectedPeripheral.subscribed {
Text("Properly Subscribed").font(.caption)
}
}
Spacer()
2021-11-29 15:59:06 -08:00
VStack(alignment: .center) {
Text("Preferred").font(.caption2)
Text("Radio").font(.caption2)
Toggle("Preferred Radio", isOn: $isPreferredRadio)
.toggleStyle(SwitchToggleStyle(tint: .accentColor))
.labelsHidden()
.onChange(of: isPreferredRadio) { value in
if value {
2021-11-29 15:59:06 -08:00
2021-12-15 23:53:45 -08:00
if bleManager.connectedPeripheral != nil {
2021-12-25 23:48:12 -08:00
2021-12-15 23:53:45 -08:00
let deviceName = (bleManager.connectedPeripheral.peripheral.name ?? "")
userSettings.preferredPeripheralName = deviceName
2021-12-25 23:48:12 -08:00
2021-10-17 15:08:12 -07:00
} else {
2021-12-25 23:48:12 -08:00
userSettings.preferredPeripheralName = bleManager.connectedPeripheral.longName
2021-10-17 15:08:12 -07:00
}
2021-11-29 15:59:06 -08:00
userSettings.preferredPeripheralId = bleManager.connectedPeripheral!.peripheral.identifier.uuidString
} else {
2021-11-29 15:59:06 -08:00
if bleManager.connectedPeripheral != nil && bleManager.connectedPeripheral.peripheral.identifier.uuidString == userSettings.preferredPeripheralId {
2021-11-29 15:59:06 -08:00
userSettings.preferredPeripheralId = ""
userSettings.preferredPeripheralName = ""
}
2021-10-17 15:08:12 -07:00
}
}
}
}
2021-10-17 15:08:12 -07:00
.swipeActions {
2021-11-29 15:59:06 -08:00
2021-10-17 15:08:12 -07:00
Button(role: .destructive) {
2021-11-29 15:59:06 -08:00
if bleManager.connectedPeripheral != nil && bleManager.connectedPeripheral.peripheral.state == CBPeripheralState.connected {
2021-11-21 13:48:28 -08:00
bleManager.disconnectPeripheral()
isPreferredRadio = false
2021-10-17 15:08:12 -07:00
}
} label: {
Label("Disconnect", systemImage: "antenna.radiowaves.left.and.right.slash")
2021-10-17 15:08:12 -07:00
}
}
.padding([.top, .bottom])
2021-11-29 15:59:06 -08:00
} else {
HStack {
2021-09-21 20:49:12 -07:00
Image(systemName: "antenna.radiowaves.left.and.right.slash")
.symbolRenderingMode(.hierarchical)
.imageScale(.large).foregroundColor(.red)
.padding(.trailing)
Text("No device connected").font(.title3)
}
.padding()
}
2021-11-29 15:59:06 -08:00
}
.textCase(nil)
2021-11-29 15:59:06 -08:00
if bleManager.peripherals.count > 0 {
Section(header: Text("Available Radios").font(.title)) {
2021-11-16 10:14:20 -08:00
ForEach(bleManager.peripherals.filter({ $0.peripheral.state == CBPeripheralState.disconnected }).sorted(by: { $0.name < $1.name })) { peripheral in
HStack {
Image(systemName: "circle.fill")
.imageScale(.large).foregroundColor(.gray)
.padding(.trailing)
Button(action: {
self.bleManager.stopScanning()
2021-11-29 15:59:06 -08:00
if bleManager.connectedPeripheral != nil && bleManager.connectedPeripheral.peripheral.state == CBPeripheralState.connected {
2021-11-21 13:48:28 -08:00
self.bleManager.disconnectPeripheral()
}
self.bleManager.connectTo(peripheral: peripheral.peripheral)
if userSettings.preferredPeripheralId == peripheral.peripheral.identifier.uuidString {
2021-11-29 15:59:06 -08:00
isPreferredRadio = true
2021-11-29 15:59:06 -08:00
} else {
isPreferredRadio = false
}
}) {
Text(peripheral.name).font(.title3)
}
Spacer()
Text(String(peripheral.rssi) + " dB").font(.title3)
2021-11-29 15:59:06 -08:00
}.padding([.bottom, .top])
}
}.textCase(nil)
}
}
2021-11-29 15:59:06 -08:00
HStack(alignment: .center) {
Spacer()
Button(action: {
self.bleManager.startScanning()
}) {
Image(systemName: "play.fill").imageScale(.large).foregroundColor(.gray)
Text("Start Scanning").font(.caption)
.font(.caption)
.foregroundColor(.gray)
2021-09-10 21:50:54 -07:00
}
.disabled(self.bleManager.isScanning)
.padding()
.background(Color(.systemGray6))
.clipShape(Capsule())
Spacer()
Button(action: {
self.bleManager.stopScanning()
}) {
Image(systemName: "stop.fill").imageScale(.large).foregroundColor(.gray)
Text("Stop Scanning")
.font(.caption)
.foregroundColor(.gray)
}
.disabled(!self.bleManager.isScanning)
.padding()
.background(Color(.systemGray6))
.clipShape(Capsule())
Spacer()
}
.padding(.bottom, 10)
2021-11-29 15:59:06 -08:00
} else {
Text("Bluetooth: OFF")
.foregroundColor(.red)
.font(.title)
2021-09-10 07:41:26 -07:00
}
}
.navigationTitle("Bluetooth Radios")
2021-09-18 21:11:54 -07:00
.navigationBarItems(trailing:
2021-11-29 15:59:06 -08:00
2021-10-12 17:54:10 -07:00
ZStack {
2021-12-25 23:48:12 -08:00
2021-12-16 14:13:54 -08:00
ConnectedDevice(
bluetoothOn: bleManager.isSwitchedOn,
deviceConnected: bleManager.connectedPeripheral != nil,
name: (bleManager.connectedPeripheral != nil) ? bleManager.connectedPeripheral.shortName :
"???")
}
2021-09-18 21:11:54 -07:00
)
2021-10-05 09:33:10 -07:00
}
2021-10-12 17:54:10 -07:00
.navigationViewStyle(StackNavigationViewStyle())
2021-10-17 15:08:12 -07:00
.onAppear(perform: {
2021-12-12 17:17:46 -08:00
self.bleManager.context = context
2021-10-17 15:08:12 -07:00
if bleManager.connectedPeripheral != nil && userSettings.preferredPeripheralId == bleManager.connectedPeripheral.peripheral.identifier.uuidString {
isPreferredRadio = true
2021-11-29 15:59:06 -08:00
} else {
isPreferredRadio = false
2021-10-17 15:08:12 -07:00
}
})
2021-08-20 07:56:05 -07:00
}
}
2021-09-18 21:11:54 -07:00
struct Connect_Previews: PreviewProvider {
static var previews: some View {
Connect()
.environmentObject(BLEManager())
2021-09-18 21:11:54 -07:00
}
}