@rnmapbox/maps 10.0.0-rc.10 → 10.0.0-rc.11

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.
@@ -41,7 +41,7 @@ enum class TileRegionPackState(val rawValue: String) {
41
41
  COMPLETE("complete"),
42
42
  UNKNOWN("unkown")
43
43
  }
44
- class TileRegionPack(var name: String, var progress: TileRegionLoadProgress?, var state: TileRegionPackState, var metadata: JSONObject) {
44
+ class TileRegionPack(var name: String, var state: TileRegionPackState = TileRegionPackState.UNKNOWN, var progress: TileRegionLoadProgress? = null, var metadata: JSONObject) {
45
45
  var cancelable: Cancelable? = null
46
46
  var loadOptions: TileRegionLoadOptions? = null
47
47
 
@@ -78,7 +78,7 @@ class TileRegionPack(var name: String, var progress: TileRegionLoadProgress?, va
78
78
  bounds: Geometry,
79
79
  zoomRange: ZoomRange,
80
80
  metadata: JSONObject
81
- ) : this(name, null, state, metadata) {
81
+ ) : this(name= name, state= state,progress= null, metadata= metadata) {
82
82
  val rnmeta = JSONObject()
83
83
  rnmeta.put("styleURI", styleURI)
84
84
  this.styleURI = styleURI
@@ -163,12 +163,23 @@ class RCTMGLOfflineModule(private val mReactContext: ReactApplicationContext) :
163
163
  @ReactMethod
164
164
  fun getPackStatus(name: String, promise: Promise) {
165
165
  val pack = tileRegionPacks[name]
166
- if (pack != null) {
167
- promise.resolve(makeRegionStatus(name, pack.progress, pack))
168
- } else {
169
- promise.reject(Error("Pack not found"))
170
- Logger.w(REACT_CLASS, "getPackStatus - Unknown offline region")
166
+ if (pack == null) {
167
+ promise.reject(Error("Pack: $name not found"))
168
+ return
171
169
  }
170
+ tileStore.getTileRegionMetadata(name) { expected ->
171
+ expected.value?.also {
172
+ val pack = TileRegionPack(
173
+ name= name,
174
+ metadata= it.toJSONObject() ?: JSONObject()
175
+ )
176
+ tileRegionPacks[name] = pack
177
+ promise.resolve(_makeRegionStatusPayload(pack))
178
+ } ?: run {
179
+ promise.reject(LOG_TAG, expected.error!!.message)
180
+ }
181
+ }
182
+
172
183
  }
173
184
 
174
185
  @ReactMethod
@@ -246,7 +257,6 @@ class RCTMGLOfflineModule(private val mReactContext: ReactApplicationContext) :
246
257
  }
247
258
  })
248
259
  }
249
-
250
260
  // endregion
251
261
 
252
262
  fun startLoading(pack: TileRegionPack) {
@@ -368,7 +378,7 @@ class RCTMGLOfflineModule(private val mReactContext: ReactApplicationContext) :
368
378
  metadata= metadataJSON ?: JSONObject()
369
379
  )
370
380
 
371
- if (region.completedResourceCount == region.requiredResourceCount) {
381
+ if (region.hasCompleted()) {
372
382
  pack.state = TileRegionPackState.COMPLETE
373
383
  }
374
384
  tileRegionPacks[region.id] = pack
@@ -404,8 +414,7 @@ class RCTMGLOfflineModule(private val mReactContext: ReactApplicationContext) :
404
414
  );
405
415
 
406
416
  if (region.requiredResourceCount > 0) {
407
- val percentage = 100.0 * region.completedResourceCount.toDouble() / region.requiredResourceCount.toDouble()
408
- result.putDouble("percentage", percentage)
417
+ result.putDouble("percentage", region.toPercentage())
409
418
  } else {
410
419
  result.putNull("percentage")
411
420
  }
@@ -429,14 +438,17 @@ class RCTMGLOfflineModule(private val mReactContext: ReactApplicationContext) :
429
438
  )
430
439
  }
431
440
 
