refactor some changes requested
This commit is contained in:
parent
67ba3131e3
commit
1dc038d321
|
@ -22,7 +22,7 @@ import { TSupportedThemes } from '../../theme';
|
|||
import { MediaTypes, downloadMediaFile, searchMediaFileAsync } from '../../lib/methods/handleMediaDownload';
|
||||
import EventEmitter from '../../lib/methods/helpers/events';
|
||||
import { PAUSE_AUDIO } from './constants';
|
||||
import { isAutoDownloadEnabled } from './helpers/mediaDownload/autoDownloadPreference';
|
||||
import { isAutoDownloadEnabled } from '../../lib/methods/autoDownloadPreference';
|
||||
|
||||
interface IButton {
|
||||
loading: boolean;
|
||||
|
@ -107,8 +107,7 @@ const Button = React.memo(({ loading, paused, onPress, disabled, theme, toDownlo
|
|||
disabled={disabled}
|
||||
onPress={onPress}
|
||||
hitSlop={BUTTON_HIT_SLOP}
|
||||
background={Touchable.SelectableBackgroundBorderless()}
|
||||
>
|
||||
background={Touchable.SelectableBackgroundBorderless()}>
|
||||
{loading ? (
|
||||
<ActivityIndicator style={[styles.playPauseButton, styles.audioLoading]} />
|
||||
) : (
|
||||
|
@ -180,7 +179,8 @@ class MessageAudio extends React.Component<IMessageAudioProps, IMessageAudioStat
|
|||
if (url) {
|
||||
const autoDownload = await isAutoDownloadEnabled('audioPreferenceDownload', { author, user });
|
||||
if (autoDownload) {
|
||||
return await this.startDownload();
|
||||
await this.startDownload();
|
||||
return;
|
||||
}
|
||||
|
||||
// MediaDownloadOption.NEVER or MediaDownloadOption.WIFI and the mobile is using mobile data
|
||||
|
@ -370,8 +370,7 @@ class MessageAudio extends React.Component<IMessageAudioProps, IMessageAudioStat
|
|||
style={[
|
||||
styles.audioContainer,
|
||||
{ backgroundColor: themes[theme].chatComponentBackground, borderColor: themes[theme].borderColor }
|
||||
]}
|
||||
>
|
||||
]}>
|
||||
<Button
|
||||
disabled={isReply}
|
||||
loading={loading}
|
||||
|
|
|
@ -22,7 +22,7 @@ import {
|
|||
isDownloadActive,
|
||||
searchMediaFileAsync
|
||||
} from '../../lib/methods/handleMediaDownload';
|
||||
import { isAutoDownloadEnabled } from './helpers/mediaDownload/autoDownloadPreference';
|
||||
import { isAutoDownloadEnabled } from '../../lib/methods/autoDownloadPreference';
|
||||
import RCActivityIndicator from '../ActivityIndicator';
|
||||
import { CustomIcon } from '../CustomIcon';
|
||||
|
||||
|
|
|
@ -25,7 +25,7 @@ import {
|
|||
isDownloadActive,
|
||||
searchMediaFileAsync
|
||||
} from '../../lib/methods/handleMediaDownload';
|
||||
import { isAutoDownloadEnabled } from './helpers/mediaDownload/autoDownloadPreference';
|
||||
import { isAutoDownloadEnabled } from '../../lib/methods/autoDownloadPreference';
|
||||
import sharedStyles from '../../views/Styles';
|
||||
|
||||
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);
|
||||
|
||||
// We don't pass the author to avoid auto-download what the user sent
|
||||
const autoDownload = await isAutoDownloadEnabled('imagesPreferenceDownload', { user });
|
||||
const autoDownload = await isAutoDownloadEnabled('imagesPreferenceDownload');
|
||||
if (autoDownload) {
|
||||
await handleDownload();
|
||||
}
|
||||
|
@ -187,8 +186,7 @@ const Video = React.memo(
|
|||
disabled={isReply}
|
||||
onPress={onPress}
|
||||
style={[styles.button, { backgroundColor: themes[theme].videoBackground }]}
|
||||
background={Touchable.Ripple(themes[theme].bannerBackground)}
|
||||
>
|
||||
background={Touchable.Ripple(themes[theme].bannerBackground)}>
|
||||
{loading ? (
|
||||
<DownloadIndicator handleCancelDownload={handleCancelDownload} />
|
||||
) : (
|
||||
|
|
|
@ -5,9 +5,9 @@ import {
|
|||
AUDIO_PREFERENCE_DOWNLOAD,
|
||||
VIDEO_PREFERENCE_DOWNLOAD,
|
||||
MediaDownloadOption
|
||||
} from '../../../../lib/constants';
|
||||
import userPreferences from '../../../../lib/methods/userPreferences';
|
||||
import { IUser, IUserMessage } from '../../../../definitions';
|
||||
} from '../constants';
|
||||
import userPreferences from './userPreferences';
|
||||
import { IUser, IUserMessage } from '../../definitions';
|
||||
|
||||
type TMediaType = typeof IMAGES_PREFERENCE_DOWNLOAD | typeof AUDIO_PREFERENCE_DOWNLOAD | typeof VIDEO_PREFERENCE_DOWNLOAD;
|
||||
interface IUsersParam {
|
||||
|
@ -15,13 +15,13 @@ interface IUsersParam {
|
|||
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 netInfoState = await NetInfo.fetch();
|
||||
|
||||
return (
|
||||
(mediaDownloadPreference === MediaDownloadOption.WIFI && netInfoState.type === NetInfoStateType.wifi) ||
|
||||
mediaDownloadPreference === MediaDownloadOption.WIFI_MOBILE_DATA ||
|
||||
author?._id === user.id
|
||||
(userParam && userParam.author?._id === userParam.user.id)
|
||||
);
|
||||
};
|
|
@ -82,6 +82,10 @@ const getExtension = (type: MediaTypes, mimeType?: string) => {
|
|||
if (mimeType === 'audio/mpeg') {
|
||||
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);
|
||||
// The mime.extension can return false when there aren't any extension
|
||||
if (!extension) {
|
||||
|
@ -127,7 +131,7 @@ export const searchMediaFileAsync = async ({
|
|||
return { file, filePath };
|
||||
};
|
||||
|
||||
export const deleteAllSpecificMediaFiles = async (type: MediaTypes, serverUrl: string): Promise<void> => {
|
||||
export const deleteMediaFiles = async (type: MediaTypes, serverUrl: string): Promise<void> => {
|
||||
try {
|
||||
const serverUrlParsed = sanitizeString(serverUrl);
|
||||
const path = `${LOCAL_DOCUMENT_PATH}${typeString[type]}${serverUrlParsed}`;
|
||||
|
|
|
@ -1,11 +1,10 @@
|
|||
import React from 'react';
|
||||
import { useMMKVStorage } from 'react-native-mmkv-storage';
|
||||
|
||||
import * as List from '../../containers/List';
|
||||
import SafeAreaView from '../../containers/SafeAreaView';
|
||||
import StatusBar from '../../containers/StatusBar';
|
||||
import ListPicker from './ListPicker';
|
||||
import userPreferences from '../../lib/methods/userPreferences';
|
||||
import { useUserPreferences } from '../../lib/methods/userPreferences';
|
||||
import {
|
||||
AUDIO_PREFERENCE_DOWNLOAD,
|
||||
IMAGES_PREFERENCE_DOWNLOAD,
|
||||
|
@ -13,22 +12,17 @@ import {
|
|||
VIDEO_PREFERENCE_DOWNLOAD
|
||||
} from '../../lib/constants';
|
||||
|
||||
const MMKV = userPreferences.getMMKV();
|
||||
|
||||
const MediaAutoDownload = () => {
|
||||
const [imagesPreference, setImagesPreference] = useMMKVStorage<MediaDownloadOption>(
|
||||
const [imagesPreference, setImagesPreference] = useUserPreferences<MediaDownloadOption>(
|
||||
IMAGES_PREFERENCE_DOWNLOAD,
|
||||
MMKV,
|
||||
MediaDownloadOption.NEVER
|
||||
);
|
||||
const [videoPreference, setVideoPreference] = useMMKVStorage<MediaDownloadOption>(
|
||||
const [videoPreference, setVideoPreference] = useUserPreferences<MediaDownloadOption>(
|
||||
VIDEO_PREFERENCE_DOWNLOAD,
|
||||
MMKV,
|
||||
MediaDownloadOption.NEVER
|
||||
);
|
||||
const [audioPreference, setAudioPreference] = useMMKVStorage<MediaDownloadOption>(
|
||||
const [audioPreference, setAudioPreference] = useUserPreferences<MediaDownloadOption>(
|
||||
AUDIO_PREFERENCE_DOWNLOAD,
|
||||
MMKV,
|
||||
MediaDownloadOption.NEVER
|
||||
);
|
||||
|
||||
|
|
|
@ -21,7 +21,7 @@ import { APP_STORE_LINK, FDROID_MARKET_LINK, isFDroidBuild, LICENSE_LINK, PLAY_M
|
|||
import database from '../../lib/database';
|
||||
import { useAppSelector } from '../../lib/hooks';
|
||||
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 EventEmitter from '../../lib/methods/helpers/events';
|
||||
import { showConfirmationAlert, showErrorAlert } from '../../lib/methods/helpers/info';
|
||||
|
@ -99,9 +99,9 @@ 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 deleteAllSpecificMediaFiles(MediaTypes.video, server);
|
||||
await deleteMediaFiles(MediaTypes.image, server);
|
||||
await deleteMediaFiles(MediaTypes.audio, server);
|
||||
await deleteMediaFiles(MediaTypes.video, server);
|
||||
await clearCache({ server });
|
||||
await FastImage.clearMemoryCache();
|
||||
await FastImage.clearDiskCache();
|
||||
|
|
Loading…
Reference in New Issue