Skip to content

Commit

Permalink
add generics to MapView
Browse files Browse the repository at this point in the history
  • Loading branch information
Patrick-Kladek committed Jun 5, 2024
1 parent 8b9390b commit 00ac500
Show file tree
Hide file tree
Showing 9 changed files with 32 additions and 30 deletions.
2 changes: 1 addition & 1 deletion Sources/MapLibreSwiftUI/Examples/Camera.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ struct CameraDirectManipulationPreview: View {
var targetCameraAfterDelay: MapViewCamera? = nil

var body: some View {
MapView(styleURL: styleURL, camera: $camera)
MapView<MapViewController>(styleURL: styleURL, camera: $camera)
.onStyleLoaded { _ in
onStyleLoaded?()
}
Expand Down
4 changes: 2 additions & 2 deletions Sources/MapLibreSwiftUI/Examples/Gestures.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import SwiftUI

#Preview("Tappable Circles") {
let tappableID = "simple-circles"
return MapView(styleURL: demoTilesURL) {
return MapView<MapViewController>(styleURL: demoTilesURL) {
// Simple symbol layer demonstration with an icon
CircleStyleLayer(identifier: tappableID, source: pointSource)
.radius(16)
Expand All @@ -24,7 +24,7 @@ import SwiftUI
}

#Preview("Tappable Countries") {
MapView(styleURL: demoTilesURL)
MapView<MapViewController>(styleURL: demoTilesURL)
.onTapMapGesture(on: ["countries-fill"], onTapChanged: { _, features in
print("Tapped on \(features.first?.description ?? "<nil>")")
})
Expand Down
14 changes: 7 additions & 7 deletions Sources/MapLibreSwiftUI/Examples/Layers.swift
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ let clustered = ShapeSource(identifier: "points", options: [.clustered: true, .c
}

#Preview("Rose Tint") {
MapView(styleURL: demoTilesURL) {
MapView<MapViewController>(styleURL: demoTilesURL) {
// Silly example: a background layer on top of everything to create a tint effect
BackgroundLayer(identifier: "rose-colored-glasses")
.backgroundColor(.systemPink.withAlphaComponent(0.3))
Expand All @@ -44,7 +44,7 @@ let clustered = ShapeSource(identifier: "points", options: [.clustered: true, .c
}

#Preview("Simple Symbol") {
MapView(styleURL: demoTilesURL) {
MapView<MapViewController>(styleURL: demoTilesURL) {
// Simple symbol layer demonstration with an icon
SymbolStyleLayer(identifier: "simple-symbols", source: pointSource)
.iconImage(UIImage(systemName: "mappin")!)
Expand All @@ -53,7 +53,7 @@ let clustered = ShapeSource(identifier: "points", options: [.clustered: true, .c
}

#Preview("Rotated Symbols (Const)") {
MapView(styleURL: demoTilesURL) {
MapView<MapViewController>(styleURL: demoTilesURL) {
// Simple symbol layer demonstration with an icon
SymbolStyleLayer(identifier: "rotated-symbols", source: pointSource)
.iconImage(UIImage(systemName: "location.north.circle.fill")!)
Expand All @@ -63,7 +63,7 @@ let clustered = ShapeSource(identifier: "points", options: [.clustered: true, .c
}

#Preview("Rotated Symbols (Dynamic)") {
MapView(styleURL: demoTilesURL) {
MapView<MapViewController>(styleURL: demoTilesURL) {
// Simple symbol layer demonstration with an icon
SymbolStyleLayer(identifier: "rotated-symbols", source: pointSource)
.iconImage(UIImage(systemName: "location.north.circle.fill")!)
Expand All @@ -73,7 +73,7 @@ let clustered = ShapeSource(identifier: "points", options: [.clustered: true, .c
}

#Preview("Circles with Symbols") {
MapView(styleURL: demoTilesURL) {
MapView<MapViewController>(styleURL: demoTilesURL) {
// Simple symbol layer demonstration with an icon
CircleStyleLayer(identifier: "simple-circles", source: pointSource)
.radius(16)
Expand All @@ -94,7 +94,7 @@ let clustered = ShapeSource(identifier: "points", options: [.clustered: true, .c
zoom: 5,
direction: 0
)
return MapView(styleURL: demoTilesURL, camera: $camera) {
return MapView<MapViewController>(styleURL: demoTilesURL, camera: $camera) {
// Clusters pins when they would touch

// Cluster == YES shows only those pins that are clustered, using .text
Expand Down Expand Up @@ -135,7 +135,7 @@ let clustered = ShapeSource(identifier: "points", options: [.clustered: true, .c

// TODO: Fixme
// #Preview("Multiple Symbol Icons") {
// MapView(styleURL: demoTilesURL) {
// MapView<MapViewController>(styleURL: demoTilesURL) {
// // Simple symbol layer demonstration with an icon
// SymbolStyleLayer(identifier: "simple-symbols", source: pointSource)
// .iconImage(attribute: "icon",
Expand Down
2 changes: 1 addition & 1 deletion Sources/MapLibreSwiftUI/Examples/Other.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import MapLibreSwiftDSL
import SwiftUI

#Preview("Unsafe MapView Modifier") {
MapView(styleURL: demoTilesURL) {
MapView<MapViewController>(styleURL: demoTilesURL) {
// A collection of points with various
// attributes
let pointSource = ShapeSource(identifier: "points") {
Expand Down
2 changes: 1 addition & 1 deletion Sources/MapLibreSwiftUI/Examples/Polyline.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ struct PolylinePreview: View {
let styleURL: URL

var body: some View {
MapView(styleURL: styleURL,
MapView<MapViewController>(styleURL: styleURL,
camera: .constant(.center(samplePedestrianWaypoints.first!, zoom: 14)))
{
// Note: This line does not add the source to the style as if it
Expand Down
4 changes: 2 additions & 2 deletions Sources/MapLibreSwiftUI/Examples/User Location.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ private let locationManager = StaticLocationManager(initialLocation: CLLocation(
))

#Preview("Track user location") {
MapView(
MapView<MapViewController>(
styleURL: demoTilesURL,
camera: .constant(.trackUserLocation(zoom: 4, pitch: 45)),
locationManager: locationManager
Expand All @@ -24,7 +24,7 @@ private let locationManager = StaticLocationManager(initialLocation: CLLocation(
}

#Preview("Track user location with Course") {
MapView(
MapView<MapViewController>(
styleURL: demoTilesURL,
camera: .constant(.trackUserLocationWithCourse(zoom: 4, pitch: 45)),
locationManager: locationManager
Expand Down
16 changes: 8 additions & 8 deletions Sources/MapLibreSwiftUI/MapView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ import MapLibre
import MapLibreSwiftDSL
import SwiftUI

public struct MapView: UIViewControllerRepresentable {
public typealias UIViewControllerType = MapViewController
public struct MapView<T: WrappedViewController>: UIViewControllerRepresentable {
public typealias UIViewControllerType = T

@Binding var camera: MapViewCamera

Expand Down Expand Up @@ -44,17 +44,17 @@ public struct MapView: UIViewControllerRepresentable {
self.locationManager = locationManager
}

public func makeCoordinator() -> MapViewCoordinator {
MapViewCoordinator(
public func makeCoordinator() -> MapViewCoordinator<T> {
MapViewCoordinator<T>(
parent: self,
onGesture: { processGesture($0, $1) },
onViewPortChanged: { onViewPortChanged?($0) }
)
}

public func makeUIViewController(context: Context) -> MapViewController {
public func makeUIViewController(context: Context) -> T {
// Create the map view
let controller = MapViewController()
let controller = T()
controller.mapView.delegate = context.coordinator
context.coordinator.mapView = controller.mapView

Expand Down Expand Up @@ -87,7 +87,7 @@ public struct MapView: UIViewControllerRepresentable {
return controller
}

public func updateUIViewController(_ uiViewController: MapViewController, context: Context) {
public func updateUIViewController(_ uiViewController: T, context: Context) {
context.coordinator.parent = self

applyModifiers(uiViewController.mapView, runUnsafe: true)
Expand Down Expand Up @@ -124,7 +124,7 @@ public struct MapView: UIViewControllerRepresentable {
}

#Preview {
MapView(styleURL: demoTilesURL)
MapView<MapViewController>(styleURL: demoTilesURL)
.ignoresSafeArea(.all)
.previewDisplayName("Vanilla Map")

Expand Down
8 changes: 6 additions & 2 deletions Sources/MapLibreSwiftUI/MapViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,13 @@ import MapLibre
import MapboxNavigation
import MapboxCoreNavigation

public final class MapViewController: UIViewController {
public protocol WrappedViewController: UIViewController {
var mapView: MLNMapView { get }
}

public final class MapViewController: UIViewController, WrappedViewController {

var mapView: MLNMapView {
public var mapView: MLNMapView {
return self.view as! MLNMapView
}

Expand Down
10 changes: 4 additions & 6 deletions Sources/MapLibreSwiftUI/MapViewCoordinator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ import Foundation
import MapLibre
import MapLibreSwiftDSL

public class MapViewCoordinator: NSObject {
public class MapViewCoordinator<T: WrappedViewController>: NSObject, MLNMapViewDelegate {
// This must be weak, the UIViewRepresentable owns the MLNMapView.
weak var mapView: MLNMapView?
var parent: MapView
var parent: MapView<T>

// Storage of variables as they were previously; these are snapshot
// every update cycle so we can avoid unnecessary updates
Expand All @@ -22,7 +22,7 @@ public class MapViewCoordinator: NSObject {
var onGesture: (MLNMapView, UIGestureRecognizer) -> Void
var onViewPortChanged: (MapViewPort) -> Void

init(parent: MapView,
init(parent: MapView<T>,
onGesture: @escaping (MLNMapView, UIGestureRecognizer) -> Void,
onViewPortChanged: @escaping (MapViewPort) -> Void)
{
Expand Down Expand Up @@ -296,11 +296,9 @@ public class MapViewCoordinator: NSObject {
}
}
}
}

// MARK: - MLNMapViewDelegate
// MARK: - MLNMapViewDelegate

extension MapViewCoordinator: MLNMapViewDelegate {
public func mapView(_: MLNMapView, didFinishLoading mglStyle: MLNStyle) {
addLayers(to: mglStyle)
onStyleLoaded?(mglStyle)
Expand Down

0 comments on commit 00ac500

Please sign in to comment.