-
Notifications
You must be signed in to change notification settings - Fork 92
/
MBVisualInstructionComponent.swift
123 lines (99 loc) · 4.6 KB
/
MBVisualInstructionComponent.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
#if os(OSX)
import Cocoa
#elseif os(watchOS)
import WatchKit
#else
import UIKit
#endif
/**
A component of a `VisualInstruction` that represents a single run of similarly formatted text or an image with a textual fallback representation.
*/
@objc(MBVisualInstructionComponent)
open class VisualInstructionComponent: NSObject, ComponentRepresentable {
/**
The URL to an image representation of this component.
The URL refers to an image that uses the device’s native screen scale.
*/
@objc public var imageURL: URL?
/**
An abbreviated representation of the `text` property.
*/
@objc public var abbreviation: String?
/**
The priority for which the component should be abbreviated.
A component with a lower abbreviation priority value should be abbreviated before a component with a higher abbreviation priority value.
*/
@objc public var abbreviationPriority: Int = NSNotFound
/**
The plain text representation of this component.
Use this property if `imageURL` is `nil` or if the URL contained in that property is not yet available.
*/
@objc public var text: String?
/**
The type of visual instruction component. You can display the component differently depending on its type.
*/
@objc public var type: VisualInstructionComponentType
public static var supportsSecureCoding: Bool = true
/**
Initializes a new visual instruction component object based on the given JSON dictionary representation.
- parameter json: A JSON object that conforms to the [banner component](https://docs.mapbox.com/api/navigation/#banner-instruction-object) format described in the Directions API documentation.
*/
@objc(initWithJSON:)
public convenience init(json: [String: Any]) {
let text = json["text"] as? String
let type = VisualInstructionComponentType(description: json["type"] as? String ?? "") ?? .text
let abbreviation = json["abbr"] as? String
let abbreviationPriority = json["abbr_priority"] as? Int ?? NSNotFound
var imageURL: URL?
if let baseURL = json["imageBaseURL"] as? String, !baseURL.isEmpty {
let scale: CGFloat
#if os(OSX)
scale = NSScreen.main?.backingScaleFactor ?? 1
#elseif os(watchOS)
scale = WKInterfaceDevice.current().screenScale
#else
scale = UIScreen.main.scale
#endif
imageURL = URL(string: "\(baseURL)@\(Int(scale))x.png")
}
self.init(type: type, text: text, imageURL: imageURL, abbreviation: abbreviation, abbreviationPriority: abbreviationPriority)
}
/**
Initializes a new visual instruction component object that displays the given information.
- parameter type: The type of visual instruction component.
- parameter text: The plain text representation of this component.
- parameter imageURL: The URL to an image representation of this component.
- parameter abbreviation: An abbreviated representation of `text`.
- parameter abbreviationPriority: The priority for which the component should be abbreviated.
*/
@objc public init(type: VisualInstructionComponentType, text: String?, imageURL: URL?, abbreviation: String?, abbreviationPriority: Int) {
self.text = text
self.type = type
self.imageURL = imageURL
self.abbreviation = abbreviation
self.abbreviationPriority = abbreviationPriority
}
@objc public required init?(coder decoder: NSCoder) {
self.text = decoder.decodeObject(of: NSString.self, forKey: "text") as String?
guard let typeString = decoder.decodeObject(of: NSString.self, forKey: "type") as String?, let type = VisualInstructionComponentType(description: typeString) else {
return nil
}
self.type = type
guard let imageURL = decoder.decodeObject(of: NSURL.self, forKey: "imageURL") as URL? else {
return nil
}
self.imageURL = imageURL
guard let abbreviation = decoder.decodeObject(of: NSString.self, forKey: "abbreviation") as String? else {
return nil
}
self.abbreviation = abbreviation
abbreviationPriority = decoder.decodeInteger(forKey: "abbreviationPriority")
}
public func encode(with coder: NSCoder) {
coder.encode(text, forKey: "text")
coder.encode(type.description, forKey: "type")
coder.encode(imageURL, forKey: "imageURL")
coder.encode(abbreviation, forKey: "abbreviation")
coder.encode(abbreviationPriority, forKey: "abbreviationPriority")
}
}