From bc75a5ae2bc6f93408f1d0082b4d3806f5944fb9 Mon Sep 17 00:00:00 2001 From: PW Date: Tue, 14 May 2024 00:33:30 +0200 Subject: [PATCH] format improvements --- .../MapViewCameraOperations.swift | 7 +- .../MapLibreSwiftUI/MapViewCoordinator.swift | 65 ++++++++++++------- .../Models/MapCamera/CameraState.swift | 8 ++- .../Models/MapCamera/MapViewCamera.swift | 34 +++++++--- .../Models/MapCamera/CameraStateTests.swift | 8 ++- 5 files changed, 87 insertions(+), 35 deletions(-) diff --git a/Sources/MapLibreSwiftUI/Extensions/MapViewCamera/MapViewCameraOperations.swift b/Sources/MapLibreSwiftUI/Extensions/MapViewCamera/MapViewCameraOperations.swift index 4545676..ef6db95 100644 --- a/Sources/MapLibreSwiftUI/Extensions/MapViewCamera/MapViewCameraOperations.swift +++ b/Sources/MapLibreSwiftUI/Extensions/MapViewCamera/MapViewCameraOperations.swift @@ -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): diff --git a/Sources/MapLibreSwiftUI/MapViewCoordinator.swift b/Sources/MapLibreSwiftUI/MapViewCoordinator.swift index 6f0cc30..d0886ed 100644 --- a/Sources/MapLibreSwiftUI/MapViewCoordinator.swift +++ b/Sources/MapLibreSwiftUI/MapViewCoordinator.swift @@ -63,9 +63,15 @@ 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. @@ -73,35 +79,35 @@ public class MapViewCoordinator: NSObject { 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 @@ -109,11 +115,16 @@ public class MapViewCoordinator: NSObject { 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): @@ -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): @@ -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): diff --git a/Sources/MapLibreSwiftUI/Models/MapCamera/CameraState.swift b/Sources/MapLibreSwiftUI/Models/MapCamera/CameraState.swift index 00cffc9..6a09a9a 100644 --- a/Sources/MapLibreSwiftUI/Models/MapCamera/CameraState.swift +++ b/Sources/MapLibreSwiftUI/Models/MapCamera/CameraState.swift @@ -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))" diff --git a/Sources/MapLibreSwiftUI/Models/MapCamera/MapViewCamera.swift b/Sources/MapLibreSwiftUI/Models/MapCamera/MapViewCamera.swift index 3029732..f6181af 100644 --- a/Sources/MapLibreSwiftUI/Models/MapCamera/MapViewCamera.swift +++ b/Sources/MapLibreSwiftUI/Models/MapCamera/MapViewCamera.swift @@ -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. @@ -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. @@ -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) @@ -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) diff --git a/Tests/MapLibreSwiftUITests/Models/MapCamera/CameraStateTests.swift b/Tests/MapLibreSwiftUITests/Models/MapCamera/CameraStateTests.swift index 8d52510..0fb061e 100644 --- a/Tests/MapLibreSwiftUITests/Models/MapCamera/CameraStateTests.swift +++ b/Tests/MapLibreSwiftUITests/Models/MapCamera/CameraStateTests.swift @@ -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) }