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.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  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 { t } = this.props;
  79. return (
  80. <LocalVideoTrackUnderlay style = { styles.welcomePage }>
  81. <View style = { pageStyle }>
  82. <Header style = { styles.header }>
  83. <TouchableOpacity onPress = { this._onShowSideBar } >
  84. <Icon
  85. name = 'menu'
  86. style = { buttonStyle } />
  87. </TouchableOpacity>
  88. <VideoSwitch />
  89. </Header>
  90. <SafeAreaView style = { styles.roomContainer } >
  91. <View style = { styles.joinControls } >
  92. <TextInput
  93. accessibilityLabel = { 'Input room name.' }
  94. autoCapitalize = 'none'
  95. autoComplete = { false }
  96. autoCorrect = { false }
  97. autoFocus = { false }
  98. onBlur = { this._onFieldBlur }
  99. onChangeText = { this._onRoomChange }
  100. onFocus = { this._onFieldFocus }
  101. onSubmitEditing = { this._onJoin }
  102. placeholder = { t('welcomepage.roomname') }
  103. placeholderTextColor = {
  104. PLACEHOLDER_TEXT_COLOR
  105. }
  106. returnKeyType = { 'go' }
  107. style = { styles.textInput }
  108. underlineColorAndroid = 'transparent'
  109. value = { this.state.room } />
  110. {
  111. this._renderHintBox()
  112. }
  113. </View>
  114. </SafeAreaView>
  115. <WelcomePageLists disabled = { this.state._fieldFocused } />
  116. <SettingsView />
  117. </View>
  118. <WelcomePageSideBar />
  119. </LocalVideoTrackUnderlay>
  120. );
  121. }
  122. /**
  123. * Constructs a style array to handle the hint box animation.
  124. *
  125. * @private
  126. * @returns {Array<Object>}
  127. */
  128. _getHintBoxStyle() {
  129. return [
  130. styles.hintContainer,
  131. {
  132. opacity: this.state.hintBoxAnimation
  133. }
  134. ];
  135. }
  136. /**
  137. * Callback for when the room field's focus changes so the hint box
  138. * must be rendered or removed.
  139. *
  140. * @private
  141. * @param {boolean} focused - The focused state of the field.
  142. * @returns {void}
  143. */
  144. _onFieldFocusChange(focused) {
  145. focused
  146. && this.setState({
  147. _fieldFocused: true
  148. });
  149. Animated.timing(
  150. this.state.hintBoxAnimation,
  151. {
  152. duration: 300,
  153. toValue: focused ? 1 : 0
  154. })
  155. .start(animationState =>
  156. animationState.finished
  157. && !focused
  158. && this.setState({
  159. _fieldFocused: false
  160. }));
  161. }
  162. /**
  163. * Toggles the side bar.
  164. *
  165. * @private
  166. * @returns {void}
  167. */
  168. _onShowSideBar() {
  169. Keyboard.dismiss();
  170. this.props.dispatch(setSideBarVisible(true));
  171. }
  172. /**
  173. * Renders the hint box if necessary.
  174. *
  175. * @private
  176. * @returns {React$Node}
  177. */
  178. _renderHintBox() {
  179. if (this.state._fieldFocused) {
  180. const { t } = this.props;
  181. return (
  182. <Animated.View style = { this._getHintBoxStyle() }>
  183. <View style = { styles.hintTextContainer } >
  184. <Text style = { styles.hintText }>
  185. { t('welcomepage.roomnameHint') }
  186. </Text>
  187. </View>
  188. <View style = { styles.hintButtonContainer } >
  189. { this._renderJoinButton() }
  190. </View>
  191. </Animated.View>
  192. );
  193. }
  194. return null;
  195. }
  196. /**
  197. * Renders the join button.
  198. *
  199. * @private
  200. * @returns {ReactElement}
  201. */
  202. _renderJoinButton() {
  203. let children;
  204. /* eslint-disable no-extra-parens */
  205. if (this.state.joining) {
  206. // TouchableHighlight is picky about what its children can be, so
  207. // wrap it in a native component, i.e. View to avoid having to
  208. // modify non-native children.
  209. children = (
  210. <View>
  211. <LoadingIndicator
  212. color = { styles.buttonText.color }
  213. size = 'small' />
  214. </View>
  215. );
  216. } else {
  217. children = (
  218. <Text style = { styles.buttonText }>
  219. { this.props.t('welcomepage.join') }
  220. </Text>
  221. );
  222. }
  223. /* eslint-enable no-extra-parens */
  224. const buttonDisabled = this._isJoinDisabled();
  225. return (
  226. <TouchableHighlight
  227. accessibilityLabel = { 'Tap to Join.' }
  228. disabled = { buttonDisabled }
  229. onPress = { this._onJoin }
  230. style = { [
  231. styles.button,
  232. buttonDisabled ? styles.buttonDisabled : null
  233. ] }
  234. underlayColor = { ColorPalette.white }>
  235. { children }
  236. </TouchableHighlight>
  237. );
  238. }
  239. }
  240. export default translate(connect(_mapStateToProps)(WelcomePage));