@mapsindoors/react-native-maps-indoors-mapbox 2.8.0 → 2.9.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (45) hide show
  1. package/README.md +84 -1
  2. package/android/build.gradle +1 -1
  3. package/android/src/main/java/com/mapsindoorsrn/MapsIndoorsPackage.java +10 -0
  4. package/android/src/main/java/com/mapsindoorsrn/core/BaseMapCacheProviders.java +94 -0
  5. package/android/src/main/java/com/mapsindoorsrn/core/DirectionsRendererModule.java +140 -49
  6. package/android/src/main/java/com/mapsindoorsrn/core/DirectionsServiceModule.java +16 -7
  7. package/android/src/main/java/com/mapsindoorsrn/core/MPDisplayRuleModule.java +1 -1
  8. package/android/src/main/java/com/mapsindoorsrn/core/MapControlModule.java +115 -20
  9. package/android/src/main/java/com/mapsindoorsrn/core/MapsIndoorsModule.java +200 -11
  10. package/android/src/main/java/com/mapsindoorsrn/core/RnGson.java +110 -0
  11. package/android/src/main/java/com/mapsindoorsrn/core/UtilsModule.java +1 -1
  12. package/android/src/main/java/com/mapsindoorsrn/core/models/DirectionsRendererOptionsModel.java +498 -0
  13. package/index.ts +25 -0
  14. package/ios/.swift-format +79 -0
  15. package/ios/core/DirectionsRendererBridge.m +11 -0
  16. package/ios/core/DirectionsRendererModule.swift +162 -1
  17. package/ios/core/DirectionsRendererOptions.swift +352 -0
  18. package/ios/core/MapsIndoorsBridge.m +14 -1
  19. package/ios/core/MapsIndoorsData.swift +1 -0
  20. package/ios/core/MapsIndoorsModule.swift +180 -21
  21. package/ios/core/UtilsModule.swift +57 -45
  22. package/package.json +1 -1
  23. package/react-native-maps-indoors-mapbox.podspec +2 -2
  24. package/specs/NativeDirectionsRendererModule.ts +3 -0
  25. package/specs/NativeMapsIndoorsModule.ts +6 -1
  26. package/src/core/EventNames.ts +3 -0
  27. package/src/core/MPDirectionsRenderer.ts +76 -1
  28. package/src/core/MPDirectionsRendererOptions.ts +287 -0
  29. package/src/core/MPError.ts +48 -1
  30. package/src/core/MPLanguageTag.ts +222 -0
  31. package/src/core/MPLegBoundaryIcons.ts +130 -0
  32. package/src/core/MPRouteAnimationType.ts +24 -0
  33. package/src/core/MPRouteArrowStyle.ts +28 -0
  34. package/src/core/MPRouteMarkerDisplayRule.ts +140 -0
  35. package/src/core/MPRouteStampType.ts +24 -0
  36. package/src/core/MPSolution.ts +33 -2
  37. package/src/core/MPStrokeStyle.ts +22 -0
  38. package/src/core/MapsIndoors.ts +155 -11
  39. package/src/core/OnBaseMapCacheProgressListener.ts +20 -0
  40. package/src/core/__tests__/MPBounds.test.ts +109 -0
  41. package/src/core/__tests__/MPFilter.test.ts +76 -0
  42. package/src/core/__tests__/MPLanguageTag.test.ts +168 -0
  43. package/src/core/__tests__/MPPoint.test.ts +82 -0
  44. package/src/core/__tests__/MPSolution.test.ts +107 -0
  45. package/src/core/__tests__/MapsIndoors.language.test.ts +180 -0
@@ -61,4 +61,15 @@
61
61
  backgroundString:(NSString *) backgroundString
62
62
  resolver:(RCTPromiseResolveBlock) resolve
63
63
  rejecter:(RCTPromiseRejectBlock) reject)
64
+
65
+ RCT_EXTERN_METHOD(setOptions: (NSString *) optionsString
66
+ resolver:(RCTPromiseResolveBlock) resolve
67
+ rejecter:(RCTPromiseRejectBlock) reject)
68
+
69
+ RCT_EXTERN_METHOD(getOptions: (RCTPromiseResolveBlock) resolve
70
+ rejecter:(RCTPromiseRejectBlock) reject)
71
+
72
+ RCT_EXTERN_METHOD(finishGuidance: (nonnull NSNumber *) usagePercentage
73
+ resolver:(RCTPromiseResolveBlock) resolve
74
+ rejecter:(RCTPromiseRejectBlock) reject)
64
75
  @end
