Skip to content

Commit

Permalink
Fix the preview crash resulted from the table view scrolling quickly. (
Browse files Browse the repository at this point in the history
  • Loading branch information
ShanMa1991 authored Oct 4, 2021
1 parent 0aa9b79 commit 5382dc0
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 5 deletions.
3 changes: 1 addition & 2 deletions Sources/MapboxNavigation/NavigationMapView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -897,8 +897,7 @@ open class NavigationMapView: UIView {
- parameter stepIndex: Zero-based index of the `RouteStep` which contains the maneuver.
*/
public func addArrow(route: Route, legIndex: Int, stepIndex: Int) {
guard route.legs.indices.contains(legIndex),
route.legs[legIndex].steps.indices.contains(stepIndex),
guard route.containsStep(at: legIndex, stepIndex: stepIndex),
let triangleImage = Bundle.mapboxNavigation.image(named: "triangle")?.withRenderingMode(.alwaysTemplate) else { return }

do {
Expand Down
1 change: 1 addition & 0 deletions Sources/MapboxNavigation/NavigationViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1104,6 +1104,7 @@ extension NavigationViewController: TopBannerViewControllerDelegate {

public func topBanner(_ banner: TopBannerViewController, didSelect legIndex: Int, stepIndex: Int, cell: StepTableViewCell) {
let progress = navigationService.routeProgress
guard progress.route.containsStep(at: legIndex, stepIndex: stepIndex) else { return }
let legProgress = RouteLegProgress(leg: progress.route.legs[legIndex], stepIndex: stepIndex)
let step = legProgress.currentStep
self.preview(step: step, in: banner, remaining: progress.remainingSteps, route: progress.route)
Expand Down
7 changes: 7 additions & 0 deletions Sources/MapboxNavigation/Route.swift
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,13 @@ extension Route {
return LineString(coordinateList)
}
}

/**
Returns true if both the legIndex and stepIndex are valid in the route.
*/
func containsStep(at legIndex: Int, stepIndex: Int) -> Bool {
return legs[safe: legIndex]?.steps.indices.contains(stepIndex) ?? false
}
}

extension RouteStep {
Expand Down
11 changes: 8 additions & 3 deletions Sources/MapboxNavigation/StepsViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -192,18 +192,23 @@ public class StepsViewController: UIViewController {
extension StepsViewController: UITableViewDelegate {
public func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
tableView.deselectRow(at: indexPath, animated: true)
let legIndex = indexPath.section
let cell = tableView.cellForRow(at: indexPath) as! StepTableViewCell
// Since as we progress, steps are removed from the list, we need to map the row the user tapped to the actual step on the leg.
// If the user selects a step on future leg, all steps are going to be there.
var stepIndex: Int
if indexPath.section > 0 {
if legIndex > 0 {
stepIndex = indexPath.row
} else {
stepIndex = indexPath.row + routeProgress.currentLegProgress.stepIndex
// For the current leg, we need to know the upcoming step.
stepIndex += indexPath.row + 1 > sections[indexPath.section].count ? 0 : 1
if sections[legIndex].indices.contains(indexPath.row) {
stepIndex += 1
}
}
delegate?.stepsViewController(self, didSelect: indexPath.section, stepIndex: stepIndex, cell: cell)

guard routeProgress.route.containsStep(at: legIndex, stepIndex: stepIndex) else { return }
delegate?.stepsViewController(self, didSelect: legIndex, stepIndex: stepIndex, cell: cell)
}
}

Expand Down
16 changes: 16 additions & 0 deletions Tests/MapboxNavigationTests/RouteTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -78,4 +78,20 @@ class RouteTests: TestCase {
XCTAssertLessThan(lastIndexedCoordinate!.coordinate.distance(to: maneuverPolyline.coordinates.last!), 1, "End of maneuver polyline for step \(stepIndex) is \(lastIndexedCoordinate!.coordinate.distance(to: maneuverPolyline.coordinates.last!)) away from outlet from intersection.")
}
}

func testContainsStep() {
guard let route = response.routes?.first else {
XCTFail("Failed to get route.")
return
}

var legIndex = route.legs.count - 1
var stepsIndex = route.legs[legIndex].steps.count
XCTAssertFalse(route.containsStep(at: legIndex, stepIndex: stepsIndex), "Failed to check the step index when it's larger than or equal to the steps count.")

legIndex = route.legs.count
stepsIndex = 0
XCTAssertFalse(route.containsStep(at: legIndex, stepIndex: stepsIndex), "Failed to check the leg index when it's larger than or equal to the legs count.")

}
}

0 comments on commit 5382dc0

Please sign in to comment.