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.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. // @flow
  2. import Slider from '@react-native-community/slider';
  3. import _ from 'lodash';
  4. import React, { PureComponent } from 'react';
  5. import { View } from 'react-native';
  6. import { withTheme } from 'react-native-paper';
  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. * The ID of the participant.
  30. */
  31. participantID: string,
  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. <Slider
  88. maximumTrackTintColor = { palette.field02 }
  89. maximumValue = { VOLUME_SLIDER_SCALE }
  90. minimumTrackTintColor = { palette.action01 }
  91. minimumValue = { 0 }
  92. onValueChange = { onVolumeChange }
  93. style = { styles.sliderContainer }
  94. thumbTintColor = { palette.field02 }
  95. value = { volumeLevel } />
  96. </View>
  97. );
  98. }
  99. /**
  100. * Sets the internal state of the volume level for the volume slider.
  101. * Invokes the prop onVolumeChange to notify of volume changes.
  102. *
  103. * @param {number} volumeLevel - Selected volume on slider.
  104. * @private
  105. * @returns {void}
  106. */
  107. _onVolumeChange(volumeLevel) {
  108. const { dispatch, participantID } = this.props;
  109. dispatch(setVolume(participantID, volumeLevel));
  110. }
  111. }
  112. /**
  113. * Maps (parts of) the Redux state to the associated props for the
  114. * {@code VolumeSlider} component.
  115. *
  116. * @param {Object} state - The Redux state.
  117. * @param {Object} ownProps - The own props of the component.
  118. * @returns {Props}
  119. */
  120. function mapStateToProps(state, ownProps): Object {
  121. const { participantID } = ownProps;
  122. const { participantsVolume } = state['features/participants-pane'];
  123. const { startSilent } = state['features/base/config'];
  124. return {
  125. _startSilent: Boolean(startSilent),
  126. _volume: participantID && participantsVolume[participantID]
  127. };
  128. }
  129. export default connect(mapStateToProps)(withTheme(VolumeSlider));