@@ -14,6 +14,49 @@ import React
14
14
  public class DirectionsRendererModule: RCTEventEmitter {
15
15
  private var isListeningForLegChanges: Bool = false
16
16
  private var animationDuration: NSNumber = 5
17
+ /// The custom stamp URL last applied through ``setOptions``.
18
+ ///
19
+ /// The SDK stores the downloaded `UIImage`, not its URL, so ``getOptions`` cannot recover it from
20
+ /// the renderer. Without it a read-modify-write of the options would feed back a `custom` stamp
21
+ /// type with no image and silently drop the stamp.
22
+ ///
23
+ /// Only ever touched on the main actor - written at the end of ``setOptions``, read in
24
+ /// ``getOptions`` - since React Native invokes this module off the main queue
25
+ /// (``requiresMainQueueSetup()`` is `false`) and both would otherwise race.
26
+ @MainActor private var appliedStampImageUrl: String?
27
+
28
+ /// Guards ``setOptionsGeneration``, which is taken on React Native's method queue and read on
29
+ /// the main actor.
30
+ private let setOptionsGenerationLock = NSLock()
31
+
32
+ /// Identifies the most recent ``setOptions`` call.
33
+ ///
34
+ /// A custom stamp has to be downloaded before the options can be applied, so two calls can finish
35
+ /// out of order and a slow download would otherwise overwrite a newer call's result. Each call
36
+ /// takes a token and applies its result only while it is still the newest, making last-call-wins
37
+ /// hold regardless of how long each download took.
38
+ ///
39
+ /// The token has to be taken *synchronously*, before the call's `Task` is created. React Native
40
+ /// serialises this module's methods on one queue, so taking it there makes the token follow call
41
+ /// order. Taking it inside the `Task` would instead number the calls by whichever one the
42
+ /// cooperative pool happened to schedule first, which is not the order they were made in - the
43
+ /// newer call could take the lower token and then be discarded in favour of the older one.
44
+ private var setOptionsGeneration: UInt64 = 0
45
+
46
+ /// Takes the next token, in call order.
47
+ private func nextSetOptionsGeneration() -> UInt64 {
48
+ setOptionsGenerationLock.lock()
49
+ defer { setOptionsGenerationLock.unlock() }
50
+ setOptionsGeneration &+= 1
51
+ return setOptionsGeneration
52
+ }
53
+
54
+ /// Whether `generation` is still the newest token, i.e. no later ``setOptions`` call has started.
55
+ private func isCurrentSetOptionsGeneration(_ generation: UInt64) -> Bool {
56
+ setOptionsGenerationLock.lock()
57
+ defer { setOptionsGenerationLock.unlock() }
58
+ return generation == setOptionsGeneration
59
+ }
17
60
 
18
61
  @objc public override static func requiresMainQueueSetup() -> Bool { return false }
19
62
 
@@ -269,6 +312,96 @@ public class DirectionsRendererModule: RCTEventEmitter {
269
312
  }
270
313
  }
271
314
 
