Skip to content

Commit e588a4b

Browse files
committed
Update.
1 parent 2d34ede commit e588a4b

File tree

6 files changed

+109
-54
lines changed

6 files changed

+109
-54
lines changed

CarPlayExample/AppDelegate.swift

Lines changed: 41 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import UIKit
22
import CarPlay
3-
43
import MapboxCoreNavigation
54
import MapboxNavigation
65
import MapboxDirections
@@ -45,27 +44,35 @@ extension AppDelegate: CPTemplateApplicationSceneDelegate {
4544
func templateApplicationScene(_ templateApplicationScene: CPTemplateApplicationScene,
4645
didConnect interfaceController: CPInterfaceController,
4746
to window: CPWindow) {
48-
carPlayManager.delegate = self
49-
carPlayManager.templateApplicationScene(templateApplicationScene,
50-
didConnectCarInterfaceController: interfaceController,
51-
to: window)
47+
guard let appDelegate = UIApplication.shared.delegate as? AppDelegate else { return }
48+
49+
appDelegate.carPlayManager.delegate = appDelegate
50+
51+
appDelegate.carPlayManager.application(UIApplication.shared,
52+
didConnectCarInterfaceController: interfaceController,
53+
to: window)
54+
55+
appDelegate.carPlayManager.templateApplicationScene(templateApplicationScene,
56+
didConnectCarInterfaceController: interfaceController,
57+
to: window)
5258

53-
carPlayManager.application(UIApplication.shared,
54-
didConnectCarInterfaceController: interfaceController,
55-
to: window)
59+
appDelegate.carPlayManager.interfaceController?.delegate = self
5660
}
5761

5862
func templateApplicationScene(_ templateApplicationScene: CPTemplateApplicationScene,
5963
didDisconnect interfaceController: CPInterfaceController,
6064
from window: CPWindow) {
61-
carPlayManager.delegate = nil
62-
carPlayManager.application(UIApplication.shared,
63-
didDisconnectCarInterfaceController: interfaceController,
64-
from: window)
65+
guard let appDelegate = UIApplication.shared.delegate as? AppDelegate else { return }
6566

66-
carPlayManager.templateApplicationScene(templateApplicationScene,
67-
didDisconnectCarInterfaceController: interfaceController,
68-
from: window)
67+
appDelegate.carPlayManager.delegate = nil
68+
69+
appDelegate.carPlayManager.application(UIApplication.shared,
70+
didDisconnectCarInterfaceController: interfaceController,
71+
from: window)
72+
73+
appDelegate.carPlayManager.templateApplicationScene(templateApplicationScene,
74+
didDisconnectCarInterfaceController: interfaceController,
75+
from: window)
6976
}
7077
}
7178

@@ -144,7 +151,16 @@ extension AppDelegate: CarPlayManagerDelegate {
144151
didFailToFetchRouteBetween waypoints: [Waypoint]?,
145152
options: RouteOptions,
146153
error: DirectionsError) -> CPNavigationAlert? {
147-
return nil
154+
let alertAction = CPAlertAction(title: "Dismiss", style: .default, handler: { _ in })
155+
156+
let navigationAlert = CPNavigationAlert(titleVariants: ["Failed to fetch"],
157+
subtitleVariants: nil,
158+
image: nil,
159+
primaryAction: alertAction,
160+
secondaryAction: nil,
161+
duration: 2.5)
162+
163+
return navigationAlert
148164
}
149165

