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

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