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

SettingsView.js 17KB

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