UNPKG

@gabriel-sisjr/react-native-background-location

Version:

React Native library for background location tracking using TurboModules. Track user location even when the app is minimized or in the background.

115 lines (109 loc) 4.62 kB
"use strict"; import BackgroundLocationModule from "./NativeBackgroundLocation.js"; import { LocationPermissionStatus as LocationPermissionStatusEnum, LocationAccuracy as LocationAccuracyEnum, NotificationPriority as NotificationPriorityEnum } from "./types/enums.js"; // Export types // Export enums (as values) - import and re-export as named exports to ensure they're available at runtime export const LocationPermissionStatus = LocationPermissionStatusEnum; export const LocationAccuracy = LocationAccuracyEnum; export const NotificationPriority = NotificationPriorityEnum; // Export hooks export { useLocationPermissions, useBackgroundLocation, useLocationTracking, useLocationUpdates } from "./hooks/index.js"; // Check if native module is available (won't be in simulator without proper setup) const isNativeModuleAvailable = () => { try { // Check if methods are available (works with Proxy mocks) // This must be checked first before checking if module exists if (typeof BackgroundLocationModule?.isTracking !== 'function') { return false; } // Check if module exists and is not null if (!BackgroundLocationModule || BackgroundLocationModule === null) { return false; } return true; } catch { return false; } }; /** * Background Location Tracking Module * * Provides location tracking capabilities that continue when the app is in background. * All methods return promises and will warn gracefully if native module is unavailable. */ export default { /** * Starts location tracking in background for a specific trip * @param tripId Optional trip identifier. If omitted, a new one will be generated * @param options Optional tracking configuration options * @returns Promise resolving to the effective tripId (received or generated) */ startTracking(tripId, options) { if (!isNativeModuleAvailable()) { console.warn('BackgroundLocation not available - running in simulator or module not linked?'); return Promise.resolve(tripId || `simulator-trip-${Date.now()}`); } // Convert TrackingOptions (with enums) to TrackingOptionsSpec (with strings) for Codegen const specOptions = options ? { updateInterval: options.updateInterval, fastestInterval: options.fastestInterval, maxWaitTime: options.maxWaitTime, accuracy: options.accuracy ? String(options.accuracy) : undefined, waitForAccurateLocation: options.waitForAccurateLocation, notificationTitle: options.notificationTitle, notificationText: options.notificationText, notificationChannelName: options.notificationChannelName, notificationPriority: options.notificationPriority ? String(options.notificationPriority) : undefined } : undefined; return BackgroundLocationModule.startTracking(tripId, specOptions); }, /** * Stops all location tracking and terminates the background service * @returns Promise that resolves when tracking is stopped */ stopTracking() { if (!isNativeModuleAvailable()) { console.warn('BackgroundLocation not available - running in simulator or module not linked?'); return Promise.resolve(); } return BackgroundLocationModule.stopTracking(); }, /** * Checks if location tracking is currently active * @returns Promise resolving to object with active status and current tripId if tracking */ isTracking() { if (!isNativeModuleAvailable()) { console.warn('BackgroundLocation not available - running in simulator or module not linked?'); return Promise.resolve({ active: false }); } return BackgroundLocationModule.isTracking(); }, /** * Retrieves all stored location points for a specific trip * @param tripId The trip identifier * @returns Promise resolving to array of location coordinates with extended location data */ getLocations(tripId) { if (!isNativeModuleAvailable()) { console.warn('BackgroundLocation not available - running in simulator or module not linked?'); return Promise.resolve([]); } return BackgroundLocationModule.getLocations(tripId); }, /** * Clears all stored location data for a specific trip * @param tripId The trip identifier to clear * @returns Promise that resolves when data is cleared */ clearTrip(tripId) { if (!isNativeModuleAvailable()) { console.warn('BackgroundLocation not available - running in simulator or module not linked?'); return Promise.resolve(); } return BackgroundLocationModule.clearTrip(tripId); } }; //# sourceMappingURL=index.js.map