@@ -11,26 +11,15 @@ class ViewController: UIViewController {
1111
1212 var navigationRouteOptions : NavigationRouteOptions !
1313
14- var currentRoute : Route ? {
15- get {
16- return routes? . first
17- }
18- set {
19- guard let selected = newValue else { routes = nil ; return }
20- guard let routes = routes else { self . routes = [ selected] ; return }
21- self . routes = [ selected] + routes. filter { $0 != selected }
22- }
23- }
24-
25- var routes : [ Route ] ? {
14+ var routeResponse : RouteResponse ? {
2615 didSet {
27- guard let routes = routes, let currentRoute = routes. first else {
16+ guard let routes = routeResponse ? . routes, let currentRoute = routes. first else {
2817 navigationMapView. removeRoutes ( )
2918 navigationMapView. removeWaypoints ( )
3019 waypoints. removeAll ( )
3120 return
3221 }
33-
22+
3423 navigationMapView. show ( routes)
3524 navigationMapView. showWaypoints ( on: currentRoute)
3625 }
@@ -90,7 +79,7 @@ class ViewController: UIViewController {
9079 preferredStyle: . actionSheet)
9180
9281 let startNavigation : ActionHandler = { _ in self . startNavigation ( ) }
93- let removeRoutes : ActionHandler = { _ in self . routes = nil }
82+ let removeRoutes : ActionHandler = { _ in self . routeResponse = nil }
9483
9584 let actions : [ ( String , UIAlertAction . Style , ActionHandler ? ) ] = [
9685 ( " Start Navigation " , . default, startNavigation) ,
@@ -110,20 +99,17 @@ class ViewController: UIViewController {
11099 }
111100
112101 func startNavigation( ) {
113- guard let route = currentRoute, let navigationRouteOptions = navigationRouteOptions else {
102+ guard let routeResponse = routeResponse,
103+ let navigationRouteOptions = navigationRouteOptions else {
114104 presentAlert ( message: " Please select at least one destination coordinate to start navigation. " )
115105 return
116106 }
117-
118- let navigationService = MapboxNavigationService ( route : route ,
107+
108+ let navigationService = MapboxNavigationService ( routeResponse : routeResponse ,
119109 routeIndex: 0 ,
120110 routeOptions: navigationRouteOptions,
121111 simulating: . always)
122- let navigationOptions = NavigationOptions ( navigationService: navigationService)
123- let navigationViewController = NavigationViewController ( for: route,
124- routeIndex: 0 ,
125- routeOptions: navigationRouteOptions,
126- navigationOptions: navigationOptions)
112+ let navigationViewController = NavigationViewController ( navigationService: navigationService)
127113 navigationViewController. delegate = self
128114 navigationViewController. modalPresentationStyle = . fullScreen
129115
@@ -169,24 +155,17 @@ class ViewController: UIViewController {
169155 func requestRoute( ) {
170156 let navigationRouteOptions = NavigationRouteOptions ( waypoints: waypoints)
171157 Directions . shared. calculate ( navigationRouteOptions) { [ weak self] ( _, result) in
158+ guard let self = self else { return }
159+
172160 switch result {
173161 case . failure( let error) :
174- self ? . presentAlert ( message: error. localizedDescription)
175-
162+ self . presentAlert ( message: error. localizedDescription)
163+
176164 // In case if direction calculation failed - remove last destination waypoint.
177- self ? . waypoints. removeLast ( )
165+ self . waypoints. removeLast ( )
178166 case . success( let response) :
179- guard let routes = response. routes else { return }
180- self ? . navigationRouteOptions = navigationRouteOptions
181- self ? . routes = routes
182- self ? . navigationMapView. show ( routes)
183- if let currentRoute = self ? . currentRoute {
184- self ? . navigationMapView. showWaypoints ( on: currentRoute)
185- }
186-
187- if let coordinates = self ? . waypoints. compactMap ( { $0. targetCoordinate } ) {
188- self ? . navigationMapView. highlightBuildings ( at: coordinates, in3D: true )
189- }
167+ self . routeResponse = response
168+ self . navigationRouteOptions = navigationRouteOptions
190169 }
191170 }
192171 }
@@ -208,7 +187,10 @@ class ViewController: UIViewController {
208187extension ViewController : NavigationMapViewDelegate {
209188
210189 func navigationMapView( _ mapView: NavigationMapView , didSelect route: Route ) {
211- self . currentRoute = route
190+ guard let routes = routeResponse? . routes,
191+ let routeIndex = routes. firstIndex ( where: { $0 === route } ) else { return }
192+
193+ routeResponse? . routes? . swapAt ( routeIndex, 0 )
212194 }
213195}
214196
0 commit comments