add the possibility to cancel image download and clear local images
This commit is contained in:
parent
cdad2884b9
commit
991cdf1476
|
@ -5,6 +5,7 @@ import { dequal } from 'dequal';
|
||||||
import { createImageProgress } from 'react-native-image-progress';
|
import { createImageProgress } from 'react-native-image-progress';
|
||||||
import * as Progress from 'react-native-progress';
|
import * as Progress from 'react-native-progress';
|
||||||
import { BlurView } from '@react-native-community/blur';
|
import { BlurView } from '@react-native-community/blur';
|
||||||
|
import * as FileSystem from 'expo-file-system';
|
||||||
|
|
||||||
import Touchable from './Touchable';
|
import Touchable from './Touchable';
|
||||||
import Markdown from '../markdown';
|
import Markdown from '../markdown';
|
||||||
|
@ -98,6 +99,7 @@ const ImageContainer = React.memo(
|
||||||
const { baseUrl, user } = useContext(MessageContext);
|
const { baseUrl, user } = useContext(MessageContext);
|
||||||
const img = imageUrl || formatAttachmentUrl(file.image_url, user.id, user.token, baseUrl);
|
const img = imageUrl || formatAttachmentUrl(file.image_url, user.id, user.token, baseUrl);
|
||||||
const filePath = useRef('');
|
const filePath = useRef('');
|
||||||
|
const downloadResumable = useRef<FileSystem.DownloadResumable | null>(null);
|
||||||
|
|
||||||
useLayoutEffect(() => {
|
useLayoutEffect(() => {
|
||||||
const handleAutoDownload = async () => {
|
const handleAutoDownload = async () => {
|
||||||
|
@ -129,7 +131,16 @@ const ImageContainer = React.memo(
|
||||||
const handleDownload = async () => {
|
const handleDownload = async () => {
|
||||||
setLoading(true);
|
setLoading(true);
|
||||||
const imgUrl = imageUrl || formatAttachmentUrl(file.title_link || file.image_url, user.id, user.token, baseUrl);
|
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;
|
file.title_link = imageUri;
|
||||||
setToDownload(false);
|
setToDownload(false);
|
||||||
setLoading(false);
|
setLoading(false);
|
||||||
|
@ -137,8 +148,7 @@ const ImageContainer = React.memo(
|
||||||
|
|
||||||
const onPress = () => {
|
const onPress = () => {
|
||||||
if (loading) {
|
if (loading) {
|
||||||
// Cancelar o download
|
return downloadResumable.current?.cancelAsync();
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (toDownload && !loading) {
|
if (toDownload && !loading) {
|
||||||
|
|
|
@ -99,6 +99,7 @@ const SettingsView = (): React.ReactElement => {
|
||||||
confirmationText: I18n.t('Clear'),
|
confirmationText: I18n.t('Clear'),
|
||||||
onPress: async () => {
|
onPress: async () => {
|
||||||
dispatch(appStart({ root: RootEnum.ROOT_LOADING, text: I18n.t('Clear_cache_loading') }));
|
dispatch(appStart({ root: RootEnum.ROOT_LOADING, text: I18n.t('Clear_cache_loading') }));
|
||||||
|
await deleteAllSpecificMediaFiles(MediaTypes.image, server);
|
||||||
await deleteAllSpecificMediaFiles(MediaTypes.audio, server);
|
await deleteAllSpecificMediaFiles(MediaTypes.audio, server);
|
||||||
await clearCache({ server });
|
await clearCache({ server });
|
||||||
await FastImage.clearMemoryCache();
|
await FastImage.clearMemoryCache();
|
||||||
|
|
Loading…
Reference in New Issue