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

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