Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

WelcomePage.native.js 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418
  1. import React from 'react';
  2. import { Animated, SafeAreaView, TouchableHighlight, View } from 'react-native';
  3. import { getName } from '../../app/functions';
  4. import { translate } from '../../base/i18n';
  5. import { Icon, IconWarning } from '../../base/icons';
  6. import { LoadingIndicator, Text } from '../../base/react';
  7. import { connect } from '../../base/redux';
  8. import BaseTheme from '../../base/ui/components/BaseTheme.native';
  9. import Button from '../../base/ui/components/native/Button';
  10. import Input from '../../base/ui/components/native/Input';
  11. import { BUTTON_TYPES } from '../../base/ui/constants.native';
  12. import WelcomePageTabs
  13. from '../../mobile/navigation/components/welcome/components/WelcomePageTabs';
  14. import {
  15. type Props as AbstractProps,
  16. AbstractWelcomePage,
  17. _mapStateToProps as _abstractMapStateToProps
  18. } from './AbstractWelcomePage';
  19. import styles from './styles';
  20. type Props = AbstractProps & {
  21. /**
  22. * Default prop for navigating between screen components(React Navigation).
  23. */
  24. navigation: Object,
  25. /**
  26. * The translate function.
  27. */
  28. t: Function
  29. };
  30. /**
  31. * The native container rendering the welcome page.
  32. *
  33. * @augments AbstractWelcomePage
  34. */
  35. class WelcomePage extends AbstractWelcomePage<*> {
  36. /**
  37. * Constructor of the Component.
  38. *
  39. * @inheritdoc
  40. */
  41. constructor(props: Props) {
  42. super(props);
  43. this.state._fieldFocused = false;
  44. this.state.isSettingsScreenFocused = false;
  45. this.state.roomNameInputAnimation = new Animated.Value(1);
  46. this.state.hintBoxAnimation = new Animated.Value(0);
  47. // Bind event handlers so they are only bound once per instance.
  48. this._onFieldFocusChange = this._onFieldFocusChange.bind(this);
  49. this._renderHintBox = this._renderHintBox.bind(this);
  50. // Specially bind functions to avoid function definition on render.
  51. this._onFieldBlur = this._onFieldFocusChange.bind(this, false);
  52. this._onFieldFocus = this._onFieldFocusChange.bind(this, true);
  53. this._onSettingsScreenFocused = this._onSettingsScreenFocused.bind(this);
  54. }
  55. _onFieldBlur: () => void;
  56. _onFieldFocus: () => void;
  57. _onJoin: () => void;
  58. _onRoomChange: (string) => void;
  59. _updateRoomname: () => void;
  60. /**
  61. * Implements React's {@link Component#componentDidMount()}. Invoked
  62. * immediately after mounting occurs. Creates a local video track if none
  63. * is available and the camera permission was already granted.
  64. *
  65. * @inheritdoc
  66. * @returns {void}
  67. */
  68. componentDidMount() {
  69. super.componentDidMount();
  70. const {
  71. navigation,
  72. t
  73. } = this.props;
  74. navigation.setOptions({
  75. headerTitle: t('welcomepage.headerTitle')
  76. });
  77. navigation.addListener('focus', () => {
  78. this._updateRoomname();
  79. });
  80. navigation.addListener('blur', () => {
  81. this._clearTimeouts();
  82. this.setState({
  83. generatedRoomname: '',
  84. insecureRoomName: false,
  85. room: ''
  86. });
  87. });
  88. }
  89. /**
  90. * Implements React's {@link Component#render()}. Renders a prompt for
  91. * entering a room name.
  92. *
  93. * @inheritdoc
  94. * @returns {ReactElement}
  95. */
  96. render() {
  97. // We want to have the welcome page support the reduced UI layout,
  98. // but we ran into serious issues enabling it so we disable it
  99. // until we have a proper fix in place. We leave the code here though, because
  100. // this part should be fine when the bug is fixed.
  101. //
  102. // NOTE: when re-enabling, don't forget to uncomment the respective _mapStateToProps line too
  103. /*
  104. const { _reducedUI } = this.props;
  105. if (_reducedUI) {
  106. return this._renderReducedUI();
  107. }
  108. */
  109. return this._renderFullUI();
  110. }
  111. /**
  112. * Renders the insecure room name warning.
  113. *
  114. * @inheritdoc
  115. */
  116. _doRenderInsecureRoomNameWarning() {
  117. return (
  118. <View
  119. style = { [
  120. styles.messageContainer,
  121. styles.insecureRoomNameWarningContainer
  122. ] }>
  123. <Icon
  124. src = { IconWarning }
  125. style = { styles.insecureRoomNameWarningIcon } />
  126. <Text style = { styles.insecureRoomNameWarningText }>
  127. { this.props.t('security.insecureRoomNameWarning') }
  128. </Text>
  129. </View>
  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.messageContainer,
  141. styles.hintContainer,
  142. {
  143. opacity: this.state.hintBoxAnimation
  144. }
  145. ];
  146. }
  147. _onFieldFocusChange: (boolean) => void;
  148. /**
  149. * Callback for when the room field's focus changes so the hint box
  150. * must be rendered or removed.
  151. *
  152. * @private
  153. * @param {boolean} focused - The focused state of the field.
  154. * @returns {void}
  155. */
  156. _onFieldFocusChange(focused) {
  157. if (focused) {
  158. // Stop placeholder animation.
  159. this._clearTimeouts();
  160. this.setState({
  161. _fieldFocused: true,
  162. roomPlaceholder: ''
  163. });
  164. } else {
  165. // Restart room placeholder animation.
  166. this._updateRoomname();
  167. }
  168. Animated.timing(
  169. this.state.hintBoxAnimation,
  170. {
  171. duration: 300,
  172. toValue: focused ? 1 : 0,
  173. useNativeDriver: true
  174. })
  175. .start(animationState =>
  176. animationState.finished
  177. && !focused
  178. && this.setState({
  179. _fieldFocused: false
  180. }));
  181. }
  182. _onSettingsScreenFocused: boolean => void;
  183. /**
  184. * Callback for when the settings screen is focused.
  185. *
  186. * @private
  187. * @param {boolean} focused - The focused state of the screen.
  188. * @returns {void}
  189. */
  190. _onSettingsScreenFocused(focused) {
  191. this.setState({
  192. isSettingsScreenFocused: focused
  193. });
  194. this.props.navigation.setOptions({
  195. headerShown: !focused
  196. });
  197. Animated.timing(
  198. this.state.roomNameInputAnimation,
  199. {
  200. toValue: focused ? 0 : 1,
  201. duration: 500,
  202. useNativeDriver: true
  203. })
  204. .start();
  205. }
  206. _renderHintBox: () => React.ReactElement;
  207. /**
  208. * Renders the hint box if necessary.
  209. *
  210. * @private
  211. * @returns {React$Node}
  212. */
  213. _renderHintBox() {
  214. const { t } = this.props;
  215. if (this.state._fieldFocused) {
  216. return (
  217. <Animated.View style = { this._getHintBoxStyle() }>
  218. <View style = { styles.hintTextContainer } >
  219. <Text style = { styles.hintText }>
  220. { t('welcomepage.roomnameHint') }
  221. </Text>
  222. </View>
  223. <View style = { styles.hintButtonContainer } >
  224. { this._renderJoinButton() }
  225. </View>
  226. </Animated.View>
  227. );
  228. }
  229. return null;
  230. }
  231. /**
  232. * Renders the join button.
  233. *
  234. * @private
  235. * @returns {ReactElement}
  236. */
  237. _renderJoinButton() {
  238. const { t } = this.props;
  239. let joinButton;
  240. if (this.state.joining) {
  241. // TouchableHighlight is picky about what its children can be, so
  242. // wrap it in a native component, i.e. View to avoid having to
  243. // modify non-native children.
  244. joinButton = (
  245. <TouchableHighlight
  246. accessibilityLabel =
  247. { t('welcomepage.accessibilityLabel.join') }
  248. onPress = { this._onJoin }
  249. style = { styles.button }>
  250. <View>
  251. <LoadingIndicator
  252. color = { BaseTheme.palette.icon01 }
  253. size = 'small' />
  254. </View>
  255. </TouchableHighlight>
  256. );
  257. } else {
  258. joinButton = (
  259. <Button
  260. accessibilityLabel = { 'welcomepage.accessibilityLabel.join' }
  261. labelKey = { 'welcomepage.join' }
  262. labelStyle = { styles.joinButtonLabel }
  263. onClick = { this._onJoin }
  264. style = { styles.buttonText }
  265. type = { BUTTON_TYPES.PRIMARY } />
  266. );
  267. }
  268. return joinButton;
  269. }
  270. /**
  271. * Renders the room name input.
  272. *
  273. * @private
  274. * @returns {ReactElement}
  275. */
  276. _renderRoomNameInput() {
  277. const roomnameAccLabel = 'welcomepage.accessibilityLabel.roomname';
  278. const { t } = this.props;
  279. const { isSettingsScreenFocused } = this.state;
  280. return (
  281. <Animated.View
  282. style = { [
  283. isSettingsScreenFocused && styles.roomNameInputContainer,
  284. { opacity: this.state.roomNameInputAnimation }
  285. ] }>
  286. <SafeAreaView style = { styles.roomContainer }>
  287. <View style = { styles.joinControls } >
  288. <Text style = { styles.enterRoomText }>
  289. { t('welcomepage.roomname') }
  290. </Text>
  291. <Input
  292. accessibilityLabel = { t(roomnameAccLabel) }
  293. autoCapitalize = { 'none' }
  294. autoComplete = { 'off' }
  295. autoCorrect = { false }
  296. autoFocus = { false }
  297. customStyles = {{ input: styles.customInput }}
  298. onBlur = { this._onFieldBlur }
  299. onChange = { this._onRoomChange }
  300. onFocus = { this._onFieldFocus }
  301. onSubmitEditing = { this._onJoin }
  302. placeholder = { this.state.roomPlaceholder }
  303. returnKeyType = { 'go' }
  304. value = { this.state.room } />
  305. {
  306. this._renderInsecureRoomNameWarning()
  307. }
  308. {
  309. this._renderHintBox()
  310. }
  311. </View>
  312. </SafeAreaView>
  313. </Animated.View>
  314. );
  315. }
  316. /**
  317. * Renders the full welcome page.
  318. *
  319. * @returns {ReactElement}
  320. */
  321. _renderFullUI() {
  322. return (
  323. <>
  324. { this._renderRoomNameInput() }
  325. <View style = { styles.welcomePage }>
  326. <WelcomePageTabs
  327. disabled = { this.state._fieldFocused }
  328. onListContainerPress = { this._onFieldBlur }
  329. onSettingsScreenFocused = { this._onSettingsScreenFocused } />
  330. </View>
  331. </>
  332. );
  333. }
  334. /**
  335. * Renders a "reduced" version of the welcome page.
  336. *
  337. * @returns {ReactElement}
  338. */
  339. _renderReducedUI() {
  340. const { t } = this.props;
  341. return (
  342. <View style = { styles.reducedUIContainer }>
  343. <Text style = { styles.reducedUIText }>
  344. { t('welcomepage.reducedUIText', { app: getName() }) }
  345. </Text>
  346. </View>
  347. );
  348. }
  349. }
  350. /**
  351. * Maps part of the Redux state to the props of this component.
  352. *
  353. * @param {Object} state - The Redux state.
  354. * @returns {Object}
  355. */
  356. function _mapStateToProps(state) {
  357. return {
  358. ..._abstractMapStateToProps(state)
  359. // _reducedUI: state['features/base/responsive-ui'].reducedUI
  360. };
  361. }
  362. export default translate(connect(_mapStateToProps)(WelcomePage));