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.

VolumeSlider.js 4.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. // @flow
  2. import _ from 'lodash';
  3. import React, { PureComponent } from 'react';
  4. import { Slider, View } from 'react-native';
  5. import { withTheme } from 'react-native-paper';
  6. import { translate } from '../../../base/i18n';
  7. import { Icon, IconVolumeEmpty } from '../../../base/icons';
  8. import { connect } from '../../../base/redux';
  9. import { setVolume } from '../../../participants-pane/actions.native';
  10. import { VOLUME_SLIDER_SCALE } from '../../constants';
  11. import styles from './styles';
  12. /**
  13. * The type of the React {@code Component} props of {@link VolumeSlider}.
  14. */
  15. type Props = {
  16. /**
  17. * Whether the participant enters the conference silent.
  18. */
  19. _startSilent: boolean,
  20. /**
  21. * The volume level for the participant.
  22. */
  23. _volume: number,
  24. /**
  25. * The redux dispatch function.
  26. */
  27. dispatch: Function,
  28. /**
  29. * Participant reference
  30. */
  31. participant: Object,
  32. /**
  33. * Theme used for styles.
  34. */
  35. theme: Object
  36. };
  37. /**
  38. * The type of the React {@code Component} state of {@link VolumeSlider}.
  39. */
  40. type State = {
  41. /**
  42. * The volume of the participant's audio element. The value will
  43. * be represented by a slider.
  44. */
  45. volumeLevel: number
  46. };
  47. /**
  48. * Component that renders the volume slider.
  49. *
  50. * @returns {React$Element<any>}
  51. */
  52. class VolumeSlider extends PureComponent<Props, State> {
  53. _onVolumeChange: Function;
  54. _originalVolumeChange: Function;
  55. /**
  56. * Initializes a new {@code VolumeSlider} instance.
  57. *
  58. * @param {Object} props - The read-only properties with which the new
  59. * instance is to be initialized.
  60. */
  61. constructor(props: Props) {
  62. super(props);
  63. this.state = {
  64. volumeLevel: props._volume || 0
  65. };
  66. this._originalVolumeChange = this._onVolumeChange;
  67. this._onVolumeChange = _.throttle(
  68. volumeLevel => this._originalVolumeChange(volumeLevel), 500
  69. );
  70. }
  71. /**
  72. * Implements React's {@link Component#render()}.
  73. *
  74. * @inheritdoc
  75. * @returns {ReactElement}
  76. */
  77. render() {
  78. const { _startSilent, theme } = this.props;
  79. const { volumeLevel } = this.state;
  80. const { palette } = theme;
  81. const onVolumeChange = _startSilent ? undefined : this._onVolumeChange;
  82. return (
  83. <View style = { styles.volumeSliderContainer } >
  84. <Icon
  85. size = { 24 }
  86. src = { IconVolumeEmpty }
  87. style = { styles.volumeIcon } />
  88. <Slider
  89. maximumTrackTintColor = { palette.field02 }
  90. maximumValue = { VOLUME_SLIDER_SCALE }
  91. minimumTrackTintColor = { palette.action01 }
  92. minimumValue = { 0 }
  93. onValueChange = { onVolumeChange }
  94. style = { styles.sliderContainer }
  95. thumbTintColor = { palette.field02 }
  96. value = { volumeLevel } />
  97. </View>
  98. );
  99. }
  100. /**
  101. * Sets the internal state of the volume level for the volume slider.
  102. * Invokes the prop onVolumeChange to notify of volume changes.
  103. *
  104. * @param {number} volumeLevel - Selected volume on slider.
  105. * @private
  106. * @returns {void}
  107. */
  108. _onVolumeChange(volumeLevel) {
  109. const { dispatch, participant } = this.props;
  110. const { id } = participant;
  111. dispatch(setVolume(id, volumeLevel));
  112. }
  113. }
  114. /**
  115. * Maps (parts of) the Redux state to the associated props for the
  116. * {@code VolumeSlider} component.
  117. *
  118. * @param {Object} state - The Redux state.
  119. * @param {Object} ownProps - The own props of the component.
  120. * @returns {Props}
  121. */
  122. function mapStateToProps(state, ownProps): Object {
  123. const { participant } = ownProps;
  124. const { id, local } = participant;
  125. const { participantsVolume } = state['features/participants-pane'];
  126. const { startSilent } = state['features/base/config'];
  127. return {
  128. _startSilent: Boolean(startSilent),
  129. _volume: local ? undefined : participantsVolume[id]
  130. };
  131. }
  132. export default translate(connect(mapStateToProps)(withTheme(VolumeSlider)));