-
Notifications
You must be signed in to change notification settings - Fork 322
/
Copy pathLanesView.swift
234 lines (195 loc) · 8.61 KB
/
LanesView.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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
import UIKit
import MapboxCoreNavigation
import MapboxDirections
/// :nodoc:
@IBDesignable
open class LanesView: UIView, NavigationComponent {
// MARK: Displaying the Lanes
public var isCurrentlyVisible: Bool = false
/**
A vertical separator for the trailing side of the view.
*/
var trailingSeparatorView: SeparatorView!
/**
A closure that is called after either presenting or dismissing lanes view.
- parameter completed: Boolean value that indicates whether or not the animation actually
finished before the completion handler was called.
*/
public typealias CompletionHandler = (_ completed: Bool) -> Void
/**
Updates the tertiary instructions banner info with a given `VisualInstructionBanner`.
- parameter visualInstruction: Current instruction, which will be displayed in the lanes view.
- parameter animated: If `true`, lanes view presentation or dismissal is animated.
- parameter duration: Duration of the animation (in seconds). In case if `animated` parameter
is set to `false` this value is ignored.
- parameter completion: A completion handler that will be called once lanes view is either shown or hidden.
*/
public func update(for visualInstruction: VisualInstructionBanner?,
animated: Bool = true,
duration: TimeInterval = 0.5,
completion: CompletionHandler? = nil) {
clearLaneViews()
guard let tertiaryInstruction = visualInstruction?.tertiaryInstruction else {
hide(animated: animated,
duration: duration) { completed in
completion?(completed)
}
return
}
let subviews = tertiaryInstruction.components.compactMap { (component) -> LaneView? in
if case let .lane(indications: indications,
isUsable: isUsable,
preferredDirection: preferredDirection) = component {
let maneuverDirection = preferredDirection ?? visualInstruction?.primaryInstruction.maneuverDirection
return LaneView(indications: indications,
isUsable: isUsable,
direction: maneuverDirection)
} else {
return nil
}
}
guard !subviews.isEmpty && subviews.contains(where: { $0.isUsable }) else {
hide(animated: animated,
duration: duration) { completed in
completion?(completed)
}
return
}
stackView.addArrangedSubviews(subviews)
show(animated: animated,
duration: duration) { completed in
completion?(completed)
}
}
/**
Shows lanes view.
- parameter animated: If `true`, lanes view presentation is animated. Defaults to `true`.
- parameter duration: Duration of the animation (in seconds). In case if `animated` parameter
is set to `false` this value is ignored.
- parameter completion: A completion handler that will be called once lanes view is shown.
*/
public func show(animated: Bool = true,
duration: TimeInterval = 0.5,
completion: CompletionHandler? = nil) {
guard isHidden else {
completion?(true)
return
}
if animated {
UIView.defaultAnimation(duration, animations: {
self.isCurrentlyVisible = true
self.isHidden = false
}) { completed in
completion?(completed)
}
} else {
isHidden = false
completion?(true)
}
}
/**
Hides lanes view.
- parameter animated: If `true`, lanes view dismissal is animated. Defaults to `true`.
- parameter duration: Duration of the animation (in seconds). In case if `animated` parameter
is set to `false` this value is ignored.
- parameter completion: A completion handler that will be called after lanes view dismissal.
*/
public func hide(animated: Bool = true,
duration: TimeInterval = 0.5,
completion: CompletionHandler? = nil) {
guard !isHidden else {
completion?(true)
return
}
if animated {
UIView.defaultAnimation(duration, animations: {
self.isCurrentlyVisible = false
self.isHidden = true
}) { completed in
completion?(completed)
}
} else {
isHidden = true
completion?(true)
}
}
fileprivate func clearLaneViews() {
stackView.arrangedSubviews.forEach {
stackView.removeArrangedSubview($0)
$0.removeFromSuperview()
}
}
// MARK: NavigationComponent Implementation
public func navigationService(_ service: NavigationService,
didPassVisualInstructionPoint instruction: VisualInstructionBanner,
routeProgress: RouteProgress) {
update(for: instruction)
}
// MARK: View Display
weak var stackView: UIStackView!
weak var separatorView: SeparatorView!
func laneArrowView() -> LaneView {
let view = LaneView(frame: CGRect(origin: .zero, size: CGSize(width: 30, height: 30)))
view.backgroundColor = .clear
return view
}
override init(frame: CGRect) {
super.init(frame: frame)
commonInit()
}
public required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
commonInit()
}
open override func prepareForInterfaceBuilder() {
super.prepareForInterfaceBuilder()
for _ in 0...4 {
let laneView = laneArrowView()
stackView.addArrangedSubview(laneView)
}
}
func commonInit() {
translatesAutoresizingMaskIntoConstraints = false
let heightConstraint = heightAnchor.constraint(equalToConstant: 40)
heightConstraint.priority = UILayoutPriority(rawValue: 999)
heightConstraint.isActive = true
let stackView = UIStackView(arrangedSubviews: [])
stackView.axis = .horizontal
stackView.semanticContentAttribute = .spatial
stackView.spacing = 4
stackView.distribution = .equalCentering
stackView.alignment = .center
stackView.translatesAutoresizingMaskIntoConstraints = false
addSubview(stackView)
self.stackView = stackView
stackView.topAnchor.constraint(equalTo: topAnchor).isActive = true
stackView.bottomAnchor.constraint(equalTo: bottomAnchor).isActive = true
stackView.centerXAnchor.constraint(equalTo: centerXAnchor).isActive = true
let separatorView = SeparatorView()
separatorView.translatesAutoresizingMaskIntoConstraints = false
addSubview(separatorView)
self.separatorView = separatorView
separatorView.heightAnchor.constraint(equalToConstant: 1 / UIScreen.main.scale).isActive = true
separatorView.leadingAnchor.constraint(equalTo: leadingAnchor).isActive = true
separatorView.bottomAnchor.constraint(equalTo: bottomAnchor).isActive = true
separatorView.trailingAnchor.constraint(equalTo: trailingAnchor).isActive = true
let trailingSeparatorView = SeparatorView()
trailingSeparatorView.translatesAutoresizingMaskIntoConstraints = false
addSubview(trailingSeparatorView)
self.trailingSeparatorView = trailingSeparatorView
trailingSeparatorView.widthAnchor.constraint(equalToConstant: 1 / UIScreen.main.scale).isActive = true
trailingSeparatorView.topAnchor.constraint(equalTo: topAnchor).isActive = true
trailingSeparatorView.bottomAnchor.constraint(equalTo: bottomAnchor).isActive = true
trailingSeparatorView.trailingAnchor.constraint(equalTo: trailingAnchor).isActive = true
}
open override func traitCollectionDidChange(_ previousTraitCollection: UITraitCollection?) {
super.traitCollectionDidChange(previousTraitCollection)
if previousTraitCollection == traitCollection { return }
// Do not show trailing separator view in case of regular layout.
if traitCollection.verticalSizeClass == .regular {
trailingSeparatorView.isHidden = true
} else {
trailingSeparatorView.isHidden = false
}
}
}