2017-10-24 08:40:39 +00:00
|
|
|
// @flow
|
|
|
|
|
2018-04-18 14:34:40 +00:00
|
|
|
import React from 'react';
|
2017-10-24 08:40:39 +00:00
|
|
|
import {
|
|
|
|
findNodeHandle,
|
|
|
|
NativeModules,
|
2017-11-14 20:18:16 +00:00
|
|
|
requireNativeComponent,
|
2017-10-24 08:40:39 +00:00
|
|
|
View
|
|
|
|
} from 'react-native';
|
|
|
|
|
2018-05-10 23:01:55 +00:00
|
|
|
import { openDialog } from '../../../base/dialog';
|
|
|
|
import { translate } from '../../../base/i18n';
|
2019-03-21 16:38:29 +00:00
|
|
|
import { connect } from '../../../base/redux';
|
2018-05-10 23:01:55 +00:00
|
|
|
import { AbstractButton } from '../../../base/toolbox';
|
|
|
|
import type { AbstractButtonProps } from '../../../base/toolbox';
|
2017-10-24 08:40:39 +00:00
|
|
|
|
2018-05-10 23:01:55 +00:00
|
|
|
import AudioRoutePickerDialog from './AudioRoutePickerDialog';
|
2017-10-24 08:40:39 +00:00
|
|
|
|
|
|
|
/**
|
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
|
2018-10-03 09:29:19 +00:00
|
|
|
&& requireNativeComponent('MPVolumeView');
|
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' };
|
|
|
|
|
2018-04-18 14:34:40 +00:00
|
|
|
type Props = AbstractButtonProps & {
|
2017-10-24 08:40:39 +00:00
|
|
|
|
|
|
|
/**
|
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
|
|
|
*/
|
2018-04-18 14:34:40 +00:00
|
|
|
dispatch: Function
|
2017-10-24 08:40:39 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* A toolbar button which triggers an audio route picker when pressed.
|
|
|
|
*/
|
2018-04-18 14:34:40 +00:00
|
|
|
class AudioRouteButton extends AbstractButton<Props, *> {
|
2018-06-07 20:32:18 +00:00
|
|
|
accessibilityLabel = 'toolbar.accessibilityLabel.audioRoute';
|
2018-04-18 14:34:40 +00:00
|
|
|
iconName = 'icon-volume';
|
|
|
|
label = 'toolbar.audioRoute';
|
|
|
|
|
2017-10-24 08:40:39 +00:00
|
|
|
_volumeComponent: ?Object;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Initializes a new {@code AudioRouteButton} instance.
|
|
|
|
*
|
2018-04-18 14:34:40 +00:00
|
|
|
* @param {Props} props - The React {@code Component} props to initialize
|
2017-10-24 08:40:39 +00:00
|
|
|
* the new {@code AudioRouteButton} instance with.
|
|
|
|
*/
|
2018-04-18 14:34:40 +00:00
|
|
|
constructor(props: Props) {
|
2017-10-24 08:40:39 +00:00
|
|
|
super(props);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The internal reference to the React {@code MPVolumeView} for
|
|
|
|
* showing the volume control view.
|
|
|
|
*
|
|
|
|
* @private
|
2018-04-18 14:34:40 +00:00
|
|
|
* @type {ReactElement}
|
2017-10-24 08:40:39 +00:00
|
|
|
*/
|
|
|
|
this._volumeComponent = null;
|
|
|
|
|
|
|
|
// Bind event handlers so they are only bound once per instance.
|
|
|
|
this._setVolumeComponent = this._setVolumeComponent.bind(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-04-18 14:34:40 +00:00
|
|
|
* Handles clicking / pressing the button, and opens the appropriate dialog.
|
2017-10-24 08:40:39 +00:00
|
|
|
*
|
|
|
|
* @private
|
|
|
|
* @returns {void}
|
|
|
|
*/
|
2018-04-18 14:34:40 +00:00
|
|
|
_handleClick() {
|
2017-10-24 08:40:39 +00:00
|
|
|
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));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-18 14:34:40 +00:00
|
|
|
_setVolumeComponent: (?Object) => void;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets the internal reference to the React Component wrapping the
|
|
|
|
* {@code MPVolumeView} component.
|
|
|
|
*
|
|
|
|
* @param {ReactElement} component - React Component.
|
|
|
|
* @private
|
|
|
|
* @returns {void}
|
|
|
|
*/
|
|
|
|
_setVolumeComponent(component) {
|
|
|
|
this._volumeComponent = component;
|
|
|
|
}
|
|
|
|
|
2017-10-24 08:40:39 +00:00
|
|
|
/**
|
|
|
|
* Implements React's {@link Component#render()}.
|
|
|
|
*
|
|
|
|
* @inheritdoc
|
2018-05-11 02:10:26 +00:00
|
|
|
* @returns {React$Node}
|
2017-10-24 08:40:39 +00:00
|
|
|
*/
|
|
|
|
render() {
|
2018-04-18 14:34:40 +00:00
|
|
|
if (!MPVolumeView && !AudioRoutePickerDialog) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
const element = super.render();
|
2017-10-24 08:40:39 +00:00
|
|
|
|
|
|
|
return (
|
|
|
|
<View>
|
2018-04-18 14:34:40 +00:00
|
|
|
{ element }
|
2017-10-24 08:40:39 +00:00
|
|
|
{
|
|
|
|
MPVolumeView
|
|
|
|
&& <MPVolumeView
|
|
|
|
ref = { this._setVolumeComponent }
|
|
|
|
style = { HIDE_VIEW_STYLE } />
|
|
|
|
}
|
|
|
|
</View>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-18 14:34:40 +00:00
|
|
|
export default translate(connect()(AudioRouteButton));
|