您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

SettingsView.js 14KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492
  1. // @flow
  2. import React from 'react';
  3. import { Alert, NativeModules, SafeAreaView, ScrollView, Switch, Text, TextInput, View } from 'react-native';
  4. import { ColorSchemeRegistry } from '../../../base/color-scheme';
  5. import { translate } from '../../../base/i18n';
  6. import { HeaderWithNavigation, Modal } from '../../../base/react';
  7. import { connect } from '../../../base/redux';
  8. import {
  9. AbstractSettingsView,
  10. _mapStateToProps as _abstractMapStateToProps,
  11. type Props as AbstractProps
  12. } from '../AbstractSettingsView';
  13. import { setSettingsViewVisible } from '../../actions';
  14. import FormRow from './FormRow';
  15. import FormSectionHeader from './FormSectionHeader';
  16. import { normalizeUserInputURL } from '../../functions';
  17. import styles from './styles';
  18. /**
  19. * Application information module.
  20. */
  21. const { AppInfo } = NativeModules;
  22. type Props = AbstractProps & {
  23. /**
  24. * Color schemed style of the header component.
  25. */
  26. _headerStyles: Object
  27. }
  28. type State = {
  29. /**
  30. * State variable for the disable call integration switch.
  31. */
  32. disableCallIntegration: boolean,
  33. /**
  34. * State variable for the disable p2p switch.
  35. */
  36. disableP2P: boolean,
  37. /**
  38. * State variable for the display name field.
  39. */
  40. displayName: string,
  41. /**
  42. * State variable for the email field.
  43. */
  44. email: string,
  45. /**
  46. * State variable for the server URL field.
  47. */
  48. serverURL: string,
  49. /**
  50. * Whether to show advanced settings or not.
  51. */
  52. showAdvanced: boolean,
  53. /**
  54. * State variable for the start with audio muted switch.
  55. */
  56. startWithAudioMuted: boolean,
  57. /**
  58. * State variable for the start with video muted switch.
  59. */
  60. startWithVideoMuted: boolean,
  61. }
  62. /**
  63. * The native container rendering the app settings page.
  64. *
  65. * @extends AbstractSettingsView
  66. */
  67. class SettingsView extends AbstractSettingsView<Props, State> {
  68. _urlField: Object;
  69. /**
  70. * Initializes a new {@code SettingsView} instance.
  71. *
  72. * @inheritdoc
  73. */
  74. constructor(props) {
  75. super(props);
  76. const {
  77. disableCallIntegration,
  78. disableP2P,
  79. displayName,
  80. email,
  81. serverURL,
  82. startWithAudioMuted,
  83. startWithVideoMuted
  84. } = props._settings || {};
  85. this.state = {
  86. disableCallIntegration,
  87. disableP2P,
  88. displayName,
  89. email,
  90. serverURL,
  91. showAdvanced: false,
  92. startWithAudioMuted,
  93. startWithVideoMuted
  94. };
  95. // Bind event handlers so they are only bound once per instance.
  96. this._onBlurServerURL = this._onBlurServerURL.bind(this);
  97. this._onDisableCallIntegration = this._onDisableCallIntegration.bind(this);
  98. this._onDisableP2P = this._onDisableP2P.bind(this);
  99. this._onRequestClose = this._onRequestClose.bind(this);
  100. this._onShowAdvanced = this._onShowAdvanced.bind(this);
  101. this._setURLFieldReference = this._setURLFieldReference.bind(this);
  102. this._showURLAlert = this._showURLAlert.bind(this);
  103. }
  104. /**
  105. * Implements React's {@link Component#render()}, renders the settings page.
  106. *
  107. * @inheritdoc
  108. * @returns {ReactElement}
  109. */
  110. render() {
  111. return (
  112. <Modal
  113. onRequestClose = { this._onRequestClose }
  114. presentationStyle = 'overFullScreen'
  115. visible = { this.props._visible }>
  116. <View style = { this.props._headerStyles.page }>
  117. { this._renderHeader() }
  118. { this._renderBody() }
  119. </View>
  120. </Modal>
  121. );
  122. }
  123. _onBlurServerURL: () => void;
  124. /**
  125. * Handler the server URL lose focus event. Here we validate the server URL
  126. * and update it to the normalized version, or show an error if incorrect.
  127. *
  128. * @private
  129. * @returns {void}
  130. */
  131. _onBlurServerURL() {
  132. this._processServerURL(false /* hideOnSuccess */);
  133. }
  134. /**
  135. * Callback to update the display name.
  136. *
  137. * @param {string} displayName - The new value to set.
  138. * @returns {void}
  139. */
  140. _onChangeDisplayName(displayName) {
  141. super._onChangeDisplayName(displayName);
  142. this.setState({
  143. displayName
  144. });
  145. }
  146. /**
  147. * Callback to update the email.
  148. *
  149. * @param {string} email - The new value to set.
  150. * @returns {void}
  151. */
  152. _onChangeEmail(email) {
  153. super._onChangeEmail(email);
  154. this.setState({
  155. email
  156. });
  157. }
  158. /**
  159. * Callback to update the server URL.
  160. *
  161. * @param {string} serverURL - The new value to set.
  162. * @returns {void}
  163. */
  164. _onChangeServerURL(serverURL) {
  165. super._onChangeServerURL(serverURL);
  166. this.setState({
  167. serverURL
  168. });
  169. }
  170. _onDisableCallIntegration: (boolean) => void;
  171. /**
  172. * Handles the disable call integration change event.
  173. *
  174. * @param {boolean} disableCallIntegration - The new value
  175. * option.
  176. * @private
  177. * @returns {void}
  178. */
  179. _onDisableCallIntegration(disableCallIntegration) {
  180. this._updateSettings({
  181. disableCallIntegration
  182. });
  183. this.setState({
  184. disableCallIntegration
  185. });
  186. }
  187. _onDisableP2P: (boolean) => void;
  188. /**
  189. * Handles the disable P2P change event.
  190. *
  191. * @param {boolean} disableP2P - The new value
  192. * option.
  193. * @private
  194. * @returns {void}
  195. */
  196. _onDisableP2P(disableP2P) {
  197. this._updateSettings({
  198. disableP2P
  199. });
  200. this.setState({
  201. disableP2P
  202. });
  203. }
  204. _onRequestClose: () => void;
  205. /**
  206. * Handles the back button. Also invokes normalizeUserInputURL to validate
  207. * the URL entered by the user.
  208. *
  209. * @returns {void}
  210. */
  211. _onRequestClose() {
  212. this.setState({ showAdvanced: false });
  213. this._processServerURL(true /* hideOnSuccess */);
  214. }
  215. _onShowAdvanced: () => void;
  216. /**
  217. * Handles the advanced settings button.
  218. *
  219. * @returns {void}
  220. */
  221. _onShowAdvanced() {
  222. this.setState({ showAdvanced: !this.state.showAdvanced });
  223. }
  224. /**
  225. * Callback to update the start with audio muted value.
  226. *
  227. * @param {boolean} startWithAudioMuted - The new value to set.
  228. * @returns {void}
  229. */
  230. _onStartAudioMutedChange(startWithAudioMuted) {
  231. super._onStartAudioMutedChange(startWithAudioMuted);
  232. this.setState({
  233. startWithAudioMuted
  234. });
  235. }
  236. /**
  237. * Callback to update the start with video muted value.
  238. *
  239. * @param {boolean} startWithVideoMuted - The new value to set.
  240. * @returns {void}
  241. */
  242. _onStartVideoMutedChange(startWithVideoMuted) {
  243. super._onStartVideoMutedChange(startWithVideoMuted);
  244. this.setState({
  245. startWithVideoMuted
  246. });
  247. }
  248. /**
  249. * Processes the server URL. It normalizes it and an error alert is
  250. * displayed in case it's incorrect.
  251. *
  252. * @param {boolean} hideOnSuccess - True if the dialog should be hidden if
  253. * normalization / validation succeeds, false otherwise.
  254. * @private
  255. * @returns {void}
  256. */
  257. _processServerURL(hideOnSuccess: boolean) {
  258. const { serverURL } = this.props._settings;
  259. const normalizedURL = normalizeUserInputURL(serverURL);
  260. if (normalizedURL === null) {
  261. this._showURLAlert();
  262. } else {
  263. this._onChangeServerURL(normalizedURL);
  264. if (hideOnSuccess) {
  265. this.props.dispatch(setSettingsViewVisible(false));
  266. }
  267. }
  268. }
  269. /**
  270. * Renders the advanced settings options.
  271. *
  272. * @private
  273. * @returns {React$Element}
  274. */
  275. _renderAdvancedSettings() {
  276. const { disableCallIntegration, disableP2P, showAdvanced } = this.state;
  277. if (!showAdvanced) {
  278. return (
  279. <FormRow
  280. fieldSeparator = { true }
  281. label = 'settingsView.showAdvanced'>
  282. <Switch
  283. onValueChange = { this._onShowAdvanced }
  284. value = { showAdvanced } />
  285. </FormRow>
  286. );
  287. }
  288. return (
  289. <>
  290. <FormRow
  291. fieldSeparator = { true }
  292. label = 'settingsView.disableCallIntegration'>
  293. <Switch
  294. onValueChange = { this._onDisableCallIntegration }
  295. value = { disableCallIntegration } />
  296. </FormRow>
  297. <FormRow
  298. fieldSeparator = { true }
  299. label = 'settingsView.disableP2P'>
  300. <Switch
  301. onValueChange = { this._onDisableP2P }
  302. value = { disableP2P } />
  303. </FormRow>
  304. </>
  305. );
  306. }
  307. /**
  308. * Renders the body (under the header) of {@code SettingsView}.
  309. *
  310. * @private
  311. * @returns {React$Element}
  312. */
  313. _renderBody() {
  314. const { displayName, email, serverURL, startWithAudioMuted, startWithVideoMuted } = this.state;
  315. return (
  316. <SafeAreaView style = { styles.settingsForm }>
  317. <ScrollView>
  318. <FormSectionHeader
  319. label = 'settingsView.profileSection' />
  320. <FormRow
  321. fieldSeparator = { true }
  322. label = 'settingsView.displayName'
  323. layout = 'column'>
  324. <TextInput
  325. autoCorrect = { false }
  326. onChangeText = { this._onChangeDisplayName }
  327. placeholder = 'John Doe'
  328. value = { displayName } />
  329. </FormRow>
  330. <FormRow
  331. label = 'settingsView.email'
  332. layout = 'column'>
  333. <TextInput
  334. autoCapitalize = 'none'
  335. autoCorrect = { false }
  336. keyboardType = { 'email-address' }
  337. onChangeText = { this._onChangeEmail }
  338. placeholder = 'email@example.com'
  339. value = { email } />
  340. </FormRow>
  341. <FormSectionHeader
  342. label = 'settingsView.conferenceSection' />
  343. <FormRow
  344. fieldSeparator = { true }
  345. label = 'settingsView.serverURL'
  346. layout = 'column'>
  347. <TextInput
  348. autoCapitalize = 'none'
  349. autoCorrect = { false }
  350. onBlur = { this._onBlurServerURL }
  351. onChangeText = { this._onChangeServerURL }
  352. placeholder = { this.props._serverURL }
  353. value = { serverURL } />
  354. </FormRow>
  355. <FormRow
  356. fieldSeparator = { true }
  357. label = 'settingsView.startWithAudioMuted'>
  358. <Switch
  359. onValueChange = { this._onStartAudioMutedChange }
  360. value = { startWithAudioMuted } />
  361. </FormRow>
  362. <FormRow label = 'settingsView.startWithVideoMuted'>
  363. <Switch
  364. onValueChange = { this._onStartVideoMutedChange }
  365. value = { startWithVideoMuted } />
  366. </FormRow>
  367. <FormSectionHeader
  368. label = 'settingsView.buildInfoSection' />
  369. <FormRow
  370. label = 'settingsView.version'>
  371. <Text>
  372. { `${AppInfo.version} build ${AppInfo.buildNumber}` }
  373. </Text>
  374. </FormRow>
  375. <FormSectionHeader
  376. label = 'settingsView.advanced' />
  377. { this._renderAdvancedSettings() }
  378. </ScrollView>
  379. </SafeAreaView>
  380. );
  381. }
  382. /**
  383. * Renders the header of {@code SettingsView}.
  384. *
  385. * @private
  386. * @returns {React$Element}
  387. */
  388. _renderHeader() {
  389. return (
  390. <HeaderWithNavigation
  391. headerLabelKey = 'settingsView.header'
  392. onPressBack = { this._onRequestClose } />
  393. );
  394. }
  395. _setURLFieldReference: (React$ElementRef<*> | null) => void;
  396. /**
  397. * Stores a reference to the URL field for later use.
  398. *
  399. * @param {Object} component - The field component.
  400. * @protected
  401. * @returns {void}
  402. */
  403. _setURLFieldReference(component) {
  404. this._urlField = component;
  405. }
  406. _showURLAlert: () => void;
  407. /**
  408. * Shows an alert telling the user that the URL he/she entered was invalid.
  409. *
  410. * @returns {void}
  411. */
  412. _showURLAlert() {
  413. const { t } = this.props;
  414. Alert.alert(
  415. t('settingsView.alertTitle'),
  416. t('settingsView.alertURLText'),
  417. [
  418. {
  419. onPress: () => this._urlField.focus(),
  420. text: t('settingsView.alertOk')
  421. }
  422. ]
  423. );
  424. }
  425. _updateSettings: (Object) => void;
  426. }
  427. /**
  428. * Maps part of the Redux state to the props of this component.
  429. *
  430. * @param {Object} state - The Redux state.
  431. * @returns {{
  432. * _headerStyles: Object
  433. * }}
  434. */
  435. function _mapStateToProps(state) {
  436. return {
  437. ..._abstractMapStateToProps(state),
  438. _headerStyles: ColorSchemeRegistry.get(state, 'Header')
  439. };
  440. }
  441. export default translate(connect(_mapStateToProps)(SettingsView));