Skip to content

Commit f4a2d21

Browse files
Add MapActivity to track what MapView is being used for. (#81)
* Use self.init instead of setting member variables directly. - This way if the initializer should change, it will be a little more obvious for this client when re-building. * Add MapActivity to track what MapView is being used for. - Typically in an application, there is one MNMapView. When using CarPlay, there are two. This helps debugging the scenario so that two views can be distinguished from each other when debugging. - It sets the `UIView.tag` property of the `MLNMapView` to the enum's rawValue. It also exists in its enumerated type elsewhere. Depends upon #80 --------- Co-authored-by: Ian Wagner <[email protected]>
1 parent 9324755 commit f4a2d21

File tree

2 files changed

+22
-1
lines changed

2 files changed

+22
-1
lines changed

Sources/MapLibreSwiftUI/MLNMapViewController.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,15 @@ public protocol MapViewHostViewController: UIViewController {
77
}
88

99
public final class MLNMapViewController: UIViewController, MapViewHostViewController {
10+
var activity: MapActivity = .standard
11+
1012
@MainActor
1113
public var mapView: MLNMapView {
1214
view as! MLNMapView
1315
}
1416

1517
override public func loadView() {
1618
view = MLNMapView(frame: .zero)
19+
view.tag = activity.rawValue
1720
}
1821
}

Sources/MapLibreSwiftUI/MapView.swift

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,14 @@ import MapLibre
33
import MapLibreSwiftDSL
44
import SwiftUI
55

6+
/// Identifies the activity this ``MapView`` is being used for. Useful for debugging purposes.
7+
public enum MapActivity: Int {
8+
/// Navigation in a standard window. Default.
9+
case standard = 0
10+
/// Navigation in a CarPlay template.
11+
case carplay = 2025
12+
}
13+
614
public struct MapView<T: MapViewHostViewController>: UIViewControllerRepresentable {
715
public typealias UIViewControllerType = T
816
var cameraDisabled: Bool = false
@@ -33,18 +41,22 @@ public struct MapView<T: MapViewHostViewController>: UIViewControllerRepresentab
3341

3442
var clusteredLayers: [ClusterLayer]?
3543

44+
let activity: MapActivity
45+
3646
public init(
3747
makeViewController: @autoclosure @escaping () -> T,
3848
styleURL: URL,
3949
camera: Binding<MapViewCamera> = .constant(.default()),
4050
locationManager: MLNLocationManager? = nil,
51+
activity: MapActivity = .standard,
4152
@MapViewContentBuilder _ makeMapContent: () -> [StyleLayerDefinition] = { [] }
4253
) {
4354
self.makeViewController = makeViewController
4455
styleSource = .url(styleURL)
4556
_camera = camera
4657
userLayers = makeMapContent()
4758
self.locationManager = locationManager
59+
self.activity = activity
4860
}
4961

5062
public func makeCoordinator() -> MapViewCoordinator<T> {
@@ -138,13 +150,19 @@ public extension MapView where T == MLNMapViewController {
138150
styleURL: URL,
139151
camera: Binding<MapViewCamera> = .constant(.default()),
140152
locationManager: MLNLocationManager? = nil,
153+
activity: MapActivity = .standard,
141154
@MapViewContentBuilder _ makeMapContent: () -> [StyleLayerDefinition] = { [] }
142155
) {
143156
self.init(
144-
makeViewController: MLNMapViewController(),
157+
makeViewController: {
158+
let vc = MLNMapViewController()
159+
vc.activity = activity
160+
return vc
161+
}(),
145162
styleURL: styleURL,
146163
camera: camera,
147164
locationManager: locationManager,
165+
activity: activity,
148166
makeMapContent
149167
)
150168
}

0 commit comments

Comments
 (0)