Hook up waypoint form to long press gesture

This commit is contained in:
Garth Vander Houwen 2023-01-12 23:09:47 -08:00
parent f745197664
commit e4434c8605
4 changed files with 54 additions and 22 deletions

View file

@ -12,7 +12,7 @@ import MapKit
struct DistanceText: View {
var meters: CLLocationDistance
var body: some View {
let distanceFormatter = MKDistanceFormatter()
@ -23,7 +23,6 @@ struct DistanceText_Previews: PreviewProvider {
static var previews: some View {
VStack {
DistanceText(meters: 100)
DistanceText(meters: 1000)
DistanceText(meters: 10000)

View file

@ -10,6 +10,7 @@ import MapKit
struct MapViewSwiftUI: UIViewRepresentable {
var onMarkerTap: (_ waypointCoordinate: CLLocationCoordinate2D? ) -> Void
let mapView = MKMapView()
let positions: [PositionEntity]
let region: MKCoordinateRegion
@ -35,6 +36,7 @@ struct MapViewSwiftUI: UIViewRepresentable {
mapView.setUserTrackingMode(.none, animated: false)
mapView.showsCompass = true
mapView.showsScale = true
mapView.isZoomEnabled = true
mapView.isScrollEnabled = true
mapView.delegate = context.coordinator
return mapView
@ -57,7 +59,7 @@ struct MapViewSwiftUI: UIViewRepresentable {
self.parent = parent
super.init()
self.longPressRecognizer = UILongPressGestureRecognizer(target: self, action: #selector(longPressHandler))
self.longPressRecognizer.minimumPressDuration = 1.0
self.longPressRecognizer.minimumPressDuration = 0.2
self.longPressRecognizer.delegate = self
self.parent.mapView.addGestureRecognizer(longPressRecognizer)
}
@ -83,13 +85,21 @@ struct MapViewSwiftUI: UIViewRepresentable {
}
@objc func longPressHandler(_ gesture: UILongPressGestureRecognizer) {
if gesture.state == .ended {
//if gesture.state == .ended {
// 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)
print(coordinate)
}
// Add annotation:
let annotation = MKPointAnnotation()
annotation.title = "Dropped Pin"
annotation.coordinate = coordinate
parent.mapView.addAnnotation(annotation)
parent.onMarkerTap(coordinate)
UINotificationFeedbackGenerator().notificationOccurred(.success)
//}
}
}

View file

@ -6,26 +6,35 @@
//
import SwiftUI
import CoreLocation
struct WaypointFormView: View {
@State var coordinate: CLLocationCoordinate2D
@Environment(\.dismiss) private var dismiss
@FocusState private var emojiIsFocused: Bool
@State private var id: Int32?
@State private var name: String = ""
@State private var description: String = ""
@State private var emoji: String = "📍"
@State private var latitude: Double = 0.0
@State private var longitude: Double = 0.0
@State private var expire: Date = Date.now.addingTimeInterval(60 * 120) // 1 minute * 120 = 2 Hours
@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 locked: Bool = false
var body: some View {
Form {
Section(header: Text("Waypoint").font(.title3)) {
Text("Distance Away").foregroundColor(Color.gray)
Text("Lat/Long ") + Text(" \(String(latitude) + "," + String(longitude))").foregroundColor(Color.gray)
let distance = CLLocation(latitude: LocationHelper.currentLocation.latitude, longitude: LocationHelper.currentLocation.longitude).distance(from: CLLocation(latitude: coordinate.latitude, longitude: coordinate.longitude))
Section(header: Text("Waypoint")) {
HStack {
Text("Location: \(String(format: "%.5f", coordinate.latitude ) + "," + String(format: "%.5f", coordinate.longitude ))")
.foregroundColor(Color.gray)
.font(.caption2)
if coordinate.latitude != LocationHelper.DefaultLocation.latitude && coordinate.longitude != LocationHelper.DefaultLocation.longitude {
DistanceText(meters: distance)
.foregroundColor(Color.gray)
.font(.caption2)
}
}
HStack {
Text("Name")
Spacer()
@ -91,9 +100,15 @@ struct WaypointFormView: View {
}
}
DatePicker("Expire", selection: $expire, in: Date.now...)
.datePickerStyle(.compact)
.font(.callout)
Toggle(isOn: $expires) {
Label("Expires", systemImage: "clock.badge.xmark")
}
.toggleStyle(SwitchToggleStyle(tint: .accentColor))
if expires {
DatePicker("Expire", selection: $expire, in: Date.now...)
.datePickerStyle(.compact)
.font(.callout)
}
Toggle(isOn: $locked) {
Label("Locked", systemImage: "lock")
}
@ -103,13 +118,14 @@ struct WaypointFormView: View {
HStack {
Button {
dismiss()
} label: {
Label("save", systemImage: "square.and.arrow.down")
}
.buttonStyle(.bordered)
.buttonBorderShape(.capsule)
.controlSize(.large)
.padding(5)
.padding()
Button {
dismiss()
@ -119,7 +135,11 @@ struct WaypointFormView: View {
.buttonStyle(.bordered)
.buttonBorderShape(.capsule)
.controlSize(.large)
.padding(5)
.padding()
}
}
}
//var smiley = "😊"
//var data: NSData = smiley.dataUsingEncoding(NSUTF32LittleEndianStringEncoding, allowLossyConversion: false)!
//var unicode:UInt32 = UInt32()
//data.getBytes(&unicode)

View file

@ -13,10 +13,11 @@ struct NodeDetail: View {
@EnvironmentObject var bleManager: BLEManager
@State var satsInView = 0
@State private var mapType: MKMapType = .standard
@State var waypointCoordinate: CLLocationCoordinate2D?
@State private var showingDetailsPopover = false
@State private var showingShutdownConfirm: Bool = false
@State private var showingRebootConfirm: Bool = false
@State private var presentingWaypointForm = true
@State private var presentingWaypointForm = false
var node: NodeInfoEntity
@ -33,7 +34,10 @@ struct NodeDetail: View {
ZStack {
let annotations = node.positions?.array as! [PositionEntity]
ZStack {
MapViewSwiftUI(positions: annotations, region: MKCoordinateRegion(center: nodeCoordinatePosition, span: MKCoordinateSpan(latitudeDelta: 0.005, longitudeDelta: 0.005)), mapViewType: mapType)
MapViewSwiftUI(onMarkerTap: { coord in
presentingWaypointForm = true
waypointCoordinate = coord
}, positions: annotations, region: MKCoordinateRegion(center: nodeCoordinatePosition, span: MKCoordinateSpan(latitudeDelta: 0.005, longitudeDelta: 0.005)), mapViewType: mapType)
VStack {
Spacer()
Text(mostRecent.satsInView > 0 ? "Sats: \(mostRecent.satsInView)" : " ")
@ -362,8 +366,7 @@ struct NodeDetail: View {
}
.edgesIgnoringSafeArea([.leading, .trailing])
.sheet(isPresented: $presentingWaypointForm ) {//, onDismiss: didDismissSheet) {
WaypointFormView()
WaypointFormView(coordinate: waypointCoordinate ?? LocationHelper.DefaultLocation)
.presentationDetents([.medium, .large])
.presentationDragIndicator(.automatic)
}