From 991cdf1476256a232bc8cb67e720f881b633ce90 Mon Sep 17 00:00:00 2001 From: Reinaldo Neto Date: Mon, 15 May 2023 15:31:24 -0300 Subject: [PATCH] add the possibility to cancel image download and clear local images --- app/containers/message/Image.tsx | 16 +++++++++++++--- app/views/SettingsView/index.tsx | 1 + 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/app/containers/message/Image.tsx b/app/containers/message/Image.tsx index d428b7519..09eb15ed0 100644 --- a/app/containers/message/Image.tsx +++ b/app/containers/message/Image.tsx @@ -5,6 +5,7 @@ import { dequal } from 'dequal'; import { createImageProgress } from 'react-native-image-progress'; import * as Progress from 'react-native-progress'; import { BlurView } from '@react-native-community/blur'; +import * as FileSystem from 'expo-file-system'; import Touchable from './Touchable'; import Markdown from '../markdown'; @@ -98,6 +99,7 @@ const ImageContainer = React.memo( const { baseUrl, user } = useContext(MessageContext); const img = imageUrl || formatAttachmentUrl(file.image_url, user.id, user.token, baseUrl); const filePath = useRef(''); + const downloadResumable = useRef(null); useLayoutEffect(() => { const handleAutoDownload = async () => { @@ -129,7 +131,16 @@ const ImageContainer = React.memo( const handleDownload = async () => { setLoading(true); const imgUrl = imageUrl || formatAttachmentUrl(file.title_link || file.image_url, user.id, user.token, baseUrl); - const imageUri = await downloadMediaFile({ url: imgUrl, filePath: filePath.current }); + downloadResumable.current = FileSystem.createDownloadResumable(imgUrl, filePath.current); + const imageUri = await downloadMediaFile({ + url: imgUrl, + filePath: filePath.current, + downloadResumable: downloadResumable.current + }); + if (!imageUri) { + setLoading(false); + return setToDownload(true); + } file.title_link = imageUri; setToDownload(false); setLoading(false); @@ -137,8 +148,7 @@ const ImageContainer = React.memo( const onPress = () => { if (loading) { - // Cancelar o download - return; + return downloadResumable.current?.cancelAsync(); } if (toDownload && !loading) { diff --git a/app/views/SettingsView/index.tsx b/app/views/SettingsView/index.tsx index d39bdcf30..5f2f9e57d 100644 --- a/app/views/SettingsView/index.tsx +++ b/app/views/SettingsView/index.tsx @@ -99,6 +99,7 @@ const SettingsView = (): React.ReactElement => { confirmationText: I18n.t('Clear'), onPress: async () => { dispatch(appStart({ root: RootEnum.ROOT_LOADING, text: I18n.t('Clear_cache_loading') })); + await deleteAllSpecificMediaFiles(MediaTypes.image, server); await deleteAllSpecificMediaFiles(MediaTypes.audio, server); await clearCache({ server }); await FastImage.clearMemoryCache();