Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
82 changes: 41 additions & 41 deletions src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,47 +10,47 @@ import type {

const { LocationEnabler } = NativeModules;
const EVENT_NAME = 'onChangeLocationSettings';

// Override
const locationEnabler = new NativeEventEmitter(LocationEnabler);

LocationEnabler.addListener = (listener: Listener, context?: any) =>
Platform.OS === 'android'
? locationEnabler.addListener(EVENT_NAME, listener, context)
: () => {};

LocationEnabler.once = (listener: Listener, context?: any) =>
locationEnabler.once(EVENT_NAME, listener, context);

LocationEnabler.PRIORITIES = LocationEnabler.getConstants();

LocationEnabler.useLocationSettings = (
settings: Config,
initial?: LocationStatus
): LocationSettings => {
const [enabled, setEnabled] = useState<LocationStatus>(initial || undefined);

const callback = useCallback(() => {
const listner = LocationEnabler.addListener(
({ locationEnabled }: { locationEnabled: boolean }) =>
setEnabled(locationEnabled)
if(Platform.OS === 'android'){
// Override
const locationEnabler = new NativeEventEmitter(LocationEnabler);

LocationEnabler.addListener = (listener: Listener, context?: any) =>
Platform.OS === 'android'
? locationEnabler.addListener(EVENT_NAME, listener, context)
: () => {};

LocationEnabler.once = (listener: Listener, context?: any) =>
locationEnabler.once(EVENT_NAME, listener, context);

LocationEnabler.PRIORITIES = LocationEnabler.getConstants();

LocationEnabler.useLocationSettings = (
settings: Config,
initial?: LocationStatus
): LocationSettings => {
const [enabled, setEnabled] = useState<LocationStatus>(initial || undefined);

const callback = useCallback(() => {
const listner = LocationEnabler.addListener(
({ locationEnabled }: { locationEnabled: boolean }) =>
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]
);
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];
};

return [enabled, requestResolutionSettings];
};
}
export default LocationEnabler as LocationEnablerType;