Skip to content

Commit

Permalink
Add abbreviation support (#244)
Browse files Browse the repository at this point in the history
* Add abbreviation support

* use Int.max since zero is the highest priority

* use nsnotfound

* Test

* add test
  • Loading branch information
Bobby Sudekum committed Mar 7, 2018
1 parent 6019b44 commit 2e9f8d2
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 19 deletions.
28 changes: 26 additions & 2 deletions MapboxDirections/MBVisualInstructionComponent.swift
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,16 @@ open class VisualInstructionComponent: NSObject, NSSecureCoding {
*/
@objc public var maneuverDirection: ManeuverDirection

/**
An abbreviated version of the text for a given component.
*/
@objc public var abbreviation: String?

/**
The priority in which the component should be abbreviated. Lower numbers should be abbreviated first.
*/
@objc public var abbreviationPriority: Int = NSNotFound

/**
:nodoc:
Initialize A `VisualInstructionComponent`.
Expand All @@ -62,6 +72,9 @@ open class VisualInstructionComponent: NSObject, NSSecureCoding {
type = .destination
}

let abbreviation = json["abbr"] as? String
let abbreviationPriority = json["abbr_priority"] as? Int ?? NSNotFound

var imageURL: URL?
if let baseURL = json["imageBaseURL"] as? String {
let scale: CGFloat
Expand All @@ -75,19 +88,21 @@ open class VisualInstructionComponent: NSObject, NSSecureCoding {
imageURL = URL(string: "\(baseURL)@\(Int(scale))x.png")
}

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

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

@objc public required init?(coder decoder: NSCoder) {
Expand Down Expand Up @@ -115,6 +130,13 @@ open class VisualInstructionComponent: NSObject, NSSecureCoding {
return nil
}
self.maneuverDirection = maneuverDirection

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

abbreviationPriority = decoder.decodeInteger(forKey: "abbreviationPriority")
}

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

0 comments on commit 2e9f8d2

Please sign in to comment.