Skip to content

Commit

Permalink
Change route on the fly (#302)
Browse files Browse the repository at this point in the history
* Change route on the fly

* Remove excessive reset

* Fix route progress
  • Loading branch information
frederoni authored Jun 22, 2017
1 parent b25f611 commit ff7311d
Show file tree
Hide file tree
Showing 8 changed files with 82 additions and 6 deletions.
2 changes: 1 addition & 1 deletion Cartfile
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
binary "https://www.mapbox.com/ios-sdk/Mapbox-iOS-SDK.json" ~> 3.5
github "mapbox/MapboxDirections.swift" ~> 0.9.0
github "mapbox/MapboxDirections.swift" "e6d3d4c4e2261e1b3d356c21e061bb020ebec4ee"
github "52inc/Pulley" ~> 1.3.1
github "rs/SDWebImage" ~> 4.0
github "Project-OSRM/osrm-text-instructions.swift" ~> 0.1.2
Expand Down
2 changes: 1 addition & 1 deletion Cartfile.resolved
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@ github "52inc/Pulley" "1.4"
github "Project-OSRM/osrm-text-instructions.swift" "v0.1.2"
github "facebook/ios-snapshot-test-case" "2.1.4"
github "frederoni/aws-sdk-ios" "40c7d5d60e0b80d70cd1502ef0cf8507f023f8d5"
github "mapbox/MapboxDirections.swift" "v0.9.1"
github "mapbox/MapboxDirections.swift" "e6d3d4c4e2261e1b3d356c21e061bb020ebec4ee"
github "raphaelmor/Polyline" "v4.1.1"
github "rs/SDWebImage" "4.0.0"
18 changes: 17 additions & 1 deletion MapboxNavigation.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,8 @@
354A01EC1E6626EF00D765C2 /* Mapbox.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 35A1D3651E6624EF00A48FE8 /* Mapbox.framework */; };
354A01ED1E6626EF00D765C2 /* Mapbox.framework in Embed Frameworks */ = {isa = PBXBuildFile; fileRef = 35A1D3651E6624EF00A48FE8 /* Mapbox.framework */; settings = {ATTRIBUTES = (CodeSignOnCopy, RemoveHeadersOnCopy, ); }; };
354BD4F51E6637D500AE1344 /* AWSPolly.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 354BD4F11E6634C500AE1344 /* AWSPolly.framework */; };
355DB5751EFA78070091BFB7 /* GGPark-to-BernalHeights.route in Resources */ = {isa = PBXBuildFile; fileRef = 355DB5741EFA78070091BFB7 /* GGPark-to-BernalHeights.route */; };
355DB5771EFA780E0091BFB7 /* UnionSquare-to-GGPark.route in Resources */ = {isa = PBXBuildFile; fileRef = 355DB5761EFA780E0091BFB7 /* UnionSquare-to-GGPark.route */; };
358D14661E5E3B7700ADE590 /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 358D14651E5E3B7700ADE590 /* AppDelegate.swift */; };
358D14681E5E3B7700ADE590 /* ViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 358D14671E5E3B7700ADE590 /* ViewController.swift */; };
359D00CF1E732D7100C2E770 /* Polyline.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 354A01BC1E66259600D765C2 /* Polyline.framework */; };
Expand Down Expand Up @@ -287,6 +289,8 @@
354A01BE1E6625A900D765C2 /* SDWebImage.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = SDWebImage.framework; path = Carthage/Build/iOS/SDWebImage.framework; sourceTree = "<group>"; };
354BD4F11E6634C500AE1344 /* AWSPolly.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = AWSPolly.framework; path = Carthage/Build/iOS/AWSPolly.framework; sourceTree = "<group>"; };
354BD4F61E6638E800AE1344 /* AWSCore.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = AWSCore.framework; path = Carthage/Build/iOS/AWSCore.framework; sourceTree = "<group>"; };
355DB5741EFA78070091BFB7 /* GGPark-to-BernalHeights.route */ = {isa = PBXFileReference; lastKnownFileType = file.bplist; path = "GGPark-to-BernalHeights.route"; sourceTree = "<group>"; };
355DB5761EFA780E0091BFB7 /* UnionSquare-to-GGPark.route */ = {isa = PBXFileReference; lastKnownFileType = file.bplist; path = "UnionSquare-to-GGPark.route"; sourceTree = "<group>"; };
355EF9641EDF264200749641 /* sv */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; name = sv; path = sv.lproj/Abbreviations.plist; sourceTree = "<group>"; };
356B7D8A1EE166E100FE5B89 /* scripts */ = {isa = PBXFileReference; lastKnownFileType = folder; path = scripts; sourceTree = SOURCE_ROOT; };
357F0DF01EB9D99F00A0B53C /* sv */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.plist.strings; name = sv; path = sv.lproj/Localizable.strings; sourceTree = "<group>"; };
Expand Down Expand Up @@ -517,6 +521,16 @@
path = "Examples/Objective-CTests";
sourceTree = "<group>";
};
355DB5731EFA73410091BFB7 /* Fixtures */ = {
isa = PBXGroup;
children = (
352690481ECC843700E387BD /* Fixture.swift */,
355DB5741EFA78070091BFB7 /* GGPark-to-BernalHeights.route */,
355DB5761EFA780E0091BFB7 /* UnionSquare-to-GGPark.route */,
);
path = Fixtures;
sourceTree = "<group>";
};
358D14641E5E3B7700ADE590 /* Swift */ = {
isa = PBXGroup;
children = (
Expand Down Expand Up @@ -544,7 +558,7 @@
35B711D01E5E7AD2001EDA8D /* MapboxNavigationTests */ = {
isa = PBXGroup;
children = (
352690481ECC843700E387BD /* Fixture.swift */,
355DB5731EFA73410091BFB7 /* Fixtures */,
35B711D11E5E7AD2001EDA8D /* MapboxNavigationTests.swift */,
35B711D31E5E7AD2001EDA8D /* Info.plist */,
3527D2B61EC45FBD00C07FC9 /* Fixtures.xcassets */,
Expand Down Expand Up @@ -998,6 +1012,8 @@
buildActionMask = 2147483647;
files = (
3527D2B91EC4619400C07FC9 /* Fixtures.xcassets in Resources */,
355DB5771EFA780E0091BFB7 /* UnionSquare-to-GGPark.route in Resources */,
355DB5751EFA78070091BFB7 /* GGPark-to-BernalHeights.route in Resources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down
9 changes: 6 additions & 3 deletions MapboxNavigation/NavigationViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,10 @@ public class NavigationViewController: NavigationPulleyViewController, RouteMapV
// A `route` object constructed by [MapboxDirections.swift](https://github.com/mapbox/MapboxDirections.swift)
public var route: Route! {
didSet {
setupRouteController()
ensureRouteController()
routeController.routeProgress = RouteProgress(route: route)
mapViewController?.notifyDidReroute(route: route)
tableViewController?.notifyDidReroute()
}
}

Expand Down Expand Up @@ -228,7 +231,7 @@ public class NavigationViewController: NavigationPulleyViewController, RouteMapV

self.directions = directions
self.route = route
self.setupRouteController()
self.ensureRouteController()
self.mapViewController = mapViewController
self.tableViewController = tableViewController

Expand Down Expand Up @@ -357,7 +360,7 @@ public class NavigationViewController: NavigationPulleyViewController, RouteMapV
UIApplication.shared.scheduleLocalNotification(notification)
}

func setupRouteController() {
func ensureRouteController() {
if routeController == nil {
routeController = RouteController(along: route, directions: directions)
routeController.delegate = self
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import XCTest
import Foundation
import MapboxDirections
import CoreLocation

internal class Fixture {
internal class func stringFromFileNamed(name: String) -> String {
Expand Down Expand Up @@ -31,4 +33,22 @@ internal class Fixture {
return [:]
}
}

class func downloadRouteFixture(coordinates: [CLLocationCoordinate2D], fileName: String, completion: @escaping () -> Void) {
let accessToken = "<# Mapbox Access Token #>"
let directions = Directions(accessToken: accessToken)

let options = RouteOptions(coordinates: coordinates, profileIdentifier: .automobileAvoidingTraffic)
options.includesSteps = true
options.routeShapeResolution = .full
let filePath = (NSTemporaryDirectory() as NSString).appendingPathComponent(fileName)

_ = directions.calculate(options, completionHandler: { (waypoints, routes, error) in
guard let route = routes?.first else { return }

NSKeyedArchiver.archiveRootObject(route, toFile: filePath)
print("Route downloaded to \(filePath)")
completion()
})
}
}
Binary file not shown.
Binary file not shown.
37 changes: 37 additions & 0 deletions MapboxNavigationTests/MapboxNavigationTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import FBSnapshotTestCase
@testable import MapboxNavigation
@testable import MapboxCoreNavigation

let directions = Directions(accessToken: "pk.feedCafeDeadBeefBadeBede")

class MapboxNavigationTests: FBSnapshotTestCase {

var shieldImage: UIImage {
Expand Down Expand Up @@ -88,4 +90,39 @@ class MapboxNavigationTests: FBSnapshotTestCase {

FBSnapshotVerifyView(controller.view)
}

func testRouteSwitching() {
let bundle = Bundle(for: MapboxNavigationTests.self)
var filePath = bundle.path(forResource: "UnionSquare-to-GGPark", ofType: "route")!
var route = NSKeyedUnarchiver.unarchiveObject(withFile: filePath) as! Route

let navigation = NavigationViewController(for: route, directions: directions)
navigation.loadViewIfNeeded()

filePath = bundle.path(forResource: "GGPark-to-BernalHeights", ofType: "route")!
let newRoute = NSKeyedUnarchiver.unarchiveObject(withFile: filePath) as! Route

navigation.route = newRoute

XCTAssertTrue(navigation.routeController.routeProgress.route == newRoute, "Route should be equal the new route")

let tableViewController = navigation.tableViewController!
let numberOfRows = tableViewController.tableView(tableViewController.tableView, numberOfRowsInSection: 0)
XCTAssertTrue(numberOfRows == newRoute.legs[0].steps.count,
"It should be same amount of cells as steps in the new route")
}
}

extension CLLocationCoordinate2D {
static var unionSquare: CLLocationCoordinate2D {
return CLLocationCoordinate2D(latitude: 37.786902, longitude: -122.407668)
}

static var goldenGatePark: CLLocationCoordinate2D {
return CLLocationCoordinate2D(latitude: 37.770935, longitude: -122.479346)
}

static var bernalHeights: CLLocationCoordinate2D {
return CLLocationCoordinate2D(latitude: 37.739912, longitude: -122.420100)
}
}

0 comments on commit ff7311d

Please sign in to comment.