|
| 1 | +/** |
| 2 | + * Copyright 2026 Google LLC |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +import { Alert } from 'react-native'; |
| 18 | +import { |
| 19 | + RouteStatus, |
| 20 | + type NavigationController, |
| 21 | + type ContinueToNextDestinationResponse, |
| 22 | +} from '@googlemaps/react-native-navigation-sdk'; |
| 23 | +import { showSnackbar } from './snackbar'; |
| 24 | + |
| 25 | +/** |
| 26 | + * Handles a RouteStatus result from setDestinations or continueToNextDestination. |
| 27 | + * Shows appropriate user feedback and returns whether the route was successful. |
| 28 | + * |
| 29 | + * @param routeStatus - The route status to handle. |
| 30 | + * @returns true if the route status is OK, false otherwise. |
| 31 | + */ |
| 32 | +export const handleRouteStatus = (routeStatus: RouteStatus): boolean => { |
| 33 | + switch (routeStatus) { |
| 34 | + case RouteStatus.OK: |
| 35 | + showSnackbar('Route created successfully'); |
| 36 | + return true; |
| 37 | + case RouteStatus.ROUTE_CANCELED: |
| 38 | + Alert.alert('Error', 'Route Cancelled'); |
| 39 | + return false; |
| 40 | + case RouteStatus.NO_ROUTE_FOUND: |
| 41 | + Alert.alert('Error', 'No Route Found'); |
| 42 | + return false; |
| 43 | + case RouteStatus.NETWORK_ERROR: |
| 44 | + Alert.alert('Error', 'Network Error'); |
| 45 | + return false; |
| 46 | + case RouteStatus.LOCATION_DISABLED: |
| 47 | + Alert.alert('Error', 'Location Disabled'); |
| 48 | + return false; |
| 49 | + case RouteStatus.LOCATION_UNKNOWN: |
| 50 | + Alert.alert('Error', 'Location Unknown'); |
| 51 | + return false; |
| 52 | + case RouteStatus.DUPLICATE_WAYPOINTS_ERROR: |
| 53 | + Alert.alert('Error', 'Consecutive duplicate waypoints are not allowed'); |
| 54 | + return false; |
| 55 | + default: |
| 56 | + showSnackbar('Route status: ' + routeStatus); |
| 57 | + Alert.alert('Error', 'Starting Guidance Error'); |
| 58 | + return false; |
| 59 | + } |
| 60 | +}; |
| 61 | + |
| 62 | +/** |
| 63 | + * Handles the response from continueToNextDestination. |
| 64 | + * |
| 65 | + * Checks the route status (if available, i.e. on iOS) and stops guidance |
| 66 | + * on failure. Returns the response for further processing. |
| 67 | + * |
| 68 | + * @param navigationController - The navigation controller instance. |
| 69 | + * @param response - The response from continueToNextDestination. |
| 70 | + * @returns true if navigation can continue, false if it was aborted. |
| 71 | + */ |
| 72 | +export const handleContinueToNextDestination = async ( |
| 73 | + navigationController: NavigationController, |
| 74 | + response: ContinueToNextDestinationResponse |
| 75 | +): Promise<boolean> => { |
| 76 | + // If routeStatus is available (iOS), check for errors |
| 77 | + if (response.routeStatus !== undefined) { |
| 78 | + if (!handleRouteStatus(response.routeStatus)) { |
| 79 | + await navigationController.stopGuidance(); |
| 80 | + return false; |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + if (response.waypoint === null) { |
| 85 | + showSnackbar('No more waypoints remaining'); |
| 86 | + await navigationController.stopGuidance(); |
| 87 | + return false; |
| 88 | + } |
| 89 | + |
| 90 | + return true; |
| 91 | +}; |
0 commit comments