43 lines
1.4 KiB
TypeScript
43 lines
1.4 KiB
TypeScript
import { Platform, PermissionsAndroid } from 'react-native';
|
|
import { request, PERMISSIONS, RESULTS } from 'react-native-permissions';
|
|
import { Coordinates } from '@app-types/index';
|
|
|
|
class LocationService {
|
|
async requestPermission(): Promise<boolean> {
|
|
if (Platform.OS === 'android') {
|
|
const granted = await PermissionsAndroid.request(
|
|
PermissionsAndroid.PERMISSIONS.ACCESS_FINE_LOCATION,
|
|
{
|
|
title: 'Location Permission',
|
|
message: 'This app needs access to your location to track deliveries.',
|
|
buttonNeutral: 'Ask Me Later',
|
|
buttonNegative: 'Cancel',
|
|
buttonPositive: 'OK',
|
|
}
|
|
);
|
|
return granted === PermissionsAndroid.RESULTS.GRANTED;
|
|
} else {
|
|
const result = await request(PERMISSIONS.IOS.LOCATION_WHEN_IN_USE);
|
|
return result === RESULTS.GRANTED;
|
|
}
|
|
}
|
|
|
|
// Mocked location resolver
|
|
async getCurrentLocation(): Promise<{ coordinates: Coordinates; address: string; city: string; state: string; pincode: string }> {
|
|
// Return mock coordinates and address after a delay
|
|
await new Promise<void>(resolve => setTimeout(resolve, 800));
|
|
return {
|
|
coordinates: {
|
|
latitude: 12.9716,
|
|
longitude: 77.5946,
|
|
},
|
|
address: 'Koramangala 4th Block, Bengaluru',
|
|
city: 'Bengaluru',
|
|
state: 'Karnataka',
|
|
pincode: '560034',
|
|
};
|
|
}
|
|
}
|
|
|
|
export const locationService = new LocationService();
|