2017-12-02 13:19:58 +00:00
|
|
|
import React from 'react';
|
2022-03-23 14:49:45 +00:00
|
|
|
import { StyleProp, StyleSheet, Text, TextStyle, View } from 'react-native';
|
2022-04-01 21:52:38 +00:00
|
|
|
import { Audio, AVPlaybackStatus } from 'expo-av';
|
2019-10-04 13:38:35 +00:00
|
|
|
import Slider from '@react-native-community/slider';
|
2018-09-11 16:32:52 +00:00
|
|
|
import moment from 'moment';
|
2021-02-26 16:01:45 +00:00
|
|
|
import { dequal } from 'dequal';
|
2020-05-08 12:55:26 +00:00
|
|
|
import { activateKeepAwake, deactivateKeepAwake } from 'expo-keep-awake';
|
2022-04-01 21:52:38 +00:00
|
|
|
import { Sound } from 'expo-av/build/Audio/Sound';
|
2018-09-11 16:32:52 +00:00
|
|
|
|
2020-04-30 20:05:59 +00:00
|
|
|
import Touchable from './Touchable';
|
2019-08-27 12:25:38 +00:00
|
|
|
import Markdown from '../markdown';
|
2022-05-02 19:21:15 +00:00
|
|
|
import { CustomIcon } from '../CustomIcon';
|
2019-03-29 19:36:07 +00:00
|
|
|
import sharedStyles from '../../views/Styles';
|
2022-04-07 14:10:03 +00:00
|
|
|
import { themes } from '../../lib/constants';
|
2022-06-06 14:17:51 +00:00
|
|
|
import { isAndroid, isIOS } from '../../lib/methods/helpers';
|
2020-04-30 20:05:59 +00:00
|
|
|
import MessageContext from './Context';
|
2020-04-30 18:54:27 +00:00
|
|
|
import ActivityIndicator from '../ActivityIndicator';
|
2020-06-17 17:35:58 +00:00
|
|
|
import { withDimensions } from '../../dimensions';
|
2022-02-17 15:27:01 +00:00
|
|
|
import { TGetCustomEmoji } from '../../definitions/IEmoji';
|
2022-03-21 20:44:06 +00:00
|
|
|
import { IAttachment } from '../../definitions';
|
2022-04-12 16:27:05 +00:00
|
|
|
import { TSupportedThemes } from '../../theme';
|
2020-04-30 18:54:27 +00:00
|
|
|
|
2021-09-13 20:41:05 +00:00
|
|
|
interface IButton {
|
|
|
|
loading: boolean;
|
|
|
|
paused: boolean;
|
2022-04-12 16:27:05 +00:00
|
|
|
theme: TSupportedThemes;
|
2022-03-21 20:44:06 +00:00
|
|
|
disabled?: boolean;
|
2022-04-01 21:52:38 +00:00
|
|
|
onPress: () => void;
|
2021-09-13 20:41:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
interface IMessageAudioProps {
|
2022-03-21 20:44:06 +00:00
|
|
|
file: IAttachment;
|
|
|
|
isReply?: boolean;
|
|
|
|
style?: StyleProp<TextStyle>[];
|
2022-04-12 16:27:05 +00:00
|
|
|
theme: TSupportedThemes;
|
2022-02-17 15:27:01 +00:00
|
|
|
getCustomEmoji: TGetCustomEmoji;
|
2022-01-17 16:10:39 +00:00
|
|
|
scale?: number;
|
2021-09-13 20:41:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
interface IMessageAudioState {
|
|
|
|
loading: boolean;
|
|
|
|
currentTime: number;
|
|
|
|
duration: number;
|
|
|
|
paused: boolean;
|
|
|
|
}
|
|
|
|
|
2020-04-30 18:54:27 +00:00
|
|
|
const mode = {
|
|
|
|
allowsRecordingIOS: false,
|
|
|
|
playsInSilentModeIOS: true,
|
2021-09-21 14:47:00 +00:00
|
|
|
staysActiveInBackground: true,
|
2020-04-30 18:54:27 +00:00
|
|
|
shouldDuckAndroid: true,
|
|
|
|
playThroughEarpieceAndroid: false,
|
|
|
|
interruptionModeIOS: Audio.INTERRUPTION_MODE_IOS_DO_NOT_MIX,
|
|
|
|
interruptionModeAndroid: Audio.INTERRUPTION_MODE_ANDROID_DO_NOT_MIX
|
|
|
|
};
|
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'
|
|
|
|
},
|
2020-04-30 18:54:27 +00:00
|
|
|
audioLoading: {
|
|
|
|
marginHorizontal: 8
|
|
|
|
},
|
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
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2021-09-13 20:41:05 +00:00
|
|
|
const formatTime = (seconds: number) => moment.utc(seconds * 1000).format('mm:ss');
|
|
|
|
|
|
|
|
const BUTTON_HIT_SLOP = { top: 12, right: 12, bottom: 12, left: 12 };
|
|
|
|
|
2022-03-21 20:44:06 +00:00
|
|
|
const Button = React.memo(({ loading, paused, onPress, disabled, theme }: IButton) => (
|
2019-05-20 20:43:50 +00:00
|
|
|
<Touchable
|
|
|
|
style={styles.playPauseButton}
|
2022-03-21 20:44:06 +00:00
|
|
|
disabled={disabled}
|
2019-05-20 20:43:50 +00:00
|
|
|
onPress={onPress}
|
|
|
|
hitSlop={BUTTON_HIT_SLOP}
|
2021-09-13 20:41:05 +00:00
|
|
|
background={Touchable.SelectableBackgroundBorderless()}>
|
|
|
|
{loading ? (
|
2022-03-18 02:37:10 +00:00
|
|
|
<ActivityIndicator style={[styles.playPauseButton, styles.audioLoading]} />
|
2021-09-13 20:41:05 +00:00
|
|
|
) : (
|
2022-03-21 20:44:06 +00:00
|
|
|
<CustomIcon
|
|
|
|
name={paused ? 'play-filled' : 'pause-filled'}
|
|
|
|
size={36}
|
|
|
|
color={disabled ? themes[theme].tintDisabled : themes[theme].tintColor}
|
|
|
|
/>
|
2021-09-13 20:41:05 +00:00
|
|
|
)}
|
2019-05-20 20:43:50 +00:00
|
|
|
</Touchable>
|
|
|
|
));
|
|
|
|
|
|
|
|
Button.displayName = 'MessageAudioButton';
|
2017-12-02 13:19:58 +00:00
|
|
|
|
2021-09-13 20:41:05 +00:00
|
|
|
class MessageAudio extends React.Component<IMessageAudioProps, IMessageAudioState> {
|
2020-04-30 20:05:59 +00:00
|
|
|
static contextType = MessageContext;
|
|
|
|
|
2022-04-01 21:52:38 +00:00
|
|
|
private sound: Sound;
|
2017-12-02 13:19:58 +00:00
|
|
|
|
2021-09-13 20:41:05 +00:00
|
|
|
constructor(props: IMessageAudioProps) {
|
2017-12-02 13:19:58 +00:00
|
|
|
super(props);
|
|
|
|
this.state = {
|
2020-04-30 18:54:27 +00:00
|
|
|
loading: false,
|
2017-12-02 13:19:58 +00:00
|
|
|
currentTime: 0,
|
|
|
|
duration: 0,
|
2020-04-30 20:05:59 +00:00
|
|
|
paused: true
|
2017-12-02 13:19:58 +00:00
|
|
|
};
|
2020-04-30 18:54:27 +00:00
|
|
|
|
|
|
|
this.sound = new Audio.Sound();
|
|
|
|
this.sound.setOnPlaybackStatusUpdate(this.onPlaybackStatusUpdate);
|
|
|
|
}
|
|
|
|
|
|
|
|
async componentDidMount() {
|
2020-04-30 20:05:59 +00:00
|
|
|
const { file } = this.props;
|
|
|
|
const { baseUrl, user } = this.context;
|
2020-04-30 18:54:27 +00:00
|
|
|
|
2020-05-08 17:07:58 +00:00
|
|
|
let url = file.audio_url;
|
2022-03-21 20:44:06 +00:00
|
|
|
if (url && !url.startsWith('http')) {
|
2021-09-13 20:41:05 +00:00
|
|
|
url = `${baseUrl}${file.audio_url}`;
|
2020-05-08 17:07:58 +00:00
|
|
|
}
|
|
|
|
|
2020-04-30 18:54:27 +00:00
|
|
|
this.setState({ loading: true });
|
|
|
|
try {
|
2021-09-13 20:41:05 +00:00
|
|
|
await this.sound.loadAsync({ uri: `${url}?rc_uid=${user.id}&rc_token=${user.token}` });
|
2020-04-30 18:54:27 +00:00
|
|
|
} catch {
|
|
|
|
// Do nothing
|
|
|
|
}
|
|
|
|
this.setState({ loading: false });
|
2017-12-02 13:19:58 +00:00
|
|
|
}
|
|
|
|
|
2022-04-01 21:52:38 +00:00
|
|
|
shouldComponentUpdate(nextProps: IMessageAudioProps, nextState: IMessageAudioState) {
|
2021-09-13 20:41:05 +00:00
|
|
|
const { currentTime, duration, paused, loading } = this.state;
|
2020-06-15 14:00:46 +00:00
|
|
|
const { file, theme } = this.props;
|
2019-12-04 16:39:53 +00:00
|
|
|
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;
|
|
|
|
}
|
2021-02-26 16:01:45 +00:00
|
|
|
if (!dequal(nextProps.file, file)) {
|
2018-12-21 10:55:35 +00:00
|
|
|
return true;
|
|
|
|
}
|
2020-04-30 18:54:27 +00:00
|
|
|
if (nextState.loading !== loading) {
|
|
|
|
return true;
|
|
|
|
}
|
2018-12-21 10:55:35 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-05-08 12:55:26 +00:00
|
|
|
componentDidUpdate() {
|
|
|
|
const { paused } = this.state;
|
|
|
|
if (paused) {
|
|
|
|
deactivateKeepAwake();
|
|
|
|
} else {
|
|
|
|
activateKeepAwake();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-30 18:54:27 +00:00
|
|
|
async componentWillUnmount() {
|
|
|
|
try {
|
|
|
|
await this.sound.stopAsync();
|
|
|
|
} catch {
|
|
|
|
// Do nothing
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-01 21:52:38 +00:00
|
|
|
onPlaybackStatusUpdate = (status: AVPlaybackStatus) => {
|
2020-04-30 18:54:27 +00:00
|
|
|
if (status) {
|
|
|
|
this.onLoad(status);
|
|
|
|
this.onProgress(status);
|
|
|
|
this.onEnd(status);
|
|
|
|
}
|
2021-09-13 20:41:05 +00:00
|
|
|
};
|
2020-04-30 18:54:27 +00:00
|
|
|
|
2022-04-01 21:52:38 +00:00
|
|
|
onLoad = (data: AVPlaybackStatus) => {
|
|
|
|
if (data.isLoaded && data.durationMillis) {
|
|
|
|
const duration = data.durationMillis / 1000;
|
|
|
|
this.setState({ duration: duration > 0 ? duration : 0 });
|
|
|
|
}
|
2021-09-13 20:41:05 +00:00
|
|
|
};
|
2017-12-02 13:19:58 +00:00
|
|
|
|
2022-04-01 21:52:38 +00:00
|
|
|
onProgress = (data: AVPlaybackStatus) => {
|
|
|
|
if (data.isLoaded) {
|
|
|
|
const { duration } = this.state;
|
|
|
|
const currentTime = data.positionMillis / 1000;
|
|
|
|
if (currentTime <= duration) {
|
|
|
|
this.setState({ currentTime });
|
|
|
|
}
|
2017-12-02 13:19:58 +00:00
|
|
|
}
|
2021-09-13 20:41:05 +00:00
|
|
|
};
|
2017-12-02 13:19:58 +00:00
|
|
|
|
2022-04-01 21:52:38 +00:00
|
|
|
onEnd = async (data: AVPlaybackStatus) => {
|
|
|
|
if (data.isLoaded) {
|
|
|
|
if (data.didJustFinish) {
|
|
|
|
try {
|
|
|
|
await this.sound.stopAsync();
|
|
|
|
this.setState({ paused: true, currentTime: 0 });
|
|
|
|
} catch {
|
|
|
|
// do nothing
|
|
|
|
}
|
2020-04-30 18:54:27 +00:00
|
|
|
}
|
|
|
|
}
|
2021-09-13 20:41:05 +00:00
|
|
|
};
|
2017-12-02 13:19:58 +00:00
|
|
|
|
2019-05-20 20:43:50 +00:00
|
|
|
get duration() {
|
2020-04-30 18:54:27 +00:00
|
|
|
const { currentTime, duration } = this.state;
|
|
|
|
return formatTime(currentTime || duration);
|
2017-12-02 13:19:58 +00:00
|
|
|
}
|
|
|
|
|
2019-04-24 18:36:29 +00:00
|
|
|
togglePlayPause = () => {
|
2018-09-25 19:28:42 +00:00
|
|
|
const { paused } = this.state;
|
2020-04-30 18:54:27 +00:00
|
|
|
this.setState({ paused: !paused }, this.playPause);
|
2021-09-13 20:41:05 +00:00
|
|
|
};
|
2017-12-02 13:19:58 +00:00
|
|
|
|
2021-09-13 20:41:05 +00:00
|
|
|
playPause = async () => {
|
2020-04-30 18:54:27 +00:00
|
|
|
const { paused } = this.state;
|
|
|
|
try {
|
|
|
|
if (paused) {
|
|
|
|
await this.sound.pauseAsync();
|
|
|
|
} else {
|
2020-07-06 19:23:46 +00:00
|
|
|
await Audio.setAudioModeAsync(mode);
|
2020-04-30 18:54:27 +00:00
|
|
|
await this.sound.playAsync();
|
|
|
|
}
|
|
|
|
} catch {
|
|
|
|
// Do nothing
|
|
|
|
}
|
2021-09-13 20:41:05 +00:00
|
|
|
};
|
2020-04-30 18:54:27 +00:00
|
|
|
|
2022-04-01 21:52:38 +00:00
|
|
|
onValueChange = async (value: number) => {
|
2020-04-30 18:54:27 +00:00
|
|
|
try {
|
|
|
|
this.setState({ currentTime: value });
|
|
|
|
await this.sound.setPositionAsync(value * 1000);
|
|
|
|
} catch {
|
|
|
|
// Do nothing
|
|
|
|
}
|
2021-09-13 20:41:05 +00:00
|
|
|
};
|
2019-05-20 20:43:50 +00:00
|
|
|
|
2017-12-02 13:19:58 +00:00
|
|
|
render() {
|
2021-09-13 20:41:05 +00:00
|
|
|
const { loading, paused, currentTime, duration } = this.state;
|
2022-03-21 20:44:06 +00:00
|
|
|
const { file, getCustomEmoji, theme, scale, isReply, style } = this.props;
|
2018-09-11 16:32:52 +00:00
|
|
|
const { description } = file;
|
2020-04-30 20:05:59 +00:00
|
|
|
const { baseUrl, user } = this.context;
|
2018-09-14 19:39:52 +00:00
|
|
|
|
|
|
|
if (!baseUrl) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2022-04-12 16:27:05 +00:00
|
|
|
let thumbColor;
|
|
|
|
if (isAndroid && isReply) {
|
|
|
|
thumbColor = themes[theme].tintDisabled;
|
|
|
|
} else if (isAndroid) {
|
|
|
|
thumbColor = themes[theme].tintColor;
|
|
|
|
}
|
|
|
|
|
2017-12-02 13:19:58 +00:00
|
|
|
return (
|
2019-09-24 20:26:56 +00:00
|
|
|
<>
|
2022-03-21 20:44:06 +00:00
|
|
|
<Markdown
|
|
|
|
msg={description}
|
|
|
|
style={[isReply && style]}
|
|
|
|
baseUrl={baseUrl}
|
|
|
|
username={user.username}
|
|
|
|
getCustomEmoji={getCustomEmoji}
|
2022-07-04 18:10:14 +00:00
|
|
|
theme={theme}
|
2022-03-21 20:44:06 +00:00
|
|
|
/>
|
2019-12-04 16:39:53 +00:00
|
|
|
<View
|
|
|
|
style={[
|
|
|
|
styles.audioContainer,
|
2020-06-15 14:00:46 +00:00
|
|
|
{ backgroundColor: themes[theme].chatComponentBackground, borderColor: themes[theme].borderColor }
|
2021-09-13 20:41:05 +00:00
|
|
|
]}>
|
2022-03-21 20:44:06 +00:00
|
|
|
<Button disabled={isReply} loading={loading} paused={paused} onPress={this.togglePlayPause} theme={theme} />
|
2018-09-11 16:32:52 +00:00
|
|
|
<Slider
|
2022-03-21 20:44:06 +00:00
|
|
|
disabled={isReply}
|
2018-09-11 16:32:52 +00:00
|
|
|
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}
|
2022-04-12 16:27:05 +00:00
|
|
|
thumbTintColor={thumbColor}
|
2019-12-04 16:39:53 +00:00
|
|
|
minimumTrackTintColor={themes[theme].tintColor}
|
|
|
|
maximumTrackTintColor={themes[theme].auxiliaryText}
|
2019-05-20 20:43:50 +00:00
|
|
|
onValueChange={this.onValueChange}
|
2022-03-21 20:44:06 +00:00
|
|
|
thumbImage={isIOS ? { uri: 'audio_thumb', scale } : undefined}
|
2018-09-11 16:32:52 +00:00
|
|
|
/>
|
2019-12-04 16:39:53 +00:00
|
|
|
<Text style={[styles.duration, { color: themes[theme].auxiliaryText }]}>{this.duration}</Text>
|
2019-05-20 20:43:50 +00:00
|
|
|
</View>
|
2019-09-24 20:26:56 +00:00
|
|
|
</>
|
2017-12-02 13:19:58 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
2019-11-25 20:01:17 +00:00
|
|
|
|
2020-06-17 17:35:58 +00:00
|
|
|
export default withDimensions(MessageAudio);
|