From 77e65f501008bf3614e72cf09952f2755e03913d Mon Sep 17 00:00:00 2001 From: PW Date: Sun, 14 Apr 2024 13:59:04 +0200 Subject: [PATCH 1/3] allow changing of mapstyle after creation --- Sources/MapLibreSwiftUI/MapView.swift | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/Sources/MapLibreSwiftUI/MapView.swift b/Sources/MapLibreSwiftUI/MapView.swift index f3e7be9..30bf1cc 100644 --- a/Sources/MapLibreSwiftUI/MapView.swift +++ b/Sources/MapLibreSwiftUI/MapView.swift @@ -83,6 +83,13 @@ public struct MapView: UIViewRepresentable { public func updateUIView(_ mapView: MLNMapView, context: Context) { context.coordinator.parent = self + + switch styleSource { + case let .url(styleURL): + if styleURL != mapView.styleURL { + mapView.styleURL = styleURL + } + } applyModifiers(mapView, runUnsafe: true) From 1cb4ef01de84f6db1358a768afa93ed8b36fc6a7 Mon Sep 17 00:00:00 2001 From: PW Date: Sun, 14 Apr 2024 15:51:16 +0200 Subject: [PATCH 2/3] linting --- Sources/MapLibreSwiftUI/MapView.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Sources/MapLibreSwiftUI/MapView.swift b/Sources/MapLibreSwiftUI/MapView.swift index 30bf1cc..6b043b3 100644 --- a/Sources/MapLibreSwiftUI/MapView.swift +++ b/Sources/MapLibreSwiftUI/MapView.swift @@ -83,7 +83,7 @@ public struct MapView: UIViewRepresentable { public func updateUIView(_ mapView: MLNMapView, context: Context) { context.coordinator.parent = self - + switch styleSource { case let .url(styleURL): if styleURL != mapView.styleURL { From f6b8b09689e2b83cda4dcae70ac23dfeff5c37cd Mon Sep 17 00:00:00 2001 From: Ian Wagner Date: Tue, 16 Apr 2024 11:33:00 +0900 Subject: [PATCH 3/3] Improve robustness to future failure cases; ensure no unnecessary updates --- Sources/MapLibreSwiftUI/MapView.swift | 7 ------- Sources/MapLibreSwiftUI/MapViewCoordinator.swift | 7 ++++++- 2 files changed, 6 insertions(+), 8 deletions(-) diff --git a/Sources/MapLibreSwiftUI/MapView.swift b/Sources/MapLibreSwiftUI/MapView.swift index 6b043b3..f3e7be9 100644 --- a/Sources/MapLibreSwiftUI/MapView.swift +++ b/Sources/MapLibreSwiftUI/MapView.swift @@ -84,13 +84,6 @@ public struct MapView: UIViewRepresentable { public func updateUIView(_ mapView: MLNMapView, context: Context) { context.coordinator.parent = self - switch styleSource { - case let .url(styleURL): - if styleURL != mapView.styleURL { - mapView.styleURL = styleURL - } - } - applyModifiers(mapView, runUnsafe: true) // FIXME: This should be a more selective update diff --git a/Sources/MapLibreSwiftUI/MapViewCoordinator.swift b/Sources/MapLibreSwiftUI/MapViewCoordinator.swift index 5a23041..3a6f1b4 100644 --- a/Sources/MapLibreSwiftUI/MapViewCoordinator.swift +++ b/Sources/MapLibreSwiftUI/MapViewCoordinator.swift @@ -11,6 +11,7 @@ public class MapViewCoordinator: NSObject { // every update cycle so we can avoid unnecessary updates private var snapshotUserLayers: [StyleLayerDefinition] = [] private var snapshotCamera: MapViewCamera? + private var snapshotStyleSource: MapStyleSource? // Indicates whether we are currently in a push-down camera update cycle. // This is necessary in order to ensure we don't keep trying to reset a state value which we were already processing @@ -102,12 +103,16 @@ public class MapViewCoordinator: NSObject { // MARK: - Coordinator API - Styles + Layers @MainActor func updateStyleSource(_ source: MapStyleSource, mapView: MLNMapView) { - switch (source, parent.styleSource) { + switch (source, snapshotStyleSource) { case let (.url(newURL), .url(oldURL)): if newURL != oldURL { mapView.styleURL = newURL } + case let (.url(newURL), .none): + mapView.styleURL = newURL } + + snapshotStyleSource = source } @MainActor func updateLayers(mapView: MLNMapView) {