Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

functions.native.ts 5.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. import { NativeModules, Platform } from 'react-native';
  2. import RNCalendarEvents from 'react-native-calendar-events';
  3. import { IReduxState, IStore } from '../app/types';
  4. import { IStateful } from '../base/app/types';
  5. import { CALENDAR_ENABLED } from '../base/flags/constants';
  6. import { getFeatureFlag } from '../base/flags/functions';
  7. import { getShareInfoText } from '../invite/functions';
  8. import { setCalendarAuthorization } from './actions.native';
  9. import { FETCH_END_DAYS, FETCH_START_DAYS } from './constants';
  10. import { _updateCalendarEntries } from './functions.native';
  11. import logger from './logger';
  12. export * from './functions.any';
  13. /**
  14. * Adds a Jitsi link to a calendar entry.
  15. *
  16. * @param {Object} state - The Redux state.
  17. * @param {string} id - The ID of the calendar entry.
  18. * @param {string} link - The link to add info with.
  19. * @returns {Promise<*>}
  20. */
  21. export function addLinkToCalendarEntry(
  22. state: IReduxState, id: string, link: string): Promise<any> {
  23. return new Promise((resolve, reject) => {
  24. getShareInfoText(state, link, true).then((shareInfoText: string) => {
  25. RNCalendarEvents.findEventById(id).then((event: any) => {
  26. const updateText
  27. = event.description
  28. ? `${event.description}\n\n${shareInfoText}`
  29. : shareInfoText;
  30. const updateObject = {
  31. id: event.id,
  32. ...Platform.select({
  33. ios: {
  34. notes: updateText
  35. },
  36. android: {
  37. description: updateText
  38. }
  39. })
  40. };
  41. // @ts-ignore
  42. RNCalendarEvents.saveEvent(event.title, updateObject)
  43. .then(resolve, reject);
  44. }, reject);
  45. }, reject);
  46. });
  47. }
  48. /**
  49. * Determines whether the calendar feature is enabled by the app. For
  50. * example, Apple through its App Store requires
  51. * {@code NSCalendarsUsageDescription} in the app's Info.plist or App Store
  52. * rejects the app. It could also be disabled with a feature flag.
  53. *
  54. * @param {Function|Object} stateful - The redux store or {@code getState}
  55. * function.
  56. * @returns {boolean} If the app has enabled the calendar feature, {@code true};
  57. * otherwise, {@code false}.
  58. */
  59. export function isCalendarEnabled(stateful: IStateful) {
  60. const flag = getFeatureFlag(stateful, CALENDAR_ENABLED);
  61. if (typeof flag !== 'undefined') {
  62. return flag;
  63. }
  64. const { calendarEnabled = true } = NativeModules.AppInfo;
  65. return calendarEnabled;
  66. }
  67. /**
  68. * Reads the user's calendar and updates the stored entries if need be.
  69. *
  70. * @param {Object} store - The redux store.
  71. * @param {boolean} maybePromptForPermission - Flag to tell the app if it should
  72. * prompt for a calendar permission if it wasn't granted yet.
  73. * @param {boolean|undefined} forcePermission - Whether to force to re-ask for
  74. * the permission or not.
  75. * @private
  76. * @returns {void}
  77. */
  78. export function _fetchCalendarEntries(
  79. store: IStore,
  80. maybePromptForPermission: boolean,
  81. forcePermission?: boolean) {
  82. const { dispatch, getState } = store;
  83. const promptForPermission
  84. = (maybePromptForPermission
  85. && !getState()['features/calendar-sync'].authorization)
  86. || forcePermission;
  87. _ensureCalendarAccess(promptForPermission, dispatch)
  88. .then(accessGranted => {
  89. if (accessGranted) {
  90. const startDate = new Date();
  91. const endDate = new Date();
  92. startDate.setDate(startDate.getDate() + FETCH_START_DAYS);
  93. endDate.setDate(endDate.getDate() + FETCH_END_DAYS);
  94. RNCalendarEvents.fetchAllEvents(
  95. // @ts-ignore
  96. startDate.getTime(),
  97. endDate.getTime(),
  98. [])
  99. .then(_updateCalendarEntries.bind(store))
  100. .catch(error =>
  101. logger.error('Error fetching calendar.', error));
  102. } else {
  103. logger.warn('Calendar access not granted.');
  104. }
  105. })
  106. .catch(reason => logger.error('Error accessing calendar.', reason));
  107. }
  108. /**
  109. * Ensures calendar access if possible and resolves the promise if it's granted.
  110. *
  111. * @param {boolean} promptForPermission - Flag to tell the app if it should
  112. * prompt for a calendar permission if it wasn't granted yet.
  113. * @param {Function} dispatch - The Redux dispatch function.
  114. * @private
  115. * @returns {Promise}
  116. */
  117. function _ensureCalendarAccess(promptForPermission: boolean | undefined, dispatch: IStore['dispatch']) {
  118. return new Promise((resolve, reject) => {
  119. RNCalendarEvents.checkPermissions()
  120. .then(status => {
  121. if (status === 'authorized') {
  122. resolve(true);
  123. } else if (promptForPermission) {
  124. RNCalendarEvents.requestPermissions()
  125. .then(result => {
  126. dispatch(setCalendarAuthorization(result));
  127. resolve(result === 'authorized');
  128. })
  129. .catch(reject);
  130. } else {
  131. resolve(false);
  132. }
  133. })
  134. .catch(reject);
  135. });
  136. }