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

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