mirror of
https://github.com/meshtastic/Meshtastic-Apple.git
synced 2026-04-20 22:13:56 +00:00
Hook together SaveChannelQRCode View
This commit is contained in:
parent
0d1e92189e
commit
9e84d53a82
4 changed files with 61 additions and 40 deletions
|
|
@ -1057,6 +1057,43 @@ class BLEManager: NSObject, ObservableObject, CBCentralManagerDelegate, CBPeriph
|
|||
return false
|
||||
}
|
||||
|
||||
public func connectToPreferredPeripheral() -> Bool {
|
||||
|
||||
var success = false
|
||||
// Return false if we are not properly connected to a device, handle retry logic in the view for now
|
||||
if connectedPeripheral == nil || connectedPeripheral!.peripheral.state != CBPeripheralState.connected {
|
||||
|
||||
self.disconnectPeripheral()
|
||||
self.startScanning()
|
||||
|
||||
// Try and connect to the preferredPeripherial first
|
||||
let preferredPeripheral = peripherals.filter({ $0.peripheral.identifier.uuidString == UserDefaults.standard.object(forKey: "preferredPeripheralId") as? String ?? "" }).first
|
||||
if preferredPeripheral != nil && preferredPeripheral?.peripheral != nil {
|
||||
connectTo(peripheral: preferredPeripheral!.peripheral)
|
||||
success = true
|
||||
}
|
||||
} else if connectedPeripheral != nil && isSubscribed {
|
||||
success = true
|
||||
}
|
||||
return success
|
||||
}
|
||||
|
||||
public func saveChannelSet(base64String: String) -> Bool {
|
||||
|
||||
if isConnected {
|
||||
var decodedString = base64String.base64urlToBase64()
|
||||
if let decodedData = Data(base64Encoded: decodedString) {
|
||||
do {
|
||||
var channelSet: ChannelSet = try ChannelSet(serializedData: decodedData)
|
||||
print(channelSet)
|
||||
} catch {
|
||||
print("Invalid Meshtastic QR Code Link")
|
||||
}
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
public func saveUser(config: User, fromUser: UserEntity, toUser: UserEntity) -> Int64 {
|
||||
|
||||
var adminPacket = AdminMessage()
|
||||
|
|
|
|||
|
|
@ -24,12 +24,10 @@ struct MeshtasticAppleApp: App {
|
|||
.environmentObject(bleManager)
|
||||
.environmentObject(userSettings)
|
||||
.sheet(isPresented: $saveChannels) {
|
||||
|
||||
SaveChannelQRCode(channelHash: channelSettings ?? "Empty Channel URL")
|
||||
SaveChannelQRCode(channelHash: channelSettings ?? "Empty Channel URL", bleManager: bleManager)
|
||||
.presentationDetents([.medium, .large])
|
||||
.presentationDragIndicator(.visible)
|
||||
}
|
||||
|
||||
.onContinueUserActivity(NSUserActivityTypeBrowsingWeb) { userActivity in
|
||||
|
||||
print("URL received \(userActivity)")
|
||||
|
|
|
|||
|
|
@ -16,7 +16,6 @@ struct DeviceConfig: View {
|
|||
@State private var isPresentingNodeDBResetConfirm = false
|
||||
@State private var isPresentingFactoryResetConfirm = false
|
||||
@State private var isPresentingSaveConfirm = false
|
||||
@State var initialLoad: Bool = true
|
||||
@State var hasChanges = false
|
||||
|
||||
@State var deviceRole = 0
|
||||
|
|
@ -166,17 +165,11 @@ struct DeviceConfig: View {
|
|||
ConnectedDevice(bluetoothOn: bleManager.isSwitchedOn, deviceConnected: bleManager.connectedPeripheral != nil, name: (bleManager.connectedPeripheral != nil) ? bleManager.connectedPeripheral.shortName : "????")
|
||||
})
|
||||
.onAppear {
|
||||
|
||||
if self.initialLoad{
|
||||
|
||||
self.bleManager.context = context
|
||||
|
||||
self.deviceRole = Int(node?.deviceConfig?.role ?? 0)
|
||||
self.serialEnabled = (node?.deviceConfig?.serialEnabled ?? true)
|
||||
self.debugLogEnabled = node?.deviceConfig?.debugLogEnabled ?? false
|
||||
self.hasChanges = false
|
||||
self.initialLoad = false
|
||||
}
|
||||
self.bleManager.context = context
|
||||
self.deviceRole = Int(node?.deviceConfig?.role ?? 0)
|
||||
self.serialEnabled = (node?.deviceConfig?.serialEnabled ?? true)
|
||||
self.debugLogEnabled = node?.deviceConfig?.debugLogEnabled ?? false
|
||||
self.hasChanges = false
|
||||
}
|
||||
.onChange(of: deviceRole) { newRole in
|
||||
|
||||
|
|
|
|||
|
|
@ -8,7 +8,9 @@ import SwiftUI
|
|||
|
||||
struct SaveChannelQRCode: View {
|
||||
var channelHash: String
|
||||
|
||||
var bleManager: BLEManager
|
||||
@State var connectedToDevice = false
|
||||
|
||||
var body: some View {
|
||||
VStack {
|
||||
Text("Save Channel Settings?")
|
||||
|
|
@ -17,32 +19,23 @@ struct SaveChannelQRCode: View {
|
|||
.foregroundColor(.gray)
|
||||
.font(.callout)
|
||||
.padding()
|
||||
Text(channelHash)
|
||||
.font(.caption2)
|
||||
.foregroundColor(.gray)
|
||||
.padding()
|
||||
|
||||
Button {
|
||||
|
||||
} label: {
|
||||
|
||||
Label("Save", systemImage: "square.and.arrow.down")
|
||||
}
|
||||
.buttonStyle(.bordered)
|
||||
.buttonBorderShape(.capsule)
|
||||
.controlSize(.large)
|
||||
.padding()
|
||||
}
|
||||
.onChange(of: channelHash) { newSettings in
|
||||
var decodedString = newSettings.base64urlToBase64()
|
||||
if let decodedData = Data(base64Encoded: decodedString) {
|
||||
do {
|
||||
var channelSet: ChannelSet = try ChannelSet(serializedData: decodedData)
|
||||
print(channelSet)
|
||||
} catch {
|
||||
print("Invalid Meshtastic QR Code Link")
|
||||
|
||||
Button {
|
||||
let success = bleManager.saveChannelSet(base64String: channelHash)
|
||||
|
||||
} label: {
|
||||
Label("Save", systemImage: "square.and.arrow.down")
|
||||
}
|
||||
}
|
||||
.buttonStyle(.bordered)
|
||||
.buttonBorderShape(.capsule)
|
||||
.controlSize(.large)
|
||||
.padding()
|
||||
.disabled(!connectedToDevice)
|
||||
|
||||
}
|
||||
.onAppear {
|
||||
connectedToDevice = bleManager.connectToPreferredPeripheral()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue