103 lines
2.6 KiB
TypeScript
103 lines
2.6 KiB
TypeScript
/* eslint-disable @typescript-eslint/no-explicit-any */
|
|
/**
|
|
* Configurable stand-in for `expo-location`.
|
|
*
|
|
* `__configure` lets a test pick permission / services / fix / failure per case.
|
|
*/
|
|
|
|
interface Position {
|
|
coords: {
|
|
latitude: number
|
|
longitude: number
|
|
accuracy?: number | null
|
|
altitude?: number | null
|
|
altitudeAccuracy?: number | null
|
|
heading?: number | null
|
|
speed?: number | null
|
|
}
|
|
timestamp: number
|
|
}
|
|
|
|
interface LocationConfig {
|
|
permission: 'granted' | 'denied' | 'undetermined'
|
|
servicesEnabled: boolean
|
|
position: Position | null
|
|
throwOnPermission: boolean
|
|
throwOnServices: boolean
|
|
throwOnPosition: boolean
|
|
}
|
|
|
|
const defaultPosition: Position = {
|
|
coords: { latitude: 17.00012, longitude: 78.00034, accuracy: 5 },
|
|
timestamp: 1_700_000_000_000,
|
|
}
|
|
|
|
const config: LocationConfig = {
|
|
permission: 'granted',
|
|
servicesEnabled: true,
|
|
position: defaultPosition,
|
|
throwOnPermission: false,
|
|
throwOnServices: false,
|
|
throwOnPosition: false,
|
|
}
|
|
|
|
export function __configure(next: Partial<LocationConfig>) {
|
|
Object.assign(config, next)
|
|
}
|
|
|
|
export function __reset() {
|
|
config.permission = 'granted'
|
|
config.servicesEnabled = true
|
|
config.position = defaultPosition
|
|
config.throwOnPermission = false
|
|
config.throwOnServices = false
|
|
config.throwOnPosition = false
|
|
}
|
|
|
|
export const Accuracy = {
|
|
Lowest: 1,
|
|
Low: 2,
|
|
Balanced: 3,
|
|
High: 4,
|
|
Highest: 5,
|
|
BestForNavigation: 6,
|
|
} as const
|
|
|
|
export async function requestForegroundPermissionsAsync() {
|
|
if (config.throwOnPermission) throw new Error('permission request failed')
|
|
return { status: config.permission, granted: config.permission === 'granted', canAskAgain: true }
|
|
}
|
|
|
|
export async function getForegroundPermissionsAsync() {
|
|
if (config.throwOnPermission) throw new Error('permission read failed')
|
|
return { status: config.permission, granted: config.permission === 'granted', canAskAgain: true }
|
|
}
|
|
|
|
export async function hasServicesEnabledAsync() {
|
|
if (config.throwOnServices) throw new Error('services check failed')
|
|
return config.servicesEnabled
|
|
}
|
|
|
|
export async function getCurrentPositionAsync(_options?: unknown): Promise<Position> {
|
|
if (config.throwOnPosition) throw new Error('no fix')
|
|
if (!config.position) throw new Error('no fix')
|
|
return config.position
|
|
}
|
|
|
|
export function watchPositionAsync(): { remove: () => void } {
|
|
return { remove: jest.fn() }
|
|
}
|
|
|
|
export const LocationAccuracy = Accuracy
|
|
export const GeofencingEventType = { Enter: 1, Exit: 2 }
|
|
|
|
export default {
|
|
Accuracy,
|
|
requestForegroundPermissionsAsync,
|
|
getForegroundPermissionsAsync,
|
|
hasServicesEnabledAsync,
|
|
getCurrentPositionAsync,
|
|
watchPositionAsync,
|
|
__configure,
|
|
__reset,
|
|
}
|