@baeckerherz/expo-mapbox-navigation 1.0.4 → 1.0.5

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.
@@ -13,6 +13,7 @@ import com.mapbox.navigation.core.trip.session.RouteProgressObserver
13
13
  import com.mapbox.navigation.core.trip.session.OffRouteObserver
14
14
  import com.mapbox.navigation.core.directions.session.RoutesObserver
15
15
  import com.mapbox.navigation.dropin.NavigationView
16
+ import com.mapbox.navigation.core.directions.session.RoutesSetCallback
16
17
  import com.mapbox.api.directions.v5.DirectionsCriteria
17
18
  import com.mapbox.api.directions.v5.models.RouteOptions
18
19
 
@@ -45,12 +46,40 @@ class ExpoMapboxNavigationView(
45
46
  private var hasStartedNavigation = false
46
47
 
47
48
  fun setCoordinates(raw: List<Map<String, Double>>) {
48
- coordinates = raw.mapNotNull { map ->
49
+ val newCoords = raw.mapNotNull { map ->
49
50
  val lat = map["latitude"] ?: return@mapNotNull null
50
51
  val lng = map["longitude"] ?: return@mapNotNull null
51
52
  Point.fromLngLat(lng, lat)
52
53
  }
53
- startNavigationIfReady()
54
+ val isRouteChange = hasStartedNavigation && coordinatesChanged(newCoords)
55
+ if (isRouteChange) {
56
+ removeCurrentNavigationView { startNavigationIfReady() }
57
+ }
58
+ coordinates = newCoords
59
+ if (!isRouteChange) {
60
+ startNavigationIfReady()
61
+ }
62
+ }
63
+
64
+ private fun coordinatesChanged(newCoords: List<Point>): Boolean {
65
+ if (coordinates.size != newCoords.size) return true
66
+ return coordinates.zip(newCoords).any { (a, b) ->
67
+ a.latitude() != b.latitude() || a.longitude() != b.longitude()
68
+ }
69
+ }
70
+
71
+ private fun removeCurrentNavigationView(onCleared: (() -> Unit)? = null) {
72
+ navigationView?.let { removeView(it) }
73
+ navigationView = null
74
+ hasStartedNavigation = false
75
+ val nav = MapboxNavigationApp.current()
76
+ if (nav != null) {
77
+ nav.setNavigationRoutes(emptyList(), 0, RoutesSetCallback { _ ->
78
+ post { onCleared?.invoke() }
79
+ })
80
+ } else {
81
+ post { onCleared?.invoke() }
82
+ }
54
83
  }
55
84
 
56
85
  private fun startNavigationIfReady() {
@@ -158,7 +187,6 @@ class ExpoMapboxNavigationView(
158
187
 
159
188
  override fun onDetachedFromWindow() {
160
189
  super.onDetachedFromWindow()
161
- navigationView?.let { removeView(it) }
162
- navigationView = null
190
+ removeCurrentNavigationView()
163
191
  }
164
192
  }
@@ -156,13 +156,39 @@ class ExpoMapboxNavigationView: ExpoView {
156
156
  // MARK: Props
157
157
 
158
158
  func setCoordinates(_ raw: [[String: Double]]) {
159
- coordinates = raw.compactMap { dict in
159
+ let newCoords = raw.compactMap { dict -> CLLocationCoordinate2D? in
160
160
  guard let lat = dict["latitude"], let lng = dict["longitude"] else {
161
161
  return nil
162
162
  }
163
163
  return CLLocationCoordinate2D(latitude: lat, longitude: lng)
164
164
  }
165
- startNavigationIfReady()
165
+ let isRouteChange = hasStartedNavigation && coordinatesDiffer(from: newCoords)
166
+ if isRouteChange {
167
+ removeCurrentNavigation()
168
+ }
169
+ coordinates = newCoords
170
+ if isRouteChange {
171
+ DispatchQueue.main.async { [weak self] in
172
+ self?.startNavigationIfReady()
173
+ }
174
+ } else {
175
+ startNavigationIfReady()
176
+ }
177
+ }
178
+
179
+ private func coordinatesDiffer(from newCoords: [CLLocationCoordinate2D]) -> Bool {
180
+ guard coordinates.count == newCoords.count else { return true }
181
+ return zip(coordinates, newCoords).contains { a, b in
182
+ a.latitude != b.latitude || a.longitude != b.longitude
183
+ }
184
+ }
185
+
186
+ private func removeCurrentNavigation() {
187
+ navigationViewController?.willMove(toParent: nil)
188
+ navigationViewController?.view.removeFromSuperview()
189
+ navigationViewController?.removeFromParent()
190
+ navigationViewController = nil
191
+ hasStartedNavigation = false
166
192
  }
167
193
 
168
194
  // MARK: Layout
@@ -308,9 +334,7 @@ class ExpoMapboxNavigationView: ExpoView {
308
334
  // MARK: Cleanup
309
335
 
310
336
  deinit {
311
- navigationViewController?.willMove(toParent: nil)
312
- navigationViewController?.view.removeFromSuperview()
313
- navigationViewController?.removeFromParent()
337
+ removeCurrentNavigation()
314
338
  }
315
339
  }
316
340
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@baeckerherz/expo-mapbox-navigation",
3
- "version": "1.0.4",
3
+ "version": "1.0.5",
4
4
  "description": "Expo module for turn-by-turn navigation (Mapbox Navigation SDK v3) on iOS and Android. Alpha. Single MapboxNavigation component, Expo config plugin.",
5
5
  "main": "src/index.ts",
6
6
  "types": "src/index.ts",