11import UIKit
2-
32import MapboxCoreNavigation
43import MapboxNavigation
54import 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}
0 commit comments