You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

functions.native.js 3.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. import { NativeModules } from 'react-native';
  2. import RNCalendarEvents from 'react-native-calendar-events';
  3. import { setCalendarAuthorization } from './actions';
  4. import { FETCH_END_DAYS, FETCH_START_DAYS } from './constants';
  5. import { _updateCalendarEntries } from './functions';
  6. export * from './functions.any';
  7. const logger = require('jitsi-meet-logger').getLogger(__filename);
  8. /**
  9. * Determines whether the calendar feature is enabled by the app. For
  10. * example, Apple through its App Store requires
  11. * {@code NSCalendarsUsageDescription} in the app's Info.plist or App Store
  12. * rejects the app.
  13. *
  14. * @returns {boolean} If the app has enabled the calendar feature, {@code true};
  15. * otherwise, {@code false}.
  16. */
  17. export function isCalendarEnabled() {
  18. const { calendarEnabled } = NativeModules.AppInfo;
  19. return typeof calendarEnabled === 'undefined' ? true : calendarEnabled;
  20. }
  21. /**
  22. * Reads the user's calendar and updates the stored entries if need be.
  23. *
  24. * @param {Object} store - The redux store.
  25. * @param {boolean} maybePromptForPermission - Flag to tell the app if it should
  26. * prompt for a calendar permission if it wasn't granted yet.
  27. * @param {boolean|undefined} forcePermission - Whether to force to re-ask for
  28. * the permission or not.
  29. * @private
  30. * @returns {void}
  31. */
  32. export function _fetchCalendarEntries(
  33. store,
  34. maybePromptForPermission,
  35. forcePermission) {
  36. const { dispatch, getState } = store;
  37. const promptForPermission
  38. = (maybePromptForPermission
  39. && !getState()['features/calendar-sync'].authorization)
  40. || forcePermission;
  41. _ensureCalendarAccess(promptForPermission, dispatch)
  42. .then(accessGranted => {
  43. if (accessGranted) {
  44. const startDate = new Date();
  45. const endDate = new Date();
  46. startDate.setDate(startDate.getDate() + FETCH_START_DAYS);
  47. endDate.setDate(endDate.getDate() + FETCH_END_DAYS);
  48. RNCalendarEvents.fetchAllEvents(
  49. startDate.getTime(),
  50. endDate.getTime(),
  51. [])
  52. .then(_updateCalendarEntries.bind(store))
  53. .catch(error =>
  54. logger.error('Error fetching calendar.', error));
  55. } else {
  56. logger.warn('Calendar access not granted.');
  57. }
  58. })
  59. .catch(reason => logger.error('Error accessing calendar.', reason));
  60. }
  61. /**
  62. * Ensures calendar access if possible and resolves the promise if it's granted.
  63. *
  64. * @param {boolean} promptForPermission - Flag to tell the app if it should
  65. * prompt for a calendar permission if it wasn't granted yet.
  66. * @param {Function} dispatch - The Redux dispatch function.
  67. * @private
  68. * @returns {Promise}
  69. */
  70. function _ensureCalendarAccess(promptForPermission, dispatch) {
  71. return new Promise((resolve, reject) => {
  72. RNCalendarEvents.authorizationStatus()
  73. .then(status => {
  74. if (status === 'authorized') {
  75. resolve(true);
  76. } else if (promptForPermission) {
  77. RNCalendarEvents.authorizeEventStore()
  78. .then(result => {
  79. dispatch(setCalendarAuthorization(result));
  80. resolve(result === 'authorized');
  81. })
  82. .catch(reject);
  83. } else {
  84. resolve(false);
  85. }
  86. })
  87. .catch(reject);
  88. });
  89. }