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.

AudioRouteButton.js 3.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. // @flow
  2. import React from 'react';
  3. import {
  4. findNodeHandle,
  5. NativeModules,
  6. requireNativeComponent,
  7. View
  8. } from 'react-native';
  9. import { connect } from 'react-redux';
  10. import { openDialog } from '../../../base/dialog';
  11. import { translate } from '../../../base/i18n';
  12. import { AbstractButton } from '../../../base/toolbox';
  13. import type { AbstractButtonProps } from '../../../base/toolbox';
  14. import AudioRoutePickerDialog from './AudioRoutePickerDialog';
  15. /**
  16. * The {@code MPVolumeView} React {@code Component}. It will only be available
  17. * on iOS.
  18. */
  19. const MPVolumeView
  20. = NativeModules.MPVolumeViewManager
  21. && requireNativeComponent('MPVolumeView', null);
  22. /**
  23. * The style required to hide the {@code MPVolumeView}, since it's displayed
  24. * programmatically.
  25. */
  26. const HIDE_VIEW_STYLE = { display: 'none' };
  27. type Props = AbstractButtonProps & {
  28. /**
  29. * The redux {@code dispatch} function used to open/show the
  30. * {@code AudioRoutePickerDialog}.
  31. */
  32. dispatch: Function
  33. };
  34. /**
  35. * A toolbar button which triggers an audio route picker when pressed.
  36. */
  37. class AudioRouteButton extends AbstractButton<Props, *> {
  38. accessibilityLabel = 'Audio route';
  39. iconName = 'icon-volume';
  40. label = 'toolbar.audioRoute';
  41. _volumeComponent: ?Object;
  42. /**
  43. * Initializes a new {@code AudioRouteButton} instance.
  44. *
  45. * @param {Props} props - The React {@code Component} props to initialize
  46. * the new {@code AudioRouteButton} instance with.
  47. */
  48. constructor(props: Props) {
  49. super(props);
  50. /**
  51. * The internal reference to the React {@code MPVolumeView} for
  52. * showing the volume control view.
  53. *
  54. * @private
  55. * @type {ReactElement}
  56. */
  57. this._volumeComponent = null;
  58. // Bind event handlers so they are only bound once per instance.
  59. this._setVolumeComponent = this._setVolumeComponent.bind(this);
  60. }
  61. /**
  62. * Handles clicking / pressing the button, and opens the appropriate dialog.
  63. *
  64. * @private
  65. * @returns {void}
  66. */
  67. _handleClick() {
  68. if (MPVolumeView) {
  69. NativeModules.MPVolumeViewManager.show(
  70. findNodeHandle(this._volumeComponent));
  71. } else if (AudioRoutePickerDialog) {
  72. this.props.dispatch(openDialog(AudioRoutePickerDialog));
  73. }
  74. }
  75. /**
  76. * Indicates whether this button is disabled or not.
  77. *
  78. * @override
  79. * @private
  80. * @returns {boolean}
  81. */
  82. _isDisabled() {
  83. return false;
  84. }
  85. _setVolumeComponent: (?Object) => void;
  86. /**
  87. * Sets the internal reference to the React Component wrapping the
  88. * {@code MPVolumeView} component.
  89. *
  90. * @param {ReactElement} component - React Component.
  91. * @private
  92. * @returns {void}
  93. */
  94. _setVolumeComponent(component) {
  95. this._volumeComponent = component;
  96. }
  97. /**
  98. * Implements React's {@link Component#render()}.
  99. *
  100. * @inheritdoc
  101. * @returns {?ReactElement}
  102. */
  103. render() {
  104. if (!MPVolumeView && !AudioRoutePickerDialog) {
  105. // $FlowFixMe
  106. return null;
  107. }
  108. const element = super.render();
  109. return (
  110. <View>
  111. { element }
  112. {
  113. MPVolumeView
  114. && <MPVolumeView
  115. ref = { this._setVolumeComponent }
  116. style = { HIDE_VIEW_STYLE } />
  117. }
  118. </View>
  119. );
  120. }
  121. }
  122. export default translate(connect()(AudioRouteButton));