vn-verdnaturachat/app/containers/FileModal.js

133 lines
3.6 KiB
JavaScript
Raw Normal View History

import React, { useState } from 'react';
import {
2019-12-04 16:39:53 +00:00
View, Text, TouchableWithoutFeedback, StyleSheet, SafeAreaView
} from 'react-native';
import FastImage from 'react-native-fast-image';
import PropTypes from 'prop-types';
import Modal from 'react-native-modal';
import ImageViewer from 'react-native-image-zoom-viewer';
import { Video } from 'expo-av';
import sharedStyles from '../views/Styles';
import { formatAttachmentUrl } from '../lib/utils';
2019-12-04 16:39:53 +00:00
import ActivityIndicator from './ActivityIndicator';
import { themes } from '../constants/colors';
import { withTheme } from '../theme';
const styles = StyleSheet.create({
safeArea: {
flex: 1
},
modal: {
margin: 0
},
titleContainer: {
width: '100%',
alignItems: 'center',
marginVertical: 10
},
title: {
textAlign: 'center',
fontSize: 16,
...sharedStyles.textSemibold
},
description: {
textAlign: 'center',
fontSize: 14,
...sharedStyles.textMedium
},
video: {
flex: 1
}
});
const ModalContent = React.memo(({
2019-12-04 16:39:53 +00:00
attachment, onClose, user, baseUrl, theme
}) => {
if (attachment && attachment.image_url) {
const url = formatAttachmentUrl(attachment.image_url, user.id, user.token, baseUrl);
return (
<SafeAreaView style={styles.safeArea}>
<TouchableWithoutFeedback onPress={onClose}>
<View style={styles.titleContainer}>
2019-12-04 16:39:53 +00:00
<Text style={[styles.title, { color: themes[theme].buttonText }]}>{attachment.title}</Text>
{attachment.description ? <Text style={[styles.description, { color: themes[theme].buttonText }]}>{attachment.description}</Text> : null}
</View>
</TouchableWithoutFeedback>
<ImageViewer
imageUrls={[{ url }]}
onClick={onClose}
backgroundColor='transparent'
enableSwipeDown
onSwipeDown={onClose}
renderIndicator={() => null}
renderImage={props => <FastImage {...props} />}
2019-12-04 16:39:53 +00:00
loadingRender={() => <ActivityIndicator size='large' theme={theme} />}
/>
</SafeAreaView>
);
}
if (attachment && attachment.video_url) {
const [loading, setLoading] = useState(true);
const uri = formatAttachmentUrl(attachment.video_url, user.id, user.token, baseUrl);
return (
<>
<Video
source={{ uri }}
rate={1.0}
volume={1.0}
isMuted={false}
2019-09-16 21:10:06 +00:00
resizeMode={Video.RESIZE_MODE_CONTAIN}
shouldPlay
isLooping={false}
style={styles.video}
useNativeControls
onReadyForDisplay={() => setLoading(false)}
onLoadStart={() => setLoading(true)}
onError={console.log}
/>
2019-12-04 16:39:53 +00:00
{ loading ? <ActivityIndicator size='large' theme={theme} absolute /> : null }
</>
);
}
return null;
});
const FileModal = React.memo(({
2019-12-04 16:39:53 +00:00
isVisible, onClose, attachment, user, baseUrl, theme
}) => (
<Modal
style={styles.modal}
isVisible={isVisible}
onBackdropPress={onClose}
onBackButtonPress={onClose}
onSwipeComplete={onClose}
swipeDirection={['up', 'down']}
>
2019-12-04 16:39:53 +00:00
<ModalContent attachment={attachment} onClose={onClose} user={user} baseUrl={baseUrl} theme={theme} />
</Modal>
2019-12-04 16:39:53 +00:00
), (prevProps, nextProps) => (
prevProps.isVisible === nextProps.isVisible && prevProps.loading === nextProps.loading && prevProps.theme === nextProps.theme
));
FileModal.propTypes = {
isVisible: PropTypes.bool,
attachment: PropTypes.object,
user: PropTypes.object,
baseUrl: PropTypes.string,
2019-12-04 16:39:53 +00:00
theme: PropTypes.string,
onClose: PropTypes.func
};
FileModal.displayName = 'FileModal';
ModalContent.propTypes = {
attachment: PropTypes.object,
user: PropTypes.object,
baseUrl: PropTypes.string,
2019-12-04 16:39:53 +00:00
theme: PropTypes.string,
onClose: PropTypes.func
};
ModalContent.displayName = 'FileModalContent';
2019-12-04 16:39:53 +00:00
export default withTheme(FileModal);