Skip to content

Commit 64d3158

Browse files
authored
Add limit check for gradient stops key value. (#4046)
1 parent 86441f0 commit 64d3158

File tree

3 files changed

+30
-6
lines changed

3 files changed

+30
-6
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
* Fixed an issue where the incorrect destination building is highlighted after panning the correct building off screen. ([#4034](https://github.com/mapbox/mapbox-navigation-ios/pull/4034))
2323
* Fixed an issue where the maneuver arrow on the route line overlapped labels for points of interest. ([#4030](https://github.com/mapbox/mapbox-navigation-ios/pull/4030))
2424
* Fixed an issue where `NavigationMapView` injected with `NavigationOptions` isn't visible in active navigation. ([#4049](https://github.com/mapbox/mapbox-navigation-ios/pull/4049))
25+
* Fixed the crash caused by showing a route that doesn't contain coordinates. ([#4046](https://github.com/mapbox/mapbox-navigation-ios/pull/4046))
2526

2627
### CarPlay
2728

Sources/MapboxNavigation/NavigationMapView+VanishingRouteLine.swift

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -323,6 +323,9 @@ extension NavigationMapView {
323323

324324
guard case let .lineString(lineString) = feature.geometry,
325325
let distance = lineString.distance() else {
326+
if gradientStops.isEmpty {
327+
gradientStops[0.0] = lineSettings.baseColor
328+
}
326329
return gradientStops
327330
}
328331
let minimumPercentGap = 2e-16
@@ -335,7 +338,7 @@ extension NavigationMapView {
335338
if index + 1 < routeLineFeatures.count {
336339
let segmentEndPercentTraveled = distanceTraveled / routeDistance
337340
var currentGradientStop = lineSettings.isSoft ? segmentEndPercentTraveled - stopGap : Double(CGFloat(segmentEndPercentTraveled).nextDown)
338-
currentGradientStop = max(currentGradientStop, 0.0)
341+
currentGradientStop = min(max(currentGradientStop, 0.0), 1.0)
339342
gradientStops[currentGradientStop] = associatedFeatureColor
340343
lastRecordSegment = (currentGradientStop, associatedFeatureColor)
341344
}
@@ -349,7 +352,7 @@ extension NavigationMapView {
349352
} else {
350353
let segmentStartPercentTraveled = distanceTraveled / routeDistance
351354
var currentGradientStop = lineSettings.isSoft ? segmentStartPercentTraveled + stopGap : Double(CGFloat(segmentStartPercentTraveled).nextUp)
352-
currentGradientStop = min(currentGradientStop, 1.0)
355+
currentGradientStop = min(max(currentGradientStop, 0.0), 1.0)
353356
gradientStops[currentGradientStop] = associatedFeatureColor
354357
}
355358

@@ -361,14 +364,14 @@ extension NavigationMapView {
361364
} else {
362365
let segmentStartPercentTraveled = distanceTraveled / routeDistance
363366
var currentGradientStop = lineSettings.isSoft ? segmentStartPercentTraveled + stopGap : Double(CGFloat(segmentStartPercentTraveled).nextUp)
364-
currentGradientStop = min(currentGradientStop, 1.0)
367+
currentGradientStop = min(max(currentGradientStop, 0.0), 1.0)
365368
gradientStops[currentGradientStop] = associatedFeatureColor
366369
}
367370

368371
distanceTraveled = distanceTraveled + distance
369372
let segmentEndPercentTraveled = distanceTraveled / routeDistance
370373
var currentGradientStop = lineSettings.isSoft ? segmentEndPercentTraveled - stopGap : Double(CGFloat(segmentEndPercentTraveled).nextDown)
371-
currentGradientStop = max(currentGradientStop, 0.0)
374+
currentGradientStop = min(max(currentGradientStop, 0.0), 1.0)
372375
gradientStops[currentGradientStop] = associatedFeatureColor
373376
lastRecordSegment = (currentGradientStop, associatedFeatureColor)
374377
}
@@ -393,7 +396,7 @@ extension NavigationMapView {
393396
let baseColor: UIColor
394397
if let _ = congestionFeatures {
395398
overriddenLineLayerColor = lineLayerColorIfPresent(from: route)
396-
baseColor = overriddenLineLayerColor ?? (isMain ? .trafficUnknown : .alternativeTrafficUnknown)
399+
baseColor = overriddenLineLayerColor ?? (isMain ? trafficUnknownColor : alternativeTrafficUnknownColor)
397400
} else {
398401
overriddenLineLayerColor = lineLayerCasingColorIfPresent(from: route)
399402
baseColor = overriddenLineLayerColor ?? routeCasingColor

Tests/MapboxNavigationTests/VanishingRouteLineTests.swift

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ class VanishingRouteLineTests: TestCase {
9090
distance: routeLeg.distance,
9191
expectedTravelTime: routeLeg.expectedTravelTime,
9292
profileIdentifier: routeLeg.profileIdentifier)
93-
let emptyRoute = Route(legs: [emptyLeg], shape: route.shape, distance: route.distance, expectedTravelTime: route.expectedTravelTime)
93+
let emptyRoute = Route(legs: [emptyLeg], shape: LineString([]), distance: route.distance, expectedTravelTime: route.expectedTravelTime)
9494
return emptyRoute
9595
}
9696

@@ -236,6 +236,26 @@ class VanishingRouteLineTests: TestCase {
236236
XCTAssertEqual(navigationMapView.fractionTraveled, 0.0, accuracy: 0)
237237
}
238238

239+
func testEmptyRouteGradientStops() {
240+
let route = getEmptyRoute()
241+
let congestionFeatures = route.congestionFeatures(legIndex: 0)
242+
XCTAssertFalse(congestionFeatures.isEmpty, "Failed to generate features for empty route.")
243+
244+
congestionFeatures.forEach { feature in
245+
guard case let .lineString(lineString) = feature.geometry,
246+
lineString.distance() == nil else {
247+
XCTFail("Failed to generate nil distance features with empty coordinates shape route.")
248+
return
249+
}
250+
}
251+
252+
navigationMapView.trafficUnknownColor = UIColor.blue
253+
let gradientStops = navigationMapView.routeLineCongestionGradient(route,
254+
congestionFeatures: congestionFeatures)
255+
let expectedGradientStops: [Double: UIColor] = [0.0: navigationMapView.trafficUnknownColor]
256+
XCTAssertEqual(gradientStops, expectedGradientStops, "Failed to generate non-empty gradient stops for empty coordinates shape route.")
257+
}
258+
239259
func testUnstartedRouteProgressWithValidRoute() {
240260
let route = getRoute()
241261
let routeProgress = getUnstartedRouteProgress(route: route)

0 commit comments

Comments
 (0)