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

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