Skip to content

Commit 4f40a6d

Browse files
authored
Changing MapViewPort to MapViewProxy (maplibre#65)
* changing MapViewPort to MapViewProxy * ran swiftformat * annotation test as mainactor
1 parent e9ad082 commit 4f40a6d

File tree

8 files changed

+90
-69
lines changed

8 files changed

+90
-69
lines changed

Sources/MapLibreSwiftUI/MapView.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ public struct MapView<T: MapViewHostViewController>: UIViewControllerRepresentab
1616
var gestures = [MapGesture]()
1717

1818
var onStyleLoaded: ((MLNStyle) -> Void)?
19-
var onViewPortChanged: ((MapViewPort) -> Void)?
19+
var onViewProxyChanged: ((MapViewProxy) -> Void)?
2020

2121
var mapViewContentInset: UIEdgeInsets?
2222

@@ -50,7 +50,7 @@ public struct MapView<T: MapViewHostViewController>: UIViewControllerRepresentab
5050
MapViewCoordinator<T>(
5151
parent: self,
5252
onGesture: { processGesture($0, $1) },
53-
onViewPortChanged: { onViewPortChanged?($0) }
53+
onViewProxyChanged: { onViewProxyChanged?($0) }
5454
)
5555
}
5656

Sources/MapLibreSwiftUI/MapViewCoordinator.swift

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,15 @@ public class MapViewCoordinator<T: MapViewHostViewController>: NSObject, MLNMapV
2020

2121
var onStyleLoaded: ((MLNStyle) -> Void)?
2222
var onGesture: (MLNMapView, UIGestureRecognizer) -> Void
23-
var onViewPortChanged: (MapViewPort) -> Void
23+
var onViewProxyChanged: (MapViewProxy) -> Void
2424

2525
init(parent: MapView<T>,
2626
onGesture: @escaping (MLNMapView, UIGestureRecognizer) -> Void,
27-
onViewPortChanged: @escaping (MapViewPort) -> Void)
27+
onViewProxyChanged: @escaping (MapViewProxy) -> Void)
2828
{
2929
self.parent = parent
3030
self.onGesture = onGesture
31-
self.onViewPortChanged = onViewPortChanged
31+
self.onViewProxyChanged = onViewProxyChanged
3232
}
3333

