add the possibility to cancel image download and clear local images

This commit is contained in:
Reinaldo Neto 2023-05-15 15:31:24 -03:00
parent cdad2884b9
commit 991cdf1476
2 changed files with 14 additions and 3 deletions

View File

@ -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<FileSystem.DownloadResumable | null>(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) {

View File

@ -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();