-
Notifications
You must be signed in to change notification settings - Fork 325
Expand file tree
/
Copy pathCarPlayNavigationViewController.swift
More file actions
882 lines (707 loc) · 41.5 KB
/
Copy pathCarPlayNavigationViewController.swift
File metadata and controls
882 lines (707 loc) · 41.5 KB
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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
import Foundation
import MapboxDirections
import MapboxCoreNavigation
@_spi(Restricted) import MapboxMaps
import os.log
#if canImport(CarPlay)
import CarPlay
/**
`CarPlayNavigationViewController` is a fully-featured turn-by-turn navigation UI for CarPlay.
- seealso: `NavigationViewController`
*/
@available(iOS 12.0, *)
open class CarPlayNavigationViewController: UIViewController, BuildingHighlighting {
// MARK: Child Views and Styling Configuration
/**
A view indicating what direction the vehicle is traveling towards, snapped
to eight cardinal directions in steps of 45°.
This view is hidden by default.
*/
public var compassView: CarPlayCompassView!
/**
A view that displays the current speed limit.
*/
public var speedLimitView: SpeedLimitView!
/**
A view that displays the current road name.
*/
public var wayNameView: WayNameView!
/**
The interface styles available for display.
These are the styles available to the view controller’s internal `StyleManager` object. In CarPlay, `Style` objects primarily affect the appearance of the map, not guidance-related overlay views.
*/
public var styles: [Style] {
didSet {
styleManager?.styles = styles
}
}
/**
Controls whether the main route style layer and its casing disappears
as the user location puck travels over it. Defaults to `false`.
If `true`, the part of the route that has been traversed will be
rendered with full transparency, to give the illusion of a
disappearing route. To customize the color that appears on the
traversed section of a route, override the `traversedRouteColor` property
for the `NavigationMapView.appearance()`.
*/
public var routeLineTracksTraversal: Bool = false {
didSet {
navigationMapView?.routeLineTracksTraversal = routeLineTracksTraversal
}
}
/**
Controls whether night style will be used whenever traversing through a tunnel. Defaults to `true`.
*/
public var usesNightStyleWhileInTunnel: Bool = true
/**
Controls the styling of CarPlayNavigationViewController and its components.
The style can be modified programmatically by using `StyleManager.applyStyle(type:)`.
*/
public private(set) var styleManager: StyleManager?
/**
Allows to control highlighting of the destination building on arrival. By default destination buildings will not be highlighted.
*/
public var waypointStyle: WaypointStyle = .annotation
var approachingDestinationThreshold: CLLocationDistance = DefaultApproachingDestinationThresholdDistance
var passedApproachingDestinationThreshold: Bool = false
var currentLeg: RouteLeg?
var buildingWasFound: Bool = false
var mapTemplate: CPMapTemplate
var carInterfaceController: CPInterfaceController
private var isTraversingTunnel = false
private var roadNameFromStatus: String?
private var safeTrailingSpeedLimitViewConstraint: NSLayoutConstraint!
private var trailingSpeedLimitViewConstraint: NSLayoutConstraint!
private var safeTrailingCompassViewConstraint: NSLayoutConstraint!
private var trailingCompassViewConstraint: NSLayoutConstraint!
private let logger: OSLog = .init(subsystem: "com.mapbox.navigation", category: "CarPlayNavigationViewController")
func setupOrnaments() {
let compassView = CarPlayCompassView()
view.addSubview(compassView)
compassView.topAnchor.constraint(equalTo: view.safeTopAnchor, constant: 8).isActive = true
safeTrailingCompassViewConstraint = compassView.trailingAnchor.constraint(equalTo: view.safeTrailingAnchor,
constant: -8)
trailingCompassViewConstraint = compassView.trailingAnchor.constraint(equalTo: view.trailingAnchor,
constant: -8)
self.compassView = compassView
let speedLimitView = SpeedLimitView()
speedLimitView.translatesAutoresizingMaskIntoConstraints = false
view.addSubview(speedLimitView)
speedLimitView.topAnchor.constraint(equalTo: compassView.bottomAnchor, constant: 8).isActive = true
safeTrailingSpeedLimitViewConstraint = speedLimitView.trailingAnchor.constraint(equalTo: view.safeTrailingAnchor,
constant: -8)
trailingSpeedLimitViewConstraint = speedLimitView.trailingAnchor.constraint(equalTo: view.trailingAnchor,
constant: -8)
speedLimitView.widthAnchor.constraint(equalToConstant: 30).isActive = true
speedLimitView.heightAnchor.constraint(equalToConstant: 30).isActive = true
self.speedLimitView = speedLimitView
let wayNameView: WayNameView = .forAutoLayout()
wayNameView.containerView.isHidden = true
wayNameView.containerView.clipsToBounds = true
wayNameView.label.textAlignment = .center
view.addSubview(wayNameView)
NSLayoutConstraint.activate([
wayNameView.bottomAnchor.constraint(equalTo: view.safeBottomAnchor, constant: -8),
wayNameView.centerXAnchor.constraint(equalTo: view.safeCenterXAnchor),
wayNameView.widthAnchor.constraint(lessThanOrEqualTo: view.safeWidthAnchor, multiplier: 0.95)
])
self.wayNameView = wayNameView
}
func setupStyleManager() {
styleManager = StyleManager()
styleManager?.delegate = self
styleManager?.styles = self.styles
}
/**
Updates `CPMapTemplate.tripEstimateStyle` based on provided `UIUserInterfaceStyle` value.
- parameter userInterfaceStyle: `UIUserInterfaceStyle`, which is currently used on CarPlay.
*/
func updateTripEstimateStyle(_ userInterfaceStyle: UIUserInterfaceStyle) {
switch traitCollection.userInterfaceStyle {
case .dark:
os_log("TripEstimateStyle set to dark.", log: logger, type: .info)
mapTemplate.tripEstimateStyle = .dark
default:
os_log("TripEstimateStyle set to default/light.", log: logger, type: .info)
mapTemplate.tripEstimateStyle = .light
}
}
// MARK: Collecting User Feedback
/**
Provides methods for creating and sending user feedback.
*/
public var eventsManager: NavigationEventsManager {
return navigationService.eventsManager
}
var carFeedbackTemplate: CPGridTemplate!
/**
Shows the interface for providing feedback about the route.
*/
public func showFeedback() {
carInterfaceController.pushTemplate(self.carFeedbackTemplate, animated: true)
}
func createFeedbackUI() -> CPGridTemplate {
let feedbackItems: [FeedbackItem] = [
ActiveNavigationFeedbackType.looksIncorrect(subtype: nil),
ActiveNavigationFeedbackType.confusingAudio(subtype: nil),
ActiveNavigationFeedbackType.illegalRoute(subtype: nil),
ActiveNavigationFeedbackType.roadClosure(subtype: nil),
ActiveNavigationFeedbackType.routeQuality(subtype: nil),
ActiveNavigationFeedbackType.positioning
].map { $0.generateFeedbackItem() }
let feedbackButtonHandler: (_ : CPGridButton) -> Void = { [weak self] (button) in
guard let self = self else { return }
self.carInterfaceController.safePopTemplate(animated: true)
guard let feedback = self.eventsManager.createFeedback() else { return }
let foundItem = feedbackItems.filter { $0.image == button.image }
guard let feedbackItem = foundItem.first else { return }
self.eventsManager.sendFeedback(feedback, type: feedbackItem.type)
let dismissTitle = NSLocalizedString("CARPLAY_DISMISS",
bundle: .mapboxNavigation,
value: "Dismiss",
comment: "Title for dismiss button")
let submittedTitle = NSLocalizedString("CARPLAY_SUBMITTED_FEEDBACK",
bundle: .mapboxNavigation,
value: "Submitted",
comment: "Alert title that shows when feedback has been submitted")
let action = CPAlertAction(title: dismissTitle,
style: .default,
handler: { _ in })
let alert = CPNavigationAlert(titleVariants: [submittedTitle],
subtitleVariants: nil,
imageSet: nil,
primaryAction: action,
secondaryAction: nil,
duration: 2.5)
self.mapTemplate.present(navigationAlert: alert, animated: true)
}
let buttons: [CPGridButton] = feedbackItems.map {
return CPGridButton(titleVariants: [$0.title.components(separatedBy: "\n").joined(separator: " ")],
image: $0.image,
handler: feedbackButtonHandler)
}
let gridTitle = NSLocalizedString("CARPLAY_FEEDBACK",
bundle: .mapboxNavigation,
value: "Feedback",
comment: "Title for feedback template in CarPlay")
return CPGridTemplate(title: gridTitle, gridButtons: buttons)
}
func endOfRouteFeedbackTemplate() -> CPGridTemplate {
let buttonHandler: (_: CPGridButton) -> Void = { [weak self] (button) in
guard let self = self else { return }
if let title = button.titleVariants.first {
let rating = Int(title.components(separatedBy: CharacterSet.decimalDigits.inverted).joined())
let endOfRouteFeedback = EndOfRouteFeedback(rating: rating, comment: nil)
self.navigationService.endNavigation(feedback: endOfRouteFeedback)
}
self.carInterfaceController.safePopTemplate(animated: true)
self.exitNavigation()
}
var buttons: [CPGridButton] = []
let starImage = UIImage(named: "star",
in: .mapboxNavigation,
compatibleWith: nil)!
for rating in 1...5 {
let title = NSLocalizedString("RATING_STARS_FORMAT",
bundle: .mapboxNavigation,
value: "%ld star(s) set.",
comment: "Format for accessibility value of label indicating the existing rating; 1 = number of stars")
let titleVariant = String.localizedStringWithFormat(title, rating)
let button = CPGridButton(titleVariants: [titleVariant],
image: starImage,
handler: buttonHandler)
buttons.append(button)
}
let gridTitle = NSLocalizedString("CARPLAY_RATE_RIDE",
bundle: .mapboxNavigation,
value: "Rate your ride",
comment: "Title for rating template in CarPlay")
return CPGridTemplate(title: gridTitle, gridButtons: buttons)
}
func presentArrivalUI() {
let arrivalTitle = NSLocalizedString("CARPLAY_ARRIVED",
bundle: .mapboxNavigation,
value: "You have arrived",
comment: "Title on arrival action sheet")
let arrivalMessage = NSLocalizedString("CARPLAY_ARRIVED_MESSAGE",
bundle: .mapboxNavigation,
value: "What would you like to do?",
comment: "Message on arrival action sheet")
let exitTitle = NSLocalizedString("CARPLAY_EXIT_NAVIGATION",
bundle: .mapboxNavigation,
value: "Exit navigation",
comment: "Title on the exit button in the arrival form")
let exitAction = CPAlertAction(title: exitTitle, style: .cancel) { (action) in
self.exitNavigation()
self.dismiss(animated: true)
}
let rateTitle = NSLocalizedString("CARPLAY_RATE_TRIP",
bundle: .mapboxNavigation,
value: "Rate your trip",
comment: "Title on rate button in CarPlay")
let rateAction = CPAlertAction(title: rateTitle, style: .default) { (action) in
self.carInterfaceController.pushTemplate(self.endOfRouteFeedbackTemplate(), animated: true)
}
let alert = CPActionSheetTemplate(title: arrivalTitle,
message: arrivalMessage,
actions: [rateAction, exitAction])
carInterfaceController.dismissTemplate(animated: true)
carInterfaceController.presentTemplate(alert, animated: true)
}
// MARK: Navigating the Route
/**
The view controller’s delegate.
*/
public weak var delegate: CarPlayNavigationViewControllerDelegate?
/**
`CarPlayManager` instance, which contains main `UIWindow` content and is used by
`CarPlayNavigationViewController` for presentation.
*/
public var carPlayManager: CarPlayManager
/**
Provides all routing logic for the user.
See `NavigationService` for more information.
*/
public var navigationService: NavigationService
/**
The map view showing the route and the user’s location.
*/
public fileprivate(set) var navigationMapView: NavigationMapView?
var carSession: CPNavigationSession!
var currentLegIndexMapped: Int = 0
/**
Begins a navigation session along the given trip.
- parameter trip: The trip to begin navigating along.
*/
public func startNavigationSession(for trip: CPTrip) {
carSession = mapTemplate.startNavigationSession(for: trip)
}
/**
Ends the current navigation session.
- parameter canceled: A Boolean value indicating whether this method is being called because the user intends to cancel the trip, as opposed to letting it run to completion.
*/
public func exitNavigation(byCanceling canceled: Bool = false) {
carSession.finishTrip()
dismiss(animated: true) {
self.delegate?.carPlayNavigationViewControllerDidDismiss(self, byCanceling: canceled)
}
}
/**
Creates a new CarPlay navigation view controller for the given route controller and user interface.
- parameter navigationService: The navigation service managing location updates for the navigation session.
- parameter mapTemplate: The map template visible during the navigation session.
- parameter interfaceController: The interface controller for CarPlay.
- parameter manager: The manager for CarPlay.
- parameter styles: The interface styles that the view controller’s internal `StyleManager` object can select from for display.
- postcondition: Call `startNavigationSession(for:)` after initializing this object to begin navigation.
*/
public required init(navigationService: NavigationService,
mapTemplate: CPMapTemplate,
interfaceController: CPInterfaceController,
manager: CarPlayManager,
styles: [Style]? = nil) {
self.navigationService = navigationService
self.mapTemplate = mapTemplate
self.carInterfaceController = interfaceController
self.carPlayManager = manager
self.styles = styles ?? [DayStyle(), NightStyle()]
super.init(nibName: nil, bundle: nil)
carFeedbackTemplate = createFeedbackUI()
}
public required init?(coder decoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
public override func viewDidLoad() {
super.viewDidLoad()
setupNavigationMapView()
setupOrnaments()
setupStyleManager()
observeNotifications(navigationService)
updateManeuvers(navigationService.routeProgress)
navigationService.start()
carPlayManager.delegate?.carPlayManager(carPlayManager, didBeginNavigationWith: navigationService)
currentLegIndexMapped = navigationService.router.routeProgress.legIndex
navigationMapView?.inActiveNavigation = true
navigationMapView?.simulatesLocation = navigationService.locationManager.simulatesLocation
updateTripEstimateStyle(traitCollection.userInterfaceStyle)
}
public override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
suspendNotifications()
}
public override func traitCollectionDidChange(_ previousTraitCollection: UITraitCollection?) {
super.traitCollectionDidChange(previousTraitCollection)
if previousTraitCollection?.userInterfaceStyle != traitCollection.userInterfaceStyle {
os_log("Trait collection changed.", log: logger, type: .info)
updateTripEstimateStyle(traitCollection.userInterfaceStyle)
updateManeuvers(navigationService.routeProgress)
}
}
public override func viewSafeAreaInsetsDidChange() {
super.viewSafeAreaInsetsDidChange()
// Trigger update of view constraints to correctly position views like `SpeedLimitView` and
// `CarPlayCompassView`.
view.setNeedsUpdateConstraints()
}
public override func updateViewConstraints() {
// Since there is no ability to detect current driving side mode of the CarPlay head-unit,
// two separate `NSLayoutConstraint` objects are used to prevent `SpeedLimitView` and
// `CarPlayCompassView` disappearance:
// - first one is used when driving on the right side of the road, in this case guidance and trip
// estimate panels will be on the right.
// - second one is used when driving on the left side of the road, in this case guidance and trip
// estimate panels will be on the left.
// Similar check is performed in `CarPlayMapViewController`.
if view.safeAreaInsets.right > 38.0 {
safeTrailingCompassViewConstraint.isActive = true
trailingCompassViewConstraint.isActive = false
safeTrailingSpeedLimitViewConstraint.isActive = true
trailingSpeedLimitViewConstraint.isActive = false
} else {
safeTrailingCompassViewConstraint.isActive = false
trailingCompassViewConstraint.isActive = true
safeTrailingSpeedLimitViewConstraint.isActive = false
trailingSpeedLimitViewConstraint.isActive = true
}
super.updateViewConstraints()
}
func setupNavigationMapView() {
let navigationMapView = NavigationMapView(frame: view.bounds, navigationCameraType: .carPlay)
navigationMapView.userLocationStyle = .courseView()
navigationMapView.delegate = self
navigationMapView.navigationCamera.viewportDataSource = NavigationViewportDataSource(navigationMapView.mapView,
viewportDataSourceType: .active)
navigationMapView.translatesAutoresizingMaskIntoConstraints = false
// Reapply runtime styling changes each time the style changes.
navigationMapView.mapView.mapboxMap.onEvery(.styleLoaded) { [weak self] _ in
guard let self = self else { return }
self.navigationMapView?.localizeLabels()
self.navigationMapView?.mapView.showsTraffic = false
self.updateRouteOnMap()
}
navigationMapView.mapView.ornaments.options.compass.visibility = .hidden
navigationMapView.mapView.ornaments.options.logo.visibility = .hidden
navigationMapView.mapView.ornaments.options.attributionButton.visibility = .hidden
navigationMapView.navigationCamera.follow()
view.addSubview(navigationMapView)
navigationMapView.pinInSuperview()
self.navigationMapView = navigationMapView
if let coordinate = navigationService.routeProgress.route.shape?.coordinates.first {
navigationMapView.setInitialCamera(coordinate)
}
}
// MARK: Notifications Observer Methods
func observeNotifications(_ service: NavigationService) {
NotificationCenter.default.addObserver(self,
selector: #selector(progressDidChange(_:)),
name: .routeControllerProgressDidChange,
object: service.router)
NotificationCenter.default.addObserver(self,
selector: #selector(rerouted(_:)),
name: .routeControllerDidReroute,
object: service.router)
NotificationCenter.default.addObserver(self,
selector: #selector(refresh(_:)),
name: .routeControllerDidRefreshRoute,
object: service.router)
NotificationCenter.default.addObserver(self,
selector: #selector(visualInstructionDidChange(_:)),
name: .routeControllerDidPassVisualInstructionPoint,
object: service.router)
NotificationCenter.default.addObserver(self,
selector: #selector(simulationStateDidChange(_:)),
name: .navigationServiceSimulationDidChange,
object: service)
NotificationCenter.default.addObserver(self,
selector: #selector(didUpdateRoadNameFromStatus),
name: .currentRoadNameDidChange,
object: nil)
os_log("CarPlayNavigationViewController subscribed for notifications.", log: logger, type: .info)
}
func suspendNotifications() {
NotificationCenter.default.removeObserver(self,
name: .routeControllerProgressDidChange,
object: nil)
NotificationCenter.default.removeObserver(self,
name: .routeControllerDidReroute,
object: nil)
NotificationCenter.default.removeObserver(self,
name: .routeControllerDidRefreshRoute,
object: nil)
NotificationCenter.default.removeObserver(self,
name: .routeControllerDidPassVisualInstructionPoint,
object: nil)
NotificationCenter.default.removeObserver(self,
name: .navigationServiceSimulationDidChange,
object: nil)
NotificationCenter.default.removeObserver(self,
name: .currentRoadNameDidChange,
object: nil)
os_log("CarPlayNavigationViewController suspended notifications.", log: logger, type: .info)
}
@objc func visualInstructionDidChange(_ notification: NSNotification) {
guard let routeProgress = notification.userInfo?[RouteController.NotificationUserInfoKey.routeProgressKey] as? RouteProgress else {
assertionFailure("RouteProgress should be available.")
return
}
updateManeuvers(routeProgress)
navigationMapView?.showWaypoints(on: routeProgress.route)
navigationMapView?.addArrow(route: routeProgress.route,
legIndex: routeProgress.legIndex,
stepIndex: routeProgress.currentLegProgress.stepIndex + 1)
}
@objc func progressDidChange(_ notification: NSNotification) {
guard let routeProgress = notification.userInfo?[RouteController.NotificationUserInfoKey.routeProgressKey] as? RouteProgress,
let location = notification.userInfo?[RouteController.NotificationUserInfoKey.locationKey] as? CLLocation else {
assertionFailure("RouteProgress and CLLocation should be available.")
return
}
// Check to see if we're in a tunnel.
checkTunnelState(at: location, along: routeProgress)
attemptToHighlightBuildings(routeProgress, navigationMapView: navigationMapView)
let legIndex = routeProgress.legIndex
// Update the user puck
navigationMapView?.updatePreferredFrameRate(for: routeProgress)
navigationMapView?.moveUserLocation(to: location, animated: true)
let congestionLevel = routeProgress.averageCongestionLevelRemainingOnLeg ?? .unknown
guard let maneuver = carSession.upcomingManeuvers.first else { return }
let routeDistance = Measurement(distance: routeProgress.distanceRemaining).localized()
let routeEstimates = CPTravelEstimates(distanceRemaining: routeDistance, timeRemaining: routeProgress.durationRemaining)
mapTemplate.update(routeEstimates, for: carSession.trip, with: congestionLevel.asCPTimeRemainingColor)
let stepProgress = routeProgress.currentLegProgress.currentStepProgress
let stepDistance = Measurement(distance: stepProgress.distanceRemaining).localized()
let stepEstimates = CPTravelEstimates(distanceRemaining: stepDistance, timeRemaining: stepProgress.durationRemaining)
carSession.updateEstimates(stepEstimates, for: maneuver)
if let compassView = self.compassView, !compassView.isHidden {
compassView.course = location.course
}
if let speedLimitView = speedLimitView {
speedLimitView.signStandard = routeProgress.currentLegProgress.currentStep.speedLimitSignStandard
speedLimitView.speedLimit = routeProgress.currentLegProgress.currentSpeedLimit
}
if legIndex != currentLegIndexMapped {
navigationMapView?.showWaypoints(on: routeProgress.route, legIndex: legIndex)
navigationMapView?.show([routeProgress.route], legIndex: legIndex)
currentLegIndexMapped = legIndex
}
if routeLineTracksTraversal {
if routeProgress.routeIsComplete {
navigationMapView?.removeRoutes()
}
navigationMapView?.updateUpcomingRoutePointIndex(routeProgress: routeProgress)
navigationMapView?.travelAlongRouteLine(to: location.coordinate)
}
navigationMapView?.labelCurrentRoadFeature(at: location,
router: navigationService.router,
wayNameView: wayNameView,
roadNameFromStatus: roadNameFromStatus)
}
private func checkTunnelState(at location: CLLocation, along progress: RouteProgress) {
let inTunnel = navigationService.isInTunnel(at: location, along: progress)
// Entering tunnel
if !isTraversingTunnel, inTunnel {
isTraversingTunnel = true
if usesNightStyleWhileInTunnel {
styleManager?.applyStyle(type: .night)
}
}
// Exiting tunnel
if isTraversingTunnel, !inTunnel {
isTraversingTunnel = false
styleManager?.timeOfDayChanged()
}
}
@objc func rerouted(_ notification: NSNotification) {
updateRouteOnMap()
}
@objc func refresh(_ notification: NSNotification) {
navigationMapView?.updateRouteLine(routeProgress: navigationService.routeProgress, coordinate: navigationService.router.location?.coordinate)
}
@objc func simulationStateDidChange(_ notification: NSNotification) {
guard let simulationState = notification.userInfo?[MapboxNavigationService.NotificationUserInfoKey.simulationStateKey] as? SimulationState,
let simulatedSpeedMultiplier = notification.userInfo?[MapboxNavigationService.NotificationUserInfoKey.simulatedSpeedMultiplierKey] as? Double
else { return }
os_log("Simulation state did change.", log: logger, type: .info)
switch simulationState {
case .willBeginSimulation:
navigationMapView?.storeLocationProviderBeforeSimulation()
case .didBeginSimulation:
setUpSimulatedLocationProvider(routeProgress: navigationService.routeProgress, speedMultiplier: simulatedSpeedMultiplier)
case .inSimulation:
if let simulatesLocation = navigationMapView?.simulatesLocation, !simulatesLocation {
navigationMapView?.storeLocationProviderBeforeSimulation()
}
setUpSimulatedLocationProvider(routeProgress: navigationService.routeProgress, speedMultiplier: simulatedSpeedMultiplier)
case .willEndSimulation:
navigationMapView?.useStoredLocationProvider()
case .didEndSimulation: break
case .notInSimulation:
if let simulatesLocation = navigationMapView?.simulatesLocation, simulatesLocation {
navigationMapView?.useStoredLocationProvider()
}
}
}
@objc func didUpdateRoadNameFromStatus(_ notification: Notification) {
roadNameFromStatus = notification.userInfo?[RouteController.NotificationUserInfoKey.roadNameKey] as? String
}
func setUpSimulatedLocationProvider(routeProgress: RouteProgress, speedMultiplier: Double) {
let simulatedLocationManager = SimulatedLocationManager(routeProgress: routeProgress)
simulatedLocationManager.speedMultiplier = speedMultiplier
navigationMapView?.mapView.location.overrideLocationProvider(with: NavigationLocationProvider(locationManager: simulatedLocationManager))
}
func updateRouteOnMap() {
let progress = navigationService.routeProgress
let legIndex = progress.legIndex
let nextStep = progress.currentLegProgress.stepIndex + 1
navigationMapView?.addArrow(route: progress.route, legIndex: legIndex, stepIndex: nextStep)
navigationMapView?.updateRouteLine(routeProgress: progress, coordinate: navigationService.router.location?.coordinate)
navigationMapView?.showWaypoints(on: progress.route, legIndex: legIndex)
}
func updateManeuvers(_ routeProgress: RouteProgress) {
guard let visualInstruction = routeProgress.currentLegProgress.currentStepProgress.currentVisualInstruction else { return }
let step = navigationService.routeProgress.currentLegProgress.currentStep
let primaryManeuver = CPManeuver()
let distance = Measurement(distance: step.distance).localized()
primaryManeuver.initialTravelEstimates = CPTravelEstimates(distanceRemaining: distance, timeRemaining: step.expectedTravelTime)
// Just incase, set some default text
var text = visualInstruction.primaryInstruction.text ?? step.instructions
if let secondaryText = visualInstruction.secondaryInstruction?.text {
text += "\n\(secondaryText)"
}
primaryManeuver.instructionVariants = [text]
// Add maneuver arrow
primaryManeuver.symbolSet = visualInstruction.primaryInstruction.maneuverImageSet(side: visualInstruction.drivingSide)
// Estimating the width of Apple's maneuver view
let bounds: () -> (CGRect) = {
let widthOfManeuverView = min(self.view.bounds.width - self.view.safeArea.left,
self.view.bounds.width - self.view.safeArea.right)
return CGRect(x: 0, y: 0, width: widthOfManeuverView, height: 30)
}
// Over a certain height, CarPlay devices downsize the image and CarPlay simulators hide the image.
let shieldHeight: CGFloat = 16
let maximumImageSize = CGSize(width: .infinity, height: shieldHeight)
let imageRendererFormat = UIGraphicsImageRendererFormat(for: UITraitCollection(userInterfaceIdiom: .carPlay))
if let window = carPlayManager.carWindow {
imageRendererFormat.scale = window.screen.scale
}
if let attributedPrimary = visualInstruction.primaryInstruction.carPlayManeuverLabelAttributedText(bounds: bounds,
shieldHeight: shieldHeight,
window: carPlayManager.carWindow,
instructionLabelType: PrimaryLabel.self) {
let instruction = NSMutableAttributedString(attributedString: attributedPrimary)
if let attributedSecondary = visualInstruction.secondaryInstruction?.carPlayManeuverLabelAttributedText(bounds: bounds,
shieldHeight: shieldHeight,
window: carPlayManager.carWindow,
instructionLabelType: SecondaryLabel.self) {
instruction.append(NSAttributedString(string: "\n"))
instruction.append(attributedSecondary)
}
instruction.canonicalizeAttachments(maximumImageSize: maximumImageSize, imageRendererFormat: imageRendererFormat)
primaryManeuver.attributedInstructionVariants = [instruction]
}
var maneuvers: [CPManeuver] = [primaryManeuver]
// Add tertiary information, if available
if let tertiaryInstruction = visualInstruction.tertiaryInstruction {
let tertiaryManeuver = CPManeuver()
if tertiaryInstruction.containsLaneIndications {
// add lanes visual banner
if let imageSet = visualInstruction.tertiaryInstruction?.lanesImageSet(side: visualInstruction.drivingSide,
direction: visualInstruction.primaryInstruction.maneuverDirection,
scale: (carPlayManager.carWindow?.screen ?? UIScreen.main).scale) {
tertiaryManeuver.symbolSet = imageSet
}
tertiaryManeuver.userInfo = tertiaryInstruction
} else {
// add tertiary maneuver text
tertiaryManeuver.symbolSet = tertiaryInstruction.maneuverImageSet(side: visualInstruction.drivingSide)
if let text = tertiaryInstruction.text {
tertiaryManeuver.instructionVariants = [text]
}
if let attributedTertiary = tertiaryInstruction.carPlayManeuverLabelAttributedText(bounds: bounds,
shieldHeight: shieldHeight,
window: carPlayManager.carWindow) {
let attributedTertiary = NSMutableAttributedString(attributedString: attributedTertiary)
attributedTertiary.canonicalizeAttachments(maximumImageSize: maximumImageSize, imageRendererFormat: imageRendererFormat)
tertiaryManeuver.attributedInstructionVariants = [attributedTertiary]
}
}
if let upcomingStep = navigationService.routeProgress.currentLegProgress.upcomingStep {
let distance = Measurement(distance: upcomingStep.distance).localized()
tertiaryManeuver.initialTravelEstimates = CPTravelEstimates(distanceRemaining: distance,
timeRemaining: upcomingStep.expectedTravelTime)
}
maneuvers.append(tertiaryManeuver)
}
carSession.upcomingManeuvers = maneuvers
}
func presentWaypointArrivalUI(for waypoint: Waypoint) {
var title = NSLocalizedString("CARPLAY_ARRIVED",
bundle: .mapboxNavigation,
value: "You have arrived",
comment: "Title on arrival action sheet")
if let name = waypoint.name {
title = name
}
let continueTitle = NSLocalizedString("CARPLAY_CONTINUE",
bundle: .mapboxNavigation,
value: "Continue",
comment: "Title on continue button in CarPlay")
let continueAlert = CPAlertAction(title: continueTitle, style: .default) { (action) in
self.carInterfaceController.dismissTemplate(animated: true)
self.updateRouteOnMap()
}
let waypointArrival = CPAlertTemplate(titleVariants: [title], actions: [continueAlert])
// Template has to be dismissed because only one template may be presented at a time.
carInterfaceController.dismissTemplate(animated: true)
carInterfaceController.presentTemplate(waypointArrival, animated: true)
}
}
// MARK: StyleManagerDelegate Methods
@available(iOS 12.0, *)
extension CarPlayNavigationViewController: StyleManagerDelegate {
public func location(for styleManager: StyleManager) -> CLLocation? {
if let location = navigationService.router.location {
return location
} else if let origin = navigationService.route.shape?.coordinates.first {
return CLLocation(latitude: origin.latitude, longitude: origin.longitude)
} else {
return nil
}
}
public func styleManager(_ styleManager: StyleManager, didApply style: Style) {
let mapboxMapStyle = navigationMapView?.mapView.mapboxMap.style
if mapboxMapStyle?.uri?.rawValue != style.mapStyleURL.absoluteString {
mapboxMapStyle?.uri = StyleURI(url: style.mapStyleURL)
}
}
public func styleManagerDidRefreshAppearance(_ styleManager: StyleManager) {
guard let mapboxMap = navigationMapView?.mapView.mapboxMap,
let styleURI = mapboxMap.style.uri else { return }
mapboxMap.loadStyleURI(styleURI)
}
}
// MARK: NavigationServiceDelegate Methods
@available(iOS 12.0, *)
extension CarPlayNavigationViewController: NavigationServiceDelegate {
public func navigationService(_ service: NavigationService, didArriveAt waypoint: Waypoint) -> Bool {
let shouldPresentArrivalUI = delegate?.carPlayNavigationViewController(self, shouldPresentArrivalUIFor: waypoint) ?? true
if service.routeProgress.isFinalLeg && shouldPresentArrivalUI {
presentArrivalUI()
} else if shouldPresentArrivalUI {
presentWaypointArrivalUI(for: waypoint)
}
return true
}
}
// MARK: NavigationMapViewDelegate Methods
@available(iOS 12.0, *)
extension CarPlayNavigationViewController: NavigationMapViewDelegate {
public func navigationMapView(_ navigationMapView: NavigationMapView,
didAdd finalDestinationAnnotation: PointAnnotation,
pointAnnotationManager: PointAnnotationManager) {
delegate?.carPlayNavigationViewController(self,
didAdd: finalDestinationAnnotation,
pointAnnotationManager: pointAnnotationManager)
}
}
#endif