Skip to content

Commit fc7b55b

Browse files
[CarPlay] Add Delegate Method Before CarPlayNavigationViewController Presented (#4379)
* Add delegate method for accessing navigation vc before presenting This allows the end user to modify the view before it is visible, for setting up UI elements such as the user location style * Addstest for willPresentNavigationViewController delegate method * CHANGELOG updated; accepted breaking changes --------- Co-authored-by: Jack Moseley <[email protected]>
1 parent 45550b4 commit fc7b55b

File tree

6 files changed

+42
-0
lines changed

6 files changed

+42
-0
lines changed

.breakage-allowlist

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Func CarPlayManagerDelegate.carPlayManager(_:willPresent:) has been added as a protocol requirement

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
* Added the ability to pan a map view on CarPlay. ([#4288](https://github.com/mapbox/mapbox-navigation-ios/pull/4288))
2626
* Added a new configuration property `CarPlayManager.startFreeDriveAutomatically` that controls whether `CarPlayManager` starts a Free Drive session automatically on map load. If you set this property to false, you can start a Free Drive session using `CarPlayMapViewController.startFreeDriveNavigation()` method. ([#4352](https://github.com/mapbox/mapbox-navigation-ios/pull/4352))
2727
* Added new `CarPlayManagerDelegate` methods `carPlayManager(_:didBeginPanGesture:)`, `carPlayManager(_:didEndPanGesture:)`, `carPlayManager(_:didShowPanningInterface:)`, `carPlayManager(_:willDismissPanningInterface:)`, and `carPlayManager(_:didDismissPanningInterface:)` to notify about pan gesture events. ([#4368](https://github.com/mapbox/mapbox-navigation-ios/pull/4368))
28+
* Added `CarPlayManagerDelegate.carPlayManager(_:willPresent:)` method to allow developers to query or customize properties of the `CarPlayNavigationViewController` before it is presented. ([#4376](https://github.com/mapbox/mapbox-navigation-ios/pull/4376))
2829

2930
### Electronic horizon
3031

Sources/MapboxNavigation/CarPlayManager.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -809,6 +809,9 @@ extension CarPlayManager: CPMapTemplateDelegate {
809809
carPlayNavigationViewController.delegate = self
810810
carPlayNavigationViewController.modalPresentationStyle = .fullScreen
811811
self.carPlayNavigationViewController = carPlayNavigationViewController
812+
813+
carPlayNavigationViewController.loadViewIfNeeded()
814+
delegate?.carPlayManager(self, willPresent: carPlayNavigationViewController)
812815

813816
carPlayMapViewController.present(carPlayNavigationViewController, animated: true) { [weak self] in
814817
guard let self = self else { return }

Sources/MapboxNavigation/CarPlayManagerDelegate.swift

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,21 @@ public protocol CarPlayManagerDelegate: AnyObject, UnimplementedLogging, CarPlay
235235
and enable when disconnected.
236236
*/
237237
func carPlayManagerShouldDisableIdleTimer(_ carPlayManager: CarPlayManager) -> Bool
238+
239+
/**
240+
Called when the CarPlayManager creates a new CarPlayNavigationViewController upon start of a
241+
navigation session.
242+
243+
Implementing this method will allow developers to query or customize properties of the
244+
CarPlayNavigationViewController before it is presented. For example, a developer may wish to perform custom map styling
245+
on the presented NavigationMapView.
246+
247+
- parameter carPlayManager: The CarPlay manager instance.
248+
- parameter navigationViewController: The CarPlayNavigationViewController that will be presented
249+
on the CarPlay display.
250+
*/
251+
func carPlayManager(_ carPlayManager: CarPlayManager,
252+
willPresent navigationViewController: CarPlayNavigationViewController)
238253

239254
/**
240255
Called when the CarPlayManager presents a new CarPlayNavigationViewController upon start of a
@@ -628,6 +643,14 @@ public extension CarPlayManagerDelegate {
628643
logUnimplemented(protocolType: CarPlayManagerDelegate.self, level: .debug)
629644
return true
630645
}
646+
647+
/**
648+
`UnimplementedLogging` prints a warning to standard output the first time this method is called.
649+
*/
650+
func carPlayManager(_ carPlayManager: CarPlayManager,
651+
willPresent navigationViewController: CarPlayNavigationViewController) {
652+
logUnimplemented(protocolType: CarPlayManagerDelegate.self, level: .debug)
653+
}
631654

632655
/**
633656
`UnimplementedLogging` prints a warning to standard output the first time this method is called.

Tests/MapboxNavigationTests/CarPlayManagerTests.swift

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,13 @@ class CarPlayManagerTests: TestCase {
224224
XCTAssertEqual(mapTemplateSpy.passedTripPreviews?.first, customTrip)
225225
XCTAssertEqual(mapTemplateSpy.passedPreviewTextConfiguration?.startButtonTitle, startButtonTitle)
226226
}
227+
228+
func testWillPresentNavigationViewController() {
229+
startNavigation()
230+
231+
XCTAssertTrue(delegate.willPresentCalled)
232+
XCTAssertEqual(delegate.passedWillPresentNavigationViewController, carPlayManager.carPlayNavigationViewController)
233+
}
227234

228235
func testStartWhenConfiguredToSimulate() {
229236
carPlayManager.simulatesLocations = true

Tests/MapboxNavigationTests/CarPlayUtils.swift

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ class CarPlayManagerDelegateSpy: CarPlayManagerDelegate {
7272
var didBeginNavigationCalled = false
7373
var didEndNavigationCalled = false
7474
var legacyDidEndNavigationCalled = false
75+
var willPresentCalled = false
7576
var didPresentCalled = false
7677
var didFailToFetchRouteCalled = false
7778
var didBeginPanGestureCalled = false
@@ -84,6 +85,7 @@ class CarPlayManagerDelegateSpy: CarPlayManagerDelegate {
8485
var passedError: DirectionsError?
8586
var passedTemplate: CPMapTemplate?
8687
var passedNavigationEndedByCanceling = false
88+
var passedWillPresentNavigationViewController: CarPlayNavigationViewController?
8789

8890
var returnedTripPreviewTextConfiguration: CPTripPreviewTextConfiguration?
8991
var returnedTrip: CPTrip?
@@ -118,6 +120,11 @@ class CarPlayManagerDelegateSpy: CarPlayManagerDelegate {
118120
XCTAssertTrue(didBeginNavigationCalled)
119121
legacyDidEndNavigationCalled = true
120122
}
123+
124+
func carPlayManager(_ carPlayManager: CarPlayManager, willPresent navigationViewController: CarPlayNavigationViewController) {
125+
willPresentCalled = true
126+
passedWillPresentNavigationViewController = navigationViewController
127+
}
121128

122129
func carPlayManager(_ carPlayManager: CarPlayManager, didPresent navigationViewController: CarPlayNavigationViewController) {
123130
didPresentCalled = true

0 commit comments

Comments
 (0)