2017-10-24 08:40:39 +00:00
|
|
|
// @flow
|
|
|
|
|
|
|
|
import React, { Component } from 'react';
|
|
|
|
import {
|
|
|
|
findNodeHandle,
|
|
|
|
NativeModules,
|
2017-11-14 20:18:16 +00:00
|
|
|
requireNativeComponent,
|
2017-10-24 08:40:39 +00:00
|
|
|
View
|
|
|
|
} from 'react-native';
|
|
|
|
import { connect } from 'react-redux';
|
|
|
|
|
|
|
|
import { openDialog } from '../../base/dialog';
|
|
|
|
import { AudioRoutePickerDialog } from '../../mobile/audio-mode';
|
|
|
|
|
|
|
|
import ToolbarButton from './ToolbarButton';
|
|
|
|
|
|
|
|
/**
|
2017-11-14 20:18:16 +00:00
|
|
|
* The {@code MPVolumeView} React {@code Component}. It will only be available
|
2017-10-24 08:40:39 +00:00
|
|
|
* on iOS.
|
|
|
|
*/
|
2017-11-14 20:18:16 +00:00
|
|
|
const MPVolumeView
|
|
|
|
= NativeModules.MPVolumeViewManager
|
|
|
|
&& requireNativeComponent('MPVolumeView', null);
|
2017-10-24 08:40:39 +00:00
|
|
|
|
|
|
|
/**
|
2017-11-14 20:18:16 +00:00
|
|
|
* The style required to hide the {@code MPVolumeView}, since it's displayed
|
2017-10-24 08:40:39 +00:00
|
|
|
* programmatically.
|
|
|
|
*/
|
|
|
|
const HIDE_VIEW_STYLE = { display: 'none' };
|
|
|
|
|
|
|
|
type Props = {
|
|
|
|
|
|
|
|
/**
|
2017-11-14 20:18:16 +00:00
|
|
|
* The redux {@code dispatch} function used to open/show the
|
|
|
|
* {@code AudioRoutePickerDialog}.
|
2017-10-24 08:40:39 +00:00
|
|
|
*/
|
|
|
|
dispatch: Function,
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The name of the Icon of this {@code AudioRouteButton}.
|
|
|
|
*/
|
|
|
|
iconName: string,
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The style of the Icon of this {@code AudioRouteButton}.
|
|
|
|
*/
|
|
|
|
iconStyle: Object,
|
|
|
|
|
|
|
|
/**
|
2017-11-14 20:18:16 +00:00
|
|
|
* The style(s) of {@code AudioRouteButton}.
|
2017-10-24 08:40:39 +00:00
|
|
|
*/
|
|
|
|
style: Array<*> | Object,
|
|
|
|
|
|
|
|
/**
|
2017-11-14 20:18:16 +00:00
|
|
|
* The color underlaying the button.
|
2017-10-24 08:40:39 +00:00
|
|
|
*/
|
|
|
|
underlayColor: string
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* A toolbar button which triggers an audio route picker when pressed.
|
|
|
|
*/
|
|
|
|
class AudioRouteButton extends Component<Props> {
|
|
|
|
_volumeComponent: ?Object;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Initializes a new {@code AudioRouteButton} instance.
|
|
|
|
*
|
|
|
|
* @param {Object} props - The React {@code Component} props to initialize
|
|
|
|
* the new {@code AudioRouteButton} instance with.
|
|
|
|
*/
|
|
|
|
constructor(props) {
|
|
|
|
super(props);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The internal reference to the React {@code MPVolumeView} for
|
|
|
|
* showing the volume control view.
|
|
|
|
*
|
|
|
|
* @private
|
|
|
|
* @type {ReactComponent}
|
|
|
|
*/
|
|
|
|
this._volumeComponent = null;
|
|
|
|
|
|
|
|
// Bind event handlers so they are only bound once per instance.
|
|
|
|
this._onClick = this._onClick.bind(this);
|
|
|
|
this._setVolumeComponent = this._setVolumeComponent.bind(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
_onClick: () => void;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Handles clicking/pressing this {@code AudioRouteButton} by showing an
|
|
|
|
* audio route picker.
|
|
|
|
*
|
|
|
|
* @private
|
|
|
|
* @returns {void}
|
|
|
|
*/
|
|
|
|
_onClick() {
|
|
|
|
if (MPVolumeView) {
|
2017-11-14 20:18:16 +00:00
|
|
|
NativeModules.MPVolumeViewManager.show(
|
|
|
|
findNodeHandle(this._volumeComponent));
|
2017-10-24 08:40:39 +00:00
|
|
|
} else if (AudioRoutePickerDialog) {
|
|
|
|
this.props.dispatch(openDialog(AudioRoutePickerDialog));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Implements React's {@link Component#render()}.
|
|
|
|
*
|
|
|
|
* @inheritdoc
|
|
|
|
* @returns {ReactElement}
|
|
|
|
*/
|
|
|
|
render() {
|
|
|
|
const { iconName, iconStyle, style, underlayColor } = this.props;
|
|
|
|
|
|
|
|
return (
|
|
|
|
<View>
|
|
|
|
<ToolbarButton
|
|
|
|
iconName = { iconName }
|
|
|
|
iconStyle = { iconStyle }
|
|
|
|
onClick = { this._onClick }
|
|
|
|
style = { style }
|
|
|
|
underlayColor = { underlayColor } />
|
|
|
|
{
|
|
|
|
MPVolumeView
|
|
|
|
&& <MPVolumeView
|
|
|
|
ref = { this._setVolumeComponent }
|
|
|
|
style = { HIDE_VIEW_STYLE } />
|
|
|
|
}
|
|
|
|
</View>
|
|
|
|
);
|
|
|
|
}
|
2017-11-14 20:18:16 +00:00
|
|
|
|
|
|
|
_setVolumeComponent: (?Object) => void;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets the internal reference to the React Component wrapping the
|
|
|
|
* {@code MPVolumeView} component.
|
|
|
|
*
|
|
|
|
* @param {ReactComponent} component - React Component.
|
|
|
|
* @private
|
|
|
|
* @returns {void}
|
|
|
|
*/
|
|
|
|
_setVolumeComponent(component) {
|
|
|
|
this._volumeComponent = component;
|
|
|
|
}
|
2017-10-24 08:40:39 +00:00
|
|
|
}
|
|
|
|
|
2017-11-14 20:18:16 +00:00
|
|
|
export default (MPVolumeView || AudioRoutePickerDialog)
|
|
|
|
&& connect()(AudioRouteButton);
|