2019-09-03 19:27:57 +00:00
|
|
|
import React, { useState } from 'react';
|
2019-05-20 20:43:50 +00:00
|
|
|
import {
|
|
|
|
View, Text, TouchableWithoutFeedback, ActivityIndicator, 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';
|
2019-09-03 19:27:57 +00:00
|
|
|
import { Video } from 'expo-av';
|
2019-05-20 20:43:50 +00:00
|
|
|
|
|
|
|
import sharedStyles from '../views/Styles';
|
|
|
|
import { COLOR_WHITE } from '../constants/colors';
|
|
|
|
import { formatAttachmentUrl } from '../lib/utils';
|
|
|
|
|
|
|
|
const styles = StyleSheet.create({
|
|
|
|
safeArea: {
|
|
|
|
flex: 1
|
|
|
|
},
|
|
|
|
modal: {
|
|
|
|
margin: 0
|
|
|
|
},
|
|
|
|
titleContainer: {
|
|
|
|
width: '100%',
|
|
|
|
alignItems: 'center',
|
|
|
|
marginVertical: 10
|
|
|
|
},
|
|
|
|
title: {
|
|
|
|
color: COLOR_WHITE,
|
|
|
|
textAlign: 'center',
|
|
|
|
fontSize: 16,
|
|
|
|
...sharedStyles.textSemibold
|
|
|
|
},
|
|
|
|
description: {
|
|
|
|
color: COLOR_WHITE,
|
|
|
|
textAlign: 'center',
|
|
|
|
fontSize: 14,
|
|
|
|
...sharedStyles.textMedium
|
|
|
|
},
|
|
|
|
indicator: {
|
|
|
|
flex: 1
|
2019-09-03 19:27:57 +00:00
|
|
|
},
|
|
|
|
video: {
|
|
|
|
flex: 1
|
|
|
|
},
|
|
|
|
loading: {
|
|
|
|
position: 'absolute',
|
|
|
|
left: 0,
|
|
|
|
right: 0,
|
|
|
|
top: 0,
|
|
|
|
bottom: 0,
|
|
|
|
alignItems: 'center',
|
|
|
|
justifyContent: 'center'
|
2019-05-20 20:43:50 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
const Indicator = React.memo(() => (
|
|
|
|
<ActivityIndicator style={styles.indicator} />
|
|
|
|
));
|
|
|
|
|
|
|
|
const ModalContent = React.memo(({
|
|
|
|
attachment, onClose, user, baseUrl
|
|
|
|
}) => {
|
|
|
|
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}>
|
|
|
|
<Text style={styles.title}>{attachment.title}</Text>
|
|
|
|
{attachment.description ? <Text style={styles.description}>{attachment.description}</Text> : null}
|
|
|
|
</View>
|
|
|
|
</TouchableWithoutFeedback>
|
|
|
|
<ImageViewer
|
|
|
|
imageUrls={[{ url }]}
|
|
|
|
onClick={onClose}
|
|
|
|
backgroundColor='transparent'
|
|
|
|
enableSwipeDown
|
|
|
|
onSwipeDown={onClose}
|
|
|
|
renderIndicator={() => null}
|
|
|
|
renderImage={props => <FastImage {...props} />}
|
|
|
|
loadingRender={() => <Indicator />}
|
|
|
|
/>
|
|
|
|
</SafeAreaView>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
if (attachment && attachment.video_url) {
|
2019-09-03 19:27:57 +00:00
|
|
|
const [loading, setLoading] = useState(true);
|
2019-05-20 20:43:50 +00:00
|
|
|
const uri = formatAttachmentUrl(attachment.video_url, user.id, user.token, baseUrl);
|
|
|
|
return (
|
2019-09-03 19:27:57 +00:00
|
|
|
<>
|
|
|
|
<Video
|
2019-05-20 20:43:50 +00:00
|
|
|
source={{ uri }}
|
2019-09-03 19:27:57 +00:00
|
|
|
rate={1.0}
|
|
|
|
volume={1.0}
|
|
|
|
isMuted={false}
|
2019-10-07 20:56:30 +00:00
|
|
|
resizeMode={Video.RESIZE_MODE_CONTAIN}
|
2019-09-03 19:27:57 +00:00
|
|
|
shouldPlay
|
|
|
|
isLooping={false}
|
|
|
|
style={styles.video}
|
|
|
|
useNativeControls
|
|
|
|
onReadyForDisplay={() => setLoading(false)}
|
|
|
|
onLoadStart={() => setLoading(true)}
|
|
|
|
onError={console.log}
|
2019-05-20 20:43:50 +00:00
|
|
|
/>
|
2019-09-03 19:27:57 +00:00
|
|
|
{ loading ? <ActivityIndicator size='large' style={styles.loading} /> : null }
|
|
|
|
</>
|
2019-05-20 20:43:50 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
});
|
|
|
|
|
|
|
|
const FileModal = React.memo(({
|
|
|
|
isVisible, onClose, attachment, user, baseUrl
|
|
|
|
}) => (
|
|
|
|
<Modal
|
|
|
|
style={styles.modal}
|
|
|
|
isVisible={isVisible}
|
|
|
|
onBackdropPress={onClose}
|
|
|
|
onBackButtonPress={onClose}
|
|
|
|
onSwipeComplete={onClose}
|
2019-09-03 19:27:57 +00:00
|
|
|
swipeDirection={['up', 'down']}
|
2019-05-20 20:43:50 +00:00
|
|
|
>
|
|
|
|
<ModalContent attachment={attachment} onClose={onClose} user={user} baseUrl={baseUrl} />
|
|
|
|
</Modal>
|
2019-09-03 19:27:57 +00:00
|
|
|
), (prevProps, nextProps) => prevProps.isVisible === nextProps.isVisible && prevProps.loading === nextProps.loading);
|
2019-05-20 20:43:50 +00:00
|
|
|
|
|
|
|
FileModal.propTypes = {
|
|
|
|
isVisible: PropTypes.bool,
|
|
|
|
attachment: PropTypes.object,
|
|
|
|
user: PropTypes.object,
|
|
|
|
baseUrl: PropTypes.string,
|
|
|
|
onClose: PropTypes.func
|
|
|
|
};
|
|
|
|
FileModal.displayName = 'FileModal';
|
|
|
|
|
|
|
|
ModalContent.propTypes = {
|
|
|
|
attachment: PropTypes.object,
|
|
|
|
user: PropTypes.object,
|
|
|
|
baseUrl: PropTypes.string,
|
|
|
|
onClose: PropTypes.func
|
|
|
|
};
|
|
|
|
ModalContent.displayName = 'FileModalContent';
|
|
|
|
|
|
|
|
export default FileModal;
|