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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. // @flow
  2. import React, { Component } 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 { AudioRoutePickerDialog } from '../../mobile/audio-mode';
  12. import ToolbarButton from './ToolbarButton';
  13. /**
  14. * The {@code MPVolumeView} React {@code Component}. It will only be available
  15. * on iOS.
  16. */
  17. const MPVolumeView
  18. = NativeModules.MPVolumeViewManager
  19. && requireNativeComponent('MPVolumeView', null);
  20. /**
  21. * The style required to hide the {@code MPVolumeView}, since it's displayed
  22. * programmatically.
  23. */
  24. const HIDE_VIEW_STYLE = { display: 'none' };
  25. type Props = {
  26. /**
  27. * The redux {@code dispatch} function used to open/show the
  28. * {@code AudioRoutePickerDialog}.
  29. */
  30. dispatch: Function,
  31. /**
  32. * The name of the Icon of this {@code AudioRouteButton}.
  33. */
  34. iconName: string,
  35. /**
  36. * The style of the Icon of this {@code AudioRouteButton}.
  37. */
  38. iconStyle: Object,
  39. /**
  40. * The style(s) of {@code AudioRouteButton}.
  41. */
  42. style: Array<*> | Object,
  43. /**
  44. * The color underlaying the button.
  45. */
  46. underlayColor: string
  47. };
  48. /**
  49. * A toolbar button which triggers an audio route picker when pressed.
  50. */
  51. class AudioRouteButton extends Component<Props> {
  52. _volumeComponent: ?Object;
  53. /**
  54. * Initializes a new {@code AudioRouteButton} instance.
  55. *
  56. * @param {Object} props - The React {@code Component} props to initialize
  57. * the new {@code AudioRouteButton} instance with.
  58. */
  59. constructor(props) {
  60. super(props);
  61. /**
  62. * The internal reference to the React {@code MPVolumeView} for
  63. * showing the volume control view.
  64. *
  65. * @private
  66. * @type {ReactComponent}
  67. */
  68. this._volumeComponent = null;
  69. // Bind event handlers so they are only bound once per instance.
  70. this._onClick = this._onClick.bind(this);
  71. this._setVolumeComponent = this._setVolumeComponent.bind(this);
  72. }
  73. _onClick: () => void;
  74. /**
  75. * Handles clicking/pressing this {@code AudioRouteButton} by showing an
  76. * audio route picker.
  77. *
  78. * @private
  79. * @returns {void}
  80. */
  81. _onClick() {
  82. if (MPVolumeView) {
  83. NativeModules.MPVolumeViewManager.show(
  84. findNodeHandle(this._volumeComponent));
  85. } else if (AudioRoutePickerDialog) {
  86. this.props.dispatch(openDialog(AudioRoutePickerDialog));
  87. }
  88. }
  89. /**
  90. * Implements React's {@link Component#render()}.
  91. *
  92. * @inheritdoc
  93. * @returns {ReactElement}
  94. */
  95. render() {
  96. const { iconName, iconStyle, style, underlayColor } = this.props;
  97. return (
  98. <View>
  99. <ToolbarButton
  100. iconName = { iconName }
  101. iconStyle = { iconStyle }
  102. onClick = { this._onClick }
  103. style = { style }
  104. underlayColor = { underlayColor } />
  105. {
  106. MPVolumeView
  107. && <MPVolumeView
  108. ref = { this._setVolumeComponent }
  109. style = { HIDE_VIEW_STYLE } />
  110. }
  111. </View>
  112. );
  113. }
  114. _setVolumeComponent: (?Object) => void;
  115. /**
  116. * Sets the internal reference to the React Component wrapping the
  117. * {@code MPVolumeView} component.
  118. *
  119. * @param {ReactComponent} component - React Component.
  120. * @private
  121. * @returns {void}
  122. */
  123. _setVolumeComponent(component) {
  124. this._volumeComponent = component;
  125. }
  126. }
  127. export default (MPVolumeView || AudioRoutePickerDialog)
  128. && connect()(AudioRouteButton);