From 3424523d9840478108a1eb3e9651d2e5f73d4abc Mon Sep 17 00:00:00 2001 From: Garth Vander Houwen Date: Sat, 20 Jan 2024 19:33:11 -0800 Subject: [PATCH] Move waypoint deep linking into onchange event --- Meshtastic/Views/Nodes/MeshMap.swift | 49 +++++++++++++++------------- 1 file changed, 26 insertions(+), 23 deletions(-) diff --git a/Meshtastic/Views/Nodes/MeshMap.swift b/Meshtastic/Views/Nodes/MeshMap.swift index 98adbe69..7aedb205 100644 --- a/Meshtastic/Views/Nodes/MeshMap.swift +++ b/Meshtastic/Views/Nodes/MeshMap.swift @@ -253,6 +253,32 @@ struct MeshMap: View { .sheet(isPresented: $isEditingSettings) { MapSettingsForm(nodeHistory: $showNodeHistory, routeLines: $showRouteLines, convexHull: $showConvexHull, traffic: $showTraffic, pointsOfInterest: $showPointsOfInterest, mapLayer: $selectedMapLayer) } + .onChange(of: (appState.navigationPath)) { newPath in + + if ((newPath?.hasPrefix("meshtastic://open-waypoint")) != nil) { + guard let url = URL(string: appState.navigationPath ?? "NONE") else { + print("Invalid URL") + return + } + guard let components = URLComponents(url: url, resolvingAgainstBaseURL: true) else { + print("Invalid URL Components") + return + } + guard let action = components.host, action == "open-waypoint" else { + print("Unknown waypoint URL action") + return + } + guard let waypointId = components.queryItems?.first(where: { $0.name == "id" })?.value else { + print("Waypoint id not found") + return + } + guard let waypoint = waypoints.first(where: { $0.id == Int64(waypointId) }) else { + print("Waypoint not found") + return + } + position = .camera(MapCamera(centerCoordinate: waypoint.coordinate, distance: 150, heading: 0, pitch: 60)) + } + } .onChange(of: (selectedMapLayer)) { newMapLayer in switch selectedMapLayer { case .standard: @@ -334,29 +360,6 @@ struct MeshMap: View { case .offline: mapStyle = MapStyle.hybrid(elevation: .realistic, pointsOfInterest: showPointsOfInterest ? .all : .excludingAll, showsTraffic: showTraffic) } - if ((appState.navigationPath?.hasPrefix("meshtastic://open-waypoint")) != nil) { - guard let url = URL(string: appState.navigationPath ?? "NONE") else { - print("Invalid URL") - return - } - guard let components = URLComponents(url: url, resolvingAgainstBaseURL: true) else { - print("Invalid URL Components") - return - } - guard let action = components.host, action == "open-waypoint" else { - print("Unknown waypoint URL action") - return - } - guard let waypointId = components.queryItems?.first(where: { $0.name == "id" })?.value else { - print("Waypoint name not found") - return - } - guard let waypoint = waypoints.first(where: { $0.id == Int64(waypointId) }) else { - print("Waypoint name not found") - return - } - position = .camera(MapCamera(centerCoordinate: waypoint.coordinate, distance: 150, heading: 0, pitch: 60)) - } } .onDisappear(perform: { UIApplication.shared.isIdleTimerDisabled = false