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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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 { getLocalParticipant } from '../../../base/participants';
  9. import { connect } from '../../../base/redux';
  10. import { setVolume } from '../../../participants-pane/actions.native';
  11. import { VOLUME_SLIDER_SCALE } from '../../constants';
  12. import styles from './styles';
  13. /**
  14. * The type of the React {@code Component} props of {@link VolumeSlider}.
  15. */
  16. type Props = {
  17. /**
  18. * Whether the participant enters the conference silent.
  19. */
  20. _startSilent: boolean,
  21. /**
  22. * The volume level for the participant.
  23. */
  24. _volume: number,
  25. /**
  26. * The redux dispatch function.
  27. */
  28. dispatch: Function,
  29. /**
  30. * The ID of the participant.
  31. */
  32. participantID: string,
  33. /**
  34. * Theme used for styles.
  35. */
  36. theme: Object
  37. };
  38. /**
  39. * The type of the React {@code Component} state of {@link VolumeSlider}.
  40. */
  41. type State = {
  42. /**
  43. * The volume of the participant's audio element. The value will
  44. * be represented by a slider.
  45. */
  46. volumeLevel: number
  47. };
  48. /**
  49. * Component that renders the volume slider.
  50. *
  51. * @returns {React$Element<any>}
  52. */
  53. class VolumeSlider extends PureComponent<Props, State> {
  54. _onVolumeChange: Function;
  55. _originalVolumeChange: Function;
  56. /**
  57. * Initializes a new {@code VolumeSlider} instance.
  58. *
  59. * @param {Object} props - The read-only properties with which the new
  60. * instance is to be initialized.
  61. */
  62. constructor(props: Props) {
  63. super(props);
  64. this.state = {
  65. volumeLevel: props._volume || 0
  66. };
  67. this._originalVolumeChange = this._onVolumeChange;
  68. this._onVolumeChange = _.throttle(
  69. volumeLevel => this._originalVolumeChange(volumeLevel), 500
  70. );
  71. }
  72. /**
  73. * Implements React's {@link Component#render()}.
  74. *
  75. * @inheritdoc
  76. * @returns {ReactElement}
  77. */
  78. render() {
  79. const { _startSilent, theme } = this.props;
  80. const { volumeLevel } = this.state;
  81. const { palette } = theme;
  82. const onVolumeChange = _startSilent ? undefined : this._onVolumeChange;
  83. return (
  84. <View style = { styles.volumeSliderContainer } >
  85. <Icon
  86. size = { 20 }
  87. src = { IconVolumeEmpty } />
  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, participantID } = this.props;
  110. dispatch(setVolume(participantID, volumeLevel));
  111. }
  112. }
  113. /**
  114. * Maps (parts of) the Redux state to the associated props for the
  115. * {@code VolumeSlider} component.
  116. *
  117. * @param {Object} state - The Redux state.
  118. * @param {Object} ownProps - The own props of the component.
  119. * @returns {Props}
  120. */
  121. function mapStateToProps(state, ownProps): Object {
  122. const { participantID } = ownProps;
  123. const { participantsVolume } = state['features/participants-pane'];
  124. const { startSilent } = state['features/base/config'];
  125. const localParticipant = getLocalParticipant(state);
  126. return {
  127. _startSilent: Boolean(startSilent),
  128. _volume: localParticipant ? undefined : participantID
  129. ? participantsVolume[participantID] : undefined
  130. };
  131. }
  132. export default connect(mapStateToProps)(withTheme(VolumeSlider));