From e164a2b62484acc0512f0fb08c6f0744ac96e905 Mon Sep 17 00:00:00 2001 From: Garth Vander Houwen Date: Tue, 14 Nov 2023 18:11:25 -0800 Subject: [PATCH] Waypoints on the mesh map --- .../Nodes/Helpers/Map/WaypointForm.swift | 21 ++++++----- .../Views/Nodes/Helpers/PositionPopover.swift | 8 +++-- .../Views/Nodes/Helpers/WaypointPopover.swift | 9 ++++- Meshtastic/Views/Nodes/MeshMap.swift | 35 +++++++++---------- 4 files changed, 43 insertions(+), 30 deletions(-) diff --git a/Meshtastic/Views/Nodes/Helpers/Map/WaypointForm.swift b/Meshtastic/Views/Nodes/Helpers/Map/WaypointForm.swift index d2f32a5c..27d600f0 100644 --- a/Meshtastic/Views/Nodes/Helpers/Map/WaypointForm.swift +++ b/Meshtastic/Views/Nodes/Helpers/Map/WaypointForm.swift @@ -132,12 +132,13 @@ struct WaypointForm: View { } HStack { Button { - + /// Send a new or exiting waypoint var newWaypoint = Waypoint() - // Loading a waypoint from edit if waypoint.id == 0 { newWaypoint.id = UInt32.random(in: UInt32(UInt8.max).. 0 { - newWaypoint.id = UInt32(waypoint.id) - } + newWaypoint.id = UInt32(waypoint.id) newWaypoint.name = name.count > 0 ? name : "Dropped Pin" newWaypoint.description_p = description newWaypoint.latitudeI = waypoint.longitudeI @@ -218,9 +216,16 @@ struct WaypointForm: View { newWaypoint.lockedTo = UInt32(lockedTo) } } - newWaypoint.expire = 1 + newWaypoint.expire = UInt32(expire.timeIntervalSince1970) if bleManager.sendWaypoint(waypoint: newWaypoint) { - dismiss() + + bleManager.context!.delete(waypoint) + do { + try bleManager.context!.save() + } catch { + bleManager.context!.rollback() + } + dismiss() } else { dismiss() print("Send waypoint failed") diff --git a/Meshtastic/Views/Nodes/Helpers/PositionPopover.swift b/Meshtastic/Views/Nodes/Helpers/PositionPopover.swift index 5aaddd95..461da979 100644 --- a/Meshtastic/Views/Nodes/Helpers/PositionPopover.swift +++ b/Meshtastic/Views/Nodes/Helpers/PositionPopover.swift @@ -9,6 +9,8 @@ import SwiftUI import MapKit struct PositionPopover: View { + @Environment(\.managedObjectContext) var context + @EnvironmentObject var bleManager: BLEManager @Environment(\.dismiss) private var dismiss var position: PositionEntity var popover: Bool = true @@ -17,10 +19,9 @@ struct PositionPopover: View { VStack { HStack { CircleText(text: position.nodePosition?.user?.shortName ?? "?", color: Color(UIColor(hex: UInt32(position.nodePosition?.user?.num ?? 0))), circleSize: 65) - Spacer() + .padding(.trailing, 5) Text(position.nodePosition?.user?.longName ?? "Unknown") .font(.largeTitle) - Spacer() } Divider() HStack (alignment: .center) { @@ -128,9 +129,10 @@ struct PositionPopover: View { if position.nodePosition != nil { if position.nodePosition?.user?.vip ?? false { Image(systemName: "star.fill") + .foregroundColor(.accentColor) .symbolRenderingMode(.hierarchical) .font(.largeTitle) - .padding(.bottom) + .padding(.bottom, 5) } if position.nodePosition?.hasEnvironmentMetrics ?? false { Image(systemName: "cloud.sun.rain") diff --git a/Meshtastic/Views/Nodes/Helpers/WaypointPopover.swift b/Meshtastic/Views/Nodes/Helpers/WaypointPopover.swift index 12b1d1ad..0e01ae67 100644 --- a/Meshtastic/Views/Nodes/Helpers/WaypointPopover.swift +++ b/Meshtastic/Views/Nodes/Helpers/WaypointPopover.swift @@ -24,7 +24,14 @@ struct WaypointPopover: View { Image(systemName: "lock.fill" ) .font(.largeTitle) } else { - // Edit Button + Button { + dismiss() + } label: { + Image(systemName: "square.and.pencil" ) + .font(.largeTitle) + .symbolRenderingMode(.hierarchical) + .foregroundColor(.accentColor) + } } } Divider() diff --git a/Meshtastic/Views/Nodes/MeshMap.swift b/Meshtastic/Views/Nodes/MeshMap.swift index 1f67f1e4..358c73dc 100644 --- a/Meshtastic/Views/Nodes/MeshMap.swift +++ b/Meshtastic/Views/Nodes/MeshMap.swift @@ -8,6 +8,7 @@ import SwiftUI import CoreData import CoreLocation +import Foundation #if canImport(MapKit) import MapKit #endif @@ -18,6 +19,8 @@ struct MeshMap: View { @Environment(\.managedObjectContext) var context @EnvironmentObject var bleManager: BLEManager @StateObject var appState = AppState.shared + + @GestureState var isDetectingLongGesture = false /// Parameters @State var showUserLocation: Bool = true /// Map State User Defaults @@ -62,6 +65,7 @@ struct MeshMap: View { NavigationStack { ZStack { MapReader { reader in + Map(position: $position, bounds: MapCameraBounds(minimumDistance: 1, maximumDistance: .infinity), scope: mapScope) { /// Waypoint Annotations if waypoints.count > 0 && showWaypoints { @@ -151,23 +155,17 @@ struct MeshMap: View { } } } -// .gesture(LongPressGesture(minimumDuration: 0.5).sequenced(before: DragGesture(minimumDistance: 0, coordinateSpace: .local)) -// .onEnded { value in -// switch value { -// case .second(true, let drag): -// /// Convert the touch point to a Coordinate -// newWaypointCoord = reader.convert(drag?.location ?? .zero, from: .local) -// /// Create a new WaypointEntity using the values from the newWaypoint which will trigger the WaypointForm sheet -// editingWaypoint = WaypointEntity(context: context) -// editingWaypoint!.name = "Waypoint Pin" -// editingWaypoint!.expire = Date.now.addingTimeInterval(60 * 480) -// editingWaypoint!.latitudeI = Int32((newWaypointCoord?.latitude ?? 0) * 1e7) -// editingWaypoint!.longitudeI = Int32((newWaypointCoord?.longitude ?? 0) * 1e7) -// editingWaypoint!.id = 0 -// default: -// break -// } -// }) + .onTapGesture(perform: { location in + newWaypointCoord = reader.convert(location , from: .local) + }) + .onLongPressGesture(minimumDuration: 0.5, maximumDistance: 10) { + editingWaypoint = WaypointEntity(context: context) + editingWaypoint!.name = "Waypoint Pin" + editingWaypoint!.expire = Date.now.addingTimeInterval(60 * 480) + editingWaypoint!.latitudeI = Int32((newWaypointCoord?.latitude ?? 0) * 1e7) + editingWaypoint!.longitudeI = Int32((newWaypointCoord?.longitude ?? 0) * 1e7) + editingWaypoint!.id = 0 + } } } .mapScope(mapScope) @@ -190,7 +188,8 @@ struct MeshMap: View { .padding() } .sheet(item: $selectedWaypoint) { selection in - WaypointPopover(waypoint: selection) + //WaypointPopover(waypoint: selection) + WaypointForm(waypoint: selection) .padding() } .sheet(item: $editingWaypoint) { selection in