From 96aa9a60a6d4fa1c54edb4adcf9fe339ef46766f Mon Sep 17 00:00:00 2001 From: Garth Vander Houwen Date: Mon, 10 Oct 2022 21:21:58 -0700 Subject: [PATCH] Decode qr code string --- Meshtastic/MeshtasticApp.swift | 42 ++++++++++--------- .../Views/Settings/SaveChannelQRCode.swift | 26 ++++++++++-- Meshtastic/Views/Settings/ShareChannels.swift | 2 +- 3 files changed, 45 insertions(+), 25 deletions(-) diff --git a/Meshtastic/MeshtasticApp.swift b/Meshtastic/MeshtasticApp.swift index 25e3cbce..a4f99ec7 100644 --- a/Meshtastic/MeshtasticApp.swift +++ b/Meshtastic/MeshtasticApp.swift @@ -42,8 +42,7 @@ struct MeshtasticAppleApp: App { } } .sheet(isPresented: $saveChannels) { - - let channelSettingsString = incomingUrl?.absoluteString + SaveChannelQRCode(channelHash: channelSettings ?? "Empty Channel URL") .presentationDetents([.medium, .large]) .presentationDragIndicator(.visible) @@ -60,6 +59,7 @@ struct MeshtasticAppleApp: App { saveChannels = true print("User wants to open a Channel Settings URL: \(incomingUrl?.absoluteString ?? "No QR Code Link")") } else { + saveChannels = false print("User wants to import a MBTILES offline map file: \(incomingUrl?.absoluteString ?? "No Tiles link")") } @@ -69,26 +69,28 @@ struct MeshtasticAppleApp: App { let documentsDirectory = fileManager.urls(for: .documentDirectory, in: .userDomainMask).first! let destination = documentsDirectory.appendingPathComponent("offline_map.mbtiles", isDirectory: false) - //do we need to delete an old one? - if (fileManager.fileExists(atPath: destination.path)) { - print("ℹ️ Found an old map file. Deleting it") - try? fileManager.removeItem(atPath: destination.path) - } - - do { - try fileManager.copyItem(at: url, to: destination) - } catch { - print("Copy MB Tile file failed. Error: \(error)") - } - - if (fileManager.fileExists(atPath: destination.path)) { - print("ℹ️ Saved the map file") + if !saveChannels { + //do we need to delete an old one? + if (fileManager.fileExists(atPath: destination.path)) { + print("ℹ️ Found an old map file. Deleting it") + try? fileManager.removeItem(atPath: destination.path) + } - //need to tell the map view that it needs to update and try loading the new overlay - UserDefaults.standard.set(Date().timeIntervalSince1970, forKey: "lastUpdatedLocalMapFile") + do { + try fileManager.copyItem(at: url, to: destination) + } catch { + print("Copy MB Tile file failed. Error: \(error)") + } - } else { - print("💥 Didn't save the map file") + if (fileManager.fileExists(atPath: destination.path)) { + print("ℹ️ Saved the map file") + + //need to tell the map view that it needs to update and try loading the new overlay + UserDefaults.standard.set(Date().timeIntervalSince1970, forKey: "lastUpdatedLocalMapFile") + + } else { + print("💥 Didn't save the map file") + } } }) } diff --git a/Meshtastic/Views/Settings/SaveChannelQRCode.swift b/Meshtastic/Views/Settings/SaveChannelQRCode.swift index cfb4e281..b9f1020d 100644 --- a/Meshtastic/Views/Settings/SaveChannelQRCode.swift +++ b/Meshtastic/Views/Settings/SaveChannelQRCode.swift @@ -9,7 +9,7 @@ import SwiftUI struct SaveChannelQRCode: View { var channelHash: String - + var body: some View { VStack { @@ -17,22 +17,40 @@ struct SaveChannelQRCode: View { Text("Save Channel Settings?") .font(.title) - Text("The settings embedded in this QR code will replace the current settings on your radio.") + Text("These settings will replace the current settings on your radio.") .foregroundColor(.gray) .font(.callout) .padding() Text(channelHash) - .font(.callout) + .font(.caption2) .padding() - Text("This does not work yet.") + Text("Error Message") .font(.callout) + .foregroundColor(.red) .padding() Text("Swipe down to dismiss.") .padding() } + .onChange(of: channelHash) { newSettings in + + var decodedString = newSettings + if !decodedString.hasSuffix("==") { + decodedString = decodedString + "==" + } + + if let decodedData = Data(base64Encoded: decodedString) { + decodedString = String(data: decodedData, encoding: .utf8)! + do { + var channelSet: ChannelSet = try ChannelSet(serializedData: decodedData) + print(channelSet) + } catch { + print("Invalid Meshtastic QR Code Link") + } + } + } } } diff --git a/Meshtastic/Views/Settings/ShareChannels.swift b/Meshtastic/Views/Settings/ShareChannels.swift index aaaf0081..48000f25 100644 --- a/Meshtastic/Views/Settings/ShareChannels.swift +++ b/Meshtastic/Views/Settings/ShareChannels.swift @@ -286,7 +286,7 @@ struct ShareChannels: View { for ch in node!.myInfo!.channels!.array as! [ChannelEntity] { if ch.role > 0 { - if ch.index == 0 || ch.index == 1 && includeChannel1 || ch.index == 2 && includeChannel2 || ch.index == 3 && includeChannel3 || + if ch.index == 0 && includeChannel0 || ch.index == 1 && includeChannel1 || ch.index == 2 && includeChannel2 || ch.index == 3 && includeChannel3 || ch.index == 4 && includeChannel4 || ch.index == 5 && includeChannel5 || ch.index == 6 && includeChannel6 || ch.index == 7 && includeChannel7 { var channelSettings = ChannelSettings()