import { useState , useEffect , useCallback } from 'react' ; import { NativeModules , NativeEventEmitter } from 'react-native' ; import type { LocationEnablerType , Listener , Config , LocationStatus , LocationSettings , } from './types' ; const { LocationEnabler } = NativeModules ; const EVENT_NAME = 'onChangeLocationSettings' ; const locationEnabler = new NativeEventEmitter ( LocationEnabler ) ; LocationEnabler . addListener = ( listener , context ? ) => locationEnabler . addListener ( EVENT_NAME , listener , context ) ; LocationEnabler . once = ( listener , context ? ) => locationEnabler . once ( EVENT_NAME , listener , context ) ; LocationEnabler . PRIORITIES = LocationEnabler . getConstants ( ) ; LocationEnabler . useLocationSettings = ( settings , initial ? ) : LocationSettings => { const [ enabled , setEnabled ] = useState < LocationStatus > ( initial || undefined ) ; const callback = useCallback ( ( ) => { const listner = LocationEnabler . addListener ( ( { locationEnabled } ) => setEnabled ( locationEnabled ) ) ; LocationEnabler . checkSettings ( settings ) ; if ( enabled ) listner . remove ( ) ; else return listner ; } , [ enabled , settings ] ) ; useEffect ( ( ) => { const listner = callback ( ) ; return ( ) => listner ?. remove ( ) ; } , [ callback ] ) ; const requestResolutionSettings = useCallback ( ( ) => LocationEnabler . requestResolutionSettings ( settings ) , [ settings ] ) ; return [ enabled , requestResolutionSettings ] ; } ; export default LocationEnabler as LocationEnablerType ;