-
-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Improvements based on feedback for gestures and similar
- Loading branch information
Showing
22 changed files
with
388 additions
and
175 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,43 +1,46 @@ | ||
import Foundation | ||
|
||
@resultBuilder | ||
public enum MapViewContentBuilder { | ||
|
||
public static func buildBlock(_ layers: [StyleLayerDefinition]...) -> [StyleLayerDefinition] { | ||
return layers.flatMap { $0 } | ||
} | ||
|
||
public static func buildOptional(_ layers: [StyleLayerDefinition]?) -> [StyleLayerDefinition] { | ||
return layers ?? [] | ||
public enum MapViewContentBuilder: DefaultResultBuilder { | ||
public static func buildExpression(_ expression: StyleLayerDefinition) -> [StyleLayerDefinition] { | ||
return [expression] | ||
} | ||
|
||
public static func buildExpression(_ layer: StyleLayerDefinition) -> [StyleLayerDefinition] { | ||
return [layer] | ||
public static func buildExpression(_ expression: [StyleLayerDefinition]) -> [StyleLayerDefinition] { | ||
return expression | ||
} | ||
|
||
public static func buildExpression(_ expression: Void) -> [StyleLayerDefinition] { | ||
return [] | ||
} | ||
|
||
public static func buildExpression(_ styleCollection: StyleLayerCollection) -> [StyleLayerDefinition] { | ||
return styleCollection.layers | ||
public static func buildBlock(_ components: [StyleLayerDefinition]...) -> [StyleLayerDefinition] { | ||
return components.flatMap { $0 } | ||
} | ||
|
||
public static func buildArray(_ components: [StyleLayerDefinition]) -> [StyleLayerDefinition] { | ||
return components | ||
} | ||
|
||
public static func buildArray(_ components: [[StyleLayerDefinition]]) -> [StyleLayerDefinition] { | ||
return components.flatMap { $0 } | ||
} | ||
|
||
// Handle an array of MLNShape (if you want to directly pass arrays) | ||
public static func buildArray(_ layer: [StyleLayerDefinition]) -> [StyleLayerDefinition] { | ||
return layer | ||
public static func buildEither(first components: [StyleLayerDefinition]) -> [StyleLayerDefinition] { | ||
return components | ||
} | ||
|
||
// Handle for in of MLNShape | ||
public static func buildArray(_ layer: [[StyleLayerDefinition]]) -> [StyleLayerDefinition] { | ||
return layer.flatMap { $0 } | ||
public static func buildEither(second components: [StyleLayerDefinition]) -> [StyleLayerDefinition] { | ||
return components | ||
} | ||
|
||
public static func buildEither(first layer: [StyleLayerDefinition]) -> [StyleLayerDefinition] { | ||
return layer | ||
public static func buildOptional(_ components: [StyleLayerDefinition]?) -> [StyleLayerDefinition] { | ||
return components ?? [] | ||
} | ||
|
||
public static func buildEither(second layer: [StyleLayerDefinition]) -> [StyleLayerDefinition] { | ||
return layer | ||
// MARK: Custom Handler for StyleLayerCollection type. | ||
|
||
public static func buildExpression(_ styleCollection: StyleLayerCollection) -> [StyleLayerDefinition] { | ||
return styleCollection.layers | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
39 changes: 39 additions & 0 deletions
39
Sources/MapLibreSwiftDSL/Support/DefaultResultBuilder.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import Foundation | ||
|
||
/// Enforces a basic set of result builder definiitons. | ||
/// | ||
/// This is just a tool to make a result builder easier to build, maintain sorting, etc. | ||
public protocol DefaultResultBuilder { | ||
|
||
associatedtype Component | ||
|
||
static func buildExpression(_ expression: Component) -> [Component] | ||
|
||
static func buildExpression(_ expression: [Component]) -> [Component] | ||
|
||
// MARK: Handle void | ||
|
||
static func buildExpression(_ expression: Void) -> [Component] | ||
|
||
// MARK: Combine elements into an array | ||
|
||
static func buildBlock(_ components: [Component]...) -> [Component] | ||
|
||
// MARK: Handle Arrays | ||
|
||
static func buildArray(_ components: [Component]) -> [Component] | ||
|
||
// MARK: Handle for in loops | ||
|
||
static func buildArray(_ components: [[Component]]) -> [Component] | ||
|
||
// MARK: Handle if statements | ||
|
||
static func buildEither(first components: [Component]) -> [Component] | ||
|
||
static func buildEither(second components: [Component]) -> [Component] | ||
|
||
// MARK: Handle Optionals | ||
|
||
static func buildOptional(_ components: [Component]?) -> [Component] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
18 changes: 1 addition & 17 deletions
18
Sources/MapLibreSwiftUI/Extensions/MapLibre/MLNCameraChangeReason.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
64 changes: 64 additions & 0 deletions
64
Sources/MapLibreSwiftUI/Extensions/MapView/MapViewGestures.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
import Foundation | ||
import MapLibre | ||
|
||
extension MapView { | ||
|
||
/// Register a gesture recognizer on the MapView. | ||
/// | ||
/// - Parameters: | ||
/// - mapView: The MLNMapView that will host the gesture itself. | ||
/// - context: The UIViewRepresentable context that will orchestrate the response sender | ||
/// - gesture: The gesture definition. | ||
func registerGesture(_ mapView: MLNMapView, _ context: Context, gesture: MapGesture) { | ||
switch gesture.method { | ||
|
||
case .tap(numberOfTaps: let numberOfTaps): | ||
let gestureRecognizer = UITapGestureRecognizer(target: context.coordinator, | ||
action: #selector(context.coordinator.captureGesture(_:))) | ||
gestureRecognizer.numberOfTapsRequired = numberOfTaps | ||
mapView.addGestureRecognizer(gestureRecognizer) | ||
gesture.gestureRecognizer = gestureRecognizer | ||
|
||
case .longPress(minimumDuration: let minimumDuration): | ||
let gestureRecognizer = UILongPressGestureRecognizer(target: context.coordinator, | ||
action: #selector(context.coordinator.captureGesture(_:))) | ||
gestureRecognizer.minimumPressDuration = minimumDuration | ||
|
||
mapView.addGestureRecognizer(gestureRecognizer) | ||
gesture.gestureRecognizer = gestureRecognizer | ||
} | ||
} | ||
|
||
/// Runs on each gesture change event and filters the appropriate gesture behavior based on the | ||
/// user definition. | ||
/// | ||
/// Since the gestures run "onChange", we run this every time, event when state changes. The implementer is responsible for guarding | ||
/// and handling whatever state logic they want. | ||
/// | ||
/// - Parameters: | ||
/// - mapView: The MapView emitting the gesture. This is used to calculate the point and coordinate of the gesture. | ||
/// - sender: The UIGestureRecognizer | ||
func processGesture(_ mapView: MLNMapView, _ sender: UIGestureRecognizer) { | ||
guard let gesture = self.gestures.first(where: { $0.gestureRecognizer == sender }) else { | ||
assertionFailure("\(sender) is not a registered UIGestureRecongizer on the MapView") | ||
return | ||
} | ||
|
||
// Build the context of the gesture's event. | ||
var point: CGPoint | ||
switch gesture.method { | ||
|
||
case .tap(numberOfTaps: let numberOfTaps): | ||
point = sender.location(ofTouch: numberOfTaps - 1, in: mapView) | ||
case .longPress: | ||
point = sender.location(in: mapView) | ||
} | ||
|
||
let context = MapGestureContext(gestureMethod: gesture.method, | ||
state: sender.state, | ||
point: point, | ||
coordinate: mapView.convert(point, toCoordinateFrom: mapView)) | ||
|
||
gesture.onChange(context) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.