2020-06-26 20:22:56 +00:00
|
|
|
import React from 'react';
|
|
|
|
import { Video } from 'expo-av';
|
|
|
|
import { useSafeAreaInsets } from 'react-native-safe-area-context';
|
2021-09-13 20:41:05 +00:00
|
|
|
import { ScrollView, StyleSheet, Text } from 'react-native';
|
2020-06-26 20:22:56 +00:00
|
|
|
import prettyBytes from 'pretty-bytes';
|
|
|
|
|
2022-05-02 19:21:15 +00:00
|
|
|
import { CustomIcon, TIconsName } from '../../containers/CustomIcon';
|
2022-06-23 20:19:42 +00:00
|
|
|
import { ImageViewer, types } from '../../containers/ImageViewer';
|
2020-06-26 20:22:56 +00:00
|
|
|
import { useDimensions, useOrientation } from '../../dimensions';
|
|
|
|
import { getHeaderHeight } from '../../containers/Header';
|
|
|
|
import sharedStyles from '../Styles';
|
|
|
|
import I18n from '../../i18n';
|
2022-06-06 14:17:51 +00:00
|
|
|
import { isAndroid } from '../../lib/methods/helpers';
|
2021-09-13 20:41:05 +00:00
|
|
|
import { allowPreview } from './utils';
|
2022-04-07 14:10:03 +00:00
|
|
|
import { THUMBS_HEIGHT } from './constants';
|
2022-04-12 16:27:05 +00:00
|
|
|
import { TSupportedThemes } from '../../theme';
|
2022-04-07 14:10:03 +00:00
|
|
|
import { themes } from '../../lib/constants';
|
2022-05-11 17:59:29 +00:00
|
|
|
import { IShareAttachment } from '../../definitions';
|
2020-06-26 20:22:56 +00:00
|
|
|
|
2020-07-14 16:43:15 +00:00
|
|
|
const MESSAGEBOX_HEIGHT = 56;
|
|
|
|
|
2020-06-26 20:22:56 +00:00
|
|
|
const styles = StyleSheet.create({
|
|
|
|
fileContainer: {
|
|
|
|
alignItems: 'center',
|
|
|
|
justifyContent: 'center'
|
|
|
|
},
|
|
|
|
fileName: {
|
|
|
|
fontSize: 16,
|
|
|
|
marginHorizontal: 10,
|
2020-11-30 21:47:05 +00:00
|
|
|
...sharedStyles.textMedium,
|
|
|
|
...sharedStyles.textAlignCenter
|
2020-06-26 20:22:56 +00:00
|
|
|
},
|
|
|
|
fileSize: {
|
|
|
|
fontSize: 14,
|
|
|
|
...sharedStyles.textRegular
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2021-11-16 16:19:50 +00:00
|
|
|
interface IIconPreview {
|
2022-05-02 19:21:15 +00:00
|
|
|
iconName: TIconsName;
|
2021-11-16 16:19:50 +00:00
|
|
|
title: string;
|
|
|
|
description?: string;
|
2022-04-12 16:27:05 +00:00
|
|
|
theme: TSupportedThemes;
|
2021-11-16 16:19:50 +00:00
|
|
|
width: number;
|
|
|
|
height: number;
|
|
|
|
danger?: boolean;
|
|
|
|
}
|
|
|
|
|
|
|
|
const IconPreview = React.memo(({ iconName, title, description, theme, width, height, danger }: IIconPreview) => (
|
2020-06-26 20:22:56 +00:00
|
|
|
<ScrollView
|
|
|
|
style={{ backgroundColor: themes[theme].auxiliaryBackground }}
|
2021-09-13 20:41:05 +00:00
|
|
|
contentContainerStyle={[styles.fileContainer, { width, height }]}>
|
|
|
|
<CustomIcon name={iconName} size={56} color={danger ? themes[theme].dangerColor : themes[theme].tintColor} />
|
2020-06-26 20:22:56 +00:00
|
|
|
<Text style={[styles.fileName, { color: themes[theme].titleText }]}>{title}</Text>
|
|
|
|
{description ? <Text style={[styles.fileSize, { color: themes[theme].bodyText }]}>{description}</Text> : null}
|
|
|
|
</ScrollView>
|
|
|
|
));
|
|
|
|
|
2021-11-16 16:19:50 +00:00
|
|
|
interface IPreview {
|
2022-05-11 17:59:29 +00:00
|
|
|
item: IShareAttachment;
|
2022-04-12 16:27:05 +00:00
|
|
|
theme: TSupportedThemes;
|
2021-11-16 16:19:50 +00:00
|
|
|
isShareExtension: boolean;
|
|
|
|
length: number;
|
|
|
|
}
|
|
|
|
|
|
|
|
const Preview = React.memo(({ item, theme, isShareExtension, length }: IPreview) => {
|
2020-06-26 20:22:56 +00:00
|
|
|
const type = item?.mime;
|
2022-05-11 17:59:29 +00:00
|
|
|
const { width, height } = useDimensions();
|
2020-06-26 20:22:56 +00:00
|
|
|
const { isLandscape } = useOrientation();
|
|
|
|
const insets = useSafeAreaInsets();
|
|
|
|
const headerHeight = getHeaderHeight(isLandscape);
|
2021-09-13 20:41:05 +00:00
|
|
|
const thumbsHeight = length > 1 ? THUMBS_HEIGHT : 0;
|
2020-07-14 16:43:15 +00:00
|
|
|
const calculatedHeight = height - insets.top - insets.bottom - MESSAGEBOX_HEIGHT - thumbsHeight - headerHeight;
|
2020-06-26 20:22:56 +00:00
|
|
|
|
|
|
|
if (item?.canUpload) {
|
2021-02-01 17:18:55 +00:00
|
|
|
// Disable video preview on iOS to save memory
|
|
|
|
if (isAndroid && type?.match(/video/)) {
|
2020-06-26 20:22:56 +00:00
|
|
|
return (
|
2020-07-14 16:43:15 +00:00
|
|
|
<ScrollView style={{ height: calculatedHeight }}>
|
|
|
|
<Video
|
|
|
|
source={{ uri: item.path }}
|
|
|
|
rate={1.0}
|
|
|
|
volume={1.0}
|
|
|
|
isMuted={false}
|
|
|
|
resizeMode={Video.RESIZE_MODE_CONTAIN}
|
|
|
|
isLooping={false}
|
|
|
|
style={{ width, height: calculatedHeight }}
|
|
|
|
useNativeControls
|
|
|
|
/>
|
|
|
|
</ScrollView>
|
2020-06-26 20:22:56 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Disallow preview of images too big in order to prevent memory issues on iOS share extension
|
2021-02-01 17:18:55 +00:00
|
|
|
if (type?.match(/image/)) {
|
|
|
|
if (allowPreview(isShareExtension, item?.size)) {
|
2020-06-26 20:22:56 +00:00
|
|
|
return (
|
|
|
|
<ImageViewer
|
|
|
|
uri={item.path}
|
|
|
|
imageComponentType={isShareExtension ? types.REACT_NATIVE_IMAGE : types.FAST_IMAGE}
|
|
|
|
width={width}
|
|
|
|
height={calculatedHeight}
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return (
|
|
|
|
<IconPreview
|
2020-07-27 19:53:33 +00:00
|
|
|
iconName={type?.match(/image/) ? 'image' : 'attach'}
|
2020-06-26 20:22:56 +00:00
|
|
|
title={item?.filename}
|
|
|
|
description={prettyBytes(item?.size ?? 0)}
|
|
|
|
theme={theme}
|
|
|
|
width={width}
|
|
|
|
height={calculatedHeight}
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
|
|
|
<IconPreview
|
|
|
|
iconName='warning'
|
|
|
|
title={I18n.t(item?.error)}
|
|
|
|
description={prettyBytes(item?.size ?? 0)}
|
|
|
|
theme={theme}
|
|
|
|
width={width}
|
|
|
|
height={calculatedHeight}
|
|
|
|
danger
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
export default Preview;
|