2018-04-22 11:29:08 -05:00
|
|
|
// @flow
|
2020-04-03 21:21:00 -05:00
|
|
|
import { emitter } from '../streams/LocationStream';
|
|
|
|
|
import { getCoordsFromZip } from './GoogleMapsApi';
|
|
|
|
|
import { PermissionsAndroid, Platform } from 'react-native';
|
2019-09-26 16:48:07 -05:00
|
|
|
import AsyncStorage from '@react-native-community/async-storage';
|
2020-04-03 21:21:00 -05:00
|
|
|
import Geolocation from 'react-native-geolocation-service';
|
2018-12-22 10:27:27 -06:00
|
|
|
|
|
|
|
|
const {
|
|
|
|
|
PERMISSIONS: { ACCESS_FINE_LOCATION },
|
|
|
|
|
RESULTS,
|
2020-04-03 21:21:00 -05:00
|
|
|
request: requestPermission,
|
2018-12-22 10:27:27 -06:00
|
|
|
} = PermissionsAndroid;
|
|
|
|
|
|
|
|
|
|
let locationPermissionResponse = false;
|
|
|
|
|
|
|
|
|
|
const askLocationPermission = async () => {
|
2020-04-03 21:21:00 -05:00
|
|
|
if (Platform.OS === 'ios') {
|
2018-12-22 10:27:27 -06:00
|
|
|
locationPermissionResponse = RESULTS.GRANTED;
|
|
|
|
|
} else if (locationPermissionResponse !== RESULTS.GRANTED) {
|
|
|
|
|
locationPermissionResponse = await requestPermission(ACCESS_FINE_LOCATION, {
|
2020-04-03 21:21:00 -05:00
|
|
|
title: "Where's the TP? Location Permission",
|
|
|
|
|
message: "Where's the TP? would like to use your location to find some cookies. Is that OK?",
|
2018-12-22 10:27:27 -06:00
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
return locationPermissionResponse === RESULTS.GRANTED;
|
|
|
|
|
};
|
2018-04-22 11:29:08 -05:00
|
|
|
|
|
|
|
|
export const getCurrentPosition = () => {
|
2018-05-06 10:37:47 -05:00
|
|
|
return new Promise((resolve, reject) => {
|
2020-04-03 21:21:00 -05:00
|
|
|
askLocationPermission().then((granted) => {
|
2018-12-22 10:27:27 -06:00
|
|
|
if (!granted) {
|
2020-04-03 21:21:00 -05:00
|
|
|
return reject('Permission was not given for location');
|
2018-05-06 10:37:47 -05:00
|
|
|
}
|
2019-09-21 15:45:07 +00:00
|
|
|
Geolocation.getCurrentPosition(
|
2018-12-22 10:27:27 -06:00
|
|
|
(pos: Position) => {
|
|
|
|
|
emitter(pos), resolve(pos);
|
|
|
|
|
return pos;
|
|
|
|
|
},
|
2020-04-03 21:21:00 -05:00
|
|
|
(err) => {
|
2018-12-22 10:27:27 -06:00
|
|
|
reject(err);
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
enableHighAccuracy: true,
|
2020-04-03 21:21:00 -05:00
|
|
|
timeout: 5000,
|
2018-12-22 10:27:27 -06:00
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
});
|
2018-05-06 10:37:47 -05:00
|
|
|
});
|
2018-04-22 11:29:08 -05:00
|
|
|
};
|
|
|
|
|
|
2018-05-19 11:17:08 -05:00
|
|
|
export const getPositionFromZip = async (zip: string) => {
|
|
|
|
|
const { lat: latitude, lng: longitude } = await getCoordsFromZip(zip);
|
|
|
|
|
|
|
|
|
|
const pos: any = {
|
2018-04-22 11:29:08 -05:00
|
|
|
coords: {
|
2018-05-19 11:17:08 -05:00
|
|
|
latitude,
|
2020-04-03 21:21:00 -05:00
|
|
|
longitude,
|
|
|
|
|
},
|
2018-04-22 11:29:08 -05:00
|
|
|
};
|
2018-05-19 11:17:08 -05:00
|
|
|
|
|
|
|
|
emitter(pos);
|
|
|
|
|
|
|
|
|
|
return pos;
|
|
|
|
|
};
|
|
|
|
|
|
2018-09-16 09:58:13 -05:00
|
|
|
export const getLocation: () => Promise<Position> = async () => {
|
2020-04-03 21:21:00 -05:00
|
|
|
const currentZip = await AsyncStorage.getItem('zipcode');
|
2018-05-19 11:17:08 -05:00
|
|
|
|
2020-04-03 21:21:00 -05:00
|
|
|
if (currentZip && currentZip !== 'usegps') {
|
2018-09-16 09:58:13 -05:00
|
|
|
return getPositionFromZip(currentZip);
|
2018-05-19 11:17:08 -05:00
|
|
|
} else {
|
2020-04-03 21:21:00 -05:00
|
|
|
AsyncStorage.setItem('zipcode', 'usegps');
|
|
|
|
|
return getCurrentPosition().catch((error) => {
|
|
|
|
|
AsyncStorage.removeItem('zipcode');
|
2018-09-16 09:58:13 -05:00
|
|
|
throw error;
|
|
|
|
|
});
|
2018-05-19 11:17:08 -05:00
|
|
|
}
|
2018-04-22 11:29:08 -05:00
|
|
|
};
|