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.

SettingsView.js 13KB

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