2017-12-02 13:19:58 +00:00
|
|
|
import React from 'react';
|
|
|
|
import PropTypes from 'prop-types';
|
2018-09-25 19:28:42 +00:00
|
|
|
import {
|
2019-10-07 20:56:30 +00:00
|
|
|
View, StyleSheet, Text, Easing, Dimensions
|
2018-09-25 19:28:42 +00:00
|
|
|
} from 'react-native';
|
2017-12-02 13:19:58 +00:00
|
|
|
import Video from 'react-native-video';
|
2019-10-07 20:56:30 +00:00
|
|
|
import Slider from '@react-native-community/slider';
|
2018-09-11 16:32:52 +00:00
|
|
|
import moment from 'moment';
|
2018-12-21 10:55:35 +00:00
|
|
|
import equal from 'deep-equal';
|
2019-04-24 18:36:29 +00:00
|
|
|
import Touchable from 'react-native-platform-touchable';
|
2018-09-11 16:32:52 +00:00
|
|
|
|
2019-09-03 19:27:57 +00:00
|
|
|
import Markdown from '../markdown';
|
2019-03-01 16:49:11 +00:00
|
|
|
import { CustomIcon } from '../../lib/Icons';
|
2019-03-29 19:36:07 +00:00
|
|
|
import sharedStyles from '../../views/Styles';
|
2019-12-11 23:01:12 +00:00
|
|
|
import { themes } from '../../constants/colors';
|
2019-10-07 20:56:30 +00:00
|
|
|
import { isAndroid, isIOS } from '../../utils/deviceInfo';
|
2019-12-11 23:01:12 +00:00
|
|
|
import { withSplit } from '../../split';
|
2017-12-28 17:40:10 +00:00
|
|
|
|
2017-12-02 13:19:58 +00:00
|
|
|
const styles = StyleSheet.create({
|
|
|
|
audioContainer: {
|
|
|
|
flex: 1,
|
|
|
|
flexDirection: 'row',
|
|
|
|
alignItems: 'center',
|
2018-09-11 16:32:52 +00:00
|
|
|
height: 56,
|
2019-03-29 19:36:07 +00:00
|
|
|
borderWidth: 1,
|
2018-09-11 16:32:52 +00:00
|
|
|
borderRadius: 4,
|
2019-03-29 19:36:07 +00:00
|
|
|
marginBottom: 6
|
2017-12-02 13:19:58 +00:00
|
|
|
},
|
|
|
|
playPauseButton: {
|
2019-04-24 18:36:29 +00:00
|
|
|
marginHorizontal: 10,
|
2017-12-02 13:19:58 +00:00
|
|
|
alignItems: 'center',
|
|
|
|
backgroundColor: 'transparent'
|
|
|
|
},
|
2018-09-11 16:32:52 +00:00
|
|
|
slider: {
|
2019-04-24 18:36:29 +00:00
|
|
|
flex: 1
|
2017-12-02 13:19:58 +00:00
|
|
|
},
|
|
|
|
duration: {
|
2019-04-24 18:36:29 +00:00
|
|
|
marginHorizontal: 12,
|
2018-09-11 16:32:52 +00:00
|
|
|
fontSize: 14,
|
2019-03-29 19:36:07 +00:00
|
|
|
...sharedStyles.textRegular
|
2017-12-02 13:19:58 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2018-09-11 16:32:52 +00:00
|
|
|
const formatTime = seconds => moment.utc(seconds * 1000).format('mm:ss');
|
2019-04-24 18:36:29 +00:00
|
|
|
const BUTTON_HIT_SLOP = {
|
|
|
|
top: 12, right: 12, bottom: 12, left: 12
|
|
|
|
};
|
2019-05-20 20:43:50 +00:00
|
|
|
const sliderAnimationConfig = {
|
|
|
|
duration: 250,
|
|
|
|
easing: Easing.linear,
|
|
|
|
delay: 0
|
|
|
|
};
|
|
|
|
|
2019-12-11 23:01:12 +00:00
|
|
|
const Button = React.memo(({ paused, onPress, theme }) => (
|
2019-05-20 20:43:50 +00:00
|
|
|
<Touchable
|
|
|
|
style={styles.playPauseButton}
|
|
|
|
onPress={onPress}
|
|
|
|
hitSlop={BUTTON_HIT_SLOP}
|
|
|
|
background={Touchable.SelectableBackgroundBorderless()}
|
|
|
|
>
|
2019-12-11 23:01:12 +00:00
|
|
|
<CustomIcon name={paused ? 'play' : 'pause'} size={36} color={themes[theme].tintColor} />
|
2019-05-20 20:43:50 +00:00
|
|
|
</Touchable>
|
|
|
|
));
|
|
|
|
|
|
|
|
Button.propTypes = {
|
|
|
|
paused: PropTypes.bool,
|
2019-12-11 23:01:12 +00:00
|
|
|
theme: PropTypes.string,
|
2019-05-20 20:43:50 +00:00
|
|
|
onPress: PropTypes.func
|
|
|
|
};
|
|
|
|
Button.displayName = 'MessageAudioButton';
|
2017-12-02 13:19:58 +00:00
|
|
|
|
2019-12-11 23:01:12 +00:00
|
|
|
class Audio extends React.Component {
|
2017-12-02 13:19:58 +00:00
|
|
|
static propTypes = {
|
|
|
|
file: PropTypes.object.isRequired,
|
|
|
|
baseUrl: PropTypes.string.isRequired,
|
2018-09-11 16:32:52 +00:00
|
|
|
user: PropTypes.object.isRequired,
|
2019-12-11 23:01:12 +00:00
|
|
|
theme: PropTypes.string,
|
|
|
|
split: PropTypes.bool,
|
2019-05-20 20:43:50 +00:00
|
|
|
getCustomEmoji: PropTypes.func
|
2017-12-02 13:19:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
constructor(props) {
|
|
|
|
super(props);
|
|
|
|
const { baseUrl, file, user } = props;
|
|
|
|
this.state = {
|
|
|
|
currentTime: 0,
|
|
|
|
duration: 0,
|
|
|
|
paused: true,
|
|
|
|
uri: `${ baseUrl }${ file.audio_url }?rc_uid=${ user.id }&rc_token=${ user.token }`
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2018-12-21 10:55:35 +00:00
|
|
|
shouldComponentUpdate(nextProps, nextState) {
|
|
|
|
const {
|
|
|
|
currentTime, duration, paused, uri
|
|
|
|
} = this.state;
|
2019-12-11 23:01:12 +00:00
|
|
|
const { file, split, theme } = this.props;
|
|
|
|
if (nextProps.theme !== theme) {
|
|
|
|
return true;
|
|
|
|
}
|
2018-12-21 10:55:35 +00:00
|
|
|
if (nextState.currentTime !== currentTime) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (nextState.duration !== duration) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (nextState.paused !== paused) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (nextState.uri !== uri) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (!equal(nextProps.file, file)) {
|
|
|
|
return true;
|
|
|
|
}
|
2019-12-11 23:01:12 +00:00
|
|
|
if (nextProps.split !== split) {
|
|
|
|
return true;
|
|
|
|
}
|
2018-12-21 10:55:35 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-04-24 18:36:29 +00:00
|
|
|
onLoad = (data) => {
|
2018-03-07 00:17:20 +00:00
|
|
|
this.setState({ duration: data.duration > 0 ? data.duration : 0 });
|
2017-12-02 13:19:58 +00:00
|
|
|
}
|
|
|
|
|
2019-04-24 18:36:29 +00:00
|
|
|
onProgress = (data) => {
|
2018-09-25 19:28:42 +00:00
|
|
|
const { duration } = this.state;
|
|
|
|
if (data.currentTime <= duration) {
|
2017-12-02 13:19:58 +00:00
|
|
|
this.setState({ currentTime: data.currentTime });
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-24 18:36:29 +00:00
|
|
|
onEnd = () => {
|
2017-12-02 13:19:58 +00:00
|
|
|
this.setState({ paused: true, currentTime: 0 });
|
|
|
|
requestAnimationFrame(() => {
|
|
|
|
this.player.seek(0);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-05-20 20:43:50 +00:00
|
|
|
get duration() {
|
2018-09-25 19:28:42 +00:00
|
|
|
const { duration } = this.state;
|
|
|
|
return formatTime(duration);
|
2017-12-02 13:19:58 +00:00
|
|
|
}
|
|
|
|
|
2019-05-20 20:43:50 +00:00
|
|
|
setRef = ref => this.player = ref;
|
|
|
|
|
2019-04-24 18:36:29 +00:00
|
|
|
togglePlayPause = () => {
|
2018-09-25 19:28:42 +00:00
|
|
|
const { paused } = this.state;
|
|
|
|
this.setState({ paused: !paused });
|
2017-12-02 13:19:58 +00:00
|
|
|
}
|
|
|
|
|
2019-05-20 20:43:50 +00:00
|
|
|
onValueChange = value => this.setState({ currentTime: value });
|
|
|
|
|
2017-12-02 13:19:58 +00:00
|
|
|
render() {
|
2018-09-25 19:28:42 +00:00
|
|
|
const {
|
|
|
|
uri, paused, currentTime, duration
|
|
|
|
} = this.state;
|
2018-09-11 16:32:52 +00:00
|
|
|
const {
|
2020-03-18 21:26:17 +00:00
|
|
|
user, baseUrl, file, getCustomEmoji, split, theme
|
2018-09-11 16:32:52 +00:00
|
|
|
} = this.props;
|
|
|
|
const { description } = file;
|
2018-09-14 19:39:52 +00:00
|
|
|
|
|
|
|
if (!baseUrl) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2017-12-02 13:19:58 +00:00
|
|
|
return (
|
2019-10-07 20:56:30 +00:00
|
|
|
<>
|
2019-12-11 23:01:12 +00:00
|
|
|
<View
|
|
|
|
style={[
|
|
|
|
styles.audioContainer,
|
|
|
|
{ backgroundColor: themes[theme].chatComponentBackground, borderColor: themes[theme].borderColor },
|
|
|
|
split && sharedStyles.tabletContent
|
|
|
|
]}
|
|
|
|
>
|
2017-12-02 13:19:58 +00:00
|
|
|
<Video
|
2019-05-20 20:43:50 +00:00
|
|
|
ref={this.setRef}
|
2017-12-02 13:19:58 +00:00
|
|
|
source={{ uri }}
|
|
|
|
onLoad={this.onLoad}
|
|
|
|
onProgress={this.onProgress}
|
|
|
|
onEnd={this.onEnd}
|
|
|
|
paused={paused}
|
|
|
|
repeat={false}
|
2020-03-18 21:26:17 +00:00
|
|
|
ignoreSilentSwitch='ignore'
|
2017-12-02 13:19:58 +00:00
|
|
|
/>
|
2019-12-11 23:01:12 +00:00
|
|
|
<Button paused={paused} onPress={this.togglePlayPause} theme={theme} />
|
2018-09-11 16:32:52 +00:00
|
|
|
<Slider
|
|
|
|
style={styles.slider}
|
2018-09-25 19:28:42 +00:00
|
|
|
value={currentTime}
|
|
|
|
maximumValue={duration}
|
2018-09-11 16:32:52 +00:00
|
|
|
minimumValue={0}
|
|
|
|
animateTransitions
|
2019-05-20 20:43:50 +00:00
|
|
|
animationConfig={sliderAnimationConfig}
|
2019-12-11 23:01:12 +00:00
|
|
|
thumbTintColor={isAndroid && themes[theme].tintColor}
|
|
|
|
minimumTrackTintColor={themes[theme].tintColor}
|
|
|
|
maximumTrackTintColor={themes[theme].auxiliaryText}
|
2019-05-20 20:43:50 +00:00
|
|
|
onValueChange={this.onValueChange}
|
2019-10-07 20:56:30 +00:00
|
|
|
thumbImage={isIOS && { uri: 'audio_thumb', scale: Dimensions.get('window').scale }}
|
2018-09-11 16:32:52 +00:00
|
|
|
/>
|
2019-12-11 23:01:12 +00:00
|
|
|
<Text style={[styles.duration, { color: themes[theme].auxiliaryText }]}>{this.duration}</Text>
|
2019-05-20 20:43:50 +00:00
|
|
|
</View>
|
2020-03-18 21:26:17 +00:00
|
|
|
<Markdown msg={description} baseUrl={baseUrl} username={user.username} getCustomEmoji={getCustomEmoji} theme={theme} />
|
2019-10-07 20:56:30 +00:00
|
|
|
</>
|
2017-12-02 13:19:58 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
2019-12-11 23:01:12 +00:00
|
|
|
|
|
|
|
export default withSplit(Audio);
|