forked from philiWeitz/react-native-location-switch
-
Notifications
You must be signed in to change notification settings - Fork 1
/
LocationSwitch.js
73 lines (54 loc) · 1.75 KB
/
LocationSwitch.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
'use strict';
import {AppState, NativeModules, Platform} from "react-native";
const locationSwitchModule = NativeModules.RNReactNativeLocationSwitch;
const APP_STATE_HANDLER_NAME = 'change';
let sSuccessCallback;
let sErrorCallback;
function handleAppStateChange(nextAppState) {
if (Platform.OS === 'ios' && nextAppState === 'active') {
removeAppStateListener();
locationSwitchModule.isLocationEnabled(onIOSSuccessCallback, onIOSErrorCallback);
}
}
function removeAppStateListener() {
if (Platform.OS === 'ios') {
AppState.removeEventListener(APP_STATE_HANDLER_NAME, handleAppStateChange);
}
}
function addAppStateListener() {
if (Platform.OS === 'ios') {
AppState.addEventListener(APP_STATE_HANDLER_NAME, handleAppStateChange);
}
}
function onIOSSuccessCallback() {
removeAppStateListener();
if (sSuccessCallback) {
sSuccessCallback();
}
}
function onIOSErrorCallback() {
removeAppStateListener();
if (sErrorCallback) {
sErrorCallback();
}
}
const LocationSwitch = {
isLocationEnabled(successCallback, errorCallback) {
if (Platform.OS === 'ios') {
locationSwitchModule.isLocationEnabled(successCallback, errorCallback);
} else {
locationSwitchModule.isLocationEnabled(successCallback, errorCallback);
}
},
enableLocationService(interval, requestHighAccuracy, successCallback, errorCallback) {
if (Platform.OS === 'ios') {
sSuccessCallback = successCallback;
sErrorCallback = errorCallback;
addAppStateListener();
locationSwitchModule.enableLocationService(onIOSSuccessCallback, onIOSErrorCallback);
} else {
locationSwitchModule.enableLocationService(interval, requestHighAccuracy, successCallback, errorCallback);
}
},
};
export default LocationSwitch;