Skip to content

Commit

Permalink
format improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
hactar committed May 13, 2024
1 parent 2dffff2 commit bc75a5a
Show file tree
Hide file tree
Showing 5 changed files with 87 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,12 @@ public extension MapViewCamera {
pitchRange: pitchRange,
direction: direction)
case let .trackingUserLocation(zoom, pitch, pitchRange, direction):
state = .trackingUserLocation(zoom: zoom + increment, pitch: pitch, pitchRange: pitchRange, direction: direction)
state = .trackingUserLocation(
zoom: zoom + increment,
pitch: pitch,
pitchRange: pitchRange,
direction: direction
)
case let .trackingUserLocationWithHeading(zoom, pitch, pitchRange):
state = .trackingUserLocationWithHeading(zoom: zoom + increment, pitch: pitch, pitchRange: pitchRange)
case let .trackingUserLocationWithCourse(zoom, pitch, pitchRange):
Expand Down
65 changes: 43 additions & 22 deletions Sources/MapLibreSwiftUI/MapViewCoordinator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -63,57 +63,68 @@ public class MapViewCoordinator: NSObject {
}

switch camera.state {
case let .centered(onCoordinate: coordinate, zoom: zoom, pitch: pitch, pitchRange: pitchRange, direction: direction):
case let .centered(
onCoordinate: coordinate,
zoom: zoom,
pitch: pitch,
pitchRange: pitchRange,
direction: direction
):
mapView.userTrackingMode = .none

if mapView.frame.size == .zero {
// On init, the mapView's frame is not set up yet, so manipulation via camera is broken,
// so let's do something else instead.
mapView.setCenter(coordinate,
zoomLevel: zoom,
direction: direction,
animated: animated)

// this is a workaround for no camera - minimum and maximum will be reset below, but this adjusts it.
mapView.minimumPitch = pitch
mapView.maximumPitch = pitch

} else {
let camera = mapView.camera
camera.centerCoordinate = coordinate
camera.heading = direction
camera.pitch = pitch

let altitude = MLNAltitudeForZoomLevel(zoom, pitch, coordinate.latitude, mapView.frame.size)
camera.altitude = altitude
mapView.setCamera(camera, animated: animated)
}

mapView.minimumPitch = pitchRange.rangeValue.lowerBound
mapView.maximumPitch = pitchRange.rangeValue.upperBound
case let .trackingUserLocation(zoom: zoom, pitch: pitch, pitchRange: pitchRange, direction: direction):
mapView.userTrackingMode = .follow

if mapView.frame.size == .zero {
// On init, the mapView's frame is not set up yet, so manipulation via camera is broken,
// so let's do something else instead.
// Needs to be non-animated or else it messes up following

mapView.setZoomLevel(zoom, animated: false)
mapView.direction = direction

mapView.minimumPitch = pitch
mapView.maximumPitch = pitch

} else {
let camera = mapView.camera
camera.heading = direction
camera.pitch = pitch

let altitude = MLNAltitudeForZoomLevel(zoom, pitch, mapView.camera.centerCoordinate.latitude, mapView.frame.size)

let altitude = MLNAltitudeForZoomLevel(
zoom,
pitch,
mapView.camera.centerCoordinate.latitude,
mapView.frame.size
)
camera.altitude = altitude
mapView.setCamera(camera, animated: animated)
}
}
mapView.minimumPitch = pitchRange.rangeValue.lowerBound
mapView.maximumPitch = pitchRange.rangeValue.upperBound
case let .trackingUserLocationWithHeading(zoom: zoom, pitch: pitch, pitchRange: pitchRange):
Expand All @@ -123,20 +134,25 @@ public class MapViewCoordinator: NSObject {
// On init, the mapView's frame is not set up yet, so manipulation via camera is broken,
// so let's do something else instead.
// Needs to be non-animated or else it messes up following

mapView.setZoomLevel(zoom, animated: false)
mapView.minimumPitch = pitch
mapView.maximumPitch = pitch

} else {
let camera = mapView.camera

let altitude = MLNAltitudeForZoomLevel(zoom, pitch, mapView.camera.centerCoordinate.latitude, mapView.frame.size)

let altitude = MLNAltitudeForZoomLevel(
zoom,
pitch,
mapView.camera.centerCoordinate.latitude,
mapView.frame.size
)
camera.altitude = altitude
camera.pitch = pitch
mapView.setCamera(camera, animated: animated)
}

mapView.minimumPitch = pitchRange.rangeValue.lowerBound
mapView.maximumPitch = pitchRange.rangeValue.upperBound
case let .trackingUserLocationWithCourse(zoom: zoom, pitch: pitch, pitchRange: pitchRange):
Expand All @@ -146,20 +162,25 @@ public class MapViewCoordinator: NSObject {
// On init, the mapView's frame is not set up yet, so manipulation via camera is broken,
// so let's do something else instead.
// Needs to be non-animated or else it messes up following

mapView.setZoomLevel(zoom, animated: false)
mapView.minimumPitch = pitch
mapView.maximumPitch = pitch

} else {
let camera = mapView.camera

let altitude = MLNAltitudeForZoomLevel(zoom, pitch, mapView.camera.centerCoordinate.latitude, mapView.frame.size)

let altitude = MLNAltitudeForZoomLevel(
zoom,
pitch,
mapView.camera.centerCoordinate.latitude,
mapView.frame.size
)
camera.altitude = altitude
camera.pitch = pitch
mapView.setCamera(camera, animated: animated)
}

