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

fix(v10,ios): Reinsert style dependent components on style change #2488

Merged
merged 1 commit into from
Dec 13, 2022
Merged
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
23 changes: 23 additions & 0 deletions ios/RCTMGL-v10/RCTMGLMapView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ open class RCTMGLMapView : MapView {
var styleLoaded: Bool = false
var styleLoadWaiters : [(MapboxMap)->Void] = []
var onStyleLoadedComponents: [RCTMGLMapComponent] = []

var componentsToRefreshOnStyleChange: [RCTMGLMapComponent] = []

weak var reactCamera : RCTMGLCamera?
var images : [RCTMGLImages] = []
Expand Down Expand Up @@ -46,6 +48,7 @@ open class RCTMGLMapView : MapView {
if let mapComponent = subview as? RCTMGLMapComponent {
let style = mapView.mapboxMap.style
if mapComponent.waitForStyleLoad() {
componentsToRefreshOnStyleChange.append(mapComponent)
if (self.styleLoaded) {
mapComponent.addToMap(self, style: style)
} else {
Expand All @@ -66,6 +69,7 @@ open class RCTMGLMapView : MapView {
if let mapComponent = subview as? RCTMGLMapComponent {
if mapComponent.waitForStyleLoad() {
onStyleLoadedComponents.removeAll { $0 === mapComponent }
componentsToRefreshOnStyleChange.removeAll { $0 === mapComponent }
}
mapComponent.removeFromMap(self)
} else {
Expand Down Expand Up @@ -273,7 +277,21 @@ open class RCTMGLMapView : MapView {
self.mapView.gestures.options.pitchEnabled = value
}

func refreshComponentsBeforeStyleChange() {
componentsToRefreshOnStyleChange.forEach {
$0.removeFromMap(self)
}
}

func refreshComponentsAfterStyleChange(style: Style) {
componentsToRefreshOnStyleChange.forEach {
$0.addToMap(self, style: style)
}
}

@objc func setReactStyleURL(_ value: String?) {
var initialLoad = !self.styleLoaded
if !initialLoad { refreshComponentsBeforeStyleChange() }
self.styleLoaded = false
if let value = value {
if let _ = URL(string: value) {
Expand All @@ -283,6 +301,11 @@ open class RCTMGLMapView : MapView {
mapView.mapboxMap.loadStyleJSON(value)
}
}
if !initialLoad {
self.onNext(event: .styleLoaded) {_,_ in
self.refreshComponentsAfterStyleChange(style: self.mapboxMap.style)
}
}
}
let event = RCTMGLEvent(type:.willStartLoadingMap, payload: nil);
self.fireEvent(event: event, callback: self.reactOnMapChange)
Expand Down