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

Split out style from LocationSupportableMapView #352

Merged
merged 6 commits into from
May 17, 2021
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
24 changes: 17 additions & 7 deletions Mapbox/MapboxMaps.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,9 @@
CA4AE07B252D655000183075 /* PolygonAnnotationTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 07D2C80B2523ECC40025C2BA /* PolygonAnnotationTests.swift */; };
CA4AE0DB252D655A00183075 /* AnnotationInteractionDelegateTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 07163339252546C80014A772 /* AnnotationInteractionDelegateTests.swift */; };
CA4AE123252D656600183075 /* MockAnnotationStyleDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 07A5DEB625155B9500A27F8B /* MockAnnotationStyleDelegate.swift */; };
CA4E2DBE264DC5FD0039D80E /* MockLocationStyleDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = CA4E2DBD264DC5FD0039D80E /* MockLocationStyleDelegate.swift */; };
CA4E2DCE264DC6680039D80E /* LocationStyleDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = CA4E2DC2264DC6300039D80E /* LocationStyleDelegate.swift */; };
CA4E2DD8264DC66F0039D80E /* MockLocationStyleDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = CA4E2DBD264DC5FD0039D80E /* MockLocationStyleDelegate.swift */; };
CA548FD3251C404B00F829A3 /* PolygonTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2B863836246D300500698135 /* PolygonTests.swift */; };
CA548FD4251C404B00F829A3 /* MapboxMapsCameraTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1FC8DA47243BE4A400A19318 /* MapboxMapsCameraTests.swift */; };
CA548FD5251C404B00F829A3 /* MapboxCompassOrnamentViewTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5A3593792476DEB800E67344 /* MapboxCompassOrnamentViewTests.swift */; };
Expand Down Expand Up @@ -723,6 +726,8 @@
CA4453C72436E71500477B4F /* AppDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppDelegate.swift; sourceTree = "<group>"; };
CA4453D52436E71900477B4F /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
CA4453D92436ED2A00477B4F /* Main.storyboard */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = file.storyboard; path = Main.storyboard; sourceTree = "<group>"; };
CA4E2DBD264DC5FD0039D80E /* MockLocationStyleDelegate.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = MockLocationStyleDelegate.swift; sourceTree = "<group>"; };
CA4E2DC2264DC6300039D80E /* LocationStyleDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LocationStyleDelegate.swift; sourceTree = "<group>"; };
CA53616D2537ECA600A8AE38 /* MapViewIntegrationTestCase.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MapViewIntegrationTestCase.swift; sourceTree = "<group>"; };
CA5361862537EE0600A8AE38 /* IntegrationTestCase.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = IntegrationTestCase.swift; sourceTree = "<group>"; };
CA54900D251C404B00F829A3 /* MapboxMapsTestsWithHost.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = MapboxMapsTestsWithHost.xctest; sourceTree = BUILT_PRODUCTS_DIR; };
Expand Down Expand Up @@ -1373,15 +1378,16 @@
1FF65A51244E33E10069B561 /* MapboxMapsLocation */ = {
isa = PBXGroup;
children = (
C6B58BC7252E53AD0063AC3E /* Pucks */,
1FF65A67244E34000069B561 /* LocationManager.swift */,
1FF65A78244F78540069B561 /* AppleLocationProvider.swift */,
0C9640E02531056700CABD3E /* LocationConsumer.swift */,
1FF65A67244E34000069B561 /* LocationManager.swift */,
1FF65A86245354E30069B561 /* LocationOptions.swift */,
1FF65A6D244E53F50069B561 /* LocationPermissionsDelegate.swift */,
1FF65A74244F6E320069B561 /* LocationProvider.swift */,
1FF65A76244F77070069B561 /* LocationProviderDelegate.swift */,
1FF65A78244F78540069B561 /* AppleLocationProvider.swift */,
1FF65A86245354E30069B561 /* LocationOptions.swift */,
CA4E2DC2264DC6300039D80E /* LocationStyleDelegate.swift */,
0CECCCCC253491A80000FC64 /* LocationSupportableMapView.swift */,
C6B58BC7252E53AD0063AC3E /* Pucks */,
);
name = MapboxMapsLocation;
path = ../Sources/MapboxMaps/Location;
Expand All @@ -1390,12 +1396,13 @@
1FF65A5C244E33E10069B561 /* MapboxMapsLocationTests */ = {
isa = PBXGroup;
children = (
B53B8760260D73D600C86BAA /* Pucks */,
C64ED33B253F819B00ADADFB /* LocationConsumerMock.swift */,
C64ED320253F7E9100ADADFB /* LocationManagerTests.swift */,
B53B86D8260D735B00C86BAA /* LocationOptionsTests.swift */,
C64ED305253F7CF500ADADFB /* LocationSupportableMapViewMock.swift */,
C64ED320253F7E9100ADADFB /* LocationManagerTests.swift */,
C64ED33B253F819B00ADADFB /* LocationConsumerMock.swift */,
CA4E2DBD264DC5FD0039D80E /* MockLocationStyleDelegate.swift */,
0C2CDA2D25D1A51C006D068F /* PuckTypeTests.swift */,
B53B8760260D73D600C86BAA /* Pucks */,
);
name = MapboxMapsLocationTests;
path = ../Tests/MapboxMapsTests/Location;
Expand Down Expand Up @@ -2079,6 +2086,7 @@
0C088ACC26388E3400107B5E /* CameraAnimationsManager.swift in Sources */,
CA6245D52627E72A00C79547 /* TileRegionLoadOptions+MapboxMaps.swift in Sources */,
5A9648FE246429C3001FF05D /* MapboxCompassOrnamentView.swift in Sources */,
CA4E2DCE264DC6680039D80E /* LocationStyleDelegate.swift in Sources */,
0C8AA1F3257FE1830037FD6B /* MapEvents.swift in Sources */,
B5B55D4C260E4D2900EBB589 /* MBXEdgeInsets.swift in Sources */,
0C479F90251CEC340025EC4F /* Snapshotter.swift in Sources */,
Expand Down Expand Up @@ -2148,6 +2156,7 @@
buildActionMask = 2147483647;
files = (
B53B8623260D717700C86BAA /* XCTestCase+MapboxAccessToken.swift in Sources */,
CA4E2DD8264DC66F0039D80E /* MockLocationStyleDelegate.swift in Sources */,
B53B8663260D719100C86BAA /* TestConveniences.swift in Sources */,
B53B8650260D718B00C86BAA /* Stub.swift in Sources */,
CA0B380D25C4C01400B3396E /* IntegrationTestCase.swift in Sources */,
Expand Down Expand Up @@ -2201,6 +2210,7 @@
0CC6EF1125C3263400BFB153 /* CircleLayerIntegrationTests.swift in Sources */,
C6C5CDC9264C12D00097FCD1 /* MigrationGuideIntegrationTests.swift in Sources */,
0C5CFCF425BB951B0001E753 /* FillLayerTests.swift in Sources */,
CA4E2DBE264DC5FD0039D80E /* MockLocationStyleDelegate.swift in Sources */,
B54B7F0B25DB192C003FD6CA /* MockCameraManager.swift in Sources */,
0C5CFCEB25BB951B0001E753 /* HillshadeLayerTests.swift in Sources */,
CA0C43052602BF2D0054D9D0 /* LayerPositionTests.swift in Sources */,
Expand Down
2 changes: 1 addition & 1 deletion Sources/MapboxMaps/Foundation/MapView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ open class MapView: UIView {
ornaments = OrnamentsManager(view: self, options: OrnamentOptions())

// Initialize/Configure location manager
location = LocationManager(locationSupportableMapView: self)
location = LocationManager(locationSupportableMapView: self, style: style)

// Initialize/Configure annotations manager
annotations = AnnotationManager(for: self, mapEventsObservable: mapboxMap, with: style)
Expand Down
8 changes: 7 additions & 1 deletion Sources/MapboxMaps/Location/LocationManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ public class LocationManager: NSObject {
/// `MapView` that has specific functionality to support location
internal weak var locationSupportableMapView: LocationSupportableMapView!

/// Style that has limited functionality to support location
internal weak var style: LocationStyleDelegate?

/// Manager that handles the visual puck element.
/// Only created if `showsUserLocation` is `true`
internal var locationPuckManager: LocationPuckManager?
Expand All @@ -54,11 +57,13 @@ public class LocationManager: NSObject {
}
}

internal init(locationSupportableMapView: LocationSupportableMapView) {
internal init(locationSupportableMapView: LocationSupportableMapView, style: LocationStyleDelegate) {

/// Allows location updates to be reflected on screen using delegate method
self.locationSupportableMapView = locationSupportableMapView

self.style = style

super.init()

/// Sets our default `locationProvider`
Expand Down Expand Up @@ -196,6 +201,7 @@ private extension LocationManager {
} else {
let locationPuckManager = LocationPuckManager(
locationSupportableMapView: locationSupportableMapView,
style: style,
puckType: puckType)
consumers.add(locationPuckManager)
self.locationPuckManager = locationPuckManager
Expand Down
22 changes: 22 additions & 0 deletions Sources/MapboxMaps/Location/LocationStyleDelegate.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
internal protocol LocationStyleDelegate: AnyObject {
func addLayer(_ layer: Layer, layerPosition: LayerPosition?) throws
func removeLayer(withId id: String) throws
func layerExists(withId id: String) -> Bool
func setLayerProperties(for layerId: String, properties: [String: Any]) throws
func addSource(_ source: Source, id: String) throws
func removeSource(withId id: String) throws
func setSourceProperty(for sourceId: String, property: String, value: Any) throws

//swiftlint:disable function_parameter_count
func addImage(_ image: UIImage, id: String, sdf: Bool, stretchX: [ImageStretches], stretchY: [ImageStretches], content: ImageContent?) throws
}

extension LocationStyleDelegate {
internal func addLayer(_ layer: Layer, layerPosition: LayerPosition? = nil) throws {
try addLayer(layer, layerPosition: layerPosition)
}

internal func addImage(_ image: UIImage, id: String, sdf: Bool = false, stretchX: [ImageStretches] = [], stretchY: [ImageStretches] = [], content: ImageContent? = nil) throws {
try addImage(image, id: id, sdf: sdf, stretchX: stretchX, stretchY: stretchY, content: content)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,6 @@ import MapboxMapsStyle

public protocol LocationSupportableMapView: UIView {

/// Matching the style property in `MapView`
var style: Style! { get }

/// Returns the screen coordinate for a given location coordinate (lat-long)
func screenCoordinate(for locationCoordinate: CLLocationCoordinate2D) -> ScreenCoordinate

Expand All @@ -25,5 +22,4 @@ public protocol LocationSupportableMapView: UIView {

/// Gets meters per point at latitude for calculating accuracy ring
func metersPerPointAtLatitude(latitude: CLLocationDegrees) -> CLLocationDistance

}
21 changes: 18 additions & 3 deletions Sources/MapboxMaps/Location/Pucks/LocationPuckManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -40,17 +40,22 @@ internal class LocationPuckManager: LocationConsumer {
/// MapView that supports location events
internal private(set) weak var locationSupportableMapView: LocationSupportableMapView?

/// Style protocol that supports limited style APIs
internal private(set) weak var style: LocationStyleDelegate?

/// The current puck style
internal private(set) var puckStyle: PuckStyle

/// The current puck type
internal private(set) var puckType: PuckType

internal init(locationSupportableMapView: LocationSupportableMapView,
style: LocationStyleDelegate?,
puckType: PuckType) {
puckStyle = .precise
self.puckType = puckType
self.locationSupportableMapView = locationSupportableMapView
self.style = style
}

/// LocationConsumer protocol method that will handle location updates
Expand All @@ -66,14 +71,24 @@ internal class LocationPuckManager: LocationConsumer {
}

internal func createPuck() {
guard let locationSupportableMapView = self.locationSupportableMapView else { return }
guard let locationSupportableMapView = locationSupportableMapView,
let style = style else {
return
}

var puck: Puck

switch puckType {
case let .puck2D(configuration):
puck = Puck2D(puckStyle: puckStyle, locationSupportableMapView: locationSupportableMapView, configuration: configuration)
puck = Puck2D(puckStyle: puckStyle,
locationSupportableMapView: locationSupportableMapView,
style: style,
configuration: configuration)
case let .puck3D(configuration):
puck = Puck3D(puckStyle: puckStyle, locationSupportableMapView: locationSupportableMapView, configuration: configuration)
puck = Puck3D(puckStyle: puckStyle,
locationSupportableMapView: locationSupportableMapView,
style: style,
configuration: configuration)
}

if let location = latestLocation {
Expand Down
52 changes: 29 additions & 23 deletions Sources/MapboxMaps/Location/Pucks/Puck2D.swift
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,16 @@ internal class Puck2D: Puck {
internal var puckStyle: PuckStyle

internal weak var locationSupportableMapView: LocationSupportableMapView?
internal weak var style: LocationStyleDelegate?

// MARK: Initializers
internal init(puckStyle: PuckStyle, locationSupportableMapView: LocationSupportableMapView, configuration: Puck2DConfiguration) {
internal init(puckStyle: PuckStyle,
locationSupportableMapView: LocationSupportableMapView,
style: LocationStyleDelegate,
configuration: Puck2DConfiguration) {
self.puckStyle = puckStyle
self.locationSupportableMapView = locationSupportableMapView
self.style = style
self.configuration = configuration
}

Expand All @@ -71,8 +76,8 @@ internal class Puck2D: Puck {

// MARK: Protocol Implementation
internal func updateLocation(location: Location) {
if let locationIndicatorLayer = self.locationIndicatorLayer,
let style = locationSupportableMapView?.style {
if let locationIndicatorLayer = locationIndicatorLayer,
let style = style {

let newLocation: [Double] = [
location.coordinate.latitude,
Expand All @@ -85,21 +90,15 @@ internal class Puck2D: Puck {
bearing = latestBearing.trueHeading
}

let expectedValueLocation = style.styleManager.setStyleLayerPropertyForLayerId(locationIndicatorLayer.id,
property: "location",
value: newLocation)
let expectedValueBearing = style.styleManager.setStyleLayerPropertyForLayerId(locationIndicatorLayer.id,
property: "bearing",
value: bearing)

if expectedValueLocation.isError() {
Log.error(forMessage: "Error when updating location in location indicator layer: \(String(describing: expectedValueLocation.error))", category: "Location")
}

if expectedValueBearing.isError() {
Log.error(forMessage: "Error when updating location in location indicator layer: \(String(describing: expectedValueBearing.error))", category: "Location")
do {
try style.setLayerProperties(for: locationIndicatorLayer.id,
properties: [
"location": newLocation,
"bearing": bearing
])
} catch {
Log.error(forMessage: "Error when updating location/bearing in location indicator layer: \(error)", category: "Location")
}

} else {
updateStyle(puckStyle: puckStyle, location: location)
}
Expand Down Expand Up @@ -134,11 +133,12 @@ internal class Puck2D: Puck {

private func removePuck() {
guard let locationIndicatorLayer = self.locationIndicatorLayer,
let style = locationSupportableMapView?.style
else { return }
let style = style else {
return
}

do {
try style.removeLayer(withId: locationIndicatorLayer.id)
try style.removeLayer(withId: locationIndicatorLayer.id)
} catch {
Log.error(forMessage: "Error when removing location indicator layer: \(error)", category: "Location")
}
Expand All @@ -151,7 +151,10 @@ internal class Puck2D: Puck {

private extension Puck2D {
func createPreciseLocationIndicatorLayer(location: Location) throws {
guard let style = locationSupportableMapView?.style else { return }
guard let style = style else {
Log.warning(forMessage: "Puck2D.createPreciseLocationIndicatorLayer - Style does not exit.", category: "Location")
return
julianrex marked this conversation as resolved.
Show resolved Hide resolved
}

if style.layerExists(withId: "approximate-puck") {
try style.removeLayer(withId: "approximate-puck")
Expand Down Expand Up @@ -207,8 +210,11 @@ private extension Puck2D {
}

func createApproximateLocationIndicatorLayer(location: Location) throws {
guard let style = locationSupportableMapView?.style else { return }
// TODO: Handle removal of precise indicator properly.
guard let style = style else {
Log.warning(forMessage: "Puck2D.createApproximateLocationIndicatorLayer - Style does not exit.", category: "Location")
return
julianrex marked this conversation as resolved.
Show resolved Hide resolved
}

if style.layerExists(withId: "puck") {
try style.removeLayer(withId: "puck")
}
Expand Down
13 changes: 7 additions & 6 deletions Sources/MapboxMaps/Location/Pucks/Puck3D.swift
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,13 @@ internal class Puck3D: Puck {
// MARK: Protocol Properties
internal var puckStyle: PuckStyle
internal weak var locationSupportableMapView: LocationSupportableMapView?
internal weak var style: LocationStyleDelegate?

// MARK: Initializers
internal init(puckStyle: PuckStyle, locationSupportableMapView: LocationSupportableMapView, configuration: Puck3DConfiguration) {
internal init(puckStyle: PuckStyle, locationSupportableMapView: LocationSupportableMapView, style: LocationStyleDelegate, configuration: Puck3DConfiguration) {
self.puckStyle = puckStyle
self.locationSupportableMapView = locationSupportableMapView
self.style = style
self.configuration = configuration
modelLayer = ModelLayer(id: "puck-model-layer")
modelSource = ModelSource()
Expand Down Expand Up @@ -73,7 +75,7 @@ internal class Puck3D: Puck {

let addStyle = { [weak self] in
guard let self = self,
let style = self.locationSupportableMapView?.style else {
let style = self.style else {
return
}
self.removePuck()
Expand All @@ -94,7 +96,7 @@ internal class Puck3D: Puck {

// MARK: Protocol Implementation
internal func updateLocation(location: Location) {
guard let style = locationSupportableMapView?.style,
guard let style = style,
let key = modelSource.models?.keys.first,
var model = modelSource.models?.values.first else { return }

Expand All @@ -121,9 +123,8 @@ internal class Puck3D: Puck {

/// This function will remove the puck from `mapView`
private func removePuck() {
guard
let style = locationSupportableMapView?.style,
style.layerExists(withId: "puck-model-layer") else {
guard let style = style,
style.layerExists(withId: "puck-model-layer") else {
return
}

Expand Down
2 changes: 2 additions & 0 deletions Sources/MapboxMaps/MapView/MapView+Supportable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -53,4 +53,6 @@ extension MapView: LocationSupportableMapView {
}
}

extension Style: LocationStyleDelegate { }

extension Style: AnnotationStyleDelegate { }
4 changes: 2 additions & 2 deletions Sources/MapboxMaps/Style/Sources.swift
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@ public extension Source {
/// Information about a Source
public struct SourceInfo {
/// The identifier of the source
var id: String
public var id: String

/// The type of the source
var type: SourceType
public var type: SourceType
}
Loading