|
| 1 | +import CarPlay |
| 2 | +import MapboxCoreNavigation |
| 3 | +import MapboxNavigation |
| 4 | +import MapboxDirections |
| 5 | +import MapboxMaps |
| 6 | + |
| 7 | +// MARK: - CarPlayManagerDelegate methods |
| 8 | + |
| 9 | +extension AppDelegate: CarPlayManagerDelegate { |
| 10 | + |
| 11 | + // Delegate method, which allows to provide list of leading `CPBarButton`s for specific `CarPlayActivity`. |
| 12 | + // It's possible to provide up to two leading `CPBarButton`s. |
| 13 | + func carPlayManager(_ carPlayManager: CarPlayManager, |
| 14 | + leadingNavigationBarButtonsCompatibleWith traitCollection: UITraitCollection, |
| 15 | + in carPlayTemplate: CPTemplate, |
| 16 | + for activity: CarPlayActivity) -> [CPBarButton]? { |
| 17 | + guard let interfaceController = self.carPlayManager.interfaceController else { return nil } |
| 18 | + |
| 19 | + switch activity { |
| 20 | + |
| 21 | + case .browsing: |
| 22 | + let searchTemplate = CPSearchTemplate() |
| 23 | + searchTemplate.delegate = carPlaySearchController |
| 24 | + let searchButton = carPlaySearchController.searchTemplateButton(searchTemplate: searchTemplate, |
| 25 | + interfaceController: interfaceController, |
| 26 | + traitCollection: traitCollection) |
| 27 | + return [searchButton] |
| 28 | + case .panningInBrowsingMode: |
| 29 | + break |
| 30 | + |
| 31 | + case .previewing: |
| 32 | + break |
| 33 | + |
| 34 | + case .navigating: |
| 35 | + break |
| 36 | + } |
| 37 | + |
| 38 | + return [] |
| 39 | + } |
| 40 | + |
| 41 | + // Delegate method, which allows to provide list of trailing `CPBarButton`s for specific `CarPlayActivity`. |
| 42 | + // It's possible to provide up to two trailing `CPBarButton`s. |
| 43 | + func carPlayManager(_ carPlayManager: CarPlayManager, |
| 44 | + trailingNavigationBarButtonsCompatibleWith traitCollection: UITraitCollection, |
| 45 | + in carPlayTemplate: CPTemplate, |
| 46 | + for activity: CarPlayActivity) -> [CPBarButton]? { |
| 47 | + switch activity { |
| 48 | + |
| 49 | + case .browsing: |
| 50 | + break |
| 51 | + |
| 52 | + case .panningInBrowsingMode: |
| 53 | + break |
| 54 | + |
| 55 | + case .previewing: |
| 56 | + break |
| 57 | + |
| 58 | + case .navigating: |
| 59 | + return [carPlayManager.exitButton] |
| 60 | + } |
| 61 | + |
| 62 | + return [] |
| 63 | + } |
| 64 | + |
| 65 | + // Delegate method, which allows to provide a list of `CPMapButton`, which are shown on a map. |
| 66 | + // It's possible to provide up to four `CPMapButton`s. |
| 67 | + func carPlayManager(_ carPlayManager: CarPlayManager, |
| 68 | + mapButtonsCompatibleWith traitCollection: UITraitCollection, |
| 69 | + in carPlayTemplate: CPTemplate, |
| 70 | + for activity: CarPlayActivity) -> [CPMapButton]? { |
| 71 | + switch activity { |
| 72 | + |
| 73 | + case .browsing: |
| 74 | + break |
| 75 | + |
| 76 | + case .panningInBrowsingMode: |
| 77 | + break |
| 78 | + |
| 79 | + case .previewing: |
| 80 | + break |
| 81 | + |
| 82 | + case .navigating: |
| 83 | + break |
| 84 | + } |
| 85 | + |
| 86 | + return [] |
| 87 | + } |
| 88 | + |
| 89 | + func carPlayManager(_ carPlayManager: CarPlayManager, |
| 90 | + didFailToFetchRouteBetween waypoints: [Waypoint]?, |
| 91 | + options: RouteOptions, |
| 92 | + error: DirectionsError) -> CPNavigationAlert? { |
| 93 | + let alertAction = CPAlertAction(title: "Dismiss", style: .default, handler: { _ in }) |
| 94 | + |
| 95 | + let navigationAlert = CPNavigationAlert(titleVariants: ["Failed to fetch"], |
| 96 | + subtitleVariants: nil, |
| 97 | + image: nil, |
| 98 | + primaryAction: alertAction, |
| 99 | + secondaryAction: nil, |
| 100 | + duration: 2.5) |
| 101 | + |
| 102 | + return navigationAlert |
| 103 | + } |
| 104 | + |
| 105 | + func carPlayManager(_ carPlayManager: CarPlayManager, |
| 106 | + willPreview trip: CPTrip) -> CPTrip { |
| 107 | + return trip |
| 108 | + } |
| 109 | + |
| 110 | + func carPlayManager(_ carPlayManager: CarPlayManager, |
| 111 | + willPreview trip: CPTrip, |
| 112 | + with previewTextConfiguration: CPTripPreviewTextConfiguration) -> CPTripPreviewTextConfiguration { |
| 113 | + return previewTextConfiguration |
| 114 | + } |
| 115 | + |
| 116 | + func carPlayManager(_ carPlayManager: CarPlayManager, |
| 117 | + selectedPreviewFor trip: CPTrip, |
| 118 | + using routeChoice: CPRouteChoice) { |
| 119 | + |
| 120 | + } |
| 121 | + |
| 122 | + func carPlayManager(_ carPlayManager: CarPlayManager, |
| 123 | + didBeginNavigationWith service: NavigationService) { |
| 124 | + |
| 125 | + } |
| 126 | + |
| 127 | + // Delegate method, which is called after ending active-guidance navigation session and dismissing |
| 128 | + // `CarPlayNavigationViewController`. |
| 129 | + func carPlayManagerDidEndNavigation(_ carPlayManager: CarPlayManager) { |
| 130 | + let alertAction = CPAlertAction(title: "OK", |
| 131 | + style: .default, |
| 132 | + handler: { [weak self] _ in |
| 133 | + self?.carPlayManager.interfaceController?.dismissTemplate(animated: true) |
| 134 | + }) |
| 135 | + |
| 136 | + let alertTemplate = CPAlertTemplate(titleVariants: ["Did end active-guidance navigation."], |
| 137 | + actions: [alertAction]) |
| 138 | + |
| 139 | + carPlayManager.interfaceController?.presentTemplate(alertTemplate, animated: true) |
| 140 | + } |
| 141 | + |
| 142 | + // Delegate method, which allows to show `CPActionSheetTemplate` or `CPAlertTemplate` |
| 143 | + // after arriving to the specific `Waypoint`. |
| 144 | + func carPlayManager(_ carPlayManager: CarPlayManager, |
| 145 | + shouldPresentArrivalUIFor waypoint: Waypoint) -> Bool { |
| 146 | + return true |
| 147 | + } |
| 148 | + |
| 149 | + // Delegate method, which provides the ability to disable the idle timer to avert system sleep. |
| 150 | + func carPlayManagerShouldDisableIdleTimer(_ carPlayManager: CarPlayManager) -> Bool { |
| 151 | + return true |
| 152 | + } |
| 153 | + |
| 154 | + // Delegate method, which is called right after starting active-guidance navigation and presenting |
| 155 | + // `CarPlayNavigationViewController`. |
| 156 | + func carPlayManager(_ carPlayManager: CarPlayManager, |
| 157 | + didPresent navigationViewController: CarPlayNavigationViewController) { |
| 158 | + let alertAction = CPAlertAction(title: "OK", |
| 159 | + style: .default, |
| 160 | + handler: { [weak self] _ in |
| 161 | + self?.carPlayManager.interfaceController?.dismissTemplate(animated: true) |
| 162 | + }) |
| 163 | + |
| 164 | + let alertTemplate = CPAlertTemplate(titleVariants: ["Did present CarPlayNavigationViewController."], |
| 165 | + actions: [alertAction]) |
| 166 | + |
| 167 | + carPlayManager.interfaceController?.presentTemplate(alertTemplate, animated: true) |
| 168 | + } |
| 169 | + |
| 170 | + // Delegate method, which allows to modify final destination annotation whenever its added to |
| 171 | + // `CarPlayMapViewController` or `CarPlayNavigationViewController`. |
| 172 | + func carPlayManager(_ carPlayManager: CarPlayManager, |
| 173 | + didAdd finalDestinationAnnotation: PointAnnotation, |
| 174 | + to parentViewController: UIViewController, |
| 175 | + pointAnnotationManager: PointAnnotationManager) { |
| 176 | + var finalDestinationAnnotation = finalDestinationAnnotation |
| 177 | + if let image = UIImage(named: "marker") { |
| 178 | + finalDestinationAnnotation.image = PointAnnotation.Image.custom(image: image, name: "marker") |
| 179 | + } else { |
| 180 | + finalDestinationAnnotation.image = .default |
| 181 | + } |
| 182 | + |
| 183 | + pointAnnotationManager.syncAnnotations([finalDestinationAnnotation]) |
| 184 | + } |
| 185 | + |
| 186 | + func carPlayManager(_ carPlayManager: CarPlayManager, |
| 187 | + templateWillAppear template: CPTemplate, |
| 188 | + animated: Bool) { |
| 189 | + |
| 190 | + } |
| 191 | + |
| 192 | + func carPlayManager(_ carPlayManager: CarPlayManager, |
| 193 | + templateDidAppear template: CPTemplate, |
| 194 | + animated: Bool) { |
| 195 | + |
| 196 | + } |
| 197 | + |
| 198 | + func carPlayManager(_ carPlayManager: CarPlayManager, |
| 199 | + templateWillDisappear template: CPTemplate, |
| 200 | + animated: Bool) { |
| 201 | + |
| 202 | + } |
| 203 | + |
| 204 | + func carPlayManager(_ carPlayManager: CarPlayManager, |
| 205 | + templateDidDisappear template: CPTemplate, |
| 206 | + animated: Bool) { |
| 207 | + |
| 208 | + } |
| 209 | +} |
0 commit comments