3434
// MARK: Core UIView Functionality
@@ -366,7 +366,7 @@ public class MapViewCoordinator<T: MapViewHostViewController>: NSObject, MLNMapV
366366
public func mapView(_ mapView: MLNMapView, regionDidChangeWith reason: MLNCameraChangeReason, animated _: Bool) {
367367
// TODO: We could put this in regionIsChangingWith if we calculate significant change/debounce.
368368
MainActor.assumeIsolated {
369-
updateViewPort(mapView: mapView, reason: reason)
369+
updateViewProxy(mapView: mapView, reason: reason)
370370

371371
guard !suppressCameraUpdatePropagation else {
372372
return
@@ -376,17 +376,13 @@ public class MapViewCoordinator<T: MapViewHostViewController>: NSObject, MLNMapV
376376
}
377377
}
378378

379-
// MARK: MapViewPort
379+
// MARK: MapViewProxy
380380

381-
@MainActor private func updateViewPort(mapView: MLNMapView, reason: MLNCameraChangeReason) {
382-
// Calculate the Raw "ViewPort"
383-
let calculatedViewPort = MapViewPort(
384-
center: mapView.centerCoordinate,
385-
zoom: mapView.zoomLevel,
386-
direction: mapView.direction,
387-
lastReasonForChange: CameraChangeReason(reason)
388-
)
381+
@MainActor private func updateViewProxy(mapView: MLNMapView, reason: MLNCameraChangeReason) {
382+
// Calculate the Raw "ViewProxy"
383+
let calculatedViewProxy = MapViewProxy(mapView: mapView,
384+
lastReasonForChange: CameraChangeReason(reason))
389385

390-
onViewPortChanged(calculatedViewPort)
386+
onViewProxyChanged(calculatedViewProxy)
391387
}
392388
}

Sources/MapLibreSwiftUI/MapViewModifiers.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -130,9 +130,9 @@ public extension MapView {
130130
return result
131131
}
132132

133-
func onMapViewPortUpdate(_ onViewPortChanged: @escaping (MapViewPort) -> Void) -> Self {
133+
func onMapViewProxyUpdate(_ onViewProxyChanged: @escaping (MapViewProxy) -> Void) -> Self {
134134
var result = self
135-
result.onViewPortChanged = onViewPortChanged
135+
result.onViewProxyChanged = onViewProxyChanged
136136
return result
137137
}
138138

Sources/MapLibreSwiftUI/Models/MapCamera/CameraChangeReason.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import Foundation
22
import MapLibre
33

4+
@MainActor
45
public enum CameraChangeReason: Hashable {
56
case programmatic
67
case resetNorth

Sources/MapLibreSwiftUI/Models/MapViewPort.swift

Lines changed: 0 additions & 50 deletions
This file was deleted.
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
import CoreLocation
2+
import Foundation
3+
import MapLibre
4+
5+
/// A read only representation of the MapView's current View.
6+
///
7+
/// Provides access to properties and functions of the underlying MLNMapView,
8+
/// but properties only expose their getter, and functions are only available if they
9+
/// do no change the state of the MLNMapView. Writing directly to properties of MLNMapView
10+
/// could clash with swiftui-dsl's state management, which is why modifiying functions
11+
/// and properties are not exposed.
12+
///
13+
/// You can use `MapView.onMapViewProxyUpdate(_ onViewProxyChanged: @escaping (MapViewProxy) -> Void)` to
14+
/// recieve access to the MapViewProxy.
15+
///
16+
/// For more information about the properties and functions, see
17+
/// https://maplibre.org/maplibre-native/ios/latest/documentation/maplibre/mlnmapview
18+
@MainActor
19+
public struct MapViewProxy: Hashable, Equatable {
20+
/// The current center coordinate of the MapView
21+
public var centerCoordinate: CLLocationCoordinate2D {
22+
mapView.centerCoordinate
23+
}
24+
25+
/// The current zoom value of the MapView
26+
public var zoomLevel: Double {
27+
mapView.zoomLevel
28+
}
29+
30+
/// The current compass direction of the MapView
31+
public var direction: CLLocationDirection {
32+
mapView.direction
33+
}
34+
35+
public var visibleCoordinateBounds: MLNCoordinateBounds {
36+
mapView.visibleCoordinateBounds
37+
}
38+
39+
public var mapViewSize: CGSize {
40+
mapView.frame.size
41+
}
42+
43+
public var contentInset: UIEdgeInsets {
44+
mapView.contentInset
45+
}
46+
47+
/// The reason the view port was changed.
48+
public let lastReasonForChange: CameraChangeReason?
49+
50+
private let mapView: MLNMapView
51+
52+
public func convert(_ coordinate: CLLocationCoordinate2D, toPointTo: UIView?) -> CGPoint {
53+
mapView.convert(coordinate, toPointTo: toPointTo)
54+
}
55+
56+
public init(mapView: MLNMapView,
57+
lastReasonForChange: CameraChangeReason?)
58+
{
59+
self.mapView = mapView
60+
self.lastReasonForChange = lastReasonForChange
61+
}
62+
}
63+
64+
public extension MapViewProxy {
65+
/// Generate a basic MapViewCamera that represents the MapView
66+
///
67+
/// - Returns: The calculated MapViewCamera
68+
func asMapViewCamera() -> MapViewCamera {
69+
.center(centerCoordinate,
70+
zoom: zoomLevel,
71+
direction: direction)
72+
}
73+
}

Tests/MapLibreSwiftUITests/MapViewCoordinator/MapViewCoordinatorCameraTests.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ final class MapViewCoordinatorCameraTests: XCTestCase {
1515
mapView = MapView(styleURL: URL(string: "https://maplibre.org")!)
1616
coordinator = MapView.Coordinator(parent: mapView) { _, _ in
1717
// No action
18-
} onViewPortChanged: { _ in
18+
} onViewProxyChanged: { _ in
1919
// No action
2020
}
2121
}

Tests/MapLibreSwiftUITests/Models/MapCamera/CameraChangeReasonTests.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import MapLibre
22
import XCTest
33
@testable import MapLibreSwiftUI
44

5+
@MainActor
56
final class CameraChangeReasonTests: XCTestCase {
67
func testProgrammatic() {
78
let mlnReason: MLNCameraChangeReason = [.programmatic]

0 commit comments

Comments
 (0)