315
+ @objc public func setOptions(_ optionsString: String, resolver resolve: @escaping RCTPromiseResolveBlock, rejecter reject: @escaping RCTPromiseRejectBlock) {
316
+ if MapsIndoorsData.sharedInstance.directionsRenderer == nil {
317
+ MapsIndoorsData.sharedInstance.directionsRenderer = MapsIndoorsData.sharedInstance.mapView?.getMapControl()?.newDirectionsRenderer()
318
+ }
319
+
320
+ let directionsRenderer = MapsIndoorsData.sharedInstance.directionsRenderer
321
+
322
+ guard let directionsRenderer else {
323
+ return doReject(reject, message: "directions renderer null. MapControl needs to have been instantiated first")
324
+ }
325
+
326
+ guard let options: DirectionsRendererOptions = try? fromJSON(optionsString) else {
327
+ return doReject(reject, message: "Options could not be parsed")
328
+ }
329
+
330
+ // Taken here rather than inside the Task, so it follows the order the calls were made in.
331
+ let generation = nextSetOptionsGeneration()
332
+
333
+ Task {
334
+ // The SDK takes the custom stamp as an image, so it has to be fetched before applying.
335
+ // Immutable, because it is captured by the main-actor block below - a captured `var`
336
+ // is a concurrency error in the Swift 6 language mode.
337
+ let stampImage = await self.loadStampImage(urlString: options.stampImageUrl)
338
+
339
+ let mpOptions = options.toMPDirectionsRendererOptions(stampImage: stampImage)
340
+
341
+ let applied = await MainActor.run { () -> Bool in
342
+ // A later call has already started, so this one is stale - its download simply took
343
+ // longer. Dropping it keeps last-call-wins.
344
+ guard self.isCurrentSetOptionsGeneration(generation) else { return false }
345
+
346
+ directionsRenderer.options = mpOptions
347
+ // Only remember a URL that produced an image, so a failed download reads back as
348
+ // "no custom stamp" rather than as one the renderer is not actually drawing.
349
+ self.appliedStampImageUrl = stampImage != nil ? options.stampImageUrl : nil
350
+ return true
351
+ }
352
+
353
+ if !applied {
354
+ print("MapsIndoors: setOptions was superseded by a later call, so its options were not applied")
355
+ }
356
+
357
+ // Resolved either way, including when superseded. The caller's own later call is what
358
+ // replaced this one, and that call's options are what is in force, so this is not a
359
+ // failure to report - and rejecting would make an ordinary rapid sequence of setOptions
360
+ // calls look broken. Leaving the promise unsettled would be worse still.
361
+ resolve(nil)
362
+ }
363
+ }
364
+
365
+ @objc public func getOptions(_ resolve: @escaping RCTPromiseResolveBlock, rejecter reject: @escaping RCTPromiseRejectBlock) {
366
+ if MapsIndoorsData.sharedInstance.directionsRenderer == nil {
367
+ MapsIndoorsData.sharedInstance.directionsRenderer = MapsIndoorsData.sharedInstance.mapView?.getMapControl()?.newDirectionsRenderer()
368
+ }
369
+
370
+ let directionsRenderer = MapsIndoorsData.sharedInstance.directionsRenderer
371
+
372
+ guard let directionsRenderer else {
373
+ return doReject(reject, message: "directions renderer null. MapControl needs to have been instantiated first")
374
+ }
375
+
376
+ // Both reads have to happen on the main actor: `appliedStampImageUrl` is written there by
377
+ // setOptions, and the SDK's own options getter resolves against renderer state that is
378
+ // main-thread affine. React Native calls this module off the main queue.
379
+ Task { @MainActor in
380
+ resolve(toJSON(DirectionsRendererOptions(from: directionsRenderer.options, stampImageUrl: self.appliedStampImageUrl)))
381
+ }
382
+ }
383
+
384
+ @objc public func finishGuidance(_ usagePercentage: NSNumber, resolver resolve: @escaping RCTPromiseResolveBlock, rejecter reject: @escaping RCTPromiseRejectBlock) {
385
+ let directionsRenderer = MapsIndoorsData.sharedInstance.directionsRenderer
386
+
387
+ guard let directionsRenderer else {
388
+ return doReject(reject, message: "directions renderer null. MapControl needs to have been instantiated first")
389
+ }
390
+
391
+ // Resolve only once the work has run, matching setOptions - resolving first would report
392
+ // success before the SDK had been told anything.
393
+ Task { @MainActor in
394
+ // A negative value means the app did not supply a figure, so the SDK derives it from
395
+ // the route's own progress.
396
+ if usagePercentage.doubleValue < 0 {
397
+ directionsRenderer.finishGuidance()
398
+ } else {
399
+ directionsRenderer.finishGuidance(usagePercentage: usagePercentage.doubleValue)
400
+ }
401
+ resolve(nil)
402
+ }
403
+ }
404
+
272
405
  @objc public func setOnLegSelectedListener(_ resolve: RCTPromiseResolveBlock, rejecter reject: RCTPromiseRejectBlock) {
273
406
  isListeningForLegChanges = true
274
407
  return resolve(nil)
@@ -365,9 +498,37 @@ public class DirectionsRendererModule: RCTEventEmitter {
365
498
  }
366
499
  }
367
500
 
