Skip to content

Commit

Permalink
Added additional logic for builders
Browse files Browse the repository at this point in the history
  • Loading branch information
Archdoog committed Feb 3, 2024
1 parent 5cdf002 commit 8e4c103
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 7 deletions.
13 changes: 13 additions & 0 deletions Sources/MapLibreSwiftDSL/Data Sources.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand Down
9 changes: 9 additions & 0 deletions Sources/MapLibreSwiftDSL/MapViewContentBuilder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
15 changes: 13 additions & 2 deletions Sources/MapLibreSwiftUI/MapView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public struct MapView: UIViewRepresentable {
public func makeCoordinator() -> MapViewCoordinator {
MapViewCoordinator(
parent: self,
onGesture: { processGesture($0, $1) }
onGestureEnd: { processGestureEnd($0, $1) }
)
}

Expand Down Expand Up @@ -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)

Expand Down
10 changes: 5 additions & 5 deletions Sources/MapLibreSwiftUI/MapViewCoordinator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 8e4c103

Please sign in to comment.