150166
func carPlayManager(_ carPlayManager: CarPlayManager,
@@ -166,7 +182,7 @@ extension AppDelegate: CarPlayManagerDelegate {
166182

167183
func carPlayManager(_ carPlayManager: CarPlayManager,
168184
didBeginNavigationWith service: NavigationService) {
169-
185+
170186
}
171187

172188
func carPlayManagerDidEndNavigation(_ carPlayManager: CarPlayManager) {
@@ -184,13 +200,20 @@ extension AppDelegate: CarPlayManagerDelegate {
184200

185201
func carPlayManager(_ carPlayManager: CarPlayManager,
186202
didPresent navigationViewController: CarPlayNavigationViewController) {
187-
203+
188204
}
189205

190206
func carPlayManager(_ carPlayManager: CarPlayManager,
191207
didAdd finalDestinationAnnotation: PointAnnotation,
192208
to parentViewController: UIViewController,
193209
pointAnnotationManager: PointAnnotationManager) {
210+
var finalDestinationAnnotation = finalDestinationAnnotation
211+
if let image = UIImage(named: "marker") {
212+
finalDestinationAnnotation.image = PointAnnotation.Image.custom(image: image, name: "marker")
213+
} else {
214+
finalDestinationAnnotation.image = .default
215+
}
194216

217+
pointAnnotationManager.syncAnnotations([finalDestinationAnnotation])
195218
}
196219
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"images" : [
3+
{
4+
"filename" : "marker.pdf",
5+
"idiom" : "universal"
6+
}
7+
],
8+
"info" : {
9+
"author" : "xcode",
10+
"version" : 1
11+
}
12+
}
4.13 KB
Binary file not shown.

CarPlayExample/ViewController.swift

Lines changed: 47 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
import UIKit
2-
32
import MapboxCoreNavigation
43
import MapboxNavigation
54
import MapboxDirections
65

7-
class ViewController: UIViewController, NavigationMapViewDelegate, NavigationViewControllerDelegate, UIGestureRecognizerDelegate {
6+
class ViewController: UIViewController {
87

98
typealias ActionHandler = (UIAlertAction) -> Void
109

@@ -65,22 +64,30 @@ class ViewController: UIViewController, NavigationMapViewDelegate, NavigationVie
6564
}
6665

6766
func setupPerformActionBarButtonItem() {
68-
let settingsBarButtonItem = UIBarButtonItem(title: NSString(string: "\u{2699}\u{0000FE0E}") as String, style: .plain, target: self, action: #selector(performAction))
69-
settingsBarButtonItem.setTitleTextAttributes([.font: UIFont.systemFont(ofSize: 30)], for: .normal)
70-
settingsBarButtonItem.setTitleTextAttributes([.font: UIFont.systemFont(ofSize: 30)], for: .highlighted)
67+
let settingsBarButtonItem = UIBarButtonItem(title: NSString(string: "\u{2699}\u{0000FE0E}") as String,
68+
style: .plain,
69+
target: self,
70+
action: #selector(performAction))
71+
let attributes = [
72+
NSAttributedString.Key.font: UIFont.systemFont(ofSize: 30)
73+
]
74+
settingsBarButtonItem.setTitleTextAttributes(attributes, for: .normal)
75+
settingsBarButtonItem.setTitleTextAttributes(attributes, for: .highlighted)
7176
navigationItem.rightBarButtonItem = settingsBarButtonItem
7277
}
7378

7479
// MARK: - UIGestureRecognizer related methods
7580

7681
func setupGestureRecognizers() {
77-
let longPressGestureRecognizer = UILongPressGestureRecognizer(target: self, action: #selector(handleLongPress(_:)))
82+
let longPressGestureRecognizer = UILongPressGestureRecognizer(target: self,
83+
action: #selector(handleLongPress(_:)))
7884
navigationMapView.addGestureRecognizer(longPressGestureRecognizer)
7985
}
8086

8187
@objc func performAction(_ sender: Any) {
8288
let alertController = UIAlertController(title: "Perform action",
83-
message: "Select specific action to perform it", preferredStyle: .actionSheet)
89+
message: "Select specific action to perform it",
90+
preferredStyle: .actionSheet)
8491

8592
let startNavigation: ActionHandler = { _ in self.startNavigation() }
8693
let removeRoutes: ActionHandler = { _ in self.routes = nil }
@@ -134,7 +141,8 @@ class ViewController: UIViewController, NavigationMapViewDelegate, NavigationVie
134141
@objc func handleLongPress(_ gesture: UILongPressGestureRecognizer) {
135142
guard gesture.state == .began else { return }
136143

137-
createWaypoints(for: navigationMapView.mapView.mapboxMap.coordinate(for: gesture.location(in: navigationMapView.mapView)))
144+
let mapView = navigationMapView.mapView
145+
createWaypoints(for: mapView?.mapboxMap.coordinate(for: gesture.location(in: mapView)))
138146
requestRoute()
139147
}
140148

@@ -182,16 +190,34 @@ class ViewController: UIViewController, NavigationMapViewDelegate, NavigationVie
182190
}
183191
}
184192
}
185-
186-
// MARK: - NavigationMapViewDelegate methods
193+
194+
// MARK: - Utility methods
195+
196+
func presentAlert(_ title: String? = nil, message: String? = nil) {
197+
let alertController = UIAlertController(title: title, message: message, preferredStyle: .alert)
198+
alertController.addAction(UIAlertAction(title: "OK", style: .default, handler: { (_) in
199+
alertController.dismiss(animated: true, completion: nil)
200+
}))
201+
202+
present(alertController, animated: true, completion: nil)
203+
}
204+
}
205+
206+
// MARK: - NavigationMapViewDelegate methods
207+
208+
extension ViewController: NavigationMapViewDelegate {
187209

188210
func navigationMapView(_ mapView: NavigationMapView, didSelect route: Route) {
189211
self.currentRoute = route
190212
}
213+
}
214+
215+
// MARK: - NavigationViewControllerDelegate methods
216+
217+
extension ViewController: NavigationViewControllerDelegate {
191218

192-
// MARK: - NavigationViewControllerDelegate methods
193-
194-
func navigationViewController(_ navigationViewController: NavigationViewController, didArriveAt waypoint: Waypoint) -> Bool {
219+
func navigationViewController(_ navigationViewController: NavigationViewController,
220+
didArriveAt waypoint: Waypoint) -> Bool {
195221
if navigationViewController.navigationService.router.routeProgress.isFinalLeg {
196222
return true
197223
}
@@ -210,25 +236,19 @@ class ViewController: UIViewController, NavigationMapViewDelegate, NavigationVie
210236
return false
211237
}
212238

213-
func navigationViewControllerDidDismiss(_ navigationViewController: NavigationViewController, byCanceling canceled: Bool) {
239+
func navigationViewControllerDidDismiss(_ navigationViewController: NavigationViewController,
240+
byCanceling canceled: Bool) {
214241
dismiss(animated: true, completion: nil)
215242
}
243+
}
216244

217-
// MARK: - UIGestureRecognizerDelegate methods
245+
// MARK: - UIGestureRecognizerDelegate methods
218246

219-
func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWith otherGestureRecognizer: UIGestureRecognizer) -> Bool {
247+
extension ViewController: UIGestureRecognizerDelegate {
248+
249+
func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer,
250+
shouldRecognizeSimultaneouslyWith otherGestureRecognizer: UIGestureRecognizer) -> Bool {
220251
// Allow both route selection and building extrusion when tapping on screen.
221252
return true
222253
}
223-
224-
// MARK: - Utility methods
225-
226-
func presentAlert(_ title: String? = nil, message: String? = nil) {
227-
let alertController = UIAlertController(title: title, message: message, preferredStyle: .alert)
228-
alertController.addAction(UIAlertAction(title: "OK", style: .default, handler: { (_) in
229-
alertController.dismiss(animated: true, completion: nil)
230-
}))
231-
232-
present(alertController, animated: true, completion: nil)
233-
}
234254
}

Podfile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
platform :ios, '11.0'
22
use_frameworks!
33

4-
pod 'MapboxCoreNavigation', :git => 'https://github.com/mapbox/mapbox-navigation-ios.git', :branch => 'main'
5-
pod 'MapboxNavigation', :git => 'https://github.com/mapbox/mapbox-navigation-ios.git', :branch => 'main'
4+
pod 'MapboxCoreNavigation', :git => 'https://github.com/mapbox/mapbox-navigation-ios.git', :branch => 'maxim/car-play-refactoring'
5+
pod 'MapboxNavigation', :git => 'https://github.com/mapbox/mapbox-navigation-ios.git', :branch => 'maxim/car-play-refactoring'
66

77
target 'Navigation-Examples' do
88
end

Podfile.lock

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@ PODS:
3030
- Turf (2.0.0-beta.1)
3131

3232
DEPENDENCIES:
33-
- MapboxCoreNavigation (from `https://github.com/mapbox/mapbox-navigation-ios.git`, branch `main`)
34-
- MapboxNavigation (from `https://github.com/mapbox/mapbox-navigation-ios.git`, branch `main`)
33+
- MapboxCoreNavigation (from `https://github.com/mapbox/mapbox-navigation-ios.git`, branch `maxim/car-play-refactoring`)
34+
- MapboxNavigation (from `https://github.com/mapbox/mapbox-navigation-ios.git`, branch `maxim/car-play-refactoring`)
3535

3636
SPEC REPOS:
3737
trunk:
@@ -48,18 +48,18 @@ SPEC REPOS:
4848

4949
EXTERNAL SOURCES:
5050
MapboxCoreNavigation:
51-
:branch: main
51+
:branch: maxim/car-play-refactoring
5252
:git: https://github.com/mapbox/mapbox-navigation-ios.git
5353
MapboxNavigation:
54-
:branch: main
54+
:branch: maxim/car-play-refactoring
5555
:git: https://github.com/mapbox/mapbox-navigation-ios.git
5656

5757
CHECKOUT OPTIONS:
5858
MapboxCoreNavigation:
59-
:commit: 5c9fc1df2cd4bdc8efee539ff854ee6d150ccabc
59+
:commit: 3d471096f6ecc68376169a82a36a4af0eb0218f8
6060
:git: https://github.com/mapbox/mapbox-navigation-ios.git
6161
MapboxNavigation:
62-
:commit: 5c9fc1df2cd4bdc8efee539ff854ee6d150ccabc
62+
:commit: 3d471096f6ecc68376169a82a36a4af0eb0218f8
6363
:git: https://github.com/mapbox/mapbox-navigation-ios.git
6464

6565
SPEC CHECKSUMS:
@@ -76,6 +76,6 @@ SPEC CHECKSUMS:
7676
Solar-dev: 4612dc9878b9fed2667d23b327f1d4e54e16e8d0
7777
Turf: 8851f941a6e6476ea200bf057d31cf21b2418467
7878

79-
PODFILE CHECKSUM: 9f3bdf8f2b78c4d17175133554bc4b33a9ac4613
79+
PODFILE CHECKSUM: cdba1559ae133c7fa27daf8adf70e8089ac9c090
8080

8181
COCOAPODS: 1.10.1

0 commit comments

Comments
 (0)