From 2a38dbbb1cfab3b25e9bd1ae9b6b62c10ed5d0bd Mon Sep 17 00:00:00 2001 From: Garth Vander Houwen Date: Tue, 2 Apr 2024 23:43:00 -0700 Subject: [PATCH] Wire up adding instead of replacing channels via qr code. --- Meshtastic/Helpers/BLEManager.swift | 33 +++++++++++++------ Meshtastic/Views/Settings/ShareChannels.swift | 21 +++++++++--- 2 files changed, 40 insertions(+), 14 deletions(-) diff --git a/Meshtastic/Helpers/BLEManager.swift b/Meshtastic/Helpers/BLEManager.swift index c28363e1..b4b508f0 100644 --- a/Meshtastic/Helpers/BLEManager.swift +++ b/Meshtastic/Helpers/BLEManager.swift @@ -1306,31 +1306,44 @@ class BLEManager: NSObject, CBPeripheralDelegate, MqttClientProxyManagerDelegate public func saveChannelSet(base64UrlString: String, addChannel: Bool = false) -> Bool { if isConnected { + + var i: Int32 = 0 // Before we get started delete the existing channels from the myNodeInfo - let fetchMyInfoRequest: NSFetchRequest = NSFetchRequest.init(entityName: "MyInfoEntity") - fetchMyInfoRequest.predicate = NSPredicate(format: "myNodeNum == %lld", Int64(connectedPeripheral.num)) if !addChannel { tryClearExistingChannels() + } else { + // We are trying to add a channel so lets get the last index + let fetchMyInfoRequest: NSFetchRequest = NSFetchRequest.init(entityName: "MyInfoEntity") + fetchMyInfoRequest.predicate = NSPredicate(format: "myNodeNum == %lld", Int64(connectedPeripheral.num)) + do { + let fetchedMyInfo = try context?.fetch(fetchMyInfoRequest) as? [MyInfoEntity] ?? [] + if fetchedMyInfo.count == 1 { + if addChannel { + i = Int32(fetchedMyInfo[0].channels?.count ?? -1) + // Bail out if the index is negative or bigger than our max of 8 + if i < 0 || i > 8 { + return false + } + } + } + } catch { + print("Failed to find a node MyInfo to save these channels to") + } } let decodedString = base64UrlString.base64urlToBase64() if let decodedData = Data(base64Encoded: decodedString) { do { let channelSet: ChannelSet = try ChannelSet(serializedData: decodedData) - var i: Int32 = 0 for cs in channelSet.settings { var chan = Channel() - - if i == 0 && !addChannel { + if i == 0 { chan.role = Channel.Role.primary - } else { chan.role = Channel.Role.secondary } chan.settings = cs - if !addChannel { - chan.index = i - i += 1 - } + chan.index = i + i += 1 var adminPacket = AdminMessage() adminPacket.setChannel = chan var meshPacket: MeshPacket = MeshPacket() diff --git a/Meshtastic/Views/Settings/ShareChannels.swift b/Meshtastic/Views/Settings/ShareChannels.swift index 0c17e07b..627b7874 100644 --- a/Meshtastic/Views/Settings/ShareChannels.swift +++ b/Meshtastic/Views/Settings/ShareChannels.swift @@ -46,6 +46,7 @@ struct ShareChannels: View { @State var includeChannel5 = true @State var includeChannel6 = true @State var includeChannel7 = true + @State var replaceChannels = true var node: NodeInfoEntity? @State private var channelsUrl = "https://www.meshtastic.org/e/#" var qrCodeImage = QrCodeImage() @@ -53,9 +54,9 @@ struct ShareChannels: View { var body: some View { if #available(iOS 17.0, macOS 14.0, *) { - VStack { - TipView(ShareChannelsTip(), arrowEdge: .bottom) - } +// VStack { +// TipView(ShareChannelsTip(), arrowEdge: .bottom) +// } } GeometryReader { bounds in let smallest = min(bounds.size.width, bounds.size.height) @@ -191,6 +192,17 @@ struct ShareChannels: View { let qrImage = qrCodeImage.generateQRCode(from: channelsUrl) VStack { if node != nil { + Toggle(isOn: $replaceChannels) { + Label(replaceChannels ? "Replace Channels" : "Add Channels", systemImage: replaceChannels ? "arrow.triangle.2.circlepath.circle" : "plus.app") + } + .tint(.accentColor) + .toggleStyle(.button) + .buttonStyle(.bordered) + .buttonBorderShape(.capsule) + .controlSize(.large) + .padding(.top) + .padding(.bottom) + ShareLink("Share QR Code & Link", item: Image(uiImage: qrImage), subject: Text("Meshtastic Node \(node?.user?.shortName ?? "????") has shared channels with you"), @@ -235,6 +247,7 @@ struct ShareChannels: View { .onChange(of: includeChannel5) { _ in generateChannelSet() } .onChange(of: includeChannel6) { _ in generateChannelSet() } .onChange(of: includeChannel7) { _ in generateChannelSet() } + .onChange(of: replaceChannels) { _ in generateChannelSet() } } } func generateChannelSet() { @@ -272,7 +285,7 @@ struct ShareChannels: View { } } let settingsString = try! channelSet.serializedData().base64EncodedString() - channelsUrl = ("https://meshtastic.org/e/#" + settingsString.base64ToBase64url())// + "?add=true") + channelsUrl = ("https://meshtastic.org/e/#" + settingsString.base64ToBase64url() + (replaceChannels ? "" : "?add=true")) } } }