2018-07-17 19:10:27 +00:00
|
|
|
import React, { Component } from 'react';
|
2018-09-25 19:28:42 +00:00
|
|
|
import {
|
|
|
|
View, Text, StyleSheet, TouchableOpacity, ScrollView
|
|
|
|
} from 'react-native';
|
2018-07-17 19:10:27 +00:00
|
|
|
import PropTypes from 'prop-types';
|
|
|
|
import { responsive } from 'react-native-responsive-ui';
|
2018-12-21 10:55:35 +00:00
|
|
|
import equal from 'deep-equal';
|
2018-07-17 19:10:27 +00:00
|
|
|
|
2019-04-04 18:08:40 +00:00
|
|
|
import database, { safeAddListener } from '../../lib/realm';
|
2018-07-17 19:10:27 +00:00
|
|
|
import RocketChat from '../../lib/rocketchat';
|
|
|
|
import log from '../../utils/log';
|
|
|
|
import I18n from '../../i18n';
|
2019-03-01 16:49:11 +00:00
|
|
|
import { CustomIcon } from '../../lib/Icons';
|
2019-03-29 19:36:07 +00:00
|
|
|
import {
|
|
|
|
COLOR_SEPARATOR, COLOR_PRIMARY, COLOR_BACKGROUND_CONTAINER, COLOR_TEXT_DESCRIPTION, COLOR_DANGER
|
|
|
|
} from '../../constants/colors';
|
|
|
|
import sharedStyles from '../Styles';
|
2018-07-17 19:10:27 +00:00
|
|
|
|
|
|
|
const styles = StyleSheet.create({
|
|
|
|
container: {
|
|
|
|
position: 'absolute',
|
|
|
|
top: 0,
|
|
|
|
width: '100%',
|
|
|
|
maxHeight: 246
|
|
|
|
},
|
|
|
|
item: {
|
2019-03-29 19:36:07 +00:00
|
|
|
backgroundColor: COLOR_BACKGROUND_CONTAINER,
|
2018-07-17 19:10:27 +00:00
|
|
|
height: 54,
|
|
|
|
borderBottomWidth: StyleSheet.hairlineWidth,
|
2019-03-27 20:06:57 +00:00
|
|
|
borderColor: COLOR_SEPARATOR,
|
2018-07-17 19:10:27 +00:00
|
|
|
justifyContent: 'center',
|
|
|
|
paddingHorizontal: 20
|
|
|
|
},
|
|
|
|
row: {
|
|
|
|
flexDirection: 'row',
|
|
|
|
alignItems: 'center'
|
|
|
|
},
|
|
|
|
descriptionContainer: {
|
|
|
|
flexDirection: 'column',
|
|
|
|
flex: 1,
|
|
|
|
marginLeft: 10
|
|
|
|
},
|
|
|
|
descriptionText: {
|
|
|
|
fontSize: 16,
|
|
|
|
lineHeight: 20,
|
2019-03-29 19:36:07 +00:00
|
|
|
...sharedStyles.textColorDescription,
|
|
|
|
...sharedStyles.textRegular
|
2018-07-17 19:10:27 +00:00
|
|
|
},
|
|
|
|
progress: {
|
|
|
|
position: 'absolute',
|
|
|
|
bottom: 0,
|
2019-03-29 19:36:07 +00:00
|
|
|
backgroundColor: COLOR_PRIMARY,
|
2018-07-17 19:10:27 +00:00
|
|
|
height: 3
|
|
|
|
},
|
|
|
|
tryAgainButtonText: {
|
2019-03-29 19:36:07 +00:00
|
|
|
color: COLOR_PRIMARY,
|
2018-07-17 19:10:27 +00:00
|
|
|
fontSize: 16,
|
2019-03-29 19:36:07 +00:00
|
|
|
lineHeight: 20,
|
|
|
|
...sharedStyles.textMedium
|
2018-07-17 19:10:27 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2019-08-07 13:51:34 +00:00
|
|
|
class UploadProgress extends Component {
|
2018-07-17 19:10:27 +00:00
|
|
|
static propTypes = {
|
|
|
|
window: PropTypes.object,
|
2019-07-29 16:33:28 +00:00
|
|
|
rid: PropTypes.string,
|
|
|
|
user: PropTypes.shape({
|
|
|
|
id: PropTypes.string.isRequired,
|
|
|
|
username: PropTypes.string.isRequired,
|
|
|
|
token: PropTypes.string.isRequired
|
|
|
|
}),
|
|
|
|
baseUrl: PropTypes.string.isRequired
|
2018-07-17 19:10:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
constructor(props) {
|
|
|
|
super(props);
|
|
|
|
this.state = {
|
|
|
|
uploads: []
|
|
|
|
};
|
2018-09-25 19:28:42 +00:00
|
|
|
const { rid } = this.props;
|
|
|
|
this.uploads = database.objects('uploads').filtered('rid = $0', rid);
|
2019-04-04 18:08:40 +00:00
|
|
|
safeAddListener(this.uploads, this.updateUploads);
|
2018-07-17 19:10:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
componentDidMount() {
|
|
|
|
this.uploads.forEach((u) => {
|
|
|
|
if (!RocketChat.isUploadActive(u.path)) {
|
|
|
|
database.write(() => {
|
|
|
|
const [upload] = database.objects('uploads').filtered('path = $0', u.path);
|
|
|
|
if (upload) {
|
|
|
|
upload.error = true;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-12-21 10:55:35 +00:00
|
|
|
shouldComponentUpdate(nextProps, nextState) {
|
|
|
|
const { uploads } = this.state;
|
|
|
|
const { window } = this.props;
|
|
|
|
if (nextProps.window.width !== window.width) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (!equal(nextState.uploads, uploads)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-07-17 19:10:27 +00:00
|
|
|
componentWillUnmount() {
|
|
|
|
this.uploads.removeAllListeners();
|
|
|
|
}
|
|
|
|
|
|
|
|
deleteUpload = (item) => {
|
2019-02-01 13:30:12 +00:00
|
|
|
const uploadItem = this.uploads.filtered('path = $0', item.path);
|
|
|
|
try {
|
|
|
|
database.write(() => database.delete(uploadItem[0]));
|
|
|
|
} catch (e) {
|
2019-05-28 16:18:46 +00:00
|
|
|
log('err_upload_progress_delete', e);
|
2019-02-01 13:30:12 +00:00
|
|
|
}
|
2018-07-17 19:10:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
cancelUpload = async(item) => {
|
|
|
|
try {
|
|
|
|
await RocketChat.cancelUpload(item.path);
|
|
|
|
} catch (e) {
|
2019-05-28 16:18:46 +00:00
|
|
|
log('err_upload_progress_cancel', e);
|
2018-07-17 19:10:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
tryAgain = async(item) => {
|
2019-07-29 16:33:28 +00:00
|
|
|
const { rid, baseUrl: server, user } = this.props;
|
2018-09-25 19:28:42 +00:00
|
|
|
|
2018-07-17 19:10:27 +00:00
|
|
|
try {
|
|
|
|
database.write(() => {
|
|
|
|
item.error = false;
|
|
|
|
});
|
2019-07-29 16:33:28 +00:00
|
|
|
await RocketChat.sendFileMessage(rid, item, undefined, server, user);
|
2018-07-17 19:10:27 +00:00
|
|
|
} catch (e) {
|
2019-05-28 16:18:46 +00:00
|
|
|
log('err_upload_progress_try_again', e);
|
2018-07-17 19:10:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
updateUploads = () => {
|
2018-12-21 10:55:35 +00:00
|
|
|
const uploads = this.uploads.map(item => JSON.parse(JSON.stringify(item)));
|
|
|
|
this.setState({ uploads });
|
2018-07-17 19:10:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
renderItemContent = (item) => {
|
2018-09-25 19:28:42 +00:00
|
|
|
const { window } = this.props;
|
|
|
|
|
2018-07-17 19:10:27 +00:00
|
|
|
if (!item.error) {
|
|
|
|
return (
|
|
|
|
[
|
|
|
|
<View key='row' style={styles.row}>
|
2019-03-29 19:36:07 +00:00
|
|
|
<CustomIcon name='file-generic' size={20} color={COLOR_TEXT_DESCRIPTION} />
|
2018-07-17 19:10:27 +00:00
|
|
|
<Text style={[styles.descriptionContainer, styles.descriptionText]} ellipsizeMode='tail' numberOfLines={1}>
|
|
|
|
{I18n.t('Uploading')} {item.name}
|
|
|
|
</Text>
|
2019-03-29 19:36:07 +00:00
|
|
|
<CustomIcon name='cross' size={20} color={COLOR_TEXT_DESCRIPTION} onPress={() => this.cancelUpload(item)} />
|
2018-07-17 19:10:27 +00:00
|
|
|
</View>,
|
2018-09-25 19:28:42 +00:00
|
|
|
<View key='progress' style={[styles.progress, { width: (window.width * item.progress) / 100 }]} />
|
2018-07-17 19:10:27 +00:00
|
|
|
]
|
|
|
|
);
|
|
|
|
}
|
|
|
|
return (
|
|
|
|
<View style={styles.row}>
|
2019-03-29 19:36:07 +00:00
|
|
|
<CustomIcon name='warning' size={20} color={COLOR_DANGER} />
|
2018-07-17 19:10:27 +00:00
|
|
|
<View style={styles.descriptionContainer}>
|
|
|
|
<Text style={styles.descriptionText}>{I18n.t('Error_uploading')} {item.name}</Text>
|
|
|
|
<TouchableOpacity onPress={() => this.tryAgain(item)}>
|
|
|
|
<Text style={styles.tryAgainButtonText}>{I18n.t('Try_again')}</Text>
|
|
|
|
</TouchableOpacity>
|
|
|
|
</View>
|
2019-03-29 19:36:07 +00:00
|
|
|
<CustomIcon name='cross' size={20} color={COLOR_TEXT_DESCRIPTION} onPress={() => this.deleteUpload(item)} />
|
2018-07-17 19:10:27 +00:00
|
|
|
</View>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
renderItem = (item, index) => (
|
|
|
|
<View key={item.path} style={[styles.item, index !== 0 ? { marginTop: 10 } : {}]}>
|
|
|
|
{this.renderItemContent(item)}
|
|
|
|
</View>
|
|
|
|
);
|
|
|
|
|
|
|
|
render() {
|
|
|
|
const { uploads } = this.state;
|
|
|
|
return (
|
|
|
|
<ScrollView style={styles.container}>
|
|
|
|
{uploads.map((item, i) => this.renderItem(item, i))}
|
|
|
|
</ScrollView>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
2019-08-07 13:51:34 +00:00
|
|
|
|
|
|
|
export default responsive(UploadProgress);
|