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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  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 { LoadingIndicator, Header, 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, {
  26. PLACEHOLDER_TEXT_COLOR
  27. } from './styles';
  28. import VideoSwitch from './VideoSwitch';
  29. import WelcomePageLists from './WelcomePageLists';
  30. import WelcomePageSideBar from './WelcomePageSideBar';
  31. /**
  32. * The native container rendering the welcome page.
  33. *
  34. * @extends AbstractWelcomePage
  35. */
  36. class WelcomePage extends AbstractWelcomePage {
  37. /**
  38. * Constructor of the Component.
  39. *
  40. * @inheritdoc
  41. */
  42. constructor(props) {
  43. super(props);
  44. this.state._fieldFocused = false;
  45. this.state.hintBoxAnimation = new Animated.Value(0);
  46. // Bind event handlers so they are only bound once per instance.
  47. this._getHintBoxStyle = this._getHintBoxStyle.bind(this);
  48. this._onFieldFocusChange = this._onFieldFocusChange.bind(this);
  49. this._onShowSideBar = this._onShowSideBar.bind(this);
  50. this._renderHintBox = this._renderHintBox.bind(this);
  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._profile.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._onFieldFocusChange(false) }
  99. onChangeText = { this._onRoomChange }
  100. onFocus = { this._onFieldFocusChange(true) }
  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 {Function}
  143. */
  144. _onFieldFocusChange(focused) {
  145. return () => {
  146. if (focused) {
  147. this.setState({
  148. _fieldFocused: true
  149. });
  150. }
  151. Animated.timing(this.state.hintBoxAnimation, {
  152. duration: 300,
  153. toValue: focused ? 1 : 0
  154. }).start(animationState => {
  155. if (animationState.finished && !focused) {
  156. this.setState({
  157. _fieldFocused: false
  158. });
  159. }
  160. });
  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. let children;
  205. /* eslint-disable no-extra-parens */
  206. if (this.state.joining) {
  207. // TouchableHighlight is picky about what its children can be, so
  208. // wrap it in a native component, i.e. View to avoid having to
  209. // modify non-native children.
  210. children = (
  211. <View>
  212. <LoadingIndicator
  213. color = { styles.buttonText.color }
  214. size = 'small' />
  215. </View>
  216. );
  217. } else {
  218. children = (
  219. <Text style = { styles.buttonText }>
  220. { this.props.t('welcomepage.join') }
  221. </Text>
  222. );
  223. }
  224. /* eslint-enable no-extra-parens */
  225. const buttonDisabled = this._isJoinDisabled();
  226. return (
  227. <TouchableHighlight
  228. accessibilityLabel = { 'Tap to Join.' }
  229. disabled = { buttonDisabled }
  230. onPress = { this._onJoin }
  231. style = { [
  232. styles.button,
  233. buttonDisabled ? styles.buttonDisabled : null
  234. ] }
  235. underlayColor = { ColorPalette.white }>
  236. {
  237. children
  238. }
  239. </TouchableHighlight>
  240. );
  241. }
  242. }
  243. export default translate(connect(_mapStateToProps)(WelcomePage));