Skip to content

Commit

Permalink
Add roadSide to RouteStep (#219)
Browse files Browse the repository at this point in the history
  • Loading branch information
frederoni authored and Bobby Sudekum committed Dec 4, 2017
1 parent 07ca787 commit 7f1d2d2
Show file tree
Hide file tree
Showing 7 changed files with 64 additions and 10 deletions.
61 changes: 57 additions & 4 deletions MapboxDirections/MBRouteStep.swift
Original file line number Diff line number Diff line change
Expand Up @@ -421,6 +421,45 @@ public enum ManeuverDirection: Int, CustomStringConvertible {
}
}

/**
A `DrivingSide` indicates which side of the road cars and traffic flow.
*/
@objc(MBDrivingSide)
public enum DrivingSide: Int, CustomStringConvertible {
/**
Indicates driving occurs on the `left` side.
*/
case left

/**
Indicates driving occurs on the `right` side.
*/
case right

public init?(description: String) {
var side: DrivingSide
switch description {
case "left":
side = .left
case "right":
side = .right
default:
return nil
}

self.init(rawValue: side.rawValue)
}

public var description: String {
switch self {
case .left:
return "left"
case .right:
return "right"
}
}
}

extension String {
internal func tagValues(separatedBy separator: String) -> [String] {
return components(separatedBy: separator).map { $0.trimmingCharacters(in: .whitespaces) }.filter { !$0.isEmpty }
Expand Down Expand Up @@ -488,7 +527,7 @@ struct Road {
open class RouteStep: NSObject, NSSecureCoding {
// MARK: Creating a Step

internal init(finalHeading: CLLocationDirection?, maneuverType: ManeuverType?, maneuverDirection: ManeuverDirection?, maneuverLocation: CLLocationCoordinate2D, name: String, coordinates: [CLLocationCoordinate2D]?, json: JSONDictionary) {
internal init(finalHeading: CLLocationDirection?, maneuverType: ManeuverType?, maneuverDirection: ManeuverDirection?, drivingSide: DrivingSide, maneuverLocation: CLLocationCoordinate2D, name: String, coordinates: [CLLocationCoordinate2D]?, json: JSONDictionary) {
transportType = TransportType(description: json["mode"] as! String)

let road = Road(name: name, ref: json["ref"] as? String, exits: json["exits"] as? String, destination: json["destinations"] as? String, rotaryName: json["rotary_name"] as? String)
Expand Down Expand Up @@ -542,6 +581,7 @@ open class RouteStep: NSObject, NSSecureCoding {

self.maneuverLocation = maneuverLocation
self.coordinates = coordinates
self.drivingSide = drivingSide
}

/**
Expand All @@ -557,6 +597,7 @@ open class RouteStep: NSObject, NSSecureCoding {
let maneuverType = ManeuverType(description: maneuver["type"] as! String)
let maneuverDirection = ManeuverDirection(description: maneuver["modifier"] as? String ?? "")
let maneuverLocation = CLLocationCoordinate2D(geoJSON: maneuver["location"] as! [Double])
let drivingSide = DrivingSide(description: json["driving_side"] as! String) ?? .right

let name = json["name"] as! String

Expand All @@ -570,7 +611,7 @@ open class RouteStep: NSObject, NSSecureCoding {
coordinates = nil
}

self.init(finalHeading: finalHeading, maneuverType: maneuverType, maneuverDirection: maneuverDirection, maneuverLocation: maneuverLocation, name: name, coordinates: coordinates, json: json)
self.init(finalHeading: finalHeading, maneuverType: maneuverType, maneuverDirection: maneuverDirection, drivingSide: drivingSide, maneuverLocation: maneuverLocation, name: name, coordinates: coordinates, json: json)
}

public required init?(coder decoder: NSCoder) {
Expand Down Expand Up @@ -602,6 +643,12 @@ open class RouteStep: NSObject, NSSecureCoding {
maneuverDirection = nil
}

if let drivingSideDescription = decoder.decodeObject(of: NSString.self, forKey: "drivingSide") as String?, let drivingSide = DrivingSide(description: drivingSideDescription) {
self.drivingSide = drivingSide
} else {
self.drivingSide = .right
}

if let maneuverLocationDictionary = decoder.decodeObject(of: [NSDictionary.self, NSString.self, NSNumber.self], forKey: "maneuverLocation") as? [String: CLLocationDegrees],
let latitude = maneuverLocationDictionary["latitude"],
let longitude = maneuverLocationDictionary["longitude"] {
Expand Down Expand Up @@ -655,6 +702,7 @@ open class RouteStep: NSObject, NSSecureCoding {

coder.encode(maneuverType?.description, forKey: "maneuverType")
coder.encode(maneuverDirection?.description, forKey: "maneuverDirection")
coder.encode(drivingSide.description, forKey: "drivingSide")

coder.encode(intersections, forKey: "intersections")

Expand Down Expand Up @@ -785,6 +833,11 @@ open class RouteStep: NSObject, NSSecureCoding {
*/
open let maneuverDirection: ManeuverDirection?

/**
Indicates what side of a bidirectional road the driver must be driving on. Also referred to as the rule of the road.
*/
open let drivingSide: DrivingSide

/**
The location of the maneuver at the beginning of this step.
*/
Expand Down Expand Up @@ -952,10 +1005,10 @@ internal class RouteStepV4: RouteStep {
let maneuverType = ManeuverType(v4Description: maneuver["type"] as! String)
let maneuverDirection = ManeuverDirection(v4TypeDescription: maneuver["type"] as! String)
let maneuverLocation = CLLocationCoordinate2D(geoJSON: maneuver["location"] as! JSONDictionary)

let drivingSide = DrivingSide(description: json["driving_side"] as! String) ?? .right
let name = json["way_name"] as! String

self.init(finalHeading: heading, maneuverType: maneuverType, maneuverDirection: maneuverDirection, maneuverLocation: maneuverLocation, name: name, coordinates: nil, json: json)
self.init(finalHeading: heading, maneuverType: maneuverType, maneuverDirection: maneuverDirection, drivingSide: drivingSide, maneuverLocation: maneuverLocation, name: name, coordinates: nil, json: json)
}
}

Expand Down
2 changes: 1 addition & 1 deletion MapboxDirectionsTests/Fixtures/instructions.json

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

3 changes: 2 additions & 1 deletion MapboxDirectionsTests/RouteStepTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ class RouteStepTests: XCTestCase {
"pronunciation": "ˈaɪˌfoʊ̯n ˈtɛn",
] as [String: Any]

let step = RouteStep(finalHeading: 59, maneuverType: .reachFork, maneuverDirection: .left, maneuverLocation: CLLocationCoordinate2D(latitude: 37.853913, longitude: -122.220694), name: "", coordinates: coordinates, json: json)
let step = RouteStep(finalHeading: 59, maneuverType: .reachFork, maneuverDirection: .left, drivingSide: .left, maneuverLocation: CLLocationCoordinate2D(latitude: 37.853913, longitude: -122.220694), name: "", coordinates: coordinates, json: json)

// Encode and decode the route step securely
// This may raise an Obj-C exception if an error is encountered which will fail the tests
Expand Down Expand Up @@ -158,5 +158,6 @@ class RouteStepTests: XCTestCase {
XCTAssertEqual(unarchivedStep.destinations ?? [], step.destinations ?? [])
XCTAssertEqual(unarchivedStep.instructionsSpokenAlongStep ?? [], step.instructionsSpokenAlongStep ?? [])
XCTAssertEqual(unarchivedStep.instructionsDisplayedAlongStep ?? [], step.instructionsDisplayedAlongStep ?? [])
XCTAssertEqual(unarchivedStep.drivingSide, DrivingSide.left)
}
}

0 comments on commit 7f1d2d2

Please sign in to comment.