-
Notifications
You must be signed in to change notification settings - Fork 2.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
57603a0
commit cd8cb9b
Showing
12 changed files
with
9,464 additions
and
401 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
100 changes: 100 additions & 0 deletions
100
src/libs/getCurrentPosition/getCurrentPosition.desktop.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
import CONFIG from '../../CONFIG'; | ||
import {GetCurrentPosition, GeolocationErrorCode} from './getCurrentPosition.types'; | ||
|
||
type GoogleAPIsGeoLocateResponse = { | ||
location: { | ||
lat: number; | ||
lng: number; | ||
}; | ||
accuracy: number; | ||
}; | ||
|
||
const BASE_URL = 'https://www.googleapis.com/geolocation/v1/geolocate'; | ||
|
||
// Api request config | ||
const requestConfig: RequestInit = { | ||
method: 'POST', | ||
}; | ||
|
||
const getCurrentPosition: GetCurrentPosition = ( | ||
success, | ||
error, | ||
options, // We will ignore options.maximumAge and options.enableHighAccuracy since cant pass it to geolocate api directly | ||
) => { | ||
// Emulate the timeout param with an abort signal | ||
let timeoutID: NodeJS.Timeout; | ||
if (options?.timeout) { | ||
const abortController = new AbortController(); | ||
timeoutID = setTimeout(() => { | ||
abortController.abort(); | ||
}, options.timeout); | ||
requestConfig.signal = abortController.signal; | ||
} | ||
|
||
// Gets current location from google geolocation api | ||
fetch(`${BASE_URL}?key=${CONFIG.GOOGLE_GEOLOCATION_API_KEY}`, requestConfig) | ||
.then((response) => { | ||
if (!response.ok) { | ||
throw new Error(response.statusText); | ||
} | ||
|
||
return response.json(); | ||
}) | ||
.then((response: GoogleAPIsGeoLocateResponse) => { | ||
// Transform response to match with the window.navigator.geolocation.getCurrentPosition response | ||
const transformedResponse = { | ||
coords: { | ||
latitude: response.location.lat, | ||
longitude: response.location.lng, | ||
accuracy: response.accuracy, | ||
// Return null for these keys as we don't get them in response when api is directly called | ||
altitude: null, | ||
altitudeAccuracy: null, | ||
heading: null, | ||
speed: null, | ||
}, | ||
timestamp: Date.now(), // The api call doesn't return timestamp directly, so we emulate ourselves | ||
}; | ||
|
||
success(transformedResponse); | ||
}) | ||
.catch((apiError) => { | ||
// The base error object when api call fails | ||
const baseErrorObject = { | ||
// Since we are making a direct api call, we won't get permission denied error code | ||
PERMISSION_DENIED: GeolocationErrorCode.PERMISSION_DENIED, | ||
POSITION_UNAVAILABLE: GeolocationErrorCode.POSITION_UNAVAILABLE, | ||
TIMEOUT: GeolocationErrorCode.TIMEOUT, | ||
NOT_SUPPORTED: GeolocationErrorCode.NOT_SUPPORTED, | ||
}; | ||
|
||
// Return timeout error on abort | ||
if (apiError instanceof Error && apiError.message === 'The user aborted a request.') { | ||
error({ | ||
...baseErrorObject, | ||
code: GeolocationErrorCode.TIMEOUT, | ||
// Adds a generic message for desktop, when timeout occurs | ||
message: 'timeout', | ||
}); | ||
return; | ||
} | ||
|
||
error({ | ||
...baseErrorObject, | ||
code: GeolocationErrorCode.POSITION_UNAVAILABLE, | ||
// Adding a generic message for desktop, position unavailable can mean 'no internet' | ||
// or some other position related issues on api call failure (excluding timeout) | ||
message: 'position unavailable', | ||
}); | ||
}) | ||
.finally(() => { | ||
if (!timeoutID) { | ||
return; | ||
} | ||
|
||
// Clear any leftover timeouts | ||
clearTimeout(timeoutID); | ||
}); | ||
}; | ||
|
||
export default getCurrentPosition; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import Geolocation from '@react-native-community/geolocation'; | ||
import {GetCurrentPosition} from './getCurrentPosition.types'; | ||
|
||
Geolocation.setRNConfiguration({ | ||
skipPermissionRequests: false, | ||
authorizationLevel: 'whenInUse', | ||
locationProvider: 'auto', | ||
}); | ||
|
||
const getCurrentPosition: GetCurrentPosition = (success, error, config) => { | ||
Geolocation.getCurrentPosition(success, error, config); | ||
}; | ||
|
||
export default getCurrentPosition; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import {GeolocationErrorCode, GetCurrentPosition} from './getCurrentPosition.types'; | ||
|
||
const getCurrentPosition: GetCurrentPosition = (success, error, options) => { | ||
if (typeof navigator === 'undefined' || !('geolocation' in navigator)) { | ||
error({ | ||
code: GeolocationErrorCode.NOT_SUPPORTED, | ||
message: 'Geolocation is not supported by this environment.', | ||
PERMISSION_DENIED: GeolocationErrorCode.PERMISSION_DENIED, | ||
POSITION_UNAVAILABLE: GeolocationErrorCode.POSITION_UNAVAILABLE, | ||
TIMEOUT: GeolocationErrorCode.TIMEOUT, | ||
NOT_SUPPORTED: GeolocationErrorCode.NOT_SUPPORTED, | ||
}); | ||
return; | ||
} | ||
|
||
navigator.geolocation.getCurrentPosition(success, error, options); | ||
}; | ||
|
||
export default getCurrentPosition; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
type GeolocationSuccessCallback = (position: { | ||
coords: { | ||
latitude: number; | ||
longitude: number; | ||
altitude: number | null; | ||
accuracy: number; | ||
altitudeAccuracy: number | null; | ||
heading: number | null; | ||
speed: number | null; | ||
}; | ||
timestamp: number; | ||
}) => void; | ||
|
||
type GeolocationErrorCallback = (error: { | ||
code: (typeof GeolocationErrorCode)[keyof typeof GeolocationErrorCode]; | ||
message: string; | ||
PERMISSION_DENIED: typeof GeolocationErrorCode.PERMISSION_DENIED; | ||
POSITION_UNAVAILABLE: typeof GeolocationErrorCode.POSITION_UNAVAILABLE; | ||
TIMEOUT: typeof GeolocationErrorCode.TIMEOUT; | ||
|
||
/* Web only */ | ||
NOT_SUPPORTED?: typeof GeolocationErrorCode.NOT_SUPPORTED; | ||
}) => void; | ||
|
||
const GeolocationErrorCode = { | ||
PERMISSION_DENIED: 1, | ||
POSITION_UNAVAILABLE: 2, | ||
TIMEOUT: 3, | ||
NOT_SUPPORTED: -1, | ||
}; | ||
|
||
type GeolocationOptions = { | ||
timeout?: number; | ||
maximumAge?: number; | ||
enableHighAccuracy?: boolean; | ||
|
||
/** Native only */ | ||
distanceFilter?: number; | ||
|
||
/** Native only */ | ||
useSignificantChanges?: boolean; | ||
|
||
/** Native only */ | ||
interval?: number; | ||
|
||
/** Native only */ | ||
fastestInterval?: number; | ||
}; | ||
|
||
type GetCurrentPosition = (success: GeolocationSuccessCallback, error: GeolocationErrorCallback, options?: GeolocationOptions) => void; | ||
|
||
export {GeolocationErrorCode}; | ||
|
||
export type {GeolocationSuccessCallback, GeolocationErrorCallback, GeolocationOptions, GetCurrentPosition}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
import getCurrentPosition from './getCurrentPosition'; | ||
|
||
export default getCurrentPosition; |