From 8e4c1037a66c6f1d177fa8e648a72e9f9f9c49ec Mon Sep 17 00:00:00 2001 From: Jacob Fielding Date: Sat, 3 Feb 2024 10:22:41 -0800 Subject: [PATCH] Added additional logic for builders --- Sources/MapLibreSwiftDSL/Data Sources.swift | 13 +++++++++++++ .../MapLibreSwiftDSL/MapViewContentBuilder.swift | 9 +++++++++ Sources/MapLibreSwiftUI/MapView.swift | 15 +++++++++++++-- Sources/MapLibreSwiftUI/MapViewCoordinator.swift | 10 +++++----- 4 files changed, 40 insertions(+), 7 deletions(-) diff --git a/Sources/MapLibreSwiftDSL/Data Sources.swift b/Sources/MapLibreSwiftDSL/Data Sources.swift index 561d20b..988af55 100644 --- a/Sources/MapLibreSwiftDSL/Data Sources.swift +++ b/Sources/MapLibreSwiftDSL/Data Sources.swift @@ -70,6 +70,19 @@ public enum ShapeDataBuilder { return components.flatMap { $0 } } + // Handle if statements + public static func buildEither(first components: [MLNShape]) -> [MLNShape] { + return components + } + + public static func buildEither(second components: [MLNShape]) -> [MLNShape] { + return components + } + + public static func buildOptional(_ components: [MLNShape]?) -> [MLNShape] { + return components ?? [] + } + // Convert the collected MLNShape array to ShapeData public static func buildFinalResult(_ components: [MLNShape]) -> ShapeData { let features = components.compactMap { $0 as? MLNShape & MLNFeature } diff --git a/Sources/MapLibreSwiftDSL/MapViewContentBuilder.swift b/Sources/MapLibreSwiftDSL/MapViewContentBuilder.swift index 7062896..70965c0 100644 --- a/Sources/MapLibreSwiftDSL/MapViewContentBuilder.swift +++ b/Sources/MapLibreSwiftDSL/MapViewContentBuilder.swift @@ -23,6 +23,15 @@ public enum MapViewContentBuilder { return styleCollection.layers } + // Handle an array of MLNShape (if you want to directly pass arrays) + public static func buildArray(_ layer: [StyleLayerDefinition]) -> [StyleLayerDefinition] { + return layer + } + + // Handle for in of MLNShape + public static func buildArray(_ layer: [[StyleLayerDefinition]]) -> [StyleLayerDefinition] { + return layer.flatMap { $0 } + } public static func buildEither(first layer: [StyleLayerDefinition]) -> [StyleLayerDefinition] { return layer diff --git a/Sources/MapLibreSwiftUI/MapView.swift b/Sources/MapLibreSwiftUI/MapView.swift index e69e48a..6e792b4 100644 --- a/Sources/MapLibreSwiftUI/MapView.swift +++ b/Sources/MapLibreSwiftUI/MapView.swift @@ -38,7 +38,7 @@ public struct MapView: UIViewRepresentable { public func makeCoordinator() -> MapViewCoordinator { MapViewCoordinator( parent: self, - onGesture: { processGesture($0, $1) } + onGestureEnd: { processGestureEnd($0, $1) } ) } @@ -96,7 +96,18 @@ public struct MapView: UIViewRepresentable { animated: isStyleLoaded) } - private func processGesture(_ mapView: MLNMapView, _ sender: UIGestureRecognizer) { + /// Runs on gesture ended. + /// + /// Note: Some gestures may need additional behaviors for different gesture.states. + /// + /// - Parameters: + /// - mapView: The MapView emitting the gesture. This is used to calculate the point and coordinate of the gesture. + /// - sender: The UIGestureRecognizer + private func processGestureEnd(_ mapView: MLNMapView, _ sender: UIGestureRecognizer) { + guard sender.state == .ended else { + return + } + let point = sender.location(in: mapView) let coordinate = mapView.convert(point, toCoordinateFrom: mapView) diff --git a/Sources/MapLibreSwiftUI/MapViewCoordinator.swift b/Sources/MapLibreSwiftUI/MapViewCoordinator.swift index ac34a77..330595d 100644 --- a/Sources/MapLibreSwiftUI/MapViewCoordinator.swift +++ b/Sources/MapLibreSwiftUI/MapViewCoordinator.swift @@ -12,22 +12,22 @@ public class MapViewCoordinator: NSObject { // every update cycle so we can avoid unnecessary updates private var snapshotUserLayers: [StyleLayerDefinition] = [] private var snapshotCamera: MapViewCamera? - private var onGesture: (MLNMapView, UIGestureRecognizer) -> Void + private var onGestureEnd: (MLNMapView, UIGestureRecognizer) -> Void init(parent: MapView, - onGesture: @escaping (MLNMapView, UIGestureRecognizer) -> Void) { + onGestureEnd: @escaping (MLNMapView, UIGestureRecognizer) -> Void) { self.parent = parent - self.onGesture = onGesture + self.onGestureEnd = onGestureEnd } // MARK: Core UIView Functionality @objc func captureGesture(_ sender: UIGestureRecognizer) { - guard let mapView else { + guard let mapView, sender.state == .ended else { return } - onGesture(mapView, sender) + onGestureEnd(mapView, sender) } // MARK: - Coordinator API - Camera + Manipulation