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.

WelcomePage.native.js 9.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. import React from 'react';
  2. import {
  3. Animated,
  4. Keyboard,
  5. SafeAreaView,
  6. TextInput,
  7. TouchableHighlight,
  8. TouchableOpacity,
  9. View
  10. } from 'react-native';
  11. import { ColorSchemeRegistry } from '../../base/color-scheme';
  12. import { translate } from '../../base/i18n';
  13. import { Icon } from '../../base/font-icons';
  14. import { MEDIA_TYPE } from '../../base/media';
  15. import { Header, LoadingIndicator, Text } from '../../base/react';
  16. import { connect } from '../../base/redux';
  17. import { ColorPalette } from '../../base/styles';
  18. import {
  19. createDesiredLocalTracks,
  20. destroyLocalTracks
  21. } from '../../base/tracks';
  22. import { DialInSummary } from '../../invite';
  23. import { SettingsView } from '../../settings';
  24. import {
  25. AbstractWelcomePage,
  26. _mapStateToProps as _abstractMapStateToProps
  27. } from './AbstractWelcomePage';
  28. import { setSideBarVisible } from '../actions';
  29. import LocalVideoTrackUnderlay from './LocalVideoTrackUnderlay';
  30. import styles, { PLACEHOLDER_TEXT_COLOR } from './styles';
  31. import VideoSwitch from './VideoSwitch';
  32. import WelcomePageLists from './WelcomePageLists';
  33. import WelcomePageSideBar from './WelcomePageSideBar';
  34. /**
  35. * The native container rendering the welcome page.
  36. *
  37. * @extends AbstractWelcomePage
  38. */
  39. class WelcomePage extends AbstractWelcomePage {
  40. /**
  41. * Constructor of the Component.
  42. *
  43. * @inheritdoc
  44. */
  45. constructor(props) {
  46. super(props);
  47. this.state._fieldFocused = false;
  48. this.state.hintBoxAnimation = new Animated.Value(0);
  49. // Bind event handlers so they are only bound once per instance.
  50. this._onFieldFocusChange = this._onFieldFocusChange.bind(this);
  51. this._onShowSideBar = this._onShowSideBar.bind(this);
  52. this._renderHintBox = this._renderHintBox.bind(this);
  53. // Specially bind functions to avoid function definition on render.
  54. this._onFieldBlur = this._onFieldFocusChange.bind(this, false);
  55. this._onFieldFocus = this._onFieldFocusChange.bind(this, true);
  56. }
  57. /**
  58. * Implements React's {@link Component#componentDidMount()}. Invoked
  59. * immediately after mounting occurs. Creates a local video track if none
  60. * is available and the camera permission was already granted.
  61. *
  62. * @inheritdoc
  63. * @returns {void}
  64. */
  65. componentDidMount() {
  66. super.componentDidMount();
  67. const { dispatch } = this.props;
  68. if (this.props._settings.startAudioOnly) {
  69. dispatch(destroyLocalTracks());
  70. } else {
  71. // Make sure we don't request the permission for the camera from
  72. // the start. We will, however, create a video track iff the user
  73. // already granted the permission.
  74. navigator.permissions.query({ name: 'camera' }).then(response => {
  75. response === 'granted'
  76. && dispatch(createDesiredLocalTracks(MEDIA_TYPE.VIDEO));
  77. });
  78. }
  79. }
  80. /**
  81. * Implements React's {@link Component#render()}. Renders a prompt for
  82. * entering a room name.
  83. *
  84. * @inheritdoc
  85. * @returns {ReactElement}
  86. */
  87. render() {
  88. const roomnameAccLabel = 'welcomepage.accessibilityLabel.roomname';
  89. const { _headerStyles, t } = this.props;
  90. return (
  91. <LocalVideoTrackUnderlay style = { styles.welcomePage }>
  92. <View style = { _headerStyles.page }>
  93. <Header style = { styles.header }>
  94. <TouchableOpacity onPress = { this._onShowSideBar } >
  95. <Icon
  96. name = 'menu'
  97. style = { _headerStyles.headerButtonIcon } />
  98. </TouchableOpacity>
  99. <VideoSwitch />
  100. </Header>
  101. <SafeAreaView style = { styles.roomContainer } >
  102. <View style = { styles.joinControls } >
  103. <TextInput
  104. accessibilityLabel = { t(roomnameAccLabel) }
  105. autoCapitalize = 'none'
  106. autoComplete = 'off'
  107. autoCorrect = { false }
  108. autoFocus = { false }
  109. onBlur = { this._onFieldBlur }
  110. onChangeText = { this._onRoomChange }
  111. onFocus = { this._onFieldFocus }
  112. onSubmitEditing = { this._onJoin }
  113. placeholder = { t('welcomepage.roomname') }
  114. placeholderTextColor = {
  115. PLACEHOLDER_TEXT_COLOR
  116. }
  117. returnKeyType = { 'go' }
  118. style = { styles.textInput }
  119. underlineColorAndroid = 'transparent'
  120. value = { this.state.room } />
  121. {
  122. this._renderHintBox()
  123. }
  124. </View>
  125. </SafeAreaView>
  126. <WelcomePageLists disabled = { this.state._fieldFocused } />
  127. <SettingsView />
  128. <DialInSummary />
  129. </View>
  130. <WelcomePageSideBar />
  131. </LocalVideoTrackUnderlay>
  132. );
  133. }
  134. /**
  135. * Constructs a style array to handle the hint box animation.
  136. *
  137. * @private
  138. * @returns {Array<Object>}
  139. */
  140. _getHintBoxStyle() {
  141. return [
  142. styles.hintContainer,
  143. {
  144. opacity: this.state.hintBoxAnimation
  145. }
  146. ];
  147. }
  148. /**
  149. * Callback for when the room field's focus changes so the hint box
  150. * must be rendered or removed.
  151. *
  152. * @private
  153. * @param {boolean} focused - The focused state of the field.
  154. * @returns {void}
  155. */
  156. _onFieldFocusChange(focused) {
  157. focused
  158. && this.setState({
  159. _fieldFocused: true
  160. });
  161. Animated.timing(
  162. this.state.hintBoxAnimation,
  163. {
  164. duration: 300,
  165. toValue: focused ? 1 : 0
  166. })
  167. .start(animationState =>
  168. animationState.finished
  169. && !focused
  170. && this.setState({
  171. _fieldFocused: false
  172. }));
  173. }
  174. /**
  175. * Toggles the side bar.
  176. *
  177. * @private
  178. * @returns {void}
  179. */
  180. _onShowSideBar() {
  181. Keyboard.dismiss();
  182. this.props.dispatch(setSideBarVisible(true));
  183. }
  184. /**
  185. * Renders the hint box if necessary.
  186. *
  187. * @private
  188. * @returns {React$Node}
  189. */
  190. _renderHintBox() {
  191. if (this.state._fieldFocused) {
  192. const { t } = this.props;
  193. return (
  194. <Animated.View style = { this._getHintBoxStyle() }>
  195. <View style = { styles.hintTextContainer } >
  196. <Text style = { styles.hintText }>
  197. { t('welcomepage.roomnameHint') }
  198. </Text>
  199. </View>
  200. <View style = { styles.hintButtonContainer } >
  201. { this._renderJoinButton() }
  202. </View>
  203. </Animated.View>
  204. );
  205. }
  206. return null;
  207. }
  208. /**
  209. * Renders the join button.
  210. *
  211. * @private
  212. * @returns {ReactElement}
  213. */
  214. _renderJoinButton() {
  215. const { t } = this.props;
  216. let children;
  217. if (this.state.joining) {
  218. // TouchableHighlight is picky about what its children can be, so
  219. // wrap it in a native component, i.e. View to avoid having to
  220. // modify non-native children.
  221. children = (
  222. <View>
  223. <LoadingIndicator
  224. color = { styles.buttonText.color }
  225. size = 'small' />
  226. </View>
  227. );
  228. } else {
  229. children = (
  230. <Text style = { styles.buttonText }>
  231. { this.props.t('welcomepage.join') }
  232. </Text>
  233. );
  234. }
  235. const buttonDisabled = this._isJoinDisabled();
  236. return (
  237. <TouchableHighlight
  238. accessibilityLabel =
  239. { t('welcomepage.accessibilityLabel.join') }
  240. disabled = { buttonDisabled }
  241. onPress = { this._onJoin }
  242. style = { [
  243. styles.button,
  244. buttonDisabled ? styles.buttonDisabled : null
  245. ] }
  246. underlayColor = { ColorPalette.white }>
  247. { children }
  248. </TouchableHighlight>
  249. );
  250. }
  251. }
  252. /**
  253. * Maps part of the Redux state to the props of this component.
  254. *
  255. * @param {Object} state - The Redux state.
  256. * @returns {{
  257. * _headerStyles: Object
  258. * }}
  259. */
  260. function _mapStateToProps(state) {
  261. return {
  262. ..._abstractMapStateToProps(state),
  263. _headerStyles: ColorSchemeRegistry.get(state, 'Header')
  264. };
  265. }
  266. export default translate(connect(_mapStateToProps)(WelcomePage));