Skip to content

Commit

Permalink
feat: added change reason to view port update
Browse files Browse the repository at this point in the history
  • Loading branch information
Archdoog committed Apr 18, 2024
1 parent ac4f4d8 commit f491b6b
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 5 deletions.
8 changes: 5 additions & 3 deletions Sources/MapLibreSwiftUI/MapViewCoordinator.swift
Expand Up @@ -260,7 +260,7 @@ extension MapViewCoordinator: MLNMapViewDelegate {
// FIXME: CI complains about MainActor.assumeIsolated being unavailable before iOS 17, despite building on iOS 17.2... This is an epic hack to fix it for now. I can only assume this is an issue with Xcode pre-15.3
// TODO: We could put this in regionIsChangingWith if we calculate significant change/debounce.
Task { @MainActor in
updateViewPort(mapView: mapView)
updateViewPort(mapView: mapView, reason: reason)
}

guard !suppressCameraUpdatePropagation else {
Expand All @@ -275,12 +275,14 @@ extension MapViewCoordinator: MLNMapViewDelegate {

// MARK: MapViewPort

@MainActor private func updateViewPort(mapView: MLNMapView) {
@MainActor private func updateViewPort(mapView: MLNMapView, reason: MLNCameraChangeReason) {

// Calculate the Raw "ViewPort"
let calculatedViewPort = MapViewPort(
center: mapView.centerCoordinate,
zoom: mapView.zoomLevel,
direction: mapView.direction
direction: mapView.direction,
lastReasonForChange: CameraChangeReason(reason)
)

onViewPortChanged(calculatedViewPort)
Expand Down
12 changes: 10 additions & 2 deletions Sources/MapLibreSwiftUI/Models/MapViewPort.swift
Expand Up @@ -15,17 +15,25 @@ public struct MapViewPort: Hashable, Equatable {

/// The current compass direction of the MapView
public let direction: CLLocationDirection

/// The reason the view port was changed.
public let lastReasonForChange: CameraChangeReason?

public init(center: CLLocationCoordinate2D, zoom: Double, direction: CLLocationDirection) {
public init(center: CLLocationCoordinate2D,
zoom: Double,
direction: CLLocationDirection,
lastReasonForChange: CameraChangeReason?) {
self.center = center
self.zoom = zoom
self.direction = direction
self.lastReasonForChange = lastReasonForChange
}

public static func zero(zoom: Double = 10) -> MapViewPort {
MapViewPort(center: CLLocationCoordinate2D(latitude: 0, longitude: 0),
zoom: zoom,
direction: 0)
direction: 0,
lastReasonForChange: nil)
}
}

Expand Down

0 comments on commit f491b6b

Please sign in to comment.