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-03-01 16:49:11 +00:00
|
|
|
View, StyleSheet, Text, Easing
|
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';
|
|
|
|
import Slider from 'react-native-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
|
|
|
|
2017-12-02 13:19:58 +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';
|
|
|
|
import { COLOR_BACKGROUND_CONTAINER, COLOR_BORDER, COLOR_PRIMARY } from '../../constants/colors';
|
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
|
|
|
backgroundColor: COLOR_BACKGROUND_CONTAINER,
|
|
|
|
borderColor: COLOR_BORDER,
|
|
|
|
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
|
|
|
playPauseImage: {
|
2019-03-29 19:36:07 +00:00
|
|
|
color: COLOR_PRIMARY
|
2017-12-02 13:19:58 +00:00
|
|
|
},
|
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.textColorNormal,
|
|
|
|
...sharedStyles.textRegular
|
2018-09-11 16:32:52 +00:00
|
|
|
},
|
|
|
|
thumbStyle: {
|
|
|
|
width: 12,
|
|
|
|
height: 12
|
2019-04-24 18:36:29 +00:00
|
|
|
},
|
|
|
|
trackStyle: {
|
|
|
|
height: 2
|
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
|
|
|
|
};
|
|
|
|
|
|
|
|
const Button = React.memo(({ paused, onPress }) => (
|
|
|
|
<Touchable
|
|
|
|
style={styles.playPauseButton}
|
|
|
|
onPress={onPress}
|
|
|
|
hitSlop={BUTTON_HIT_SLOP}
|
|
|
|
background={Touchable.SelectableBackgroundBorderless()}
|
|
|
|
>
|
|
|
|
<CustomIcon name={paused ? 'play' : 'pause'} size={36} style={styles.playPauseImage} />
|
|
|
|
</Touchable>
|
|
|
|
));
|
|
|
|
|
|
|
|
Button.propTypes = {
|
|
|
|
paused: PropTypes.bool,
|
|
|
|
onPress: PropTypes.func
|
|
|
|
};
|
|
|
|
Button.displayName = 'MessageAudioButton';
|
2017-12-02 13:19:58 +00:00
|
|
|
|
2018-12-21 10:55:35 +00:00
|
|
|
export default 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-05-20 20:43:50 +00:00
|
|
|
useMarkdown: PropTypes.bool,
|
|
|
|
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;
|
|
|
|
const { file } = this.props;
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
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 {
|
2019-05-20 20:43:50 +00:00
|
|
|
user, baseUrl, file, getCustomEmoji, useMarkdown
|
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-05-20 20:43:50 +00:00
|
|
|
<React.Fragment>
|
|
|
|
<View style={styles.audioContainer}>
|
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}
|
|
|
|
/>
|
2019-05-20 20:43:50 +00:00
|
|
|
<Button paused={paused} onPress={this.togglePlayPause} />
|
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-03-29 19:36:07 +00:00
|
|
|
thumbTintColor={COLOR_PRIMARY}
|
|
|
|
minimumTrackTintColor={COLOR_PRIMARY}
|
2019-05-20 20:43:50 +00:00
|
|
|
onValueChange={this.onValueChange}
|
2018-09-11 16:32:52 +00:00
|
|
|
thumbStyle={styles.thumbStyle}
|
2019-04-24 18:36:29 +00:00
|
|
|
trackStyle={styles.trackStyle}
|
2018-09-11 16:32:52 +00:00
|
|
|
/>
|
2019-05-20 20:43:50 +00:00
|
|
|
<Text style={styles.duration}>{this.duration}</Text>
|
|
|
|
</View>
|
|
|
|
<Markdown msg={description} baseUrl={baseUrl} username={user.username} getCustomEmoji={getCustomEmoji} useMarkdown={useMarkdown} />
|
|
|
|
</React.Fragment>
|
2017-12-02 13:19:58 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|