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.

ConferenceNotification.native.js 7.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. // @flow
  2. import React, { Component } from 'react';
  3. import {
  4. Text,
  5. TouchableOpacity,
  6. View
  7. } from 'react-native';
  8. import { connect } from 'react-redux';
  9. import { appNavigate } from '../../app';
  10. import { getURLWithoutParams } from '../../base/connection';
  11. import { translate } from '../../base/i18n';
  12. import { Icon } from '../../base/font-icons';
  13. import { ASPECT_RATIO_NARROW } from '../../base/responsive-ui';
  14. import { getLocalizedDateFormatter } from '../../base/util';
  15. import styles from './styles';
  16. const ALERT_MILLISECONDS = 5 * 60 * 1000;
  17. type Props = {
  18. /**
  19. * The Redux dispatch function.
  20. */
  21. dispatch: Function,
  22. /**
  23. * The current aspect ratio of the screen.
  24. */
  25. _aspectRatio: Symbol,
  26. /**
  27. * The URL of the current conference without params.
  28. */
  29. _currentConferenceURL: string,
  30. /**
  31. * The calendar event list.
  32. */
  33. _eventList: Array<Object>,
  34. /**
  35. * The translate function.
  36. */
  37. t: Function
  38. };
  39. type State = {
  40. /**
  41. * The event object to display the notification for.
  42. */
  43. event?: Object
  44. };
  45. /**
  46. * Component to display a permanent badge-like notification on the
  47. * conference screen when another meeting is about to start.
  48. */
  49. class ConferenceNotification extends Component<Props, State> {
  50. updateIntervalId: number;
  51. /**
  52. * Constructor of the ConferenceNotification component.
  53. *
  54. * @inheritdoc
  55. */
  56. constructor(props) {
  57. super(props);
  58. this.state = {
  59. event: undefined
  60. };
  61. this._getNotificationContentStyle
  62. = this._getNotificationContentStyle.bind(this);
  63. this._getNotificationPosition
  64. = this._getNotificationPosition.bind(this);
  65. this._maybeDisplayNotification
  66. = this._maybeDisplayNotification.bind(this);
  67. this._onGoToNext = this._onGoToNext.bind(this);
  68. }
  69. /**
  70. * Implements React Component's componentDidMount.
  71. *
  72. * @inheritdoc
  73. */
  74. componentDidMount() {
  75. this.updateIntervalId = setInterval(
  76. this._maybeDisplayNotification,
  77. 10 * 1000
  78. );
  79. }
  80. /**
  81. * Implements React Component's componentWillUnmount.
  82. *
  83. * @inheritdoc
  84. */
  85. componentWillUnmount() {
  86. clearTimeout(this.updateIntervalId);
  87. }
  88. /**
  89. * Implements the React Components's render method.
  90. *
  91. * @inheritdoc
  92. */
  93. render() {
  94. const { event } = this.state;
  95. const { t } = this.props;
  96. if (event) {
  97. return (
  98. <View
  99. style = { [
  100. styles.notificationContainer,
  101. this._getNotificationPosition()
  102. ] } >
  103. <View
  104. style = { this._getNotificationContentStyle() }>
  105. <TouchableOpacity
  106. onPress = { this._onGoToNext } >
  107. <View style = { styles.touchableView }>
  108. <View
  109. style = {
  110. styles.notificationTextContainer
  111. }>
  112. <Text style = { styles.notificationText }>
  113. { t('calendarSync.nextMeeting') }
  114. </Text>
  115. <Text style = { styles.notificationText }>
  116. {
  117. getLocalizedDateFormatter(
  118. event.startDate
  119. ).fromNow()
  120. }
  121. </Text>
  122. </View>
  123. <View
  124. style = {
  125. styles.notificationIconContainer
  126. }>
  127. <Icon
  128. name = 'navigate_next'
  129. style = { styles.notificationIcon } />
  130. </View>
  131. </View>
  132. </TouchableOpacity>
  133. </View>
  134. </View>
  135. );
  136. }
  137. return null;
  138. }
  139. _getNotificationContentStyle: () => Array<Object>
  140. /**
  141. * Decides the color of the notification and some additional
  142. * styles based on notificationPosition.
  143. *
  144. * @private
  145. * @returns {Array<Object>}
  146. */
  147. _getNotificationContentStyle() {
  148. const { event } = this.state;
  149. const { _aspectRatio } = this.props;
  150. const now = Date.now();
  151. const style = [
  152. styles.notificationContent
  153. ];
  154. if (event && event.startDate < now && event.endDate > now) {
  155. style.push(styles.notificationContentPast);
  156. } else {
  157. style.push(styles.notificationContentNext);
  158. }
  159. if (_aspectRatio === ASPECT_RATIO_NARROW) {
  160. style.push(styles.notificationContentSide);
  161. } else {
  162. style.push(styles.notificationContentTop);
  163. }
  164. return style;
  165. }
  166. _getNotificationPosition: () => Object;
  167. /**
  168. * Decides the position of the notification.
  169. *
  170. * @private
  171. * @returns {Object}
  172. */
  173. _getNotificationPosition() {
  174. const { _aspectRatio } = this.props;
  175. if (_aspectRatio === ASPECT_RATIO_NARROW) {
  176. return styles.notificationContainerSide;
  177. }
  178. return styles.notificationContainerTop;
  179. }
  180. _maybeDisplayNotification: () => void;
  181. /**
  182. * Periodically checks if there is an event in the calendar for which we
  183. * need to show a notification.
  184. *
  185. * @private
  186. * @returns {void}
  187. */
  188. _maybeDisplayNotification() {
  189. const { _currentConferenceURL, _eventList } = this.props;
  190. let eventToShow;
  191. if (_eventList && _eventList.length) {
  192. const now = Date.now();
  193. for (const event of _eventList) {
  194. if (event.url !== _currentConferenceURL) {
  195. if ((!eventToShow
  196. && event.startDate > now
  197. && event.startDate < now + ALERT_MILLISECONDS)
  198. || (event.startDate < now && event.endDate > now)
  199. ) {
  200. eventToShow = event;
  201. }
  202. }
  203. }
  204. }
  205. this.setState({
  206. event: eventToShow
  207. });
  208. }
  209. _onGoToNext: () => void;
  210. /**
  211. * Opens the meeting URL that the notification shows.
  212. *
  213. * @private
  214. * @param {string} url - The URL to open.
  215. * @returns {void}
  216. */
  217. _onGoToNext() {
  218. const { event } = this.state;
  219. if (event && event.url) {
  220. this.props.dispatch(appNavigate(event.url));
  221. }
  222. }
  223. }
  224. /**
  225. * Maps redux state to component props.
  226. *
  227. * @param {Object} state - The redux state.
  228. * @returns {{
  229. * _aspectRatio: Symbol,
  230. * _currentConferenceURL: string,
  231. * _eventList: Array
  232. * }}
  233. */
  234. export function _mapStateToProps(state: Object) {
  235. const { locationURL } = state['features/base/connection'];
  236. return {
  237. _aspectRatio: state['features/base/responsive-ui'].aspectRatio,
  238. _currentConferenceURL:
  239. locationURL ? getURLWithoutParams(locationURL)._url : '',
  240. _eventList: state['features/calendar-sync'].events
  241. };
  242. }
  243. export default translate(connect(_mapStateToProps)(ConferenceNotification));