Decode qr code string

This commit is contained in:
Garth Vander Houwen 2022-10-10 21:21:58 -07:00
parent d1d5bb6537
commit 96aa9a60a6
3 changed files with 45 additions and 25 deletions

View file

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

View file

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

View file

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