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

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