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

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