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.

Conference.native.js 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401
  1. // @flow
  2. import React, { Component } from 'react';
  3. // eslint-disable-next-line react-native/split-platform-components
  4. import { BackAndroid, BackHandler, View } from 'react-native';
  5. import { connect as reactReduxConnect } from 'react-redux';
  6. import { appNavigate } from '../../app';
  7. import { connect, disconnect } from '../../base/connection';
  8. import { DialogContainer } from '../../base/dialog';
  9. import { Container, LoadingIndicator } from '../../base/react';
  10. import { createDesiredLocalTracks } from '../../base/tracks';
  11. import { Filmstrip } from '../../filmstrip';
  12. import { LargeVideo } from '../../large-video';
  13. import { setToolboxVisible, Toolbox } from '../../toolbox';
  14. import styles from './styles';
  15. /**
  16. * The timeout in milliseconds after which the Toolbox will be hidden.
  17. *
  18. * @private
  19. * @type {number}
  20. */
  21. const _TOOLBOX_TIMEOUT_MS = 5000;
  22. /**
  23. * The type of the React {@code Component} props of {@link Conference}.
  24. */
  25. type Props = {
  26. /**
  27. * The indicator which determines that we are still connecting to the
  28. * conference which includes establishing the XMPP connection and then
  29. * joining the room. If truthy, then an activity/loading indicator will
  30. * be rendered.
  31. *
  32. * @private
  33. */
  34. _connecting: boolean,
  35. /**
  36. * The handler which dispatches the (redux) action connect.
  37. *
  38. * @private
  39. */
  40. _onConnect: Function,
  41. /**
  42. * The handler which dispatches the (redux) action disconnect.
  43. *
  44. * @private
  45. */
  46. _onDisconnect: Function,
  47. /**
  48. * Handles a hardware button press for back navigation. Leaves the
  49. * associated {@code Conference}.
  50. *
  51. * @private
  52. * @returns {boolean} As the associated conference is unconditionally
  53. * left and exiting the app while it renders a {@code Conference} is
  54. * undesired, {@code true} is always returned.
  55. */
  56. _onHardwareBackPress: Function,
  57. /**
  58. * The handler which dispatches the (redux) action setToolboxVisible to
  59. * show/hide the Toolbox.
  60. *
  61. * @private
  62. */
  63. _setToolboxVisible: Function,
  64. /**
  65. * The indicator which determines whether the Toolbox is visible.
  66. *
  67. * @private
  68. */
  69. _toolboxVisible: boolean
  70. };
  71. /**
  72. * The conference page of the mobile (i.e. React Native) application.
  73. */
  74. class Conference extends Component<Props> {
  75. _backHandler: ?BackHandler;
  76. _toolboxTimeout: ?number;
  77. /**
  78. * Initializes a new Conference instance.
  79. *
  80. * @param {Object} props - The read-only properties with which the new
  81. * instance is to be initialized.
  82. */
  83. constructor(props) {
  84. super(props);
  85. /**
  86. * The numerical ID of the timeout in milliseconds after which the
  87. * Toolbox will be hidden. To be used with
  88. * {@link WindowTimers#clearTimeout()}.
  89. *
  90. * @private
  91. */
  92. this._toolboxTimeout = undefined;
  93. // Bind event handlers so they are only bound once per instance.
  94. this._onClick = this._onClick.bind(this);
  95. this._onHardwareBackPress = this._onHardwareBackPress.bind(this);
  96. }
  97. /**
  98. * Implements {@link Component#componentDidMount()}. Invoked immediately
  99. * after this component is mounted.
  100. *
  101. * @inheritdoc
  102. * @returns {void}
  103. */
  104. componentDidMount() {
  105. // Set handling any hardware button presses for back navigation up.
  106. const backHandler = BackHandler || BackAndroid;
  107. if (backHandler) {
  108. this._backHandler = backHandler;
  109. backHandler.addEventListener(
  110. 'hardwareBackPress',
  111. this._onHardwareBackPress);
  112. }
  113. this._setToolboxTimeout(this.props._toolboxVisible);
  114. }
  115. /**
  116. * Implements {@link Component#componentWillMount()}. Invoked immediately
  117. * before mounting occurs. Connects the conference described by the redux
  118. * store/state.
  119. *
  120. * @inheritdoc
  121. * @returns {void}
  122. */
  123. componentWillMount() {
  124. this.props._onConnect();
  125. }
  126. /**
  127. * Implements {@link Component#componentWillUnmount()}. Invoked immediately
  128. * before this component is unmounted and destroyed. Disconnects the
  129. * conference described by the redux store/state.
  130. *
  131. * @inheritdoc
  132. * @returns {void}
  133. */
  134. componentWillUnmount() {
  135. // Tear handling any hardware button presses for back navigation down.
  136. const backHandler = this._backHandler;
  137. if (backHandler) {
  138. this._backHandler = undefined;
  139. backHandler.removeEventListener(
  140. 'hardwareBackPress',
  141. this._onHardwareBackPress);
  142. }
  143. this._clearToolboxTimeout();
  144. this.props._onDisconnect();
  145. }
  146. /**
  147. * Implements React's {@link Component#render()}.
  148. *
  149. * @inheritdoc
  150. * @returns {ReactElement}
  151. */
  152. render() {
  153. return (
  154. <Container
  155. accessibilityLabel = 'Conference'
  156. accessible = { false }
  157. onClick = { this._onClick }
  158. style = { styles.conference }
  159. touchFeedback = { false }>
  160. {/*
  161. * The LargeVideo is the lowermost stacking layer.
  162. */}
  163. <LargeVideo />
  164. {/*
  165. * The activity/loading indicator goes above everything, except
  166. * the toolbox/toolbars and the dialogs.
  167. */
  168. this.props._connecting
  169. && <View style = { styles.connectingIndicator }>
  170. <LoadingIndicator />
  171. </View>
  172. }
  173. <View style = { styles.toolboxAndFilmstripContainer } >
  174. {/*
  175. * The Toolbox is in a stacking layer bellow the Filmstrip.
  176. */}
  177. <Toolbox />
  178. {/*
  179. * The Filmstrip is in a stacking layer above the
  180. * LargeVideo. The LargeVideo and the Filmstrip form what
  181. * the Web/React app calls "videospace". Presumably, the
  182. * name and grouping stem from the fact that these two
  183. * React Components depict the videos of the conference's
  184. * participants.
  185. */}
  186. <Filmstrip />
  187. </View>
  188. {/*
  189. * The dialogs are in the topmost stacking layers.
  190. */}
  191. <DialogContainer />
  192. </Container>
  193. );
  194. }
  195. /**
  196. * Clears {@link #_toolboxTimeout} if any.
  197. *
  198. * @private
  199. * @returns {void}
  200. */
  201. _clearToolboxTimeout() {
  202. if (this._toolboxTimeout) {
  203. clearTimeout(this._toolboxTimeout);
  204. this._toolboxTimeout = undefined;
  205. }
  206. }
  207. _onClick: () => void;
  208. /**
  209. * Changes the value of the toolboxVisible state, thus allowing us to switch
  210. * between Toolbox and Filmstrip and change their visibility.
  211. *
  212. * @private
  213. * @returns {void}
  214. */
  215. _onClick() {
  216. const toolboxVisible = !this.props._toolboxVisible;
  217. this.props._setToolboxVisible(toolboxVisible);
  218. // XXX If the user taps to toggle the visibility of the Toolbox, then no
  219. // automatic toggling of the visibility should happen.
  220. this._clearToolboxTimeout();
  221. }
  222. _onHardwareBackPress: () => boolean;
  223. /**
  224. * Handles a hardware button press for back navigation.
  225. *
  226. * @returns {boolean} If the hardware button press for back navigation was
  227. * handled by this {@code Conference}, then {@code true}; otherwise,
  228. * {@code false}.
  229. */
  230. _onHardwareBackPress() {
  231. return this._backHandler && this.props._onHardwareBackPress();
  232. }
  233. /**
  234. * Triggers the default Toolbox timeout.
  235. *
  236. * @param {boolean} toolboxVisible - Indicates whether the Toolbox is
  237. * currently visible.
  238. * @private
  239. * @returns {void}
  240. */
  241. _setToolboxTimeout(toolboxVisible) {
  242. this._clearToolboxTimeout();
  243. if (toolboxVisible) {
  244. this._toolboxTimeout
  245. = setTimeout(this._onClick, _TOOLBOX_TIMEOUT_MS);
  246. }
  247. }
  248. }
  249. /**
  250. * Maps dispatching of some action to React component props.
  251. *
  252. * @param {Function} dispatch - Redux action dispatcher.
  253. * @private
  254. * @returns {{
  255. * _onConnect: Function,
  256. * _onDisconnect: Function,
  257. * _setToolboxVisible: Function
  258. * }}
  259. */
  260. function _mapDispatchToProps(dispatch) {
  261. return {
  262. /**
  263. * Dispatches actions to create the desired local tracks and for
  264. * connecting to the conference.
  265. *
  266. * @returns {void}
  267. * @private
  268. */
  269. _onConnect() {
  270. dispatch(createDesiredLocalTracks());
  271. dispatch(connect());
  272. },
  273. /**
  274. * Dispatches an action disconnecting from the conference.
  275. *
  276. * @returns {void}
  277. * @private
  278. */
  279. _onDisconnect() {
  280. dispatch(disconnect());
  281. },
  282. /**
  283. * Handles a hardware button press for back navigation. Leaves the
  284. * associated {@code Conference}.
  285. *
  286. * @returns {boolean} As the associated conference is unconditionally
  287. * left and exiting the app while it renders a {@code Conference} is
  288. * undesired, {@code true} is always returned.
  289. */
  290. _onHardwareBackPress() {
  291. dispatch(appNavigate(undefined));
  292. return true;
  293. },
  294. /**
  295. * Dispatches an action changing the visibility of the Toolbox.
  296. *
  297. * @param {boolean} visible - True to show the Toolbox or false to hide
  298. * it.
  299. * @returns {void}
  300. * @private
  301. */
  302. _setToolboxVisible(visible) {
  303. dispatch(setToolboxVisible(visible));
  304. }
  305. };
  306. }
  307. /**
  308. * Maps (parts of) the Redux state to the associated Conference's props.
  309. *
  310. * @param {Object} state - The Redux state.
  311. * @private
  312. * @returns {{
  313. * _connecting: boolean,
  314. * _toolboxVisible: boolean
  315. * }}
  316. */
  317. function _mapStateToProps(state) {
  318. const { connecting, connection } = state['features/base/connection'];
  319. const { conference, joining, leaving } = state['features/base/conference'];
  320. // XXX There is a window of time between the successful establishment of the
  321. // XMPP connection and the subsequent commencement of joining the MUC during
  322. // which the app does not appear to be doing anything according to the redux
  323. // state. In order to not toggle the _connecting props during the window of
  324. // time in question, define _connecting as follows:
  325. // - the XMPP connection is connecting, or
  326. // - the XMPP connection is connected and the conference is joining, or
  327. // - the XMPP connection is connected and we have no conference yet, nor we
  328. // are leaving one.
  329. const connecting_
  330. = connecting || (connection && (joining || (!conference && !leaving)));
  331. return {
  332. /**
  333. * The indicator which determines that we are still connecting to the
  334. * conference which includes establishing the XMPP connection and then
  335. * joining the room. If truthy, then an activity/loading indicator will
  336. * be rendered.
  337. *
  338. * @private
  339. * @type {boolean}
  340. */
  341. _connecting: Boolean(connecting_),
  342. /**
  343. * The indicator which determines whether the Toolbox is visible.
  344. *
  345. * @private
  346. * @type {boolean}
  347. */
  348. _toolboxVisible: state['features/toolbox'].visible
  349. };
  350. }
  351. // $FlowFixMe
  352. export default reactReduxConnect(_mapStateToProps, _mapDispatchToProps)(
  353. Conference);