Add alert for firmware version to contact scanning function

This commit is contained in:
Garth Vander Houwen 2025-05-25 09:14:27 -07:00
parent d56f55d6f0
commit d84e437180

View file

@ -9,14 +9,9 @@ import MeshtasticProtobufs
@main
struct MeshtasticAppleApp: App {
@UIApplicationDelegateAdaptor(MeshtasticAppDelegate.self)
private var appDelegate
@UIApplicationDelegateAdaptor(MeshtasticAppDelegate.self) private var appDelegate
@ObservedObject
var appState: AppState
// @ObservedObject
// private var bleManager: BLEManager
@ObservedObject var appState: AppState
private let persistenceController: PersistenceController
@ -25,6 +20,7 @@ struct MeshtasticAppleApp: App {
@State var incomingUrl: URL?
@State var channelSettings: String?
@State var addChannels = false
public var minimumContactVersion = "2.6.9"
init() {
let persistenceController = PersistenceController.shared
@ -149,56 +145,71 @@ struct MeshtasticAppleApp: App {
}
func handleContactUrl(url: URL) {
let components = url.absoluteString.components(separatedBy: "#")
// Extract contact information from the URL
if let contactData = components.last {
let decodedString = contactData.base64urlToBase64()
if let decodedData = Data(base64Encoded: decodedString) {
do {
let contact = try MeshtasticProtobufs.SharedContact(serializedBytes: decodedData)
// Show an alert to confirm adding the contact
let alertController = UIAlertController(
title: "Add Contact",
message: "Would you like to add \(contact.user.longName) as a contact?",
preferredStyle: .alert
)
alertController.addAction(UIAlertAction(
title: "Yes",
style: .default,
handler: { _ in
let success = BLEManager.shared.addContactFromURL(base64UrlString: contactData)
Logger.services.debug("Contact added from URL: \(success ? "success" : "failed")")
}
))
alertController.addAction(UIAlertAction(
title: "No",
style: .cancel,
handler: nil
))
// Present the alert
if let windowScene = UIApplication.shared.connectedScenes.first as? UIWindowScene,
let rootViewController = windowScene.windows.first?.rootViewController {
rootViewController.present(alertController, animated: true)
}
Logger.services.debug("Contact data extracted from URL: \(contactData, privacy: .public)")
} catch {
Logger.services.error("Failed to parse contact data: \(error.localizedDescription, privacy: .public)")
// Show error alert to user
if let windowScene = UIApplication.shared.connectedScenes.first as? UIWindowScene,
let rootViewController = windowScene.windows.first?.rootViewController {
let errorAlert = UIAlertController(
title: "Error",
message: "Could not process contact information. Invalid format.",
let supportedVersion = UserDefaults.firmwareVersion == "0.0.0" || self.minimumContactVersion.compare(UserDefaults.firmwareVersion, options: .numeric) == .orderedAscending || minimumContactVersion.compare(UserDefaults.firmwareVersion, options: .numeric) == .orderedSame
if !supportedVersion {
// Show an alert letting the user know they need to upgrade their firmware to use the contact import.
let alertController = UIAlertController(
title: "Firmware Upgrade Required",
message: "In order to import contacts via a QR code you need firmware version 2.6.9 or greater.",
preferredStyle: .alert
)
alertController.addAction(UIAlertAction(
title: "Close",
style: .cancel,
handler: nil
))
// Present the alert
if let windowScene = UIApplication.shared.connectedScenes.first as? UIWindowScene,
let rootViewController = windowScene.windows.first?.rootViewController {
rootViewController.present(alertController, animated: true)
}
Logger.services.debug("User Alerted that a firmware upgrade is required to import contacts.")
} else {
let components = url.absoluteString.components(separatedBy: "#")
// Extract contact information from the URL
if let contactData = components.last {
let decodedString = contactData.base64urlToBase64()
if let decodedData = Data(base64Encoded: decodedString) {
do {
let contact = try MeshtasticProtobufs.SharedContact(serializedBytes: decodedData)
// Show an alert to confirm adding the contact
let alertController = UIAlertController(
title: "Add Contact",
message: "Would you like to add \(contact.user.longName) as a contact?",
preferredStyle: .alert
)
errorAlert.addAction(UIAlertAction(title: "OK", style: .default))
rootViewController.present(errorAlert, animated: true)
alertController.addAction(UIAlertAction(
title: "Yes",
style: .default,
handler: { _ in
let success = BLEManager.shared.addContactFromURL(base64UrlString: contactData)
Logger.services.debug("Contact added from URL: \(success ? "success" : "failed")")
}
))
alertController.addAction(UIAlertAction(
title: "No",
style: .cancel,
handler: nil
))
// Present the alert
if let windowScene = UIApplication.shared.connectedScenes.first as? UIWindowScene,
let rootViewController = windowScene.windows.first?.rootViewController {
rootViewController.present(alertController, animated: true)
}
Logger.services.debug("Contact data extracted from URL: \(contactData, privacy: .public)")
} catch {
Logger.services.error("Failed to parse contact data: \(error.localizedDescription, privacy: .public)")
// Show error alert to user
if let windowScene = UIApplication.shared.connectedScenes.first as? UIWindowScene,
let rootViewController = windowScene.windows.first?.rootViewController {
let errorAlert = UIAlertController(
title: "Error",
message: "Could not process contact information. Invalid format.",
preferredStyle: .alert
)
errorAlert.addAction(UIAlertAction(title: "OK", style: .default))
rootViewController.present(errorAlert, animated: true)
}
}
}
}