From 33d843937e6d22bf3870ac6ffdf337ce0b94ff74 Mon Sep 17 00:00:00 2001 From: Garth Vander Houwen Date: Mon, 16 Jan 2023 23:16:57 -0800 Subject: [PATCH] Edit waypoint round 2 --- Meshtastic/Helpers/BLEManager.swift | 2 +- Meshtastic/Persistence/QueryCoreData.swift | 4 +- .../Views/Map/Custom/MapViewSwiftUI.swift | 39 +++++++++++++------ Meshtastic/Views/Map/WaypointFormView.swift | 11 +++++- Meshtastic/Views/Nodes/NodeDetail.swift | 8 +++- Meshtastic/Views/Nodes/NodeMap.swift | 8 +++- 6 files changed, 52 insertions(+), 20 deletions(-) diff --git a/Meshtastic/Helpers/BLEManager.swift b/Meshtastic/Helpers/BLEManager.swift index 65fe7a12..4e2c12c9 100644 --- a/Meshtastic/Helpers/BLEManager.swift +++ b/Meshtastic/Helpers/BLEManager.swift @@ -735,7 +735,6 @@ class BLEManager: NSObject, CBPeripheralDelegate, ObservableObject { var meshPacket = MeshPacket() meshPacket.to = emptyNodeNum meshPacket.from = fromNodeNum - meshPacket.id = waypointPacket.id meshPacket.wantAck = true var dataMessage = DataMessage() dataMessage.payload = try! waypointPacket.serializedData() @@ -753,6 +752,7 @@ class BLEManager: NSObject, CBPeripheralDelegate, ObservableObject { success = true let wayPointEntity = getWaypoint(id: Int64(waypoint.id), context: context!) + wayPointEntity.id = Int64(waypoint.id) wayPointEntity.name = waypoint.name.count >= 1 ? waypointPacket.name : "Dropped Pin" wayPointEntity.longDescription = waypoint.description_p wayPointEntity.icon = Int64(waypoint.icon) diff --git a/Meshtastic/Persistence/QueryCoreData.swift b/Meshtastic/Persistence/QueryCoreData.swift index 3ddfe46d..72692819 100644 --- a/Meshtastic/Persistence/QueryCoreData.swift +++ b/Meshtastic/Persistence/QueryCoreData.swift @@ -18,7 +18,7 @@ public func getWaypoint(id: Int64, context: NSManagedObjectContext) -> WaypointE return fetchedWaypoint[0] } } catch { - return WaypointEntity() + return WaypointEntity(context: context) } - return WaypointEntity() + return WaypointEntity(context: context) } diff --git a/Meshtastic/Views/Map/Custom/MapViewSwiftUI.swift b/Meshtastic/Views/Map/Custom/MapViewSwiftUI.swift index 5b9d54ab..a3a83077 100644 --- a/Meshtastic/Views/Map/Custom/MapViewSwiftUI.swift +++ b/Meshtastic/Views/Map/Custom/MapViewSwiftUI.swift @@ -9,7 +9,8 @@ import MapKit struct MapViewSwiftUI: UIViewRepresentable { - var onMarkerTap: (_ waypointCoordinate: CLLocationCoordinate2D?, _ tag: Int? ) -> Void + var onLongPress: (_ waypointCoordinate: CLLocationCoordinate2D?, _ tag: Int ) -> Void + var onWaypointEdit: (_ waypointId: Int ) -> Void let mapView = MKMapView() let positions: [PositionEntity] let waypoints: [WaypointEntity] @@ -124,6 +125,7 @@ struct MapViewSwiftUI: UIViewRepresentable { case let waypointAnnotation as WaypointEntity: let annotationView = mapView.dequeueReusableAnnotationView(withIdentifier: "waypoint") as? MKMarkerAnnotationView ?? MKMarkerAnnotationView(annotation: annotation, reuseIdentifier: "Waypoint") annotationView.tag = Int(waypointAnnotation.id) + annotationView.isEnabled = true annotationView.canShowCallout = true if waypointAnnotation.icon == 0 { annotationView.glyphText = "📍" @@ -133,29 +135,42 @@ struct MapViewSwiftUI: UIViewRepresentable { annotationView.clusteringIdentifier = "waypointGroup" annotationView.markerTintColor = UIColor(.indigo) annotationView.titleVisibility = .visible + let editWaypoint = UIButton(type: .detailDisclosure) + editWaypoint.setImage(UIImage(systemName: "square.and.pencil"), for: .normal) + annotationView.rightCalloutAccessoryView = editWaypoint return annotationView default: return nil } } - func mapView(_ mapView: MKMapView, didSelect view: MKAnnotationView) - { - // Only Allow Edit for annotations with a tag - if view.tag > 0 { - // Screen Position - CGPoint - let location = longPressRecognizer.location(in: self.parent.mapView) - // Map Coordinate - CLLocationCoordinate2D - let coordinate = self.parent.mapView.convert(location, toCoordinateFrom: self.parent.mapView) - parent.onMarkerTap(coordinate, view.tag) - } + func mapView(_ mapView: MKMapView, annotationView view: MKAnnotationView, calloutAccessoryControlTapped control: UIControl) { + let control = view.annotation as! WaypointEntity + print(control) + // Only Allow Edit for annotations with a id + //if control.id > 0 { + print(view.tag) + parent.onWaypointEdit(view.tag) + //} } +// func mapView(_ mapView: MKMapView, didSelect view: MKAnnotationView) +// { +// // Only Allow Edit for annotations with a tag +// if view.tag > 0 { +// // Screen Position - CGPoint +// let location = longPressRecognizer.location(in: self.parent.mapView) +// // Map Coordinate - CLLocationCoordinate2D +// let coordinate = self.parent.mapView.convert(location, toCoordinateFrom: self.parent.mapView) +// parent.onMarkerTap(coordinate, view.tag) +// } +// } + @objc func longPressHandler(_ gesture: UILongPressGestureRecognizer) { // Screen Position - CGPoint let location = longPressRecognizer.location(in: self.parent.mapView) // Map Coordinate - CLLocationCoordinate2D let coordinate = self.parent.mapView.convert(location, toCoordinateFrom: self.parent.mapView) - parent.onMarkerTap(coordinate, 0) + parent.onLongPress(coordinate, 0) // Add annotation: let annotation = MKPointAnnotation() annotation.title = "Dropped Pin" diff --git a/Meshtastic/Views/Map/WaypointFormView.swift b/Meshtastic/Views/Map/WaypointFormView.swift index cea01009..3b0f72fd 100644 --- a/Meshtastic/Views/Map/WaypointFormView.swift +++ b/Meshtastic/Views/Map/WaypointFormView.swift @@ -21,7 +21,7 @@ struct WaypointFormView: View { @State private var description: String = "" @State private var icon: String = "📍" @State private var expires: Bool = false - @State private var expire: Date = Date()// = Date.now.addingTimeInterval(60 * 120) // 1 minute * 120 = 2 Hours + @State private var expire: Date = Date() // = Date.now.addingTimeInterval(60 * 120) // 1 minute * 120 = 2 Hours @State private var locked: Bool = false var body: some View { @@ -126,6 +126,7 @@ struct WaypointFormView: View { var newWaypoint = Waypoint() if id == 0 { + newWaypoint.id = UInt32.random(in: UInt32(UInt8.max)..