refactor some changes requested

This commit is contained in:
Reinaldo Neto 2023-05-30 11:07:34 -03:00
parent 67ba3131e3
commit 1dc038d321
7 changed files with 27 additions and 32 deletions

View File

@ -22,7 +22,7 @@ import { TSupportedThemes } from '../../theme';
import { MediaTypes, downloadMediaFile, searchMediaFileAsync } from '../../lib/methods/handleMediaDownload'; import { MediaTypes, downloadMediaFile, searchMediaFileAsync } from '../../lib/methods/handleMediaDownload';
import EventEmitter from '../../lib/methods/helpers/events'; import EventEmitter from '../../lib/methods/helpers/events';
import { PAUSE_AUDIO } from './constants'; import { PAUSE_AUDIO } from './constants';
import { isAutoDownloadEnabled } from './helpers/mediaDownload/autoDownloadPreference'; import { isAutoDownloadEnabled } from '../../lib/methods/autoDownloadPreference';
interface IButton { interface IButton {
loading: boolean; loading: boolean;
@ -107,8 +107,7 @@ const Button = React.memo(({ loading, paused, onPress, disabled, theme, toDownlo
disabled={disabled} disabled={disabled}
onPress={onPress} onPress={onPress}
hitSlop={BUTTON_HIT_SLOP} hitSlop={BUTTON_HIT_SLOP}
background={Touchable.SelectableBackgroundBorderless()} background={Touchable.SelectableBackgroundBorderless()}>
>
{loading ? ( {loading ? (
<ActivityIndicator style={[styles.playPauseButton, styles.audioLoading]} /> <ActivityIndicator style={[styles.playPauseButton, styles.audioLoading]} />
) : ( ) : (
@ -180,7 +179,8 @@ class MessageAudio extends React.Component<IMessageAudioProps, IMessageAudioStat
if (url) { if (url) {
const autoDownload = await isAutoDownloadEnabled('audioPreferenceDownload', { author, user }); const autoDownload = await isAutoDownloadEnabled('audioPreferenceDownload', { author, user });
if (autoDownload) { if (autoDownload) {
return await this.startDownload(); await this.startDownload();
return;
} }
// MediaDownloadOption.NEVER or MediaDownloadOption.WIFI and the mobile is using mobile data // MediaDownloadOption.NEVER or MediaDownloadOption.WIFI and the mobile is using mobile data
@ -370,8 +370,7 @@ class MessageAudio extends React.Component<IMessageAudioProps, IMessageAudioStat
style={[ style={[
styles.audioContainer, styles.audioContainer,
{ backgroundColor: themes[theme].chatComponentBackground, borderColor: themes[theme].borderColor } { backgroundColor: themes[theme].chatComponentBackground, borderColor: themes[theme].borderColor }
]} ]}>
>
<Button <Button
disabled={isReply} disabled={isReply}
loading={loading} loading={loading}

View File

@ -22,7 +22,7 @@ import {
isDownloadActive, isDownloadActive,
searchMediaFileAsync searchMediaFileAsync
} from '../../lib/methods/handleMediaDownload'; } from '../../lib/methods/handleMediaDownload';
import { isAutoDownloadEnabled } from './helpers/mediaDownload/autoDownloadPreference'; import { isAutoDownloadEnabled } from '../../lib/methods/autoDownloadPreference';
import RCActivityIndicator from '../ActivityIndicator'; import RCActivityIndicator from '../ActivityIndicator';
import { CustomIcon } from '../CustomIcon'; import { CustomIcon } from '../CustomIcon';

View File

@ -25,7 +25,7 @@ import {
isDownloadActive, isDownloadActive,
searchMediaFileAsync searchMediaFileAsync
} from '../../lib/methods/handleMediaDownload'; } from '../../lib/methods/handleMediaDownload';
import { isAutoDownloadEnabled } from './helpers/mediaDownload/autoDownloadPreference'; import { isAutoDownloadEnabled } from '../../lib/methods/autoDownloadPreference';
import sharedStyles from '../../views/Styles'; import sharedStyles from '../../views/Styles';
const SUPPORTED_TYPES = ['video/quicktime', 'video/mp4', ...(isIOS ? [] : ['video/3gp', 'video/mkv'])]; const SUPPORTED_TYPES = ['video/quicktime', 'video/mp4', ...(isIOS ? [] : ['video/3gp', 'video/mkv'])];
@ -107,8 +107,7 @@ const Video = React.memo(
if (downloadActive) return setLoading(true); if (downloadActive) return setLoading(true);
// We don't pass the author to avoid auto-download what the user sent const autoDownload = await isAutoDownloadEnabled('imagesPreferenceDownload');
const autoDownload = await isAutoDownloadEnabled('imagesPreferenceDownload', { user });
if (autoDownload) { if (autoDownload) {
await handleDownload(); await handleDownload();
} }
@ -187,8 +186,7 @@ const Video = React.memo(
disabled={isReply} disabled={isReply}
onPress={onPress} onPress={onPress}
style={[styles.button, { backgroundColor: themes[theme].videoBackground }]} style={[styles.button, { backgroundColor: themes[theme].videoBackground }]}
background={Touchable.Ripple(themes[theme].bannerBackground)} background={Touchable.Ripple(themes[theme].bannerBackground)}>
>
{loading ? ( {loading ? (
<DownloadIndicator handleCancelDownload={handleCancelDownload} /> <DownloadIndicator handleCancelDownload={handleCancelDownload} />
) : ( ) : (

View File

@ -5,9 +5,9 @@ import {
AUDIO_PREFERENCE_DOWNLOAD, AUDIO_PREFERENCE_DOWNLOAD,
VIDEO_PREFERENCE_DOWNLOAD, VIDEO_PREFERENCE_DOWNLOAD,
MediaDownloadOption MediaDownloadOption
} from '../../../../lib/constants'; } from '../constants';
import userPreferences from '../../../../lib/methods/userPreferences'; import userPreferences from './userPreferences';
import { IUser, IUserMessage } from '../../../../definitions'; import { IUser, IUserMessage } from '../../definitions';
type TMediaType = typeof IMAGES_PREFERENCE_DOWNLOAD | typeof AUDIO_PREFERENCE_DOWNLOAD | typeof VIDEO_PREFERENCE_DOWNLOAD; type TMediaType = typeof IMAGES_PREFERENCE_DOWNLOAD | typeof AUDIO_PREFERENCE_DOWNLOAD | typeof VIDEO_PREFERENCE_DOWNLOAD;
interface IUsersParam { interface IUsersParam {
@ -15,13 +15,13 @@ interface IUsersParam {
author?: IUserMessage; author?: IUserMessage;
} }
export const isAutoDownloadEnabled = async (mediaType: TMediaType, { author, user }: IUsersParam) => { export const isAutoDownloadEnabled = async (mediaType: TMediaType, userParam?: IUsersParam) => {
const mediaDownloadPreference = userPreferences.getString(mediaType); const mediaDownloadPreference = userPreferences.getString(mediaType);
const netInfoState = await NetInfo.fetch(); const netInfoState = await NetInfo.fetch();
return ( return (
(mediaDownloadPreference === MediaDownloadOption.WIFI && netInfoState.type === NetInfoStateType.wifi) || (mediaDownloadPreference === MediaDownloadOption.WIFI && netInfoState.type === NetInfoStateType.wifi) ||
mediaDownloadPreference === MediaDownloadOption.WIFI_MOBILE_DATA || mediaDownloadPreference === MediaDownloadOption.WIFI_MOBILE_DATA ||
author?._id === user.id (userParam && userParam.author?._id === userParam.user.id)
); );
}; };

View File

@ -82,6 +82,10 @@ const getExtension = (type: MediaTypes, mimeType?: string) => {
if (mimeType === 'audio/mpeg') { if (mimeType === 'audio/mpeg') {
return 'mp3'; return 'mp3';
} }
// For older audios the server is returning the type audio/aac and we can't play it as mp3
if (mimeType === 'audio/aac') {
return 'm4a';
}
const extension = mime.extension(mimeType); const extension = mime.extension(mimeType);
// The mime.extension can return false when there aren't any extension // The mime.extension can return false when there aren't any extension
if (!extension) { if (!extension) {
@ -127,7 +131,7 @@ export const searchMediaFileAsync = async ({
return { file, filePath }; return { file, filePath };
}; };
export const deleteAllSpecificMediaFiles = async (type: MediaTypes, serverUrl: string): Promise<void> => { export const deleteMediaFiles = async (type: MediaTypes, serverUrl: string): Promise<void> => {
try { try {
const serverUrlParsed = sanitizeString(serverUrl); const serverUrlParsed = sanitizeString(serverUrl);
const path = `${LOCAL_DOCUMENT_PATH}${typeString[type]}${serverUrlParsed}`; const path = `${LOCAL_DOCUMENT_PATH}${typeString[type]}${serverUrlParsed}`;

View File

@ -1,11 +1,10 @@
import React from 'react'; import React from 'react';
import { useMMKVStorage } from 'react-native-mmkv-storage';
import * as List from '../../containers/List'; import * as List from '../../containers/List';
import SafeAreaView from '../../containers/SafeAreaView'; import SafeAreaView from '../../containers/SafeAreaView';
import StatusBar from '../../containers/StatusBar'; import StatusBar from '../../containers/StatusBar';
import ListPicker from './ListPicker'; import ListPicker from './ListPicker';
import userPreferences from '../../lib/methods/userPreferences'; import { useUserPreferences } from '../../lib/methods/userPreferences';
import { import {
AUDIO_PREFERENCE_DOWNLOAD, AUDIO_PREFERENCE_DOWNLOAD,
IMAGES_PREFERENCE_DOWNLOAD, IMAGES_PREFERENCE_DOWNLOAD,
@ -13,22 +12,17 @@ import {
VIDEO_PREFERENCE_DOWNLOAD VIDEO_PREFERENCE_DOWNLOAD
} from '../../lib/constants'; } from '../../lib/constants';
const MMKV = userPreferences.getMMKV();
const MediaAutoDownload = () => { const MediaAutoDownload = () => {
const [imagesPreference, setImagesPreference] = useMMKVStorage<MediaDownloadOption>( const [imagesPreference, setImagesPreference] = useUserPreferences<MediaDownloadOption>(
IMAGES_PREFERENCE_DOWNLOAD, IMAGES_PREFERENCE_DOWNLOAD,
MMKV,
MediaDownloadOption.NEVER MediaDownloadOption.NEVER
); );
const [videoPreference, setVideoPreference] = useMMKVStorage<MediaDownloadOption>( const [videoPreference, setVideoPreference] = useUserPreferences<MediaDownloadOption>(
VIDEO_PREFERENCE_DOWNLOAD, VIDEO_PREFERENCE_DOWNLOAD,
MMKV,
MediaDownloadOption.NEVER MediaDownloadOption.NEVER
); );
const [audioPreference, setAudioPreference] = useMMKVStorage<MediaDownloadOption>( const [audioPreference, setAudioPreference] = useUserPreferences<MediaDownloadOption>(
AUDIO_PREFERENCE_DOWNLOAD, AUDIO_PREFERENCE_DOWNLOAD,
MMKV,
MediaDownloadOption.NEVER MediaDownloadOption.NEVER
); );

View File

@ -21,7 +21,7 @@ import { APP_STORE_LINK, FDROID_MARKET_LINK, isFDroidBuild, LICENSE_LINK, PLAY_M
import database from '../../lib/database'; import database from '../../lib/database';
import { useAppSelector } from '../../lib/hooks'; import { useAppSelector } from '../../lib/hooks';
import { clearCache } from '../../lib/methods'; import { clearCache } from '../../lib/methods';
import { deleteAllSpecificMediaFiles, MediaTypes } from '../../lib/methods/handleMediaDownload'; import { deleteMediaFiles, MediaTypes } from '../../lib/methods/handleMediaDownload';
import { getDeviceModel, getReadableVersion, isAndroid } from '../../lib/methods/helpers'; import { getDeviceModel, getReadableVersion, isAndroid } from '../../lib/methods/helpers';
import EventEmitter from '../../lib/methods/helpers/events'; import EventEmitter from '../../lib/methods/helpers/events';
import { showConfirmationAlert, showErrorAlert } from '../../lib/methods/helpers/info'; import { showConfirmationAlert, showErrorAlert } from '../../lib/methods/helpers/info';
@ -99,9 +99,9 @@ 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 deleteMediaFiles(MediaTypes.image, server);
await deleteAllSpecificMediaFiles(MediaTypes.audio, server); await deleteMediaFiles(MediaTypes.audio, server);
await deleteAllSpecificMediaFiles(MediaTypes.video, server); await deleteMediaFiles(MediaTypes.video, server);
await clearCache({ server }); await clearCache({ server });
await FastImage.clearMemoryCache(); await FastImage.clearMemoryCache();
await FastImage.clearDiskCache(); await FastImage.clearDiskCache();