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

Added setters for camera parameters #31

Merged
merged 4 commits into from
Apr 2, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ protocol MLNMapViewCameraUpdating: AnyObject {
@MainActor var userTrackingMode: MLNUserTrackingMode { get set }
@MainActor var minimumPitch: CGFloat { get set }
@MainActor var maximumPitch: CGFloat { get set }
@MainActor var direction: CLLocationDirection { get set }
@MainActor func setCenter(_ coordinate: CLLocationCoordinate2D,
zoomLevel: Double,
direction: CLLocationDirection,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
import Foundation

public extension MapViewCamera {
// MARK: Zoom

/// Set a new zoom for the current camera state.
///
/// - Parameter newZoom: The new zoom value.
mutating func setZoom(_ newZoom: Double) {
switch state {
case let .centered(onCoordinate, _, pitch, direction):
state = .centered(onCoordinate: onCoordinate,
zoom: newZoom,
pitch: pitch,
direction: direction)
case let .trackingUserLocation(_, pitch, direction):
state = .trackingUserLocation(zoom: newZoom, pitch: pitch, direction: direction)
case let .trackingUserLocationWithHeading(_, pitch):
state = .trackingUserLocationWithHeading(zoom: newZoom, pitch: pitch)
case let .trackingUserLocationWithCourse(_, pitch):
state = .trackingUserLocationWithCourse(zoom: newZoom, pitch: pitch)
case let .rect(boundingBox, edgePadding):

Check warning on line 22 in Sources/MapLibreSwiftUI/Extensions/MapViewCamera/MapViewCameraOperations.swift

View workflow job for this annotation

GitHub Actions / test (MapLibreSwiftUI-Package, platform=iOS Simulator,name=iPhone 15,OS=17.2)

immutable value 'boundingBox' was never used; consider replacing with '_' or removing it

Check warning on line 22 in Sources/MapLibreSwiftUI/Extensions/MapViewCamera/MapViewCameraOperations.swift

View workflow job for this annotation

GitHub Actions / test (MapLibreSwiftUI-Package, platform=iOS Simulator,name=iPhone 15,OS=17.2)

immutable value 'edgePadding' was never used; consider replacing with '_' or removing it
return
case let .showcase(shapeCollection):

Check warning on line 24 in Sources/MapLibreSwiftUI/Extensions/MapViewCamera/MapViewCameraOperations.swift

View workflow job for this annotation

GitHub Actions / test (MapLibreSwiftUI-Package, platform=iOS Simulator,name=iPhone 15,OS=17.2)

immutable value 'shapeCollection' was never used; consider replacing with '_' or removing it
return
}

lastReasonForChange = .programmatic
}

/// Increment the zoom of the current camera state.
///
/// - Parameter newZoom: The value to increment the zoom by. Negative decrements the value.
mutating func incrementZoom(by increment: Double) {
switch state {
case let .centered(onCoordinate, zoom, pitch, direction):
state = .centered(onCoordinate: onCoordinate,
zoom: zoom + increment,
pitch: pitch,
direction: direction)
case let .trackingUserLocation(zoom, pitch, direction):
state = .trackingUserLocation(zoom: zoom + increment, pitch: pitch, direction: direction)
case let .trackingUserLocationWithHeading(zoom, pitch):
state = .trackingUserLocationWithHeading(zoom: zoom + increment, pitch: pitch)
case let .trackingUserLocationWithCourse(zoom, pitch):
state = .trackingUserLocationWithCourse(zoom: zoom + increment, pitch: pitch)
case let .rect(boundingBox, edgePadding):

Check warning on line 47 in Sources/MapLibreSwiftUI/Extensions/MapViewCamera/MapViewCameraOperations.swift

View workflow job for this annotation

GitHub Actions / test (MapLibreSwiftUI-Package, platform=iOS Simulator,name=iPhone 15,OS=17.2)

immutable value 'boundingBox' was never used; consider replacing with '_' or removing it

Check warning on line 47 in Sources/MapLibreSwiftUI/Extensions/MapViewCamera/MapViewCameraOperations.swift

View workflow job for this annotation

GitHub Actions / test (MapLibreSwiftUI-Package, platform=iOS Simulator,name=iPhone 15,OS=17.2)

immutable value 'edgePadding' was never used; consider replacing with '_' or removing it
return
case let .showcase(shapeCollection):

Check warning on line 49 in Sources/MapLibreSwiftUI/Extensions/MapViewCamera/MapViewCameraOperations.swift

View workflow job for this annotation

GitHub Actions / test (MapLibreSwiftUI-Package, platform=iOS Simulator,name=iPhone 15,OS=17.2)

immutable value 'shapeCollection' was never used; consider replacing with '_' or removing it
return
}

lastReasonForChange = .programmatic
}

// MARK: Pitch

/// Set a new pitch for the current camera state.
///
/// - Parameter newPitch: The new pitch value.
mutating func setPitch(_ newPitch: CameraPitch) {
switch state {
case let .centered(onCoordinate, zoom, pitch, direction):

Check warning on line 63 in Sources/MapLibreSwiftUI/Extensions/MapViewCamera/MapViewCameraOperations.swift

View workflow job for this annotation

GitHub Actions / test (MapLibreSwiftUI-Package, platform=iOS Simulator,name=iPhone 15,OS=17.2)

immutable value 'pitch' was never used; consider replacing with '_' or removing it
state = .centered(onCoordinate: onCoordinate,
zoom: zoom,
pitch: newPitch,
direction: direction)
case let .trackingUserLocation(zoom, _, direction):
state = .trackingUserLocation(zoom: zoom, pitch: newPitch, direction: direction)
case let .trackingUserLocationWithHeading(zoom, _):
state = .trackingUserLocationWithHeading(zoom: zoom, pitch: newPitch)
case let .trackingUserLocationWithCourse(zoom, _):
state = .trackingUserLocationWithCourse(zoom: zoom, pitch: newPitch)
case let .rect(boundingBox, edgePadding):

Check warning on line 74 in Sources/MapLibreSwiftUI/Extensions/MapViewCamera/MapViewCameraOperations.swift

View workflow job for this annotation

GitHub Actions / test (MapLibreSwiftUI-Package, platform=iOS Simulator,name=iPhone 15,OS=17.2)

immutable value 'boundingBox' was never used; consider replacing with '_' or removing it

Check warning on line 74 in Sources/MapLibreSwiftUI/Extensions/MapViewCamera/MapViewCameraOperations.swift

View workflow job for this annotation

GitHub Actions / test (MapLibreSwiftUI-Package, platform=iOS Simulator,name=iPhone 15,OS=17.2)

immutable value 'edgePadding' was never used; consider replacing with '_' or removing it
return
case let .showcase(shapeCollection):

Check warning on line 76 in Sources/MapLibreSwiftUI/Extensions/MapViewCamera/MapViewCameraOperations.swift

View workflow job for this annotation

GitHub Actions / test (MapLibreSwiftUI-Package, platform=iOS Simulator,name=iPhone 15,OS=17.2)

immutable value 'shapeCollection' was never used; consider replacing with '_' or removing it
return
}

lastReasonForChange = .programmatic
}

// TODO: Add direction set
}
3 changes: 2 additions & 1 deletion Sources/MapLibreSwiftUI/MapViewCoordinator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,11 @@ public class MapViewCoordinator: NSObject {
animated: animated)
mapView.minimumPitch = pitch.rangeValue.lowerBound
mapView.maximumPitch = pitch.rangeValue.upperBound
case let .trackingUserLocation(zoom: zoom, pitch: pitch):
case let .trackingUserLocation(zoom: zoom, pitch: pitch, direction: direction):
mapView.userTrackingMode = .follow
// Needs to be non-animated or else it messes up following
mapView.setZoomLevel(zoom, animated: false)
mapView.direction = direction
mapView.minimumPitch = pitch.rangeValue.lowerBound
mapView.maximumPitch = pitch.rangeValue.upperBound
case let .trackingUserLocationWithHeading(zoom: zoom, pitch: pitch):
Expand Down
2 changes: 1 addition & 1 deletion Sources/MapLibreSwiftUI/Models/MapCamera/CameraState.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public enum CameraState: Hashable {
///
/// This feature uses the MLNMapView's userTrackingMode to .follow which automatically
/// follows the user from within the MLNMapView.
case trackingUserLocation(zoom: Double, pitch: CameraPitch)
case trackingUserLocation(zoom: Double, pitch: CameraPitch, direction: CLLocationDirection)

/// Follow the user's location using the MapView's internal camera with the user's heading.
///
Expand Down
5 changes: 3 additions & 2 deletions Sources/MapLibreSwiftUI/Models/MapCamera/MapViewCamera.swift
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,11 @@ public struct MapViewCamera: Hashable {
/// - pitch: Set the camera pitch method.
/// - Returns: The MapViewCamera representing the scenario
public static func trackUserLocation(zoom: Double = Defaults.zoom,
pitch: CameraPitch = Defaults.pitch) -> MapViewCamera
pitch: CameraPitch = Defaults.pitch,
direction: CLLocationDirection = Defaults.direction) -> MapViewCamera
{
// Coordinate is ignored when tracking user location. However, pitch and zoom are valid.
MapViewCamera(state: .trackingUserLocation(zoom: zoom, pitch: pitch),
MapViewCamera(state: .trackingUserLocation(zoom: zoom, pitch: pitch, direction: direction),
lastReasonForChange: .programmatic)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ final class CameraStateTests: XCTestCase {
}

func testTrackingUserLocation() {
let state: CameraState = .trackingUserLocation(zoom: 4, pitch: .free)
XCTAssertEqual(state, .trackingUserLocation(zoom: 4, pitch: .free))
let state: CameraState = .trackingUserLocation(zoom: 4, pitch: .free, direction: 12)
XCTAssertEqual(state, .trackingUserLocation(zoom: 4, pitch: .free, direction: 12))
assertSnapshot(of: state, as: .description)
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1 @@
CameraState.trackingUserLocation(zoom: (4.0, MapLibreSwiftUI.CameraPitch.free))
CameraState.trackingUserLocation(zoom: (4.0, MapLibreSwiftUI.CameraPitch.free, 12.0))
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@
▿ lastReasonForChange: Optional<CameraChangeReason>
- some: CameraChangeReason.programmatic
▿ state: CameraState
▿ trackingUserLocation: (2 elements)
▿ trackingUserLocation: (3 elements)
- zoom: 10.0
▿ pitch: CameraPitch
▿ freeWithinRange: (2 elements)
- minimum: 12.0
- maximum: 34.0
- direction: 0.0
Loading