refactor video

This commit is contained in:
Reinaldo Neto 2023-06-07 18:15:37 -03:00
parent b341528f10
commit b4a9b8af6e
3 changed files with 27 additions and 30 deletions

View File

@ -99,7 +99,6 @@ const Attachments: React.FC<IMessageAttachments> = React.memo(
getCustomEmoji={getCustomEmoji} getCustomEmoji={getCustomEmoji}
style={style} style={style}
isReply={isReply} isReply={isReply}
messageId={id}
/> />
); );
} }

View File

@ -123,8 +123,8 @@ const ImageContainer = React.memo(
const handleAutoDownload = async () => { const handleAutoDownload = async () => {
const isCurrentUserAuthor = author?._id === user.id; const isCurrentUserAuthor = author?._id === user.id;
const autoDownload = fetchAutoDownloadEnabled('imagesPreferenceDownload'); const isAutoDownloadEnabled = fetchAutoDownloadEnabled('imagesPreferenceDownload');
if (autoDownload || isCurrentUserAuthor) { if (isAutoDownloadEnabled || isCurrentUserAuthor) {
await handleDownload(); await handleDownload();
} }
}; };

View File

@ -19,13 +19,12 @@ import { useTheme } from '../../theme';
import { formatAttachmentUrl } from '../../lib/methods/helpers/formatAttachmentUrl'; import { formatAttachmentUrl } from '../../lib/methods/helpers/formatAttachmentUrl';
import { import {
LOCAL_DOCUMENT_PATH, LOCAL_DOCUMENT_PATH,
MediaTypes,
cancelDownload, cancelDownload,
downloadMediaFile, downloadMediaFile,
isDownloadActive, isDownloadActive,
searchMediaFileAsync searchMediaFileAsync
} from '../../lib/methods/handleMediaDownload'; } from '../../lib/methods/handleMediaDownload';
import { isAutoDownloadEnabled } from '../../lib/methods/autoDownloadPreference'; import { fetchAutoDownloadEnabled } from '../../lib/methods/autoDownloadPreference';
import sharedStyles from '../../views/Styles'; import sharedStyles from '../../views/Styles';
const SUPPORTED_TYPES = ['video/quicktime', 'video/mp4', ...(isIOS ? [] : ['video/3gp', 'video/mkv'])]; const SUPPORTED_TYPES = ['video/quicktime', 'video/mp4', ...(isIOS ? [] : ['video/3gp', 'video/mkv'])];
@ -57,12 +56,10 @@ interface IMessageVideo {
getCustomEmoji: TGetCustomEmoji; getCustomEmoji: TGetCustomEmoji;
style?: StyleProp<TextStyle>[]; style?: StyleProp<TextStyle>[];
isReply?: boolean; isReply?: boolean;
messageId: string;
} }
const DownloadIndicator = ({ handleCancelDownload }: { handleCancelDownload(): void }) => { const DownloadIndicator = ({ handleCancelDownload }: { handleCancelDownload(): void }) => {
const { colors } = useTheme(); const { colors } = useTheme();
return ( return (
<> <>
<View style={styles.cancelContainer}> <View style={styles.cancelContainer}>
@ -76,8 +73,8 @@ const DownloadIndicator = ({ handleCancelDownload }: { handleCancelDownload(): v
}; };
const Video = React.memo( const Video = React.memo(
({ file, showAttachment, getCustomEmoji, style, isReply, messageId }: IMessageVideo) => { ({ file, showAttachment, getCustomEmoji, style, isReply }: IMessageVideo) => {
const [newFile, setNewFile] = useState(file); const [videoCached, setVideoCached] = useState(file);
const [loading, setLoading] = useState(false); const [loading, setLoading] = useState(false);
const { baseUrl, user } = useContext(MessageContext); const { baseUrl, user } = useContext(MessageContext);
const { theme } = useTheme(); const { theme } = useTheme();
@ -85,51 +82,52 @@ const Video = React.memo(
const video = formatAttachmentUrl(file.video_url, user.id, user.token, baseUrl); const video = formatAttachmentUrl(file.video_url, user.id, user.token, baseUrl);
useEffect(() => { useEffect(() => {
const handleAutoDownload = async () => { const handleVideoSearchAndDownload = async () => {
if (video) { if (video) {
const searchVideoCached = await searchMediaFileAsync({ const searchVideoCached = await searchMediaFileAsync({
type: MediaTypes.video, type: 'video',
mimeType: file.video_type, mimeType: file.video_type,
messageId urlToCache: video
}); });
filePath.current = searchVideoCached.filePath; filePath.current = searchVideoCached.filePath;
const downloadActive = isDownloadActive(MediaTypes.video, messageId); const downloadActive = isDownloadActive('video', video);
if (searchVideoCached.file?.exists) { if (searchVideoCached.file?.exists) {
setNewFile(prev => ({ setVideoCached(prev => ({
...prev, ...prev,
video_url: searchVideoCached.file?.uri video_url: searchVideoCached.file?.uri
})); }));
if (downloadActive) { if (downloadActive) {
cancelDownload(MediaTypes.video, messageId); cancelDownload('video', video);
} }
return; return;
} }
if (downloadActive) return setLoading(true); if (downloadActive) return setLoading(true);
await handleAutoDownload();
const autoDownload = await isAutoDownloadEnabled('imagesPreferenceDownload');
if (autoDownload) {
await handleDownload();
}
} }
}; };
handleAutoDownload(); handleVideoSearchAndDownload();
}, []); }, []);
if (!baseUrl) { if (!baseUrl) {
return null; return null;
} }
const handleAutoDownload = async () => {
const isAutoDownloadEnabled = fetchAutoDownloadEnabled('imagesPreferenceDownload');
if (isAutoDownloadEnabled) {
await handleDownload();
}
};
const handleDownload = async () => { const handleDownload = async () => {
setLoading(true); setLoading(true);
try { try {
const videoUri = await downloadMediaFile({ const videoUri = await downloadMediaFile({
downloadUrl: video, downloadUrl: video,
mediaType: MediaTypes.video, mediaType: 'video',
messageId,
path: filePath.current path: filePath.current
}); });
setNewFile(prev => ({ setVideoCached(prev => ({
...prev, ...prev,
video_url: videoUri video_url: videoUri
})); }));
@ -140,13 +138,12 @@ const Video = React.memo(
const onPress = async () => { const onPress = async () => {
if (file.video_type && isTypeSupported(file.video_type) && showAttachment) { if (file.video_type && isTypeSupported(file.video_type) && showAttachment) {
if (!newFile.video_url?.startsWith(LOCAL_DOCUMENT_PATH) && !loading) { if (!videoCached.video_url?.startsWith(LOCAL_DOCUMENT_PATH) && !loading) {
// Keep the video downloading while showing the video buffering // Keep the video downloading while showing the video buffering
handleDownload(); handleDownload();
} }
return showAttachment(newFile); return showAttachment(videoCached);
} }
if (!isIOS && file.video_url) { if (!isIOS && file.video_url) {
await downloadVideoToGallery(video); await downloadVideoToGallery(video);
return; return;
@ -156,7 +153,7 @@ const Video = React.memo(
const handleCancelDownload = () => { const handleCancelDownload = () => {
if (loading) { if (loading) {
cancelDownload(MediaTypes.video, messageId); cancelDownload('video', video);
return setLoading(false); return setLoading(false);
} }
}; };
@ -186,7 +183,8 @@ const Video = React.memo(
disabled={isReply} disabled={isReply}
onPress={onPress} onPress={onPress}
style={[styles.button, { backgroundColor: themes[theme].videoBackground }]} style={[styles.button, { backgroundColor: themes[theme].videoBackground }]}
background={Touchable.Ripple(themes[theme].bannerBackground)}> background={Touchable.Ripple(themes[theme].bannerBackground)}
>
{loading ? ( {loading ? (
<DownloadIndicator handleCancelDownload={handleCancelDownload} /> <DownloadIndicator handleCancelDownload={handleCancelDownload} />
) : ( ) : (