Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

functions.native.js 5.1KB

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