Skip to content

Commit

Permalink
generic init implementation to keep cleaner callsite
Browse files Browse the repository at this point in the history
  • Loading branch information
hactar committed Jul 8, 2024
1 parent 61c26af commit 5da79ef
Show file tree
Hide file tree
Showing 11 changed files with 66 additions and 43 deletions.
2 changes: 1 addition & 1 deletion Sources/MapLibreSwiftUI/Examples/Camera.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ struct CameraDirectManipulationPreview: View {
var targetCameraAfterDelay: MapViewCamera? = nil

var body: some View {
MapView<MapViewController>(styleURL: styleURL, camera: $camera)
MapView(styleURL: styleURL, camera: $camera)
.onStyleLoaded { _ in
onStyleLoaded?()
}
Expand Down
4 changes: 2 additions & 2 deletions Sources/MapLibreSwiftUI/Examples/Gestures.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import SwiftUI

#Preview("Tappable Circles") {
let tappableID = "simple-circles"
return MapView<MapViewController>(styleURL: demoTilesURL) {
return MapView(styleURL: demoTilesURL) {
// Simple symbol layer demonstration with an icon
CircleStyleLayer(identifier: tappableID, source: pointSource)
.radius(16)
Expand All @@ -24,7 +24,7 @@ import SwiftUI
}

#Preview("Tappable Countries") {
MapView<MapViewController>(styleURL: demoTilesURL)
MapView(styleURL: demoTilesURL)
.onTapMapGesture(on: ["countries-fill"], onTapChanged: { _, features in
print("Tapped on \(features.first?.description ?? "<nil>")")
})
Expand Down
14 changes: 7 additions & 7 deletions Sources/MapLibreSwiftUI/Examples/Layers.swift
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ let clustered = ShapeSource(identifier: "points", options: [.clustered: true, .c
}

#Preview("Rose Tint") {
MapView<MapViewController>(styleURL: demoTilesURL) {
MapView(styleURL: demoTilesURL) {
// Silly example: a background layer on top of everything to create a tint effect
BackgroundLayer(identifier: "rose-colored-glasses")
.backgroundColor(.systemPink.withAlphaComponent(0.3))
Expand All @@ -44,7 +44,7 @@ let clustered = ShapeSource(identifier: "points", options: [.clustered: true, .c
}

#Preview("Simple Symbol") {
MapView<MapViewController>(styleURL: demoTilesURL) {
MapView(styleURL: demoTilesURL) {
// Simple symbol layer demonstration with an icon
SymbolStyleLayer(identifier: "simple-symbols", source: pointSource)
.iconImage(UIImage(systemName: "mappin")!)
Expand All @@ -53,7 +53,7 @@ let clustered = ShapeSource(identifier: "points", options: [.clustered: true, .c
}

#Preview("Rotated Symbols (Const)") {
MapView<MapViewController>(styleURL: demoTilesURL) {
MapView(styleURL: demoTilesURL) {
// Simple symbol layer demonstration with an icon
SymbolStyleLayer(identifier: "rotated-symbols", source: pointSource)
.iconImage(UIImage(systemName: "location.north.circle.fill")!)
Expand All @@ -63,7 +63,7 @@ let clustered = ShapeSource(identifier: "points", options: [.clustered: true, .c
}

#Preview("Rotated Symbols (Dynamic)") {
MapView<MapViewController>(styleURL: demoTilesURL) {
MapView(styleURL: demoTilesURL) {
// Simple symbol layer demonstration with an icon
SymbolStyleLayer(identifier: "rotated-symbols", source: pointSource)
.iconImage(UIImage(systemName: "location.north.circle.fill")!)
Expand All @@ -73,7 +73,7 @@ let clustered = ShapeSource(identifier: "points", options: [.clustered: true, .c
}

#Preview("Circles with Symbols") {
MapView<MapViewController>(styleURL: demoTilesURL) {
MapView(styleURL: demoTilesURL) {
// Simple symbol layer demonstration with an icon
CircleStyleLayer(identifier: "simple-circles", source: pointSource)
.radius(16)
Expand All @@ -94,7 +94,7 @@ let clustered = ShapeSource(identifier: "points", options: [.clustered: true, .c
zoom: 5,
direction: 0
)
return MapView<MapViewController>(styleURL: demoTilesURL, camera: $camera) {
return MapView(styleURL: demoTilesURL, camera: $camera) {
// Clusters pins when they would touch

// Cluster == YES shows only those pins that are clustered, using .text
Expand Down Expand Up @@ -135,7 +135,7 @@ let clustered = ShapeSource(identifier: "points", options: [.clustered: true, .c

// TODO: Fixme
// #Preview("Multiple Symbol Icons") {
// MapView<MapViewController>(styleURL: demoTilesURL) {
// MapView(styleURL: demoTilesURL) {
// // Simple symbol layer demonstration with an icon
// SymbolStyleLayer(identifier: "simple-symbols", source: pointSource)
// .iconImage(attribute: "icon",
Expand Down
2 changes: 1 addition & 1 deletion Sources/MapLibreSwiftUI/Examples/Other.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import MapLibreSwiftDSL
import SwiftUI

#Preview("Unsafe MapView Modifier") {
MapView<MapViewController>(styleURL: demoTilesURL) {
MapView(styleURL: demoTilesURL) {
// A collection of points with various
// attributes
let pointSource = ShapeSource(identifier: "points") {
Expand Down
2 changes: 1 addition & 1 deletion Sources/MapLibreSwiftUI/Examples/Polyline.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ struct PolylinePreview: View {
let styleURL: URL

var body: some View {
MapView<MapViewController>(styleURL: styleURL,
MapView(styleURL: styleURL,
camera: .constant(.center(samplePedestrianWaypoints.first!, zoom: 14)))
{
// Note: This line does not add the source to the style as if it
Expand Down
4 changes: 2 additions & 2 deletions Sources/MapLibreSwiftUI/Examples/User Location.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ private let locationManager = StaticLocationManager(initialLocation: CLLocation(
))

#Preview("Track user location") {
MapView<MapViewController>(
MapView(
styleURL: demoTilesURL,
camera: .constant(.trackUserLocation(zoom: 4, pitch: 45)),
locationManager: locationManager
Expand All @@ -24,7 +24,7 @@ private let locationManager = StaticLocationManager(initialLocation: CLLocation(
}

#Preview("Track user location with Course") {
MapView<MapViewController>(
MapView(
styleURL: demoTilesURL,
camera: .constant(.trackUserLocationWithCourse(zoom: 4, pitch: 45)),
locationManager: locationManager
Expand Down
39 changes: 23 additions & 16 deletions Sources/MapLibreSwiftUI/MapView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,33 @@ import MapLibre
import MapLibreSwiftDSL
import SwiftUI


public extension MapView where T == MapViewController {

@MainActor
init(
styleURL: URL,
camera: Binding<MapViewCamera> = .constant(.default()),
locationManager: MLNLocationManager? = nil,
@MapViewContentBuilder _ makeMapContent: () -> [StyleLayerDefinition] = { [] }
) {
makeViewController = {
return MapViewController()

}
styleSource = .url(styleURL)
_camera = camera
userLayers = makeMapContent()
self.locationManager = locationManager
}
}
public struct MapView<T: WrappedViewController>: UIViewControllerRepresentable {
public typealias UIViewControllerType = T
var cameraDisabled: Bool = true

@Binding var camera: MapViewCamera

let makeViewController: (() -> T)?
let makeViewController: (() -> T)
let styleSource: MapStyleSource
let userLayers: [StyleLayerDefinition]

Expand Down Expand Up @@ -50,19 +70,6 @@ public struct MapView<T: WrappedViewController>: UIViewControllerRepresentable {
self.locationManager = locationManager
}

public init(
styleURL: URL,
camera: Binding<MapViewCamera> = .constant(.default()),
locationManager: MLNLocationManager? = nil,
@MapViewContentBuilder _ makeMapContent: () -> [StyleLayerDefinition] = { [] }
) {
makeViewController = nil
styleSource = .url(styleURL)
_camera = camera
userLayers = makeMapContent()
self.locationManager = locationManager
}

public func makeCoordinator() -> MapViewCoordinator<T> {
MapViewCoordinator<T>(
parent: self,
Expand All @@ -73,7 +80,7 @@ public struct MapView<T: WrappedViewController>: UIViewControllerRepresentable {

public func makeUIViewController(context: Context) -> T {
// Create the map view
let controller = makeViewController?() ?? T()
let controller = makeViewController()
controller.mapView.delegate = context.coordinator
context.coordinator.mapView = controller.mapView

Expand Down Expand Up @@ -145,7 +152,7 @@ public struct MapView<T: WrappedViewController>: UIViewControllerRepresentable {
}

#Preview {
MapView<MapViewController>(styleURL: demoTilesURL)
MapView(styleURL: demoTilesURL)
.ignoresSafeArea(.all)
.previewDisplayName("Vanilla Map")

Expand Down
15 changes: 10 additions & 5 deletions Tests/MapLibreSwiftUITests/Examples/LayerPreviewTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,10 @@ final class LayerPreviewTests: XCTestCase {
}
}

@MainActor
func testRoseTint() {
assertView {
MapView<MapViewController>(styleURL: demoTilesURL) {
MapView(styleURL: demoTilesURL) {
// Silly example: a background layer on top of everything to create a tint effect
BackgroundLayer(identifier: "rose-colored-glasses")
.backgroundColor(.systemPink.withAlphaComponent(0.3))
Expand All @@ -34,19 +35,21 @@ final class LayerPreviewTests: XCTestCase {
}
}

@MainActor
func testSimpleSymbol() {
assertView {
MapView<MapViewController>(styleURL: demoTilesURL) {
MapView(styleURL: demoTilesURL) {
// Simple symbol layer demonstration with an icon
SymbolStyleLayer(identifier: "simple-symbols", source: pointSource)
.iconImage(UIImage(systemName: "mappin")!)
}
}
}

@MainActor
func testRotatedSymbolConst() {
assertView {
MapView<MapViewController>(styleURL: demoTilesURL) {
MapView(styleURL: demoTilesURL) {
// Simple symbol layer demonstration with an icon
SymbolStyleLayer(identifier: "rotated-symbols", source: pointSource)
.iconImage(UIImage(systemName: "location.north.circle.fill")!)
Expand All @@ -55,9 +58,10 @@ final class LayerPreviewTests: XCTestCase {
}
}

@MainActor
func testRotatedSymboleDynamic() {
assertView {
MapView<MapViewController>(styleURL: demoTilesURL) {
MapView(styleURL: demoTilesURL) {
// Simple symbol layer demonstration with an icon
SymbolStyleLayer(identifier: "rotated-symbols", source: pointSource)
.iconImage(UIImage(systemName: "location.north.circle.fill")!)
Expand All @@ -66,9 +70,10 @@ final class LayerPreviewTests: XCTestCase {
}
}

@MainActor
func testCirclesWithSymbols() {
assertView {
MapView<MapViewController>(styleURL: demoTilesURL) {
MapView(styleURL: demoTilesURL) {
// Simple symbol layer demonstration with an icon
CircleStyleLayer(identifier: "simple-circles", source: pointSource)
.radius(16)
Expand Down
22 changes: 15 additions & 7 deletions Tests/MapLibreSwiftUITests/Examples/MapControlsTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,65 +4,73 @@ import XCTest
@testable import MapLibreSwiftUI

final class MapControlsTests: XCTestCase {

@MainActor
func testEmptyControls() {
assertView {
MapView<MapViewController>(styleURL: demoTilesURL)
MapView(styleURL: demoTilesURL)
.mapControls {
// No controls
}
}
}

@MainActor
func testLogoOnly() {
assertView {
MapView<MapViewController>(styleURL: demoTilesURL)
MapView(styleURL: demoTilesURL)
.mapControls {
LogoView()
}
}
}

@MainActor
func testLogoChangePosition() {
assertView {
MapView<MapViewController>(styleURL: demoTilesURL)
MapView(styleURL: demoTilesURL)
.mapControls {
LogoView()
.position(.topLeft)
}
}
}

@MainActor
func testCompassOnly() {
assertView {
MapView<MapViewController>(styleURL: demoTilesURL)
MapView(styleURL: demoTilesURL)
.mapControls {
CompassView()
}
}
}

@MainActor
func testCompassChangePosition() {
assertView {
MapView<MapViewController>(styleURL: demoTilesURL)
MapView(styleURL: demoTilesURL)
.mapControls {
CompassView()
.position(.topLeft)
}
}
}

@MainActor
func testAttributionOnly() {
assertView {
MapView<MapViewController>(styleURL: demoTilesURL)
MapView(styleURL: demoTilesURL)
.mapControls {
AttributionButton()
}
}
}

@MainActor
func testAttributionChangePosition() {
assertView {
MapView<MapViewController>(styleURL: demoTilesURL)
MapView(styleURL: demoTilesURL)
.mapControls {
AttributionButton()
.position(.topLeft)
Expand Down
4 changes: 3 additions & 1 deletion Tests/MapLibreSwiftUITests/MapView/MapViewGestureTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ import XCTest

final class MapViewGestureTests: XCTestCase {
let maplibreMapView = MLNMapView()
let mapView = MapView<MapViewController>(styleURL: URL(string: "https://maplibre.org")!)

@MainActor
let mapView = MapView(styleURL: URL(string: "https://maplibre.org")!)

// MARK: Gesture View Modifiers

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ final class MapViewCoordinatorCameraTests: XCTestCase {
var mapView: MapView<MapViewController>!
var coordinator: MapView<MapViewController>.Coordinator!

@MainActor
override func setUp() async throws {
maplibreMapView = MockMLNMapViewCameraUpdating()
given(maplibreMapView).frame.willReturn(.zero)
Expand Down

0 comments on commit 5da79ef

Please sign in to comment.