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 2.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. // @flow
  2. import Slider from '@react-native-community/slider';
  3. import _ from 'lodash';
  4. import React, { Component } from 'react';
  5. import { View } from 'react-native';
  6. import { withTheme } from 'react-native-paper';
  7. import { Icon, IconVolumeEmpty } from '../../../base/icons';
  8. import { VOLUME_SLIDER_SCALE } from '../../constants';
  9. /**
  10. * The type of the React {@code Component} props of {@link VolumeSlider}.
  11. */
  12. type Props = {
  13. /**
  14. * The value of the audio slider should display at when the component first
  15. * mounts. Changes will be stored in state. The value should be a number
  16. * between 0 and 1.
  17. */
  18. initialValue: number,
  19. /**
  20. * Theme used for styles.
  21. */
  22. theme: Object
  23. };
  24. /**
  25. * The type of the React {@code Component} state of {@link VolumeSlider}.
  26. */
  27. type State = {
  28. /**
  29. * The volume of the participant's audio element. The value will
  30. * be represented by a slider.
  31. */
  32. volumeLevel: number
  33. };
  34. /**
  35. * Component that renders the volume slider.
  36. *
  37. * @returns {React$Element<any>}
  38. */
  39. class VolumeSlider extends Component<Props, State> {
  40. _onVolumeChange: Function;
  41. _originalVolumeChange: Function;
  42. /**
  43. * Initializes a new {@code VolumeSlider} instance.
  44. *
  45. * @param {Object} props - The read-only properties with which the new
  46. * instance is to be initialized.
  47. */
  48. constructor(props: Props) {
  49. super(props);
  50. this.state = {
  51. volumeLevel: (props.initialValue || 0) * VOLUME_SLIDER_SCALE
  52. };
  53. this._originalVolumeChange = this._onVolumeChange;
  54. this._onVolumeChange = _.throttle(
  55. volumeLevel => this._originalVolumeChange(volumeLevel), 500
  56. );
  57. }
  58. /**
  59. * Implements React's {@link Component#render()}.
  60. *
  61. * @inheritdoc
  62. * @returns {ReactElement}
  63. */
  64. render() {
  65. const { theme } = this.props;
  66. const { volumeLevel } = this.state;
  67. const { palette } = theme;
  68. return (
  69. <View>
  70. <Icon
  71. size = { 24 }
  72. src = { IconVolumeEmpty } />
  73. <Slider
  74. maximumTrackTintColor = { palette.field02 }
  75. maximumValue = { VOLUME_SLIDER_SCALE }
  76. minimumTrackTintColor = { palette.action01 }
  77. minimumValue = { 0 }
  78. onValueChange = { this._onVolumeChange }
  79. thumbTintColor = { palette.field02 }
  80. value = { volumeLevel } />
  81. </View>
  82. );
  83. }
  84. /**
  85. * Sets the internal state of the volume level for the volume slider.
  86. * Invokes the prop onVolumeChange to notify of volume changes.
  87. *
  88. * @param {number} volumeLevel - Selected volume on slider.
  89. * @private
  90. * @returns {void}
  91. */
  92. _onVolumeChange(volumeLevel) {
  93. this.setState({ volumeLevel });
  94. }
  95. }
  96. export default withTheme(VolumeSlider);