forked from vonovak/react-native-add-calendar-event
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
78 lines (65 loc) · 2.77 KB
/
index.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
74
75
76
77
78
import { NativeModules, Platform, PermissionsAndroid, processColor } from 'react-native';
const { WRITE_CALENDAR, READ_CALENDAR } = PermissionsAndroid.PERMISSIONS;
const AddCalendarEvent = NativeModules.AddCalendarEvent;
export const presentEventViewingDialog = options => {
const toCall = () => AddCalendarEvent.presentEventViewingDialog(processColorsIOS(options));
return withPermissionsCheck(toCall);
};
export const presentEventEditingDialog = options => {
const toCall = () => AddCalendarEvent.presentEventEditingDialog(processColorsIOS(options));
return withPermissionsCheck(toCall);
};
export const presentEventCreatingDialog = options => {
const toCall = () => AddCalendarEvent.presentEventCreatingDialog(processColorsIOS(options));
return withPermissionsCheck(toCall);
};
const processColorsIOS = config => {
if (Platform.OS === 'android' || !config) {
return config;
}
const { navigationBarIOS } = config;
if (navigationBarIOS) {
const { tintColor, backgroundColor, barTintColor, titleColor } = navigationBarIOS;
navigationBarIOS.tintColor = tintColor && processColor(tintColor);
navigationBarIOS.backgroundColor = backgroundColor && processColor(backgroundColor);
navigationBarIOS.barTintColor = barTintColor && processColor(barTintColor);
navigationBarIOS.titleColor = titleColor && processColor(titleColor);
}
return config;
};
const withPermissionsCheck = toCallWhenPermissionGranted => {
if (Platform.OS === 'android') {
return withPermissionsCheckAndroid(toCallWhenPermissionGranted);
} else {
return withPermissionsCheckIOS(toCallWhenPermissionGranted);
}
};
const permissionNotGranted = 'permissionNotGranted';
const withPermissionsCheckAndroid = async toCallWhenPermissionGranted => {
// it seems unnecessary to check first, but if permission is manually disabled
// the PermissionsAndroid.request will return granted (a RN bug?)
const [hasWritePermission, hasReadPermission] = await Promise.all([
PermissionsAndroid.check(WRITE_CALENDAR),
PermissionsAndroid.check(READ_CALENDAR),
]);
if (hasWritePermission === true && hasReadPermission === true) {
return toCallWhenPermissionGranted();
}
const results = await PermissionsAndroid.requestMultiple([WRITE_CALENDAR, READ_CALENDAR]);
if (
results[READ_CALENDAR] === PermissionsAndroid.RESULTS.GRANTED &&
results[WRITE_CALENDAR] === PermissionsAndroid.RESULTS.GRANTED
) {
return toCallWhenPermissionGranted();
} else {
return Promise.reject(permissionNotGranted);
}
};
const withPermissionsCheckIOS = async toCallWhenPermissionGranted => {
const hasPermission = await AddCalendarEvent.requestCalendarPermission();
if (hasPermission) {
return toCallWhenPermissionGranted();
} else {
return Promise.reject(permissionNotGranted);
}
};