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 8.4KB

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