-
Notifications
You must be signed in to change notification settings - Fork 321
/
Copy pathCustomViewController.swift
281 lines (222 loc) · 11.3 KB
/
CustomViewController.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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
import UIKit
import MapboxCoreNavigation
import MapboxNavigation
import Mapbox
import CoreLocation
import AVFoundation
import MapboxDirections
import Turf
class CustomViewController: UIViewController, MGLMapViewDelegate {
var destination: MGLPointAnnotation!
let directions = Directions.shared
var navigationService: NavigationService!
var simulateLocation = false
var userRoute: Route?
// Start voice instructions
var voiceController: MapboxVoiceController!
var stepsViewController: StepsViewController?
// Preview index of step, this will be nil if we are not previewing an instruction
var previewStepIndex: Int?
// View that is placed over the instructions banner while we are previewing
var previewInstructionsView: StepInstructionsView?
@IBOutlet var mapView: NavigationMapView!
@IBOutlet weak var cancelButton: UIButton!
@IBOutlet weak var instructionsBannerView: InstructionsBannerView!
lazy var feedbackViewController: FeedbackViewController = {
return FeedbackViewController(eventsManager: navigationService.eventsManager)
}()
override func viewDidLoad() {
super.viewDidLoad()
let locationManager = simulateLocation ? SimulatedLocationManager(route: userRoute!) : NavigationLocationManager()
navigationService = MapboxNavigationService(route: userRoute!, locationSource: locationManager, simulating: simulateLocation ? .always : .onPoorGPS)
voiceController = MapboxVoiceController(navigationService: navigationService)
mapView.delegate = self
mapView.compassView.isHidden = true
instructionsBannerView.delegate = self
instructionsBannerView.swipeable = true
// Add listeners for progress updates
resumeNotifications()
// Start navigation
navigationService.start()
// Center map on user
mapView.recenterMap()
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
// This applies a default style to the top banner.
DayStyle().apply()
}
deinit {
suspendNotifications()
}
func resumeNotifications() {
NotificationCenter.default.addObserver(self, selector: #selector(progressDidChange(_ :)), name: .routeControllerProgressDidChange, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(rerouted(_:)), name: .routeControllerDidReroute, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(updateInstructionsBanner(notification:)), name: .routeControllerDidPassVisualInstructionPoint, object: navigationService.router)
}
func suspendNotifications() {
NotificationCenter.default.removeObserver(self, name: .routeControllerProgressDidChange, object: nil)
NotificationCenter.default.removeObserver(self, name: .routeControllerWillReroute, object: nil)
NotificationCenter.default.removeObserver(self, name: .routeControllerDidPassVisualInstructionPoint, object: nil)
}
func mapView(_ mapView: MGLMapView, didFinishLoading style: MGLStyle) {
self.mapView.show([navigationService.route])
}
// Notifications sent on all location updates
@objc func progressDidChange(_ notification: NSNotification) {
// do not update if we are previewing instruction steps
guard previewInstructionsView == nil else { return }
let routeProgress = notification.userInfo![RouteControllerNotificationUserInfoKey.routeProgressKey] as! RouteProgress
let location = notification.userInfo![RouteControllerNotificationUserInfoKey.locationKey] as! CLLocation
// Add maneuver arrow
if routeProgress.currentLegProgress.followOnStep != nil {
mapView.addArrow(route: routeProgress.route, legIndex: routeProgress.legIndex, stepIndex: routeProgress.currentLegProgress.stepIndex + 1)
} else {
mapView.removeArrow()
}
// Update the top banner with progress updates
instructionsBannerView.updateDistance(for: routeProgress.currentLegProgress.currentStepProgress)
instructionsBannerView.isHidden = false
// Update the user puck
mapView.updateCourseTracking(location: location, animated: true)
}
@objc func updateInstructionsBanner(notification: NSNotification) {
guard let routeProgress = notification.userInfo?[RouteControllerNotificationUserInfoKey.routeProgressKey] as? RouteProgress else { return }
instructionsBannerView.update(for: routeProgress.currentLegProgress.currentStepProgress.currentVisualInstruction)
}
// Fired when the user is no longer on the route.
// Update the route on the map.
@objc func rerouted(_ notification: NSNotification) {
self.mapView.show([navigationService.route])
}
@IBAction func cancelButtonPressed(_ sender: Any) {
self.dismiss(animated: true, completion: nil)
}
@IBAction func recenterMap(_ sender: Any) {
mapView.recenterMap()
}
@IBAction func showFeedback(_ sender: Any) {
present(feedbackViewController, animated: true, completion: nil)
}
func toggleStepsList() {
// remove the preview banner while viewing the steps list
removePreviewInstruction()
if let controller = stepsViewController {
controller.dismiss()
stepsViewController = nil
} else {
guard let service = navigationService else { return }
let controller = StepsViewController(routeProgress: service.routeProgress)
controller.delegate = self
addChild(controller)
view.addSubview(controller.view)
controller.view.topAnchor.constraint(equalTo: instructionsBannerView.bottomAnchor).isActive = true
controller.view.leadingAnchor.constraint(equalTo: view.leadingAnchor).isActive = true
controller.view.bottomAnchor.constraint(equalTo: view.bottomAnchor).isActive = true
controller.view.trailingAnchor.constraint(equalTo: view.trailingAnchor).isActive = true
controller.didMove(toParent: self)
view.layoutIfNeeded()
stepsViewController = controller
return
}
}
func addPreviewInstructions(step: RouteStep) {
let route = navigationService.route
// find the leg that contains the step, legIndex, and stepIndex
guard let leg = route.legs.first(where: { $0.steps.contains(step) }),
let legIndex = route.legs.firstIndex(of: leg),
let stepIndex = leg.steps.firstIndex(of: step) else {
return
}
// find the upcoming manuever step, and update instructions banner to show preview
guard stepIndex + 1 < leg.steps.endIndex else { return }
let maneuverStep = leg.steps[stepIndex + 1]
updatePreviewBannerWith(step: step, maneuverStep: maneuverStep)
// stop tracking user, and move camera to step location
mapView.tracksUserCourse = false
mapView.userTrackingMode = .none
mapView.enableFrameByFrameCourseViewTracking(for: 1)
mapView.setCenter(maneuverStep.maneuverLocation, zoomLevel: mapView.zoomLevel, direction: maneuverStep.initialHeading!, animated: true, completionHandler: nil)
// add arrow to map for preview instruction
mapView.addArrow(route: route, legIndex: legIndex, stepIndex: stepIndex + 1)
}
func updatePreviewBannerWith(step: RouteStep, maneuverStep: RouteStep) {
// remove preview banner if it exists
removePreviewInstruction()
// grab the last instruction for step
guard let instructions = step.instructionsDisplayedAlongStep?.last else { return }
// create a StepInstructionsView and display that over the current instructions banner
let previewInstructionsView = StepInstructionsView(frame: instructionsBannerView.frame)
previewInstructionsView.delegate = self
previewInstructionsView.swipeable = true
previewInstructionsView.backgroundColor = instructionsBannerView.backgroundColor
view.addSubview(previewInstructionsView)
// update instructions banner to show all information about this step
previewInstructionsView.updateDistance(for: RouteStepProgress(step: step))
previewInstructionsView.update(for: instructions)
self.previewInstructionsView = previewInstructionsView
}
func removePreviewInstruction() {
guard let view = previewInstructionsView else { return }
view.removeFromSuperview()
// reclaim the delegate, from the preview banner
instructionsBannerView.delegate = self
// nil out both the view and index
previewInstructionsView = nil
previewStepIndex = nil
}
}
extension CustomViewController: InstructionsBannerViewDelegate {
func didTapInstructionsBanner(_ sender: BaseInstructionsBannerView) {
toggleStepsList()
}
func didSwipeInstructionsBanner(_ sender: BaseInstructionsBannerView, swipeDirection direction: UISwipeGestureRecognizer.Direction) {
if direction == .down {
toggleStepsList()
return
}
// preventing swiping if the steps list is visible
guard stepsViewController == nil else { return }
// Make sure that we actually have remaining steps left
guard let remainingSteps = navigationService?.routeProgress.remainingSteps else { return }
var previewIndex = -1
var previewStep: RouteStep?
if direction == .left {
// get the next step from our current preview step index
if let currentPreviewIndex = previewStepIndex {
previewIndex = currentPreviewIndex + 1
} else {
previewIndex = 0
}
// index is out of bounds, we have no step to show
guard previewIndex < remainingSteps.count else { return }
previewStep = remainingSteps[previewIndex]
} else {
// we are already at step 0, no need to show anything
guard let currentPreviewIndex = previewStepIndex else { return }
if currentPreviewIndex > 0 {
previewIndex = currentPreviewIndex - 1
previewStep = remainingSteps[previewIndex]
} else {
previewStep = navigationService.routeProgress.currentLegProgress.currentStep
previewIndex = -1
}
}
if let step = previewStep {
addPreviewInstructions(step: step)
previewStepIndex = previewIndex
}
}
}
extension CustomViewController: StepsViewControllerDelegate {
func didDismissStepsViewController(_ viewController: StepsViewController) {
viewController.dismiss { [weak self] in
self?.stepsViewController = nil
}
}
func stepsViewController(_ viewController: StepsViewController, didSelect legIndex: Int, stepIndex: Int, cell: StepTableViewCell) {
viewController.dismiss { [weak self] in
self?.stepsViewController = nil
}
}
}