mapView.minimumPitch = pitchRange.rangeValue.lowerBound
mapView.maximumPitch = pitchRange.rangeValue.upperBound
case let .rect(boundingBox, padding):
Expand Down
8 changes: 7 additions & 1 deletion Sources/MapLibreSwiftUI/Models/MapCamera/CameraState.swift
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,13 @@ public enum CameraState: Hashable {
extension CameraState: CustomDebugStringConvertible {
public var debugDescription: String {
switch self {
case let .centered(onCoordinate: coordinate, zoom: zoom, pitch: pitch, pitchRange: pitchRange, direction: direction):
case let .centered(
onCoordinate: coordinate,
zoom: zoom,
pitch: pitch,
pitchRange: pitchRange,
direction: direction
):
"CameraState.centered(onCoordinate: \(coordinate), zoom: \(zoom), pitch: \(pitch), pitchRange: \(pitchRange), direction: \(direction))"
case let .trackingUserLocation(zoom: zoom):
"CameraState.trackingUserLocation(zoom: \(zoom))"
Expand Down
34 changes: 24 additions & 10 deletions Sources/MapLibreSwiftUI/Models/MapCamera/MapViewCamera.swift
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,16 @@ public struct MapViewCamera: Hashable {
direction: CLLocationDirection = Defaults.direction,
reason: CameraChangeReason? = nil) -> MapViewCamera
{
MapViewCamera(state: .centered(onCoordinate: coordinate, zoom: zoom, pitch: pitch, pitchRange: pitchRange, direction: direction),
lastReasonForChange: reason)
MapViewCamera(
state: .centered(
onCoordinate: coordinate,
zoom: zoom,
pitch: pitch,
pitchRange: pitchRange,
direction: direction
),
lastReasonForChange: reason
)
}

/// Enables user location tracking within the MapView.
Expand All @@ -74,8 +82,10 @@ public struct MapViewCamera: Hashable {
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, pitchRange: pitchRange, direction: direction),
lastReasonForChange: .programmatic)
MapViewCamera(
state: .trackingUserLocation(zoom: zoom, pitch: pitch, pitchRange: pitchRange, direction: direction),
lastReasonForChange: .programmatic
)
}

/// Enables user location tracking within the MapView.
Expand All @@ -87,9 +97,11 @@ public struct MapViewCamera: Hashable {
/// pitch.
/// - pitch: Set the camera pitch method.
/// - Returns: The MapViewCamera representing the scenario
public static func trackUserLocationWithHeading(zoom: Double = Defaults.zoom,
pitch: Double = Defaults.pitch, pitchRange: CameraPitchRange = Defaults.pitchRange) -> MapViewCamera
{
public static func trackUserLocationWithHeading(
zoom: Double = Defaults.zoom,
pitch: Double = Defaults.pitch,
pitchRange: CameraPitchRange = Defaults.pitchRange
) -> MapViewCamera {
// Coordinate is ignored when tracking user location. However, pitch and zoom are valid.
MapViewCamera(state: .trackingUserLocationWithHeading(zoom: zoom, pitch: pitch, pitchRange: pitchRange),
lastReasonForChange: .programmatic)
Expand All @@ -104,9 +116,11 @@ public struct MapViewCamera: Hashable {
/// pitch.
/// - pitch: Set the camera pitch method.
/// - Returns: The MapViewCamera representing the scenario
public static func trackUserLocationWithCourse(zoom: Double = Defaults.zoom,
pitch: Double = Defaults.pitch, pitchRange: CameraPitchRange = Defaults.pitchRange) -> MapViewCamera
{
public static func trackUserLocationWithCourse(
zoom: Double = Defaults.zoom,
pitch: Double = Defaults.pitch,
pitchRange: CameraPitchRange = Defaults.pitchRange
) -> MapViewCamera {
// Coordinate is ignored when tracking user location. However, pitch and zoom are valid.
MapViewCamera(state: .trackingUserLocationWithCourse(zoom: zoom, pitch: pitch, pitchRange: pitchRange),
lastReasonForChange: .programmatic)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,13 @@ final class CameraStateTests: XCTestCase {
let coordinate = CLLocationCoordinate2D(latitude: 12.3, longitude: 23.4)

func testCenterCameraState() {
let state: CameraState = .centered(onCoordinate: coordinate, zoom: 4, pitch: 0, pitchRange: .free, direction: 42)
let state: CameraState = .centered(
onCoordinate: coordinate,
zoom: 4,
pitch: 0,
pitchRange: .free,
direction: 42
)
XCTAssertEqual(state, .centered(onCoordinate: coordinate, zoom: 4, pitch: 0, pitchRange: .free, direction: 42))
assertSnapshot(of: state, as: .description)
}
Expand Down

0 comments on commit bc75a5a

Please sign in to comment.