Skip to content

Commit

Permalink
Add maneuver and modifier to visual instructions (#239)
Browse files Browse the repository at this point in the history
  • Loading branch information
Bobby Sudekum authored Feb 16, 2018
1 parent fc3e74b commit be056cd
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 28 deletions.
2 changes: 1 addition & 1 deletion MapboxDirections/MBRouteStep.swift
Original file line number Diff line number Diff line change
Expand Up @@ -600,7 +600,7 @@ open class RouteStep: NSObject, NSSecureCoding {
self.instructionsSpokenAlongStep = voiceInstructionsJSON?.map { SpokenInstruction(json: $0) }

let instructionsDisplayedAlongStep = json["bannerInstructions"] as? [JSONDictionary]
self.instructionsDisplayedAlongStep = instructionsDisplayedAlongStep?.map { VisualInstruction(json: $0) }
self.instructionsDisplayedAlongStep = instructionsDisplayedAlongStep?.map { VisualInstruction(json: $0, drivingSide: drivingSide) }

initialHeading = maneuver["bearing_before"] as? Double
self.finalHeading = finalHeading
Expand Down
32 changes: 26 additions & 6 deletions MapboxDirections/MBVisualInstruction.swift
Original file line number Diff line number Diff line change
Expand Up @@ -41,41 +41,53 @@ open class VisualInstruction: NSObject, NSSecureCoding {
*/
@objc public let secondaryTextComponents: [VisualInstructionComponent]?


/**
:nodoc:
Indicates what side of a bidirectional road the driver must be driving on. Also referred to as the rule of the road.
*/
@objc public var drivingSide: DrivingSide

/**
:nodoc:
Initialize a `VisualInstruction` from a dictionary.
Initialize a `VisualInstruction` from a dictionary given a `DrivingSide`.
*/
@objc public convenience init(json: [String: Any]) {
@objc public convenience init(json: [String: Any], drivingSide: DrivingSide) {
let distanceAlongStep = json["distanceAlongGeometry"] as! CLLocationDistance

let primaryTextComponent = json["primary"] as! JSONDictionary
let primaryText = primaryTextComponent["text"] as! String
let primaryManeuverType = ManeuverType(description: primaryTextComponent["type"] as! String) ?? .none
let primaryManeuverDirection = ManeuverDirection(description: primaryTextComponent["modifier"] as! String) ?? .none
let primaryTextComponents = (primaryTextComponent["components"] as! [JSONDictionary]).map {
VisualInstructionComponent(json: $0)
VisualInstructionComponent(maneuverType: primaryManeuverType, maneuverDirection: primaryManeuverDirection, json: $0)
}

var secondaryText: String?
var secondaryTextComponents: [VisualInstructionComponent]?
if let secondaryTextComponent = json["secondary"] as? JSONDictionary {
secondaryText = secondaryTextComponent["text"] as? String
let secondaryManeuverType = ManeuverType(description: secondaryTextComponent["type"] as! String) ?? .none
let secondaryManeuverDirection = ManeuverDirection(description: secondaryTextComponent["modifier"] as! String) ?? .none
secondaryTextComponents = (secondaryTextComponent["components"] as! [JSONDictionary]).map {
VisualInstructionComponent(json: $0)
VisualInstructionComponent(maneuverType: secondaryManeuverType, maneuverDirection: secondaryManeuverDirection, json: $0)
}
}

self.init(distanceAlongStep: distanceAlongStep, primaryText: primaryText, primaryTextComponents: primaryTextComponents, secondaryText: secondaryText, secondaryTextComponents: secondaryTextComponents)
self.init(distanceAlongStep: distanceAlongStep, primaryText: primaryText, primaryTextComponents: primaryTextComponents, secondaryText: secondaryText, secondaryTextComponents: secondaryTextComponents, drivingSide: drivingSide)
}

/**
:nodoc:
Initialize a `VisualInstruction`.
*/
@objc public init(distanceAlongStep: CLLocationDistance, primaryText: String, primaryTextComponents: [VisualInstructionComponent], secondaryText: String?, secondaryTextComponents: [VisualInstructionComponent]?) {
@objc public init(distanceAlongStep: CLLocationDistance, primaryText: String, primaryTextComponents: [VisualInstructionComponent], secondaryText: String?, secondaryTextComponents: [VisualInstructionComponent]?, drivingSide: DrivingSide) {
self.distanceAlongStep = distanceAlongStep
self.primaryText = primaryText
self.primaryTextComponents = primaryTextComponents
self.secondaryText = secondaryText
self.secondaryTextComponents = secondaryTextComponents
self.drivingSide = drivingSide
}

public required init?(coder decoder: NSCoder) {
Expand All @@ -94,6 +106,13 @@ open class VisualInstruction: NSObject, NSSecureCoding {
self.secondaryText = secondaryText

secondaryTextComponents = decoder.decodeObject(of: [NSArray.self, VisualInstructionComponent.self], forKey: "primaryTextComponents") as? [VisualInstructionComponent]


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

open static var supportsSecureCoding = true
Expand All @@ -104,5 +123,6 @@ open class VisualInstruction: NSObject, NSSecureCoding {
coder.encode(primaryTextComponents, forKey: "primaryTextComponents")
coder.encode(secondaryText, forKey: "secondaryText")
coder.encode(secondaryTextComponents, forKey: "secondaryTextComponents")
coder.encode(drivingSide, forKey: "drivingSide")
}
}
35 changes: 30 additions & 5 deletions MapboxDirections/MBVisualInstructionComponent.swift
Original file line number Diff line number Diff line change
Expand Up @@ -31,20 +31,31 @@ open class VisualInstructionComponent: NSObject, NSSecureCoding {
*/
@objc public var imageURL: URL?


/**
:nodoc:
The type of visual instruction component. You can display the component differently depending on its type.
*/
@objc public var type: VisualInstructionComponentType

/**
:nodoc:
The maneuver type for the `VisualInstruction`.
*/
@objc public var maneuverType: ManeuverType

/**
:nodoc:
The modifier type for the `VisualInstruction`.
*/
@objc public var maneuverDirection: ManeuverDirection

/**
:nodoc:
Initialize A `VisualInstructionComponent`.
*/
@objc public convenience init(json: [String: Any]) {
@objc public convenience init(maneuverType: ManeuverType, maneuverDirection: ManeuverDirection, json: [String: Any]) {
let text = json["text"] as? String
var type: VisualInstructionComponentType?
let type: VisualInstructionComponentType
if let _ = json["delimiter"] as? Bool {
type = .delimiter
} else {
Expand All @@ -64,17 +75,19 @@ open class VisualInstructionComponent: NSObject, NSSecureCoding {
imageURL = URL(string: "\(baseURL)@\(Int(scale))x.png")
}

self.init(type: type!, text: text, imageURL: imageURL)
self.init(type: type, text: text, imageURL: imageURL, maneuverType: maneuverType, maneuverDirection: maneuverDirection)
}

/**
:nodoc:
Initialize A `VisualInstructionComponent`.
*/
@objc public init(type: VisualInstructionComponentType, text: String?, imageURL: URL?) {
@objc public init(type: VisualInstructionComponentType, text: String?, imageURL: URL?, maneuverType: ManeuverType, maneuverDirection: ManeuverDirection) {
self.text = text
self.imageURL = imageURL
self.type = type
self.maneuverType = maneuverType
self.maneuverDirection = maneuverDirection
}

@objc public required init?(coder decoder: NSCoder) {
Expand All @@ -92,6 +105,16 @@ open class VisualInstructionComponent: NSObject, NSSecureCoding {
return nil
}
self.type = type

guard let maneuverTypeString = decoder.decodeObject(of: NSString.self, forKey: "maneuverType") as String?, let maneuverType = ManeuverType(description: maneuverTypeString) else {
return nil
}
self.maneuverType = maneuverType

guard let direction = decoder.decodeObject(of: NSString.self, forKey: "maneuverDirection") as String?, let maneuverDirection = ManeuverDirection(description: direction) else {
return nil
}
self.maneuverDirection = maneuverDirection
}

open static var supportsSecureCoding = true
Expand All @@ -100,5 +123,7 @@ open class VisualInstructionComponent: NSObject, NSSecureCoding {
coder.encode(text, forKey: "text")
coder.encode(imageURL, forKey: "imageURL")
coder.encode(type, forKey: "type")
coder.encode(maneuverType, forKey: "maneuverType")
coder.encode(maneuverDirection, forKey: "maneuverDirection")
}
}
Loading

0 comments on commit be056cd

Please sign in to comment.