Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add mapViewModifier + Improve ShapeDataBuilder #14

Merged
merged 5 commits into from
Jan 23, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 27 additions & 3 deletions Sources/MapLibreSwiftDSL/Data Sources.swift
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,33 @@ public struct ShapeSource: Source {

@resultBuilder
public enum ShapeDataBuilder {
public static func buildBlock(_ components: MLNShape...) -> ShapeData {
let features = components.compactMap({ $0 as? MLNShape & MLNFeature })

// Handle a single MLNShape element
public static func buildExpression(_ expression: MLNShape) -> [MLNShape] {
return [expression]
}

public static func buildExpression(_ expression: [MLNShape]) -> [MLNShape] {
return expression
}

// Combine elements into an array
public static func buildBlock(_ components: [MLNShape]...) -> [MLNShape] {
return components.flatMap { $0 }
}

// Handle an array of MLNShape (if you want to directly pass arrays)
public static func buildArray(_ components: [MLNShape]) -> [MLNShape] {
return components
}

// Handle for in of MLNShape
public static func buildArray(_ components: [[MLNShape]]) -> [MLNShape] {
return components.flatMap { $0 }
}

// Convert the collected MLNShape array to ShapeData
public static func buildFinalResult(_ components: [MLNShape]) -> ShapeData {
let features = components.compactMap { $0 as? MLNShape & MLNFeature }
if features.count == components.count {
return .features(features)
} else {
Expand Down
27 changes: 26 additions & 1 deletion Sources/MapLibreSwiftUI/MapView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ public struct MapView: UIViewRepresentable {

let styleSource: MapStyleSource
let userLayers: [StyleLayerDefinition]
var mapViewModifier: ((MLNMapView) -> Void)?
ianthetechie marked this conversation as resolved.
Show resolved Hide resolved

public init(
styleURL: URL,
Expand All @@ -28,6 +29,27 @@ public struct MapView: UIViewRepresentable {
) {
self.init(styleURL: styleURL, camera: .constant(initialCamera), makeMapContent)
}

/// Allows you to set properties of the underlying MLNMapView directly.
/// Use this function to modify various properties of the MLNMapView instance.
/// For example, you can enable the display of the user's location on the map by setting `showUserLocation` to true.
///
/// - Parameter modifier: A closure that provides you with an MLNMapView so you can set properties.
/// - Returns: A MapView with the modifications applied.
///
/// Example:
/// ```swift
/// MapView()
/// .mapViewModifier { mapView in
/// mapView.showUserLocation = true
/// }
/// ```
///
public func mapViewModifier(_ modifier: @escaping (MLNMapView) -> Void) -> MapView {
var newMapView = self
newMapView.mapViewModifier = modifier
return newMapView
}

public class Coordinator: NSObject, MLNMapViewDelegate {
var parent: MapView
Expand Down Expand Up @@ -192,11 +214,14 @@ public struct MapView: UIViewRepresentable {

public func updateUIView(_ mapView: MLNMapView, context: Context) {
context.coordinator.parent = self
if let mapViewModifier {
mapViewModifier(mapView)
}
// FIXME: This should be a more selective update
context.coordinator.updateStyleSource(styleSource, mapView: mapView)
context.coordinator.updateLayers(mapView: mapView)

// FIXME: This isn't exactly telling us if the *map* is loaded, and the docs for setCenter say it needs t obe.
// FIXME: This isn't exactly telling us if the *map* is loaded, and the docs for setCenter say it needs to be.
let isStyleLoaded = mapView.style != nil

context.coordinator.updateCamera(mapView: mapView,
Expand Down
Loading