432
- private fun _makeRegionStatusPayload(name:String, progress: TileRegionLoadProgress?,state: TileRegionPackState, metadata: JSONObject?) : WritableMap {
441
+ private fun _makeRegionStatusPayload(pack: TileRegionPack): WritableMap {
442
+ return _makeRegionStatusPayload(pack.name, pack.progress, pack.state, pack.metadata)
443
+ }
444
+
445
+ private fun _makeRegionStatusPayload(name:String, progress: TileRegionLoadProgress?,state: TileRegionPackState, metadata: JSONObject?): WritableMap {
433
446
  var result = Arguments.createMap()
434
447
  if (progress != null) {
435
- val progressPercentage = progress.completedResourceCount.toDouble() / progress.requiredResourceCount.toDouble()
436
448
  result = writableMapOf(
437
- "state" to (if (progress.completedResourceCount == progress.requiredResourceCount) TileRegionPackState.COMPLETE.rawValue else state.rawValue),
449
+ "state" to (if (progress.hasCompleted()) TileRegionPackState.COMPLETE.rawValue else state.rawValue),
438
450
  "name" to name,
439
- "perentage" to progressPercentage * 100.0,
451
+ "percentage" to progress.toPercentage(),
440
452
  "completedResourceCount" to progress.completedResourceCount,
441
453
  "completedResourceSize" to progress.completedResourceSize,
442
454
  "erroredResourceCount" to progress.erroredResourceCount,
@@ -448,7 +460,7 @@ class RCTMGLOfflineModule(private val mReactContext: ReactApplicationContext) :
448
460
  result = writableMapOf(
449
461
  "state" to state.rawValue,
450
462
  "name" to name,
451
- "perentage" to null,
463
+ "percentage" to null,
452
464
  )
453
465
  }
454
466
  if (metadata != null) {
@@ -456,16 +468,10 @@ class RCTMGLOfflineModule(private val mReactContext: ReactApplicationContext) :
456
468
  }
457
469
  return result
458
470
  }
459
- private fun makeProgresEvent(name: String, progress: TileRegionLoadProgress, state: TileRegionPackState): OfflineEvent {
460
- return OfflineEvent(
461
- OFFLINE_PROGRESS,
462
- EventTypes.OFFLINE_STATUS,
463
- _makeRegionStatusPayload(name, progress, state, null)
464
- )
465
- }
471
+
466
472
  private fun offlinePackProgressDidChange(progress: TileRegionLoadProgress, metadata: JSONObject, state: TileRegionPackState) {
467
473
  // TODO throttle
468
- sendEvent(this.makeProgresEvent(metadata.getString("name"), progress, state))
474
+ sendEvent(this.makeProgressEvent(metadata.getString("name"), progress, state))
469
475
  }
470
476
 
471
477
  private fun offlinePackDidReceiveError(name: String, error: TileRegionError) {
@@ -505,36 +511,6 @@ class RCTMGLOfflineModule(private val mReactContext: ReactApplicationContext) :
505
511
  )
506
512
  }
507
513
 
508
- fun startPackDownload(pack: TileRegionPack) {
509
- val _this = this
510
- pack.cancelable = tileStore
511
- .loadTileRegion(
512
- pack.name,
513
- (pack.loadOptions)!!,
514
- TileRegionLoadProgressCallback { progress ->
515
- pack.progress = progress
516
- pack.state = TileRegionPackState.ACTIVE
517
- _this.sendEvent(_this.makeStatusEvent(pack.name, progress, pack))
518
- },
519
- object : TileRegionCallback {
520
- override fun run(region: Expected<TileRegionError, TileRegion>) {
521
- pack.cancelable = null
522
- if (region.isError) {
523
- pack.state = TileRegionPackState.INACTIVE
524
- _this.sendEvent(
525
- _this.makeErrorEvent(
526
- pack.name, "TileRegionError", region.error!!
527
- .message
528
- )
529
- )
530
- } else {
531
- pack.state = TileRegionPackState.COMPLETE
532
- _this.sendEvent(_this.makeStatusEvent(pack.name, pack.progress, pack))
533
- }
534
- }
535
- })
536
- }
537
-
538
514
  @ReactMethod
539
515
  fun resetDatabase(promise: Promise) {
540
516
  tileStore.getAllTileRegions { expected ->
@@ -622,38 +598,14 @@ class RCTMGLOfflineModule(private val mReactContext: ReactApplicationContext) :
622
598
  return OfflineEvent(OFFLINE_ERROR, errorType, payload)
623
599
  }
624
600
 
625
- private fun makeStatusEvent(
626
- regionName: String,
627
- status: TileRegionLoadProgress?,
628
- pack: TileRegionPack
629
- ): OfflineEvent {
601
+ private fun makeProgressEvent(name: String, progress: TileRegionLoadProgress, state: TileRegionPackState): OfflineEvent {
630
602
  return OfflineEvent(
631
603
  OFFLINE_PROGRESS,
632
604
  EventTypes.OFFLINE_STATUS,
633
- makeRegionStatus(regionName, status, pack)
605
+ _makeRegionStatusPayload(name, progress, state, null)
634
606
  )
635
607
  }
636
608
 
637
- private fun makeRegionStatus(
638
- regionName: String,
639
- status: TileRegionLoadProgress?,
640
- pack: TileRegionPack
641
- ): WritableMap {
642
- val map = Arguments.createMap()
643
- val progressPercentage =
644
- (status!!.completedResourceCount.toDouble() * 100.0) / (status.requiredResourceCount.toDouble())
645
- map.putString("name", regionName)
646
- map.putString("state", pack.state.rawValue)
647
- map.putDouble("percentage", progressPercentage)
648
- map.putInt("completedResourceCount", status.completedResourceCount.toInt())
649
- map.putInt("completedResourceSize", status.completedResourceSize.toInt())
650
- map.putInt("erroredResourceCount", status.erroredResourceCount.toInt())
651
- map.putInt("requiredResourceCount", status.requiredResourceCount.toInt())
652
- map.putInt("loadedResourceCount", status.loadedResourceCount.toInt())
653
- map.putInt("loadedResourceSize", status.loadedResourceSize.toInt())
654
- return map
655
- }
656
-
657
609
  companion object {
658
610
  const val REACT_CLASS = "RCTMGLOfflineModule"
659
611
  const val LOG_TAG = REACT_CLASS
@@ -663,6 +615,21 @@ class RCTMGLOfflineModule(private val mReactContext: ReactApplicationContext) :
663
615
  }
664
616
  }
665
617
 
618
+ fun TileRegionLoadProgress.toPercentage(): Double {
619
+ return (completedResourceCount.toDouble() * 100.0) / (requiredResourceCount.toDouble())
620
+ }
621
+
622
+ fun TileRegionLoadProgress.hasCompleted(): Boolean {
623
+ return (completedResourceCount == requiredResourceCount)
624
+ }
625
+
626
+ fun TileRegion.toPercentage(): Double {
627
+ return (completedResourceCount.toDouble() * 100.0) / (requiredResourceCount.toDouble())
628
+ }
629
+
630
+ fun TileRegion.hasCompleted(): Boolean {
631
+ return (completedResourceCount == requiredResourceCount)
632
+ }
666
633
 
667
634
 
668
635
 
@@ -1,15 +1,14 @@
1
1
  import Foundation
2
2
 
3
3
  protocol RCTMGLEventProtocol {
4
-
5
- func toJSON() -> [String: Any];
4
+ func toJSON() -> [String: Any?];
6
5
  }
7
6
 
8
7
  @objc
9
8
  class RCTMGLEvent : NSObject, RCTMGLEventProtocol {
10
9
  var type: String = ""
11
- var payload: [String:Any]? = nil
12
- func toJSON() -> [String: Any]
10
+ var payload: [String:Any?]? = nil
11
+ func toJSON() -> [String: Any?]
13
12
  {
14
13
  if let payload = payload {
15
14
  return ["type": type, "payload": payload];
@@ -41,7 +40,7 @@ class RCTMGLEvent : NSObject, RCTMGLEventProtocol {
41
40
  case shapeSourceLayerPress
42
41
  }
43
42
 
44
- init(type: EventType, payload: [String:Any]?) {
43
+ init(type: EventType, payload: [String:Any?]?) {
45
44
  self.type = type.rawValue
46
45
  self.payload = payload
47
46
  }
@@ -98,7 +98,7 @@ class RCTMGLOfflineModule: RCTEventEmitter {
98
98
  var cancelable: Cancelable? = nil
99
99
  var progress : TileRegionLoadProgress? = nil
100
100
  var state : State = .inactive
101
- var metadata : [String:Any]? = nil
101
+ var metadata : [String:Any]
102
102
 
103
103
  // Stored in metadata for resume functionality:
104
104
  var bounds: Geometry? = nil
@@ -190,22 +190,22 @@ class RCTMGLOfflineModule: RCTEventEmitter {
190
190
  resolver: @escaping RCTPromiseResolveBlock,
191
191
  rejecter: @escaping RCTPromiseRejectBlock) {
192
192
  guard let pack = tileRegionPacks[name] else {
193
- resolver(nil)
193
+ rejecter("RCTMGLOfflineModule.getPackStatus", "pack \(name) not found", nil)
194
194
  return
195
195
  }
196
196
 
197
197
  tileStore.tileRegionMetadata(forId: name) { result in
198
198
  switch result {
199
- case .failure(let error):
200
- Logger.log(level:.error, message: "Unable to fetch metadata for \(name)")
201
- rejecter("RCTMGLOfflineModule.getPackStatus", error.localizedDescription, error)
202
199
  case .success(let metadata):
203
- var pack = self.tileRegionPacks[name] ?? TileRegionPack(
200
+ var pack = TileRegionPack(
204
201
  name: name,
205
202
  metadata: logged("RCTMGLOfflineModule.getPackStatus") { metadata as? [String:Any] } ?? [:]
206
203
  )
207
204
  self.tileRegionPacks[name] = pack
208
205
  resolver(self._makeRegionStatusPayload(pack: pack))
206
+ case .failure(let error):
207
+ Logger.log(level:.error, message: "Unable to fetch metadata for \(name)")
208
+ rejecter("RCTMGLOfflineModule.getPackStatus", error.localizedDescription, error)
209
209
  }
210
210
  }
211
211
  }
@@ -325,6 +325,7 @@ class RCTMGLOfflineModule: RCTEventEmitter {
325
325
 
326
326
  func startLoading(pack: TileRegionPack) {
327
327
  let id = pack.name
328
+ let metadata = pack.metadata
328
329
  guard let bounds = pack.bounds else {
329
330
  RCTMGLLogError("RCTMGLOfflineModule.startLoading failed as there are no bounds in pack")
330
331
  return
@@ -337,10 +338,6 @@ class RCTMGLOfflineModule: RCTEventEmitter {
337
338
  RCTMGLLogError("RCTMGLOfflineModule.startLoading failed as there is no styleURI in pack")
338
339
  return
339
340
  }
340
- guard let metadata = pack.metadata else {
341
- RCTMGLLogError("RCTMGLOfflineModule.startLoading failed as there is no metadata in pack")
342
- return
343
- }
344
341
 
345
342
  let stylePackLoadOptions = StylePackLoadOptions(glyphsRasterizationMode: .ideographsRasterizedLocally, metadata: pack.metadata)
346
343
 
@@ -447,7 +444,7 @@ class RCTMGLOfflineModule: RCTEventEmitter {
447
444
  metadata: logged("RCTMGLOfflineModule.getPacks metadata is null") { metadata } ?? [:]
448
445
  )
449
446
 
450
- if ((region.completedResourceCount == region.requiredResourceCount)) {
447
+ if ((region.hasCompleted())) {
451
448
  pack.state = .complete
452
449
  }
453
450
 
@@ -482,8 +479,7 @@ class RCTMGLOfflineModule: RCTEventEmitter {
482
479
  ]
483
480
 
484
481
  if region.requiredResourceCount > 0 {
485
- let percentage = Float(100.0) * Float(region.completedResourceCount) / Float(region.requiredResourceCount)
486
- result["percentage"] = percentage
482
+ result["percentage"] = region.toPercentage()
487
483
  } else {
488
484
  result["percentage"] = nil
489
485
  }
@@ -535,12 +531,10 @@ class RCTMGLOfflineModule: RCTEventEmitter {
535
531
  func _makeRegionStatusPayload(_ name:String, progress: TileRegionLoadProgress?, state: State, metadata:[String:Any]?) -> [String:Any?] {
536
532
  var result : [String:Any?] = [:]
537
533
  if let progress = progress {
538
- let progressPercentage = Float(progress.completedResourceCount) / Float(progress.requiredResourceCount)
539
-
540
534
  result = [
541
- "state": (progress.completedResourceCount == progress.requiredResourceCount) ? State.complete.rawValue : state.rawValue,
535
+ "state": (progress.hasCompleted()) ? State.complete.rawValue : state.rawValue,
542
536
  "name": name,
543
- "percentage": progressPercentage * 100.0,
537
+ "percentage": progress.toPercentage(),
544
538
  "completedResourceCount": progress.completedResourceCount,
545
539
  "completedResourceSize": progress.completedResourceSize,
546
540
  "erroredResourceCount": progress.erroredResourceCount,
@@ -615,3 +609,20 @@ extension RCTMGLOfflineModule {
615
609
  }
616
610
  }
617
611
 
612
+ extension TileRegionLoadProgress {
613
+ func toPercentage() -> Float {
614
+ return Float(100.0) * Float(completedResourceCount) / Float(requiredResourceCount);
615
+ }
616
+ func hasCompleted() -> Bool {
617
+ return (completedResourceCount == requiredResourceCount)
618
+ }
619
+ }
620
+
621
+ extension TileRegion {
622
+ func toPercentage() -> Float {
623
+ return Float(100.0) * Float(completedResourceCount) / Float(requiredResourceCount)
624
+ }
625
+ func hasCompleted() -> Bool {
626
+ return (completedResourceCount == requiredResourceCount)
627
+ }
628
+ }
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@rnmapbox/maps",
3
3
  "description": "A Mapbox react native module for creating custom maps",
4
- "version": "10.0.0-rc.10",
4
+ "version": "10.0.0-rc.11",
5
5
  "publishConfig": {
6
6
  "access": "public"
7
7
  },