Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make RouteOptions available on route response #122

Merged
merged 3 commits into from
Apr 13, 2017
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 13 additions & 15 deletions MapboxDirections/MBRoute.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ import Polyline
open class Route: NSObject, NSSecureCoding {
// MARK: Creating a Route

internal init(profileIdentifier: MBDirectionsProfileIdentifier, legs: [RouteLeg], distance: CLLocationDistance, expectedTravelTime: TimeInterval, coordinates: [CLLocationCoordinate2D]?) {
self.profileIdentifier = profileIdentifier
internal init(routeOptions: RouteOptions, legs: [RouteLeg], distance: CLLocationDistance, expectedTravelTime: TimeInterval, coordinates: [CLLocationCoordinate2D]?) {
self.routeOptions = routeOptions
self.legs = legs
self.distance = distance
self.expectedTravelTime = expectedTravelTime
Expand All @@ -26,12 +26,12 @@ open class Route: NSObject, NSSecureCoding {
- parameter waypoints: An array of waypoints that the route visits in chronological order.
- parameter profileIdentifier: The profile identifier used to request the routes.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This parameter has been replaced by routeOptions.

*/
public convenience init(json: [String: Any], waypoints: [Waypoint], profileIdentifier: MBDirectionsProfileIdentifier) {
public convenience init(json: [String: Any], waypoints: [Waypoint], routeOptions: RouteOptions) {
// Associate each leg JSON with a source and destination. The sequence of destinations is offset by one from the sequence of sources.
let legInfo = zip(zip(waypoints.prefix(upTo: waypoints.endIndex - 1), waypoints.suffix(from: 1)),
json["legs"] as? [JSONDictionary] ?? [])
let legs = legInfo.map { (endpoints, json) -> RouteLeg in
RouteLeg(json: json, source: endpoints.0, destination: endpoints.1, profileIdentifier: profileIdentifier)
RouteLeg(json: json, source: endpoints.0, destination: endpoints.1, profileIdentifier: routeOptions.profileIdentifier)
}
let distance = json["distance"] as! Double
let expectedTravelTime = json["duration"] as! Double
Expand All @@ -46,7 +46,7 @@ open class Route: NSObject, NSSecureCoding {
coordinates = nil
}

self.init(profileIdentifier: profileIdentifier, legs: legs, distance: distance, expectedTravelTime: expectedTravelTime, coordinates: coordinates)
self.init(routeOptions: routeOptions, legs: legs, distance: distance, expectedTravelTime: expectedTravelTime, coordinates: coordinates)
}

public required init?(coder decoder: NSCoder) {
Expand All @@ -64,10 +64,10 @@ open class Route: NSObject, NSSecureCoding {
distance = decoder.decodeDouble(forKey: "distance")
expectedTravelTime = decoder.decodeDouble(forKey: "expectedTravelTime")

guard let decodedProfileIdentifier = decoder.decodeObject(of: NSString.self, forKey: "profileIdentifier") as String? else {
guard let options = decoder.decodeObject(of: [RouteOptions.self], forKey: "routeOptions") as? RouteOptions else {
return nil
}
profileIdentifier = MBDirectionsProfileIdentifier(rawValue: decodedProfileIdentifier)
routeOptions = options
}

open static var supportsSecureCoding = true
Expand All @@ -82,7 +82,7 @@ open class Route: NSObject, NSSecureCoding {
coder.encode(legs, forKey: "legs")
coder.encode(distance, forKey: "distance")
coder.encode(expectedTravelTime, forKey: "expectedTravelTime")
coder.encode(profileIdentifier, forKey: "profileIdentifier")
coder.encode(routeOptions, forKey: "routeOptions")
}

// MARK: Getting the Route Geometry
Expand Down Expand Up @@ -156,18 +156,16 @@ open class Route: NSObject, NSSecureCoding {
open let expectedTravelTime: TimeInterval

/**
A string specifying the primary mode of transportation for the route.

The value of this property is `MBDirectionsProfileIdentifierAutomobile`, `MBDirectionsProfileIdentifierAutomobileAvoidingTraffic`, `MBDirectionsProfileIdentifierCycling`, or `MBDirectionsProfileIdentifierWalking`, depending on the `profileIdentifier` property of the original `RouteOptions` object. This property reflects the primary mode of transportation used for the route. Individual steps along the route might use different modes of transportation as necessary.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Developers may not be aware of this nuance, so we might want to leave it in:

The route options object’s profileIdentifier property reflects the primary mode of transportation used for the route. Individual steps along the route might use different modes of transportation as necessary.

`RouteOptions` used to create the directions request.
*/
open let profileIdentifier: MBDirectionsProfileIdentifier
open let routeOptions: RouteOptions
}

// MARK: Support for Directions API v4

internal class RouteV4: Route {
convenience init(json: JSONDictionary, waypoints: [Waypoint], profileIdentifier: MBDirectionsProfileIdentifier) {
let leg = RouteLegV4(json: json, source: waypoints.first!, destination: waypoints.last!, profileIdentifier: profileIdentifier)
convenience init(json: JSONDictionary, waypoints: [Waypoint], routeOptions: RouteOptions) {
let leg = RouteLegV4(json: json, source: waypoints.first!, destination: waypoints.last!, profileIdentifier: routeOptions.profileIdentifier)
let distance = json["distance"] as! Double
let expectedTravelTime = json["duration"] as! Double

Expand All @@ -181,6 +179,6 @@ internal class RouteV4: Route {
coordinates = nil
}

self.init(profileIdentifier: profileIdentifier, legs: [leg], distance: distance, expectedTravelTime: expectedTravelTime, coordinates: coordinates)
self.init(routeOptions: routeOptions, legs: [leg], distance: distance, expectedTravelTime: expectedTravelTime, coordinates: coordinates)
}
}
4 changes: 2 additions & 2 deletions MapboxDirections/MBRouteOptions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,7 @@ open class RouteOptions: NSObject {
return Waypoint(coordinate: coordinate, name: waypoint["name"] as? String)
}
let routes = (json["routes"] as? [JSONDictionary])?.map {
Route(json: $0, waypoints: waypoints ?? self.waypoints, profileIdentifier: profileIdentifier)
Route(json: $0, waypoints: waypoints ?? self.waypoints, routeOptions: self)
}
return (waypoints, routes)
}
Expand Down Expand Up @@ -380,7 +380,7 @@ open class RouteOptionsV4: RouteOptions {
let intermediateWaypoints = (json["waypoints"] as! [JSONDictionary]).flatMap { Waypoint(geoJSON: $0) }
let waypoints = [sourceWaypoint] + intermediateWaypoints + [destinationWaypoint]
let routes = (json["routes"] as? [JSONDictionary])?.map {
RouteV4(json: $0, waypoints: waypoints, profileIdentifier: profileIdentifier)
RouteV4(json: $0, waypoints: waypoints, routeOptions: self)
}
return (waypoints, routes)
}
Expand Down
7 changes: 7 additions & 0 deletions MapboxDirectionsTests/V5Tests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,13 @@ class V5Tests: XCTestCase {
XCTAssertEqual(task.state, .completed)
}

let opts = route!.routeOptions

XCTAssertEqual(opts.profileIdentifier, .automobile)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Might as well assert that route!.routeOptions is equal to options. That way we can be sure they’re literally the same object; no need to compare individual properties on them.

XCTAssertTrue(opts.includesSteps)
XCTAssertTrue(opts.includesAlternativeRoutes)
XCTAssertEqual(opts.routeShapeResolution, .full)

XCTAssertNotNil(route)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Move this assertion up above any assertions that depend on route being non-nil.

XCTAssertNotNil(route!.coordinates)
XCTAssertEqual(route!.coordinates!.count, 28_442)
Expand Down