2021-11-16 15:59:58 +00:00
|
|
|
import React, { useContext, useState } from 'react';
|
2021-09-13 20:41:05 +00:00
|
|
|
import { StyleSheet } from 'react-native';
|
|
|
|
import { dequal } from 'dequal';
|
|
|
|
|
|
|
|
import Touchable from './Touchable';
|
|
|
|
import Markdown from '../markdown';
|
|
|
|
import { isIOS } from '../../utils/deviceInfo';
|
|
|
|
import { CustomIcon } from '../../lib/Icons';
|
|
|
|
import { formatAttachmentUrl } from '../../lib/utils';
|
|
|
|
import { themes } from '../../constants/colors';
|
|
|
|
import MessageContext from './Context';
|
2021-11-16 15:59:58 +00:00
|
|
|
import { fileDownload } from '../../utils/fileDownload';
|
|
|
|
import EventEmitter from '../../utils/events';
|
|
|
|
import { LISTENER } from '../Toast';
|
|
|
|
import I18n from '../../i18n';
|
|
|
|
import RCActivityIndicator from '../ActivityIndicator';
|
2021-09-13 20:41:05 +00:00
|
|
|
|
|
|
|
const SUPPORTED_TYPES = ['video/quicktime', 'video/mp4', ...(isIOS ? [] : ['video/3gp', 'video/mkv'])];
|
|
|
|
const isTypeSupported = (type: any) => SUPPORTED_TYPES.indexOf(type) !== -1;
|
|
|
|
|
|
|
|
const styles = StyleSheet.create({
|
|
|
|
button: {
|
|
|
|
flex: 1,
|
|
|
|
borderRadius: 4,
|
|
|
|
height: 150,
|
|
|
|
marginBottom: 6,
|
|
|
|
alignItems: 'center',
|
|
|
|
justifyContent: 'center'
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
interface IMessageVideo {
|
|
|
|
file: {
|
2021-11-16 15:59:58 +00:00
|
|
|
title: string;
|
|
|
|
title_link: string;
|
|
|
|
type: string;
|
2021-09-13 20:41:05 +00:00
|
|
|
video_type: string;
|
|
|
|
video_url: string;
|
|
|
|
description: string;
|
|
|
|
};
|
|
|
|
showAttachment: Function;
|
|
|
|
getCustomEmoji: Function;
|
|
|
|
theme: string;
|
|
|
|
}
|
|
|
|
|
|
|
|
const Video = React.memo(
|
|
|
|
({ file, showAttachment, getCustomEmoji, theme }: IMessageVideo) => {
|
|
|
|
const { baseUrl, user } = useContext(MessageContext);
|
2021-11-16 15:59:58 +00:00
|
|
|
const [loading, setLoading] = useState(false);
|
|
|
|
|
2021-09-13 20:41:05 +00:00
|
|
|
if (!baseUrl) {
|
|
|
|
return null;
|
|
|
|
}
|
2021-11-16 15:59:58 +00:00
|
|
|
const onPress = async () => {
|
2021-09-13 20:41:05 +00:00
|
|
|
if (isTypeSupported(file.video_type)) {
|
|
|
|
return showAttachment(file);
|
|
|
|
}
|
2021-11-16 15:59:58 +00:00
|
|
|
|
|
|
|
if (!isIOS) {
|
|
|
|
const uri = formatAttachmentUrl(file.video_url, user.id, user.token, baseUrl);
|
|
|
|
await downloadVideo(uri);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
EventEmitter.emit(LISTENER, { message: I18n.t('Unsupported_format') });
|
|
|
|
};
|
|
|
|
|
|
|
|
const downloadVideo = async (uri: string) => {
|
|
|
|
setLoading(true);
|
|
|
|
const fileDownloaded = await fileDownload(uri, file);
|
|
|
|
setLoading(false);
|
|
|
|
|
|
|
|
if (fileDownloaded) {
|
|
|
|
EventEmitter.emit(LISTENER, { message: I18n.t('saved_to_gallery') });
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
EventEmitter.emit(LISTENER, { message: I18n.t('error-save-video') });
|
2021-09-13 20:41:05 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
return (
|
|
|
|
<>
|
|
|
|
<Touchable
|
|
|
|
onPress={onPress}
|
|
|
|
style={[styles.button, { backgroundColor: themes[theme].videoBackground }]}
|
|
|
|
background={Touchable.Ripple(themes[theme].bannerBackground)}>
|
2021-11-16 15:59:58 +00:00
|
|
|
{loading ? (
|
|
|
|
<RCActivityIndicator theme={theme} />
|
|
|
|
) : (
|
|
|
|
<CustomIcon name='play-filled' size={54} color={themes[theme].buttonText} />
|
|
|
|
)}
|
2021-09-13 20:41:05 +00:00
|
|
|
</Touchable>
|
|
|
|
{/* @ts-ignore*/}
|
|
|
|
<Markdown
|
|
|
|
msg={file.description}
|
|
|
|
baseUrl={baseUrl}
|
|
|
|
username={user.username}
|
|
|
|
getCustomEmoji={getCustomEmoji}
|
|
|
|
theme={theme}
|
|
|
|
/>
|
|
|
|
</>
|
|
|
|
);
|
|
|
|
},
|
|
|
|
(prevProps, nextProps) => dequal(prevProps.file, nextProps.file) && prevProps.theme === nextProps.theme
|
|
|
|
);
|
|
|
|
|
|
|
|
export default Video;
|