您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

Conference.native.js 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394
  1. import PropTypes from 'prop-types';
  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 { OverlayContainer } from '../../overlay';
  14. import { setToolboxVisible, Toolbox } from '../../toolbox';
  15. import styles from './styles';
  16. /**
  17. * The timeout in milliseconds after which the Toolbox will be hidden.
  18. *
  19. * @private
  20. * @type {number}
  21. */
  22. const _TOOLBOX_TIMEOUT_MS = 5000;
  23. /**
  24. * The conference page of the mobile (i.e. React Native) application.
  25. */
  26. class Conference extends Component {
  27. /**
  28. * Conference component's property types.
  29. *
  30. * @static
  31. */
  32. static propTypes = {
  33. /**
  34. * The indicator which determines that we are still connecting to the
  35. * conference which includes establishing the XMPP connection and then
  36. * joining the room. If truthy, then an activity/loading indicator will
  37. * be rendered.
  38. *
  39. * @private
  40. */
  41. _connecting: PropTypes.bool,
  42. /**
  43. * The handler which dispatches the (redux) action connect.
  44. *
  45. * @private
  46. */
  47. _onConnect: PropTypes.func,
  48. /**
  49. * The handler which dispatches the (redux) action disconnect.
  50. *
  51. * @private
  52. */
  53. _onDisconnect: PropTypes.func,
  54. /**
  55. * Handles a hardware button press for back navigation. Leaves the
  56. * associated {@code Conference}.
  57. *
  58. * @private
  59. * @returns {boolean} As the associated conference is unconditionally
  60. * left and exiting the app while it renders a {@code Conference} is
  61. * undesired, {@code true} is always returned.
  62. */
  63. _onHardwareBackPress: PropTypes.func,
  64. /**
  65. * The handler which dispatches the (redux) action setToolboxVisible to
  66. * show/hide the Toolbox.
  67. *
  68. * @private
  69. */
  70. _setToolboxVisible: PropTypes.func,
  71. /**
  72. * The indicator which determines whether the Toolbox is visible.
  73. *
  74. * @private
  75. */
  76. _toolboxVisible: PropTypes.bool
  77. };
  78. /**
  79. * Initializes a new Conference instance.
  80. *
  81. * @param {Object} props - The read-only properties with which the new
  82. * instance is to be initialized.
  83. */
  84. constructor(props) {
  85. super(props);
  86. /**
  87. * The numerical ID of the timeout in milliseconds after which the
  88. * Toolbox will be hidden. To be used with
  89. * {@link WindowTimers#clearTimeout()}.
  90. *
  91. * @private
  92. */
  93. this._toolboxTimeout = undefined;
  94. // Bind event handlers so they are only bound once per instance.
  95. this._onClick = this._onClick.bind(this);
  96. this._onHardwareBackPress = this._onHardwareBackPress.bind(this);
  97. }
  98. /**
  99. * Implements {@link Component#componentDidMount()}. Invoked immediately
  100. * after this component is mounted.
  101. *
  102. * @inheritdoc
  103. * @returns {void}
  104. */
  105. componentDidMount() {
  106. // Set handling any hardware button presses for back navigation up.
  107. const backHandler = BackHandler || BackAndroid;
  108. if (backHandler) {
  109. this._backHandler = backHandler;
  110. backHandler.addEventListener(
  111. 'hardwareBackPress',
  112. this._onHardwareBackPress);
  113. }
  114. this._setToolboxTimeout(this.props._toolboxVisible);
  115. }
  116. /**
  117. * Implements {@link Component#componentWillMount()}. Invoked immediately
  118. * before mounting occurs. Connects the conference described by the redux
  119. * store/state.
  120. *
  121. * @inheritdoc
  122. * @returns {void}
  123. */
  124. componentWillMount() {
  125. this.props._onConnect();
  126. }
  127. /**
  128. * Implements {@link Component#componentWillUnmount()}. Invoked immediately
  129. * before this component is unmounted and destroyed. Disconnects the
  130. * conference described by the redux store/state.
  131. *
  132. * @inheritdoc
  133. * @returns {void}
  134. */
  135. componentWillUnmount() {
  136. // Tear handling any hardware button presses for back navigation down.
  137. const backHandler = this._backHandler;
  138. if (backHandler) {
  139. this._backHandler = undefined;
  140. backHandler.removeEventListener(
  141. 'hardwareBackPress',
  142. this._onHardwareBackPress);
  143. }
  144. this._clearToolboxTimeout();
  145. this.props._onDisconnect();
  146. }
  147. /**
  148. * Implements React's {@link Component#render()}.
  149. *
  150. * @inheritdoc
  151. * @returns {ReactElement}
  152. */
  153. render() {
  154. return (
  155. <Container
  156. onClick = { this._onClick }
  157. style = { styles.conference }
  158. touchFeedback = { false }>
  159. {/*
  160. * The LargeVideo is the lowermost stacking layer.
  161. */}
  162. <LargeVideo />
  163. {/*
  164. * The overlays need to be bellow the Toolbox so that the user
  165. * may tap the ToolbarButtons.
  166. */}
  167. <OverlayContainer />
  168. {/*
  169. * The activity/loading indicator goes above everything, except
  170. * the toolbox/toolbars and the dialogs.
  171. */
  172. this.props._connecting
  173. && <View style = { styles.connectingIndicator }>
  174. <LoadingIndicator />
  175. </View>
  176. }
  177. <View style = { styles.toolboxAndFilmstripContainer } >
  178. {/*
  179. * The Toolbox is in a stacking layer bellow the Filmstrip.
  180. */}
  181. <Toolbox />
  182. {/*
  183. * The Filmstrip is in a stacking layer above the
  184. * LargeVideo. The LargeVideo and the Filmstrip form what
  185. * the Web/React app calls "videospace". Presumably, the
  186. * name and grouping stem from the fact that these two
  187. * React Components depict the videos of the conference's
  188. * participants.
  189. */}
  190. <Filmstrip />
  191. </View>
  192. {/*
  193. * The dialogs are in the topmost stacking layers.
  194. */}
  195. <DialogContainer />
  196. </Container>
  197. );
  198. }
  199. /**
  200. * Clears {@link #_toolboxTimeout} if any.
  201. *
  202. * @private
  203. * @returns {void}
  204. */
  205. _clearToolboxTimeout() {
  206. if (this._toolboxTimeout) {
  207. clearTimeout(this._toolboxTimeout);
  208. this._toolboxTimeout = undefined;
  209. }
  210. }
  211. /**
  212. * Changes the value of the toolboxVisible state, thus allowing us to switch
  213. * between Toolbox and Filmstrip and change their visibility.
  214. *
  215. * @private
  216. * @returns {void}
  217. */
  218. _onClick() {
  219. const toolboxVisible = !this.props._toolboxVisible;
  220. this.props._setToolboxVisible(toolboxVisible);
  221. this._setToolboxTimeout(toolboxVisible);
  222. }
  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: boolean) {
  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. export default reactReduxConnect(_mapStateToProps, _mapDispatchToProps)(
  352. Conference);