Wire up adding instead of replacing channels via qr code.

This commit is contained in:
Garth Vander Houwen 2024-04-02 23:43:00 -07:00
parent 6a41cf63be
commit 2a38dbbb1c
2 changed files with 40 additions and 14 deletions

View file

@ -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<NSFetchRequestResult> = 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<NSFetchRequestResult> = 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()

View file

@ -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"))
}
}
}