Skip to content

Commit

Permalink
feat: allow othermodules to define MapComponents (#3321)
Browse files Browse the repository at this point in the history
  • Loading branch information
mfazekas authored Jan 15, 2024
1 parent b27e5f1 commit 71fafa1
Show file tree
Hide file tree
Showing 17 changed files with 63 additions and 59 deletions.
8 changes: 4 additions & 4 deletions ios/RNMBX/RNMBXAtmosphere.swift
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import MapboxMaps

#if RNMBX_11
typealias Style = StyleManager
public typealias Style = StyleManager
#endif

@objc(RNMBXAtmosphere)
Expand All @@ -12,7 +12,7 @@ public class RNMBXAtmosphere : RNMBXSingletonLayer, RNMBXMapComponent, RNMBXSour
return Atmosphere()
}

func addToMap(_ map: RNMBXMapView, style: Style) {
public func addToMap(_ map: RNMBXMapView, style: Style) {
self.map = map
self.style = style

Expand All @@ -21,7 +21,7 @@ public class RNMBXAtmosphere : RNMBXSingletonLayer, RNMBXMapComponent, RNMBXSour
addStylesAndUpdate()
}

func removeFromMap(_ map: RNMBXMapView, reason _: RemovalReason) -> Bool {
public func removeFromMap(_ map: RNMBXMapView, reason _: RemovalReason) -> Bool {
self.map = nil

guard let mapboxMap = map.mapboxMap else {
Expand All @@ -33,7 +33,7 @@ public class RNMBXAtmosphere : RNMBXSingletonLayer, RNMBXMapComponent, RNMBXSour
return true
}

func waitForStyleLoad() -> Bool {
public func waitForStyleLoad() -> Bool {
return true
}

Expand Down
14 changes: 7 additions & 7 deletions ios/RNMBX/RNMBXCamera.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@ extension NSNumber {
}
#endif

enum RemovalReason {
public enum RemovalReason {
case ViewRemoval, StyleChange, OnDestroy, ComponentChange, Reorder
}

protocol RNMBXMapComponent: AnyObject {
public protocol RNMBXMapComponent: AnyObject {
func addToMap(_ map: RNMBXMapView, style: Style)
func removeFromMap(_ map: RNMBXMapView, reason: RemovalReason) -> Bool

Expand Down Expand Up @@ -99,19 +99,19 @@ open class RNMBXMapComponentBase : UIView, RNMBXMapComponent {
}
}

func waitForStyleLoad() -> Bool {
public func waitForStyleLoad() -> Bool {
return false
}

func addToMap(_ map: RNMBXMapView, style: Style) {
public func addToMap(_ map: RNMBXMapView, style: Style) {
_mapCallbacks.forEach { callback in
callback(map)
}
_mapCallbacks = []
_map = map
}

func removeFromMap(_ map: RNMBXMapView, reason: RemovalReason) -> Bool {
public func removeFromMap(_ map: RNMBXMapView, reason: RemovalReason) -> Bool {
_mapCallbacks = []
_map = nil
return true
Expand Down Expand Up @@ -514,12 +514,12 @@ open class RNMBXCamera : RNMBXMapComponentBase {
_updateCamera()
}

override func addToMap(_ map: RNMBXMapView, style: Style) {
public override func addToMap(_ map: RNMBXMapView, style: Style) {
super.addToMap(map, style: style)
map.reactCamera = self
}

override func removeFromMap(_ map: RNMBXMapView, reason: RemovalReason) -> Bool {
public override func removeFromMap(_ map: RNMBXMapView, reason: RemovalReason) -> Bool {
if (reason == .StyleChange) {
return false
}
Expand Down
6 changes: 3 additions & 3 deletions ios/RNMBX/RNMBXCustomLocationProvider.swift
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public class RNMBXCustomLocationProvider: UIView, RNMBXMapComponent {
var defaultLocationProvider: LocationProvider?
#endif

func addToMap(_ map: RNMBXMapView, style: Style) {
public func addToMap(_ map: RNMBXMapView, style: Style) {
self.map = map
if let mapView = map.mapView {
installCustomeLocationProviderIfNeeded(mapView: mapView)
Expand All @@ -58,15 +58,15 @@ public class RNMBXCustomLocationProvider: UIView, RNMBXMapComponent {
updateHeading(heading: heading.doubleValue)
}

func removeFromMap(_ map: RNMBXMapView, reason: RemovalReason) -> Bool {
public func removeFromMap(_ map: RNMBXMapView, reason: RemovalReason) -> Bool {
if let mapView = map.mapView {
removeCustomLocationProvider(mapView: mapView)
}
self.map = nil
return true
}

func waitForStyleLoad() -> Bool {
public func waitForStyleLoad() -> Bool {
false
}
}
Expand Down
6 changes: 3 additions & 3 deletions ios/RNMBX/RNMBXImages.swift
Original file line number Diff line number Diff line change
Expand Up @@ -75,11 +75,11 @@ open class RNMBXImages : UIView, RNMBXMapComponent {

// MARK: - RNMBXMapComponent

func waitForStyleLoad() -> Bool {
public func waitForStyleLoad() -> Bool {
return false
}

func addToMap(_ map: RNMBXMapView, style: Style) {
public func addToMap(_ map: RNMBXMapView, style: Style) {
self.style = style
imageManager = map.imageManager
map.images.append(self)
Expand All @@ -89,7 +89,7 @@ open class RNMBXImages : UIView, RNMBXMapComponent {
self.addImageViews(style: style, imageViews: imageViews)
}

func removeFromMap(_ map: RNMBXMapView, reason: RemovalReason) -> Bool {
public func removeFromMap(_ map: RNMBXMapView, reason: RemovalReason) -> Bool {
self.style = nil
imageManager = nil
// v10todo
Expand Down
6 changes: 3 additions & 3 deletions ios/RNMBX/RNMBXInteractiveElement.swift
Original file line number Diff line number Diff line change
Expand Up @@ -46,19 +46,19 @@ public class RNMBXInteractiveElement : UIView, RNMBXMapComponent {
}

// MARK: - RNMBXMapComponent
func addToMap(_ map: RNMBXMapView, style: Style) {
public func addToMap(_ map: RNMBXMapView, style: Style) {
if (self.id == nil) {
Logger.log(level: .error, message: "id is required on \(self) but not specified")
}
self.map = map
}

func removeFromMap(_ map: RNMBXMapView, reason: RemovalReason) -> Bool {
public func removeFromMap(_ map: RNMBXMapView, reason: RemovalReason) -> Bool {
self.map = nil
return true
}

func waitForStyleLoad() -> Bool {
public func waitForStyleLoad() -> Bool {
return true
}
}
6 changes: 3 additions & 3 deletions ios/RNMBX/RNMBXLayer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ public class RNMBXLayer : UIView, RNMBXMapComponent, RNMBXSourceConsumer {
var existingLayer = false

// MARK: - RNMBXMapComponent
func waitForStyleLoad() -> Bool {
public func waitForStyleLoad() -> Bool {
return true
}

Expand Down Expand Up @@ -220,7 +220,7 @@ public class RNMBXLayer : UIView, RNMBXMapComponent, RNMBXSourceConsumer {

}

func addToMap(_ map: RNMBXMapView, style: Style) {
public func addToMap(_ map: RNMBXMapView, style: Style) {
self.map = map
self.style = style
guard let id = id else {
Expand Down Expand Up @@ -321,7 +321,7 @@ public class RNMBXLayer : UIView, RNMBXMapComponent, RNMBXSourceConsumer {
}
}

func removeFromMap(_ map: RNMBXMapView, reason: RemovalReason) -> Bool {
public func removeFromMap(_ map: RNMBXMapView, reason: RemovalReason) -> Bool {
removeFromMap(map.mapboxMap.style)
return true
}
Expand Down
6 changes: 3 additions & 3 deletions ios/RNMBX/RNMBXLight.swift
Original file line number Diff line number Diff line change
Expand Up @@ -51,18 +51,18 @@ public class RNMBXLight: UIView, RNMBXMapComponent {

// MARK: - RNMBXMapComponent

func waitForStyleLoad() -> Bool {
public func waitForStyleLoad() -> Bool {
return true
}

func addToMap(_ map: RNMBXMapView, style: Style) {
public func addToMap(_ map: RNMBXMapView, style: Style) {
self.map = map.mapboxMap
if (reactStyle != nil) {
addStyles()
}
}

func removeFromMap(_ map: RNMBXMapView, reason: RemovalReason) -> Bool {
public func removeFromMap(_ map: RNMBXMapView, reason: RemovalReason) -> Bool {
self.map = nil
return true
}
Expand Down
2 changes: 1 addition & 1 deletion ios/RNMBX/RNMBXMapView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ open class RNMBXMapView: UIView {
}
}

var mapView : MapView! {
public var mapView : MapView! {
get { return _mapView }
}
var mapboxMap: MapboxMap! {
Expand Down
10 changes: 4 additions & 6 deletions ios/RNMBX/RNMBXMarkerView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -96,12 +96,12 @@ public class RNMBXMarkerView: UIView, RNMBXMapComponent {

// MARK: - RNMBXMapComponent methods

func addToMap(_ map: RNMBXMapView, style: Style) {
public func addToMap(_ map: RNMBXMapView, style: Style) {
self.map = map
add()
}

func removeFromMap(_ map: RNMBXMapView, reason: RemovalReason) -> Bool {
public func removeFromMap(_ map: RNMBXMapView, reason: RemovalReason) -> Bool {
remove()
return true
}
Expand Down Expand Up @@ -149,10 +149,8 @@ public class RNMBXMarkerView: UIView, RNMBXMapComponent {
@objc public func updateAnnotationViewSize(_ next: CGRect, _ prev: CGRect) {
annotationView.updateSize(next.size, oldOffset:calcOffset(size: prev.size), newOffset: calcOffset(size: next.size))
}



func waitForStyleLoad() -> Bool {

public func waitForStyleLoad() -> Bool {
true
}

Expand Down
6 changes: 3 additions & 3 deletions ios/RNMBX/RNMBXModels.swift
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ open class RNMBXModels : UIView, RNMBXMapComponent {

}

func addToMap(_ map: RNMBXMapView, style: Style) {
public func addToMap(_ map: RNMBXMapView, style: Style) {
modelIdToUrl.forEach { (id, uri) in
logged("Models.addStyleModel") {
if let link = URL(string: uri), let scheme = link.scheme, let host = link.host,
Expand All @@ -48,7 +48,7 @@ open class RNMBXModels : UIView, RNMBXMapComponent {
}
}

func removeFromMap(_ map: RNMBXMapView, reason: RemovalReason) -> Bool {
public func removeFromMap(_ map: RNMBXMapView, reason: RemovalReason) -> Bool {
modelIdToUrl.forEach { (id, _) in
#if RNMBX_11
try? map._mapView?.mapboxMap.removeStyleModel(modelId: id)
Expand All @@ -57,7 +57,7 @@ open class RNMBXModels : UIView, RNMBXMapComponent {
return true
}

func waitForStyleLoad() -> Bool {
public func waitForStyleLoad() -> Bool {
true
}
}
6 changes: 3 additions & 3 deletions ios/RNMBX/RNMBXNativeUserLocation.swift
Original file line number Diff line number Diff line change
Expand Up @@ -214,14 +214,14 @@ public class RNMBXNativeUserLocation: UIView, RNMBXMapComponent {
}
}

func addToMap(_ map: RNMBXMapView, style: Style) {
public func addToMap(_ map: RNMBXMapView, style: Style) {
self.map = map

_fetchImages(map)
_apply()
}

func removeFromMap(_ map: RNMBXMapView, reason: RemovalReason) -> Bool {
public func removeFromMap(_ map: RNMBXMapView, reason: RemovalReason) -> Bool {
if let location = map.mapView.location {
location.options.puckType = nil
location.options.puckType = .none
Expand All @@ -234,7 +234,7 @@ public class RNMBXNativeUserLocation: UIView, RNMBXMapComponent {
return true
}

func waitForStyleLoad() -> Bool {
public func waitForStyleLoad() -> Bool {
return true
}
}
Expand Down
4 changes: 2 additions & 2 deletions ios/RNMBX/RNMBXPointAnnotation.swift
Original file line number Diff line number Diff line change
Expand Up @@ -250,13 +250,13 @@ public class RNMBXPointAnnotation : RNMBXInteractiveElement {

// MARK: - RNMBXMapComponent

override func addToMap(_ map: RNMBXMapView, style: Style) {
public override func addToMap(_ map: RNMBXMapView, style: Style) {
super.addToMap(map, style: style)
self.map = map
addIfPossible()
}

override func removeFromMap(_ map: RNMBXMapView, reason: RemovalReason) -> Bool {
public override func removeFromMap(_ map: RNMBXMapView, reason: RemovalReason) -> Bool {
removeIfAdded()
self.map = nil
return true
Expand Down
4 changes: 2 additions & 2 deletions ios/RNMBX/RNMBXSource.swift
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ public class RNMBXSource : RNMBXInteractiveElement {

// MARK: - RNMBXInteractiveElement

override func addToMap(_ map: RNMBXMapView, style: Style) {
public override func addToMap(_ map: RNMBXMapView, style: Style) {
self.map = map

if style.sourceExists(withId: self.id) {
Expand Down Expand Up @@ -108,7 +108,7 @@ public class RNMBXSource : RNMBXInteractiveElement {
}
}

override func removeFromMap(_ map: RNMBXMapView, reason: RemovalReason) -> Bool {
public override func removeFromMap(_ map: RNMBXMapView, reason: RemovalReason) -> Bool {
self.map = nil

for layer in self.layers {
Expand Down
6 changes: 3 additions & 3 deletions ios/RNMBX/RNMBXStyleImport.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,16 @@ open class RNMBXStyleImport : UIView, RNMBXMapComponent {
}
}

func waitForStyleLoad() -> Bool {
public func waitForStyleLoad() -> Bool {
true
}

func addToMap(_ map: RNMBXMapView, style: Style) {
public func addToMap(_ map: RNMBXMapView, style: Style) {
mapView = map.mapView
apply(mapView: map.mapView)
}

func removeFromMap(_ map: RNMBXMapView, reason: RemovalReason) -> Bool {
public func removeFromMap(_ map: RNMBXMapView, reason: RemovalReason) -> Bool {
self.mapView = nil
return true
}
Expand Down
6 changes: 3 additions & 3 deletions ios/RNMBX/RNMBXTerrain.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public class RNMBXTerrain : RNMBXSingletonLayer, RNMBXMapComponent, RNMBXSourceC
return Terrain(sourceId: sourceID)
}

func addToMap(_ map: RNMBXMapView, style: Style) {
public func addToMap(_ map: RNMBXMapView, style: Style) {
self.map = map
self.style = style

Expand All @@ -22,7 +22,7 @@ public class RNMBXTerrain : RNMBXSingletonLayer, RNMBXMapComponent, RNMBXSourceC
addStylesAndUpdate()
}

func removeFromMap(_ map: RNMBXMapView, reason: RemovalReason) -> Bool {
public func removeFromMap(_ map: RNMBXMapView, reason: RemovalReason) -> Bool {
self.map = nil

guard let mapboxMap = map.mapboxMap else {
Expand All @@ -34,7 +34,7 @@ public class RNMBXTerrain : RNMBXSingletonLayer, RNMBXMapComponent, RNMBXSourceC
return true
}

func waitForStyleLoad() -> Bool {
public func waitForStyleLoad() -> Bool {
return true
}

Expand Down
Loading

0 comments on commit 71fafa1

Please sign in to comment.