creation of isAutoDownloadEnabled, evaluate hist hook, Image Full Size preload done

This commit is contained in:
Reinaldo Neto 2023-05-11 20:20:48 -03:00
parent b73cf46829
commit 7fc975f63e
4 changed files with 59 additions and 12 deletions

View File

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

View File

@ -6,13 +6,12 @@ import moment from 'moment';
import { dequal } from 'dequal';
import { activateKeepAwake, deactivateKeepAwake } from 'expo-keep-awake';
import { Sound } from 'expo-av/build/Audio/Sound';
import NetInfo, { NetInfoStateType } from '@react-native-community/netinfo';
import Touchable from './Touchable';
import Markdown from '../markdown';
import { CustomIcon } from '../CustomIcon';
import sharedStyles from '../../views/Styles';
import { MediaDownloadOption, VIDEO_PREFERENCE_DOWNLOAD, themes } from '../../lib/constants';
import { themes } from '../../lib/constants';
import { isAndroid, isIOS } from '../../lib/methods/helpers';
import MessageContext from './Context';
import ActivityIndicator from '../ActivityIndicator';
@ -23,7 +22,7 @@ import { TSupportedThemes } from '../../theme';
import { downloadAudioFile, searchAudioFileAsync } from '../../lib/methods/audioFile';
import EventEmitter from '../../lib/methods/helpers/events';
import { PAUSE_AUDIO } from './constants';
import userPreferences from '../../lib/methods/userPreferences';
import { isAutoDownloadEnabled } from './helpers/mediaDownload/autoDownloadPreference';
interface IButton {
loading: boolean;
@ -173,13 +172,7 @@ class MessageAudio extends React.Component<IMessageAudioProps, IMessageAudioStat
return this.setState({ loading: false });
}
const audioDownloadPreference = userPreferences.getString(VIDEO_PREFERENCE_DOWNLOAD);
const netInfoState = await NetInfo.fetch();
const autoDownload =
(audioDownloadPreference === MediaDownloadOption.WIFI && netInfoState.type === NetInfoStateType.wifi) ||
audioDownloadPreference === MediaDownloadOption.WIFI_MOBILE_DATA ||
author?._id === user.id;
const autoDownload = await isAutoDownloadEnabled('audioPreferenceDownload', { author, user });
if (autoDownload) {
await this.startDownload();
}

View File

@ -11,9 +11,10 @@ import styles from './styles';
import { themes } from '../../lib/constants';
import MessageContext from './Context';
import { TGetCustomEmoji } from '../../definitions/IEmoji';
import { IAttachment } from '../../definitions';
import { IAttachment, IUserMessage } from '../../definitions';
import { TSupportedThemes, useTheme } from '../../theme';
import { formatAttachmentUrl } from '../../lib/methods/helpers/formatAttachmentUrl';
import { isAutoDownloadEnabled } from './helpers/mediaDownload/autoDownloadPreference';
interface IMessageButton {
children: React.ReactElement;
@ -29,6 +30,7 @@ interface IMessageImage {
style?: StyleProp<TextStyle>[];
isReply?: boolean;
getCustomEmoji?: TGetCustomEmoji;
author?: IUserMessage;
}
const ImageProgress = createImageProgress(FastImage);
@ -57,7 +59,7 @@ export const MessageImage = React.memo(({ imgUri, theme }: { imgUri: string; the
));
const ImageContainer = React.memo(
({ file, imageUrl, showAttachment, getCustomEmoji, style, isReply }: IMessageImage) => {
({ file, imageUrl, showAttachment, getCustomEmoji, style, isReply, author }: IMessageImage) => {
const { theme } = useTheme();
const { baseUrl, user } = useContext(MessageContext);
const img = imageUrl || formatAttachmentUrl(file.image_url, user.id, user.token, baseUrl);
@ -66,6 +68,14 @@ const ImageContainer = React.memo(
return null;
}
isAutoDownloadEnabled('imagesPreferenceDownload', { user, author }).then(result => {
if (result && file.title_link) {
// Since https://github.com/RocketChat/Rocket.Chat.ReactNative/pull/3370 the title_link is considered the full image
const imgBestQualityPreload = formatAttachmentUrl(file.title_link, user.id, user.token, baseUrl);
FastImage.preload([{ uri: imgBestQualityPreload }]);
}
});
const onPress = () => {
if (!showAttachment) {
return;

View File

@ -0,0 +1,43 @@
import NetInfo, { NetInfoStateType } from '@react-native-community/netinfo';
import { useEffect, useState } from 'react';
import {
IMAGES_PREFERENCE_DOWNLOAD,
AUDIO_PREFERENCE_DOWNLOAD,
VIDEO_PREFERENCE_DOWNLOAD,
MediaDownloadOption
} from '../../../../lib/constants';
import userPreferences from '../../../../lib/methods/userPreferences';
import { IUser, IUserMessage } from '../../../../definitions';
type TMediaType = typeof IMAGES_PREFERENCE_DOWNLOAD | typeof AUDIO_PREFERENCE_DOWNLOAD | typeof VIDEO_PREFERENCE_DOWNLOAD;
interface IUsersParam {
user: IUser;
author?: IUserMessage;
}
export const isAutoDownloadEnabled = async (mediaType: TMediaType, { author, user }: IUsersParam) => {
const mediaDownloadPreference = userPreferences.getString(mediaType);
const netInfoState = await NetInfo.fetch();
return (
(mediaDownloadPreference === MediaDownloadOption.WIFI && netInfoState.type === NetInfoStateType.wifi) ||
mediaDownloadPreference === MediaDownloadOption.WIFI_MOBILE_DATA ||
author?._id === user.id
);
};
export const useAutoDownloadEnabled = (mediaType: TMediaType, { author, user }: IUsersParam) => {
const [enabled, setEnabled] = useState(false);
useEffect(() => {
const handleAutoDownload = async () => {
const result = await isAutoDownloadEnabled(mediaType, { author, user });
console.log('🚀 ~ file: autoDownloadPreference.ts:38 ~ handleAutoDownload ~ result:', result);
setEnabled(result);
};
handleAutoDownload();
}, []);
return enabled;
};