From c819d21d893e58c920a4b33fc022d046359a2e5d Mon Sep 17 00:00:00 2001 From: Garth Vander Houwen Date: Fri, 20 Jan 2023 21:49:54 -0800 Subject: [PATCH] Delete button for waypoints --- .../Views/Map/Custom/MapViewSwiftUI.swift | 4 +-- Meshtastic/Views/Map/WaypointFormView.swift | 30 +++++++++++++++---- Meshtastic/Views/Nodes/NodeDetail.swift | 9 ++---- Meshtastic/Views/Nodes/NodeMap.swift | 9 ++---- 4 files changed, 33 insertions(+), 19 deletions(-) diff --git a/Meshtastic/Views/Map/Custom/MapViewSwiftUI.swift b/Meshtastic/Views/Map/Custom/MapViewSwiftUI.swift index 649b5064..d4de2fb5 100644 --- a/Meshtastic/Views/Map/Custom/MapViewSwiftUI.swift +++ b/Meshtastic/Views/Map/Custom/MapViewSwiftUI.swift @@ -9,7 +9,7 @@ import MapKit struct MapViewSwiftUI: UIViewRepresentable { - var onLongPress: (_ waypointCoordinate: CLLocationCoordinate2D?, _ tag: Int ) -> Void + var onLongPress: (_ waypointCoordinate: CLLocationCoordinate2D) -> Void var onWaypointEdit: (_ waypointId: Int ) -> Void let mapView = MKMapView() let positions: [PositionEntity] @@ -172,7 +172,7 @@ struct MapViewSwiftUI: UIViewRepresentable { annotation.coordinate = coordinate parent.mapView.addAnnotation(annotation) UINotificationFeedbackGenerator().notificationOccurred(.success) - parent.onLongPress(coordinate, 0) + parent.onLongPress(coordinate) } } diff --git a/Meshtastic/Views/Map/WaypointFormView.swift b/Meshtastic/Views/Map/WaypointFormView.swift index 59a149a9..bf8fdee5 100644 --- a/Meshtastic/Views/Map/WaypointFormView.swift +++ b/Meshtastic/Views/Map/WaypointFormView.swift @@ -27,6 +27,7 @@ struct WaypointFormView: View { @State private var locked: Bool = false var body: some View { + Form { let distance = CLLocation(latitude: LocationHelper.currentLocation.latitude, longitude: LocationHelper.currentLocation.longitude).distance(from: CLLocation(latitude: coordinate.latitude, longitude: coordinate.longitude)) Section(header: Text((waypointId > 0) ? "Editing Waypoint" : "Create Waypoint")) { @@ -132,7 +133,7 @@ struct WaypointFormView: View { } else { newWaypoint.id = UInt32.random(in: UInt32(UInt8.max).. 0 ? name : "Dropped Pin" newWaypoint.description_p = description newWaypoint.latitudeI = Int32(coordinate.latitude * 1e7) newWaypoint.longitudeI = Int32(coordinate.longitude * 1e7) @@ -160,17 +161,36 @@ struct WaypointFormView: View { .buttonBorderShape(.capsule) .controlSize(.large) .disabled(bleManager.connectedPeripheral == nil) - .padding() + .padding(.bottom) - Button { + Button(role:.cancel) { dismiss() } label: { - Label("cancel", systemImage: "xmark") + Label("cancel", systemImage: "x.circle") } .buttonStyle(.bordered) .buttonBorderShape(.capsule) .controlSize(.large) - .padding() + .padding(.bottom) + + if waypointId > 0 { + Button(role: .destructive) { + let waypoint = getWaypoint(id: Int64(waypointId), context: bleManager.context!) + bleManager.context!.delete(waypoint) + do { + try bleManager.context!.save() + } catch { + bleManager.context!.rollback() + } + dismiss() + } label: { + Label("delete", systemImage: "trash") + } + .buttonStyle(.bordered) + .buttonBorderShape(.capsule) + .controlSize(.large) + .padding(.bottom) + } } .onChange(of: waypointId) { newId in print(newId) diff --git a/Meshtastic/Views/Nodes/NodeDetail.swift b/Meshtastic/Views/Nodes/NodeDetail.swift index ccd84b5e..032c72be 100644 --- a/Meshtastic/Views/Nodes/NodeDetail.swift +++ b/Meshtastic/Views/Nodes/NodeDetail.swift @@ -48,13 +48,10 @@ struct NodeDetail: View { ZStack { let annotations = node.positions?.array as! [PositionEntity] ZStack { - MapViewSwiftUI(onLongPress: { coord, id in + MapViewSwiftUI(onLongPress: { coord in waypointCoordinate = coord - - if waypointCoordinate != nil { - editingWaypoint = 0 - presentingWaypointForm = true - } + editingWaypoint = 0 + presentingWaypointForm = true }, onWaypointEdit: { wpId in if wpId > 0 { editingWaypoint = wpId diff --git a/Meshtastic/Views/Nodes/NodeMap.swift b/Meshtastic/Views/Nodes/NodeMap.swift index 9e026e8a..75b85af9 100644 --- a/Meshtastic/Views/Nodes/NodeMap.swift +++ b/Meshtastic/Views/Nodes/NodeMap.swift @@ -55,13 +55,10 @@ struct NodeMap: View { NavigationStack { ZStack { - MapViewSwiftUI(onLongPress: { coord, id in + MapViewSwiftUI(onLongPress: { coord in waypointCoordinate = coord - - if waypointCoordinate != nil { - editingWaypoint = 0 - presentingWaypointForm = true - } + editingWaypoint = 0 + presentingWaypointForm = true }, onWaypointEdit: { wpId in if wpId > 0 { editingWaypoint = wpId