From 7fc975f63e16aa2a57c234cebb677b43ab5c9467 Mon Sep 17 00:00:00 2001 From: Reinaldo Neto Date: Thu, 11 May 2023 20:20:48 -0300 Subject: [PATCH] creation of isAutoDownloadEnabled, evaluate hist hook, Image Full Size preload done --- app/containers/message/Attachments.tsx | 1 + app/containers/message/Audio.tsx | 13 ++---- app/containers/message/Image.tsx | 14 +++++- .../mediaDownload/autoDownloadPreference.ts | 43 +++++++++++++++++++ 4 files changed, 59 insertions(+), 12 deletions(-) create mode 100644 app/containers/message/helpers/mediaDownload/autoDownloadPreference.ts diff --git a/app/containers/message/Attachments.tsx b/app/containers/message/Attachments.tsx index cd5e3bb01..479d3b1a7 100644 --- a/app/containers/message/Attachments.tsx +++ b/app/containers/message/Attachments.tsx @@ -71,6 +71,7 @@ const Attachments: React.FC = React.memo( getCustomEmoji={getCustomEmoji} style={style} isReply={isReply} + author={author} /> ); } diff --git a/app/containers/message/Audio.tsx b/app/containers/message/Audio.tsx index 0915a4e47..46443f73d 100644 --- a/app/containers/message/Audio.tsx +++ b/app/containers/message/Audio.tsx @@ -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[]; 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; diff --git a/app/containers/message/helpers/mediaDownload/autoDownloadPreference.ts b/app/containers/message/helpers/mediaDownload/autoDownloadPreference.ts new file mode 100644 index 000000000..d096fb41a --- /dev/null +++ b/app/containers/message/helpers/mediaDownload/autoDownloadPreference.ts @@ -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; +};