Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: crated thumbnail using expo-video-thumbnail #5893

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
119 changes: 78 additions & 41 deletions app/containers/message/Components/Attachments/Video.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React, { useContext } from 'react';
import { StyleProp, StyleSheet, Text, TextStyle, View } from 'react-native';
import React, { useContext, useEffect, useState } from 'react';
import { StyleProp, StyleSheet, TextStyle, View } from 'react-native';

import { IUserMessage } from '../../../../definitions';
import { IAttachment } from '../../../../definitions/IAttachment';
Expand All @@ -9,31 +9,96 @@ import { fileDownload, isIOS } from '../../../../lib/methods/helpers';
import EventEmitter from '../../../../lib/methods/helpers/events';
import { useTheme } from '../../../../theme';
import sharedStyles from '../../../../views/Styles';
import { TIconsName } from '../../../CustomIcon';
import { LISTENER } from '../../../Toast';
import Markdown from '../../../markdown';
import MessageContext from '../../Context';
import Touchable from '../../Touchable';
import { useMediaAutoDownload } from '../../hooks/useMediaAutoDownload';
import BlurComponent from '../OverlayComponent';
import { TDownloadState } from '../../../../lib/methods/handleMediaDownload';
import messageStyles from '../../styles';
import { getThumbnailAsync } from 'expo-video-thumbnails';
import OverlayComponent from '../OverlayComponent';
import FastImage from 'react-native-fast-image';
import { CustomIcon } from '../../../CustomIcon';

const SUPPORTED_TYPES = ['video/quicktime', 'video/mp4', ...(isIOS ? [] : ['video/3gp', 'video/mkv'])];
const isTypeSupported = (type: string) => SUPPORTED_TYPES.indexOf(type) !== -1;

const styles = StyleSheet.create({
cancelContainer: {
position: 'absolute',
top: 8,
right: 8
container: {
flex: 1,
alignItems: 'center',
justifyContent: 'center'
},
overlay: {
flex: 1
},
image: {
width: '100%',
height: '100%'
},
text: {
...sharedStyles.textRegular,
fontSize: 12
playerIcon: {
position: 'absolute',
shadowColor: '#000',
shadowOpacity: 0.3,
shadowOffset: {
width: 1,
height: 1
}
}
});

type Image = {
loading: boolean;
uri: string | null;
};

type ThumbnailProps = {
url: string;
encrypted?: boolean;
};

const Thumbnail = ({ url, encrypted = false }: ThumbnailProps) => {
const icon = encrypted ? 'encrypted' : 'play-filled';

const [image, setImage] = useState<Image>({
loading: true,
uri: null
});

const generateThumbnail = async () => {
try {
if (!url) return;

const { uri } = await getThumbnailAsync(url, {
time: 1
});
setImage({
loading: false,
uri
});
} catch (e) {
console.warn(e);
}
};

useEffect(() => {
generateThumbnail();
}, [url]);

return (
<View style={styles.container}>
{image.loading || !image.uri ? (
<OverlayComponent style={styles.overlay} loading={image.loading} iconName='arrow-down-circle' />
) : (
<>
<FastImage style={styles.image} source={{ uri: image.uri }} />
<CustomIcon name={icon} size={54} color='#fff' style={styles.playerIcon} />
</>
)}
</View>
);
};

interface IMessageVideo {
file: IAttachment;
showAttachment?: (file: IAttachment) => void;
Expand All @@ -44,34 +109,6 @@ interface IMessageVideo {
msg?: string;
}

const CancelIndicator = () => {
const { colors } = useTheme();
return (
<View style={styles.cancelContainer}>
<Text style={[styles.text, { color: colors.fontSecondaryInfo }]}>{I18n.t('Cancel')}</Text>
</View>
);
};

const Thumbnail = ({ status, encrypted = false }: { status: TDownloadState; encrypted: boolean }) => {
const { colors } = useTheme();
let icon: TIconsName = status === 'downloaded' ? 'play-filled' : 'arrow-down-circle';
if (encrypted && status === 'downloaded') {
icon = 'encrypted';
}

return (
<>
<BlurComponent
iconName={icon}
loading={status === 'loading'}
style={[messageStyles.image, { borderColor: colors.strokeLight, borderWidth: 1 }]}
/>
{status === 'loading' ? <CancelIndicator /> : null}
</>
);
};

const Video = ({
file,
showAttachment,
Expand Down Expand Up @@ -112,7 +149,7 @@ const Video = ({
<>
<Markdown msg={msg} username={user.username} getCustomEmoji={getCustomEmoji} style={[isReply && style]} theme={theme} />
<Touchable onPress={_onPress} style={messageStyles.image} background={Touchable.Ripple(colors.surfaceNeutral)}>
<Thumbnail status={status} encrypted={isEncrypted} />
<Thumbnail url={url} encrypted={isEncrypted} />
</Touchable>
</>
);
Expand Down