501
+ /// Fetches the custom stamp icon, or nil when there is none, the URL is malformed, or the
502
+ /// download fails.
503
+ ///
504
+ /// A failure is logged rather than rejected: the rest of the options still apply, so failing the
505
+ /// whole call would misreport it - but a silently missing stamp is otherwise undiagnosable.
506
+ private func loadStampImage(urlString: String?) async -> UIImage? {
507
+ guard let urlString else { return nil }
508
+ guard let url = URL(string: urlString) else {
509
+ print("MapsIndoors: route stamp image URL is malformed: \(urlString)")
510
+ return nil
511
+ }
512
+ do {
513
+ return try await downloadImage(from: url)
514
+ } catch {
515
+ print("MapsIndoors: could not load the route stamp image at \(urlString): \(error)")
516
+ return nil
517
+ }
518
+ }
519
+
520
+ enum ImageDownloadError: Error {
521
+ /// The response body was fetched but is not decodable as an image - what a 404 or an error
522
+ /// page produces. Force unwrapping here would trap instead, which no caller can catch.
523
+ case notAnImage(URL)
524
+ }
525
+
368
526
  func downloadImage(from url: URL) async throws -> UIImage {
369
527
  let (data, _) = try await URLSession.shared.data(from: url)
370
- return UIImage(data: data)!
528
+ guard let image = UIImage(data: data) else {
529
+ throw ImageDownloadError.notAnImage(url)
530
+ }
531
+ return image
371
532
  }
372
533
 
373
534
  func isValidUrl(_ urlString: String) -> Bool {
@@ -0,0 +1,352 @@
1
+ //
2
+ // DirectionsRendererOptions.swift
3
+ // react-native-maps-indoors
4
+ //
5
+
6
+ import Foundation
7
+ import MapsIndoors
8
+ import UIKit
9
+
10
+ /// The flat directions renderer options as they cross the React Native bridge.
11
+ ///
12
+ /// Mirrors ``MPDirectionsRendererOptions``, with colors as hex strings, icons as URL strings and
13
+ /// enums as their raw wire values. Every field is optional, so an option the app left out stays
14
+ /// `nil` and is inherited from the solution config, or from the SDK's built-in default.
15
+ struct DirectionsRendererOptions: Codable {
16
+ var strokeColor: String?
17
+ var strokeOpacity: Double?
18
+ var strokeWeight: Double?
19
+ var strokeStyle: String?
20
+ var backgroundColorEnabled: Bool?
21
+ var backgroundColor: String?
22
+ var backgroundColorOpacity: Double?
23
+ var backgroundColorWeight: Double?
24
+ var animationType: String?
25
+ var animationSpeed: Double?
26
+ var animationMinDuration: Double?
27
+ var animationRepeating: Bool?
28
+ var forceAnimation: Bool?
29
+ var animatedOverlayColor: String?
30
+ var animatedOverlayOpacity: Double?
31
+ var animatedOverlayWeight: Double?
32
+ var stampType: String?
33
+ var stampImageUrl: String?
34
+ var stampSpacing: Double?
35
+ var stampScale: Double?
36
+ var arrowStyle: String?
37
+ var stampColor: String?
38
+ var startDisplayRule: RouteMarkerDisplayRule?
39
+ var endDisplayRule: RouteMarkerDisplayRule?
40
+ var legBoundaryIcons: LegBoundaryIcons?
41
+ var elevated: Bool?
42
+ var elevationHeight: Double?
43
+ var fitBoundsMaxZoom: Double?
44
+
45
+ /// The styling of a route's origin or destination marker.
46
+ struct RouteMarkerDisplayRule: Codable {
47
+ var iconUrl: String?
48
+ var iconVisible: Bool?
49
+ var iconSize: IconSize?
50
+ var label: String?
51
+ var labelVisible: Bool?
52
+ var labelTextSize: Double?
53
+ var labelTextColor: String?
54
+ var labelHaloColor: String?
55
+ var labelHaloWidth: Double?
56
+ var zoomFrom: Double?
57
+ var zoomTo: Double?
58
+
59
+ func toMPRouteMarkerDisplayRule() -> MPRouteMarkerDisplayRule {
60
+ let rule = MPRouteMarkerDisplayRule()
61
+ rule.iconUrl = iconUrl.flatMap { URL(string: $0) }
62
+ rule.iconVisible = iconVisible.map { NSNumber(value: $0) }
63
+ rule.iconSize = iconSize.flatMap { $0.toCGSize() }
64
+ rule.label = label
65
+ rule.labelVisible = labelVisible.map { NSNumber(value: $0) }
66
+ rule.labelTextSize = labelTextSize.map { NSNumber(value: $0) }
67
+ rule.labelTextColor = labelTextColor.flatMap { UIColor(hex: $0) }
68
+ rule.labelHaloColor = labelHaloColor.flatMap { UIColor(hex: $0) }
69
+ rule.labelHaloWidth = labelHaloWidth.map { NSNumber(value: $0) }
70
+ rule.zoomFrom = zoomFrom.map { NSNumber(value: $0) }
71
+ rule.zoomTo = zoomTo.map { NSNumber(value: $0) }
72
+ return rule
73
+ }
74
+
75
+ init() {}
76
+
77
+ init(from rule: MPRouteMarkerDisplayRule) {
78
+ iconUrl = rule.iconUrl?.absoluteString
79
+ iconVisible = rule.iconVisible?.boolValue
80
+ iconSize = rule.iconSize.map { IconSize(from: $0) }
81
+ label = rule.label
82
+ labelVisible = rule.labelVisible?.boolValue
83
+ labelTextSize = rule.labelTextSize?.doubleValue
84
+ labelTextColor = rule.labelTextColor.flatMap { $0.hexString }
85
+ labelHaloColor = rule.labelHaloColor.flatMap { $0.hexString }
86
+ labelHaloWidth = rule.labelHaloWidth?.doubleValue
87
+ zoomFrom = rule.zoomFrom?.doubleValue
88
+ zoomTo = rule.zoomTo?.doubleValue
89
+ }
90
+ }
91
+
92
+ /// Explicit icon dimensions in points.
93
+ struct IconSize: Codable {
94
+ var width: Double?
95
+ var height: Double?
96
+
97
+ /// The size, or `nil` when either dimension is missing.
98
+ ///
99
+ /// ``MPRouteMarkerDisplayRule/iconSize`` is a whole `CGSize`, so a half-specified size cannot
100
+ /// be expressed. Substituting `0` for the missing half would hide the marker; leaving the whole
101
+ /// size unset keeps it inherited, which is what an unspecified dimension means on Android too.
102
+ func toCGSize() -> CGSize? {
103
+ guard let width, let height else { return nil }
104
+ return CGSize(width: width, height: height)
105
+ }
106
+
107
+ init(from size: CGSize) {
108
+ width = size.width
109
+ height = size.height
110
+ }
111
+ }
112
+
113
+ /// Per connector type leg boundary icons.
114
+ struct LegBoundaryIcons: Codable {
115
+ var defaultIcon: String?
116
+ var elevator: String?
117
+ var escalator: String?
118
+ var stairs: String?
119
+ var ramp: String?
120
+ var wheelchairRamp: String?
121
+ var wheelchairLift: String?
122
+ var ladder: String?
123
+ var entry: String?
124
+ var scale: Double?
125
+
126
+ func toMPLegBoundaryIcons() -> MPLegBoundaryIcons {
127
+ let icons = MPLegBoundaryIcons()
128
+ icons.defaultIcon = defaultIcon.flatMap { URL(string: $0) }
129
+ icons.elevator = elevator.flatMap { URL(string: $0) }
130
+ icons.escalator = escalator.flatMap { URL(string: $0) }
131
+ icons.stairs = stairs.flatMap { URL(string: $0) }
132
+ icons.ramp = ramp.flatMap { URL(string: $0) }
133
+ icons.wheelchairRamp = wheelchairRamp.flatMap { URL(string: $0) }
134
+ icons.wheelchairLift = wheelchairLift.flatMap { URL(string: $0) }
135
+ icons.ladder = ladder.flatMap { URL(string: $0) }
136
+ icons.entry = entry.flatMap { URL(string: $0) }
137
+ if let scale {
138
+ icons.scale = scale
139
+ }
140
+ return icons
141
+ }
142
+
143
+ init() {}
144
+
145
+ init(from icons: MPLegBoundaryIcons) {
146
+ defaultIcon = icons.defaultIcon?.absoluteString
147
+ elevator = icons.elevator?.absoluteString
148
+ escalator = icons.escalator?.absoluteString
149
+ stairs = icons.stairs?.absoluteString
150
+ ramp = icons.ramp?.absoluteString
151
+ wheelchairRamp = icons.wheelchairRamp?.absoluteString
152
+ wheelchairLift = icons.wheelchairLift?.absoluteString
153
+ ladder = icons.ladder?.absoluteString
154
+ entry = icons.entry?.absoluteString
155
+ scale = icons.scale
156
+ }
157
+ }
158
+
159
+ /// Builds the SDK's options object.
160
+ ///
161
+ /// Every unset property is left `nil`, which is what makes an option the app omitted *inherited*
162
+ /// rather than reset: the iOS SDK resolves each property independently as app-set -> CMS
163
+ /// solution default -> built-in default, so assigning this whole object overrides only the
164
+ /// properties it actually sets. The Android module has to reconstruct that resolution by hand,
165
+ /// because its SDK merges a runtime config over the solution config whole-block.
166
+ ///
167
+ /// - Parameter stampImage: the already downloaded icon for a custom stamp, since
168
+ /// ``MPDirectionsRendererOptions/stampImage`` takes a `UIImage` rather than a URL.
169
+ func toMPDirectionsRendererOptions(stampImage: UIImage? = nil) -> MPDirectionsRendererOptions {
170
+ let options = MPDirectionsRendererOptions()
171
+ options.strokeColor = strokeColor.flatMap { UIColor(hex: $0) }
172
+ options.strokeOpacity = strokeOpacity.map { NSNumber(value: $0) }
173
+ options.strokeWeight = strokeWeight.map { NSNumber(value: $0) }
174
+ options.strokeStyle = strokeStyle.flatMap { DirectionsRendererOptions.strokeStyle(from: $0) }
175
+ options.backgroundColorEnabled = backgroundColorEnabled.map { NSNumber(value: $0) }
176
+ options.backgroundColor = backgroundColor.flatMap { UIColor(hex: $0) }
177
+ options.backgroundColorOpacity = backgroundColorOpacity.map { NSNumber(value: $0) }
178
+ options.backgroundColorWeight = backgroundColorWeight.map { NSNumber(value: $0) }
179
+ options.animationType = animationType.flatMap { DirectionsRendererOptions.animationType(from: $0) }
180
+ options.animationSpeed = animationSpeed.map { NSNumber(value: $0) }
181
+ options.animationMinDuration = animationMinDuration.map { NSNumber(value: $0) }
182
+ if let animationRepeating {
183
+ options.animationRepeating = animationRepeating
184
+ }
185
+ options.forceAnimation = forceAnimation.map { NSNumber(value: $0) }
186
+ options.animatedOverlayColor = animatedOverlayColor.flatMap { UIColor(hex: $0) }
187
+ options.animatedOverlayOpacity = animatedOverlayOpacity.map { NSNumber(value: $0) }
188
+ options.animatedOverlayWeight = animatedOverlayWeight.map { NSNumber(value: $0) }
189
+ options.stampType = stampType.flatMap { DirectionsRendererOptions.stampType(from: $0) }
190
+ options.stampImage = stampImage
191
+ options.stampSpacing = stampSpacing.map { NSNumber(value: $0) }
192
+ options.stampScale = stampScale.map { NSNumber(value: $0) }
193
+ options.arrowStyle = arrowStyle.flatMap { DirectionsRendererOptions.arrowStyle(from: $0) }
194
+ options.stampColor = stampColor.flatMap { UIColor(hex: $0) }
195
+ options.startDisplayRule = startDisplayRule?.toMPRouteMarkerDisplayRule()
196
+ options.endDisplayRule = endDisplayRule?.toMPRouteMarkerDisplayRule()
197
+ options.legBoundaryIcons = legBoundaryIcons?.toMPLegBoundaryIcons()
198
+ options.elevated = elevated.map { NSNumber(value: $0) }
199
+ options.elevationHeight = elevationHeight.map { NSNumber(value: $0) }
200
+ options.fitBoundsMaxZoom = fitBoundsMaxZoom.map { NSNumber(value: $0) }
201
+ return options
202
+ }
203
+
204
+ init() {}
205
+
206
+ /// Flattens the options the renderer currently holds back into the bridge's shape.
207
+ ///
208
+ /// - Parameter stampImageUrl: the URL of the custom stamp last applied through this bridge. The SDK
209
+ /// holds the downloaded `UIImage` rather than its URL, so it cannot be recovered from `options` -
210
+ /// without it a read-modify-write would feed back `stampType == .custom` with no image and silently
211
+ /// drop the stamp. When the URL is unknown, ``stampType`` is reported as `nil` instead, so the pair
212
+ /// stays self-consistent and the stamp is inherited rather than blanked.
213
+ init(from options: MPDirectionsRendererOptions, stampImageUrl: String? = nil) {
214
+ strokeColor = options.strokeColor.flatMap { $0.hexString }
215
+ strokeOpacity = options.strokeOpacity?.doubleValue
216
+ strokeWeight = options.strokeWeight?.doubleValue
217
+ strokeStyle = options.strokeStyle.map { DirectionsRendererOptions.string(from: $0) }
218
+ backgroundColorEnabled = options.backgroundColorEnabled?.boolValue
219
+ backgroundColor = options.backgroundColor.flatMap { $0.hexString }
220
+ backgroundColorOpacity = options.backgroundColorOpacity?.doubleValue
221
+ backgroundColorWeight = options.backgroundColorWeight?.doubleValue
222
+ animationType = options.animationType.map { DirectionsRendererOptions.string(from: $0) }
223
+ animationSpeed = options.animationSpeed?.doubleValue
224
+ animationMinDuration = options.animationMinDuration?.doubleValue
225
+ animationRepeating = options.animationRepeating
226
+ forceAnimation = options.forceAnimation?.boolValue
227
+ animatedOverlayColor = options.animatedOverlayColor.flatMap { $0.hexString }
228
+ animatedOverlayOpacity = options.animatedOverlayOpacity?.doubleValue
229
+ animatedOverlayWeight = options.animatedOverlayWeight?.doubleValue
230
+ self.stampImageUrl = stampImageUrl
231
+ let resolvedStampType = options.stampType.map { DirectionsRendererOptions.string(from: $0) }
232
+ stampType = resolvedStampType == "custom" && stampImageUrl == nil ? nil : resolvedStampType
233
+ stampSpacing = options.stampSpacing?.doubleValue
234
+ stampScale = options.stampScale?.doubleValue
235
+ arrowStyle = options.arrowStyle.map { DirectionsRendererOptions.string(from: $0) }
236
+ stampColor = options.stampColor.flatMap { $0.hexString }
237
+ startDisplayRule = options.startDisplayRule.map { RouteMarkerDisplayRule(from: $0) }
238
+ endDisplayRule = options.endDisplayRule.map { RouteMarkerDisplayRule(from: $0) }
239
+ legBoundaryIcons = options.legBoundaryIcons.map { LegBoundaryIcons(from: $0) }
240
+ elevated = options.elevated?.boolValue
241
+ elevationHeight = options.elevationHeight?.doubleValue
242
+ fitBoundsMaxZoom = options.fitBoundsMaxZoom?.doubleValue
243
+ }
244
+
245
+ // MARK: - Enum mapping
246
+
247
+ private static func strokeStyle(from value: String) -> MPStrokeStyle? {
248
+ switch value {
249
+ case "solid": return .solid
250
+ case "dashed": return .dashed
251
+ case "dotted": return .dotted
252
+ default: return nil
253
+ }
254
+ }
255
+
256
+ private static func string(from value: MPStrokeStyle) -> String {
257
+ switch value {
258
+ case .solid: return "solid"
259
+ case .dashed: return "dashed"
260
+ case .dotted: return "dotted"
261
+ @unknown default: return "solid"
262
+ }
263
+ }
264
+
265
+ private static func animationType(from value: String) -> MPRouteAnimationType? {
266
+ switch value {
267
+ case "none": return MPRouteAnimationType.none
268
+ case "flow": return .flow
269
+ case "pulse": return .pulse
270
+ case "comet": return .comet
271
+ default: return nil
272
+ }
273
+ }
274
+
275
+ private static func string(from value: MPRouteAnimationType) -> String {
276
+ switch value {
277
+ case .none: return "none"
278
+ case .flow: return "flow"
279
+ case .pulse: return "pulse"
280
+ case .comet: return "comet"
281
+ @unknown default: return "flow"
282
+ }
283
+ }
284
+
285
+ private static func stampType(from value: String) -> MPRouteStampType? {
286
+ switch value {
287
+ case "none": return MPRouteStampType.none
288
+ case "arrow": return .arrow
289
+ case "custom": return .custom
290
+ default: return nil
291
+ }
292
+ }
293
+
294
+ private static func string(from value: MPRouteStampType) -> String {
295
+ switch value {
296
+ case .none: return "none"
297
+ case .arrow: return "arrow"
298
+ case .custom: return "custom"
299
+ @unknown default: return "none"
300
+ }
301
+ }
302
+
303
+ private static func arrowStyle(from value: String) -> MPRouteArrowStyle? {
304
+ switch value {
305
+ case "chevron": return .chevron
306
+ case "chevronDouble": return .chevronDouble
307
+ case "triangle": return .triangle
308
+ case "arrow": return .arrow
309
+ case "dart": return .dart
310
+ default: return nil
311
+ }
312
+ }
313
+
314
+ private static func string(from value: MPRouteArrowStyle) -> String {
315
+ switch value {
316
+ case .chevron: return "chevron"
317
+ case .chevronDouble: return "chevronDouble"
318
+ case .triangle: return "triangle"
319
+ case .arrow: return "arrow"
320
+ case .dart: return "dart"
321
+ @unknown default: return "chevron"
322
+ }
323
+ }
324
+ }
325
+
326
+ extension UIColor {
327
+ /// The color as an `#RRGGBB` hex string, or `nil` when it has no describable components.
328
+ ///
329
+ /// The bridge keeps colors opaque and carries transparency in the separate opacity options, so
330
+ /// that a color reads back the same on both platforms - the two SDKs disagree on where the
331
+ /// alpha component of an 8 digit hex string sits.
332
+ var hexString: String? {
333
+ let component = { (value: CGFloat) in Int((max(0, min(1, value)) * 255).rounded()) }
334
+
335
+ var red: CGFloat = 0, green: CGFloat = 0, blue: CGFloat = 0, alpha: CGFloat = 0
336
+ if getRed(&red, green: &green, blue: &blue, alpha: &alpha) {
337
+ return String(format: "#%02X%02X%02X", component(red), component(green), component(blue))
338
+ }
339
+
340
+ // A monochrome colour has no RGB components to report, so ask for its white level and
341
+ // describe it as the equivalent grey rather than failing.
342
+ var white: CGFloat = 0
343
+ if getWhite(&white, alpha: &alpha) {
344
+ let level = component(white)
345
+ return String(format: "#%02X%02X%02X", level, level, level)
346
+ }
347
+
348
+ // A pattern colour, or anything else with no numeric components: report it as absent rather
349
+ // than inventing black, which would read back as a colour the app never set.
350
+ return nil
351
+ }
352
+ }
@@ -1,6 +1,7 @@
1
1
  #import <React/RCTBridgeModule.h>
2
+ #import <React/RCTEventEmitter.h>
2
3
 
3
- @interface RCT_EXTERN_MODULE(MapsIndoorsModule, NSObject)
4
+ @interface RCT_EXTERN_MODULE(MapsIndoorsModule, RCTEventEmitter)
4
5
 
5
6
  RCT_EXTERN_METHOD(loadMapsIndoors:(NSString *)apiKey
6
7
  optionalStrings:(NSArray<NSString *> _Nullable)optionalStrings
@@ -125,4 +126,16 @@
125
126
  resolver:(RCTPromiseResolveBlock) resolve
126
127
  rejecter:(RCTPromiseRejectBlock) reject)
127
128
 
129
+ RCT_EXTERN_METHOD(isBaseMapCachingSupported:(RCTPromiseResolveBlock)resolve
130
+ rejecter:(RCTPromiseRejectBlock)reject)
131
+
132
+ RCT_EXTERN_METHOD(setBaseMapTilesEnabled:(BOOL)enabled
133
+ apiKey:(NSString *)apiKey
134
+ resolver:(RCTPromiseResolveBlock)resolve
135
+ rejecter:(RCTPromiseRejectBlock)reject)
136
+
137
+ RCT_EXTERN_METHOD(synchronizeBaseMapTiles:(NSArray<NSString *> _Nullable)apiKeys
138
+ resolver:(RCTPromiseResolveBlock)resolve
139
+ rejecter:(RCTPromiseRejectBlock)reject)
140
+
128
141
  @end
@@ -33,6 +33,7 @@ class MapsIndoorsData {
33
33
  case onInfoWindowClick
34
34
  case onLiveLocationUpdate
35
35
  case onLegSelected
36
+ case onBaseMapCacheProgress
36
37
  }
37
38
 
38
39
  lazy var allEvents: [String] = {