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';
|
2019-10-07 20:56:30 +00:00
|
|
|
import { Q } from '@nozbe/watermelondb';
|
2018-07-17 19:10:27 +00:00
|
|
|
|
2019-10-07 20:56:30 +00:00
|
|
|
import database from '../../lib/database';
|
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-12-11 23:01:12 +00:00
|
|
|
import { themes } from '../../constants/colors';
|
2019-03-29 19:36:07 +00:00
|
|
|
import sharedStyles from '../Styles';
|
2019-12-11 23:01:12 +00:00
|
|
|
import { withTheme } from '../../theme';
|
2018-07-17 19:10:27 +00:00
|
|
|
|
|
|
|
const styles = StyleSheet.create({
|
|
|
|
container: {
|
|
|
|
position: 'absolute',
|
|
|
|
top: 0,
|
|
|
|
width: '100%',
|
|
|
|
maxHeight: 246
|
|
|
|
},
|
|
|
|
item: {
|
|
|
|
height: 54,
|
|
|
|
borderBottomWidth: StyleSheet.hairlineWidth,
|
|
|
|
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.textRegular
|
2018-07-17 19:10:27 +00:00
|
|
|
},
|
|
|
|
progress: {
|
|
|
|
position: 'absolute',
|
|
|
|
bottom: 0,
|
|
|
|
height: 3
|
|
|
|
},
|
|
|
|
tryAgainButtonText: {
|
|
|
|
fontSize: 16,
|
2019-03-29 19:36:07 +00:00
|
|
|
lineHeight: 20,
|
|
|
|
...sharedStyles.textMedium
|
2018-07-17 19:10:27 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2019-08-20 19:24:22 +00:00
|
|
|
class UploadProgress extends Component {
|
2018-07-17 19:10:27 +00:00
|
|
|
static propTypes = {
|
|
|
|
window: PropTypes.object,
|
2019-07-29 20:44:39 +00:00
|
|
|
rid: PropTypes.string,
|
2019-12-11 23:01:12 +00:00
|
|
|
theme: PropTypes.string,
|
2019-07-29 20:44:39 +00:00
|
|
|
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);
|
2019-10-07 20:56:30 +00:00
|
|
|
this.mounted = false;
|
|
|
|
this.ranInitialUploadCheck = false;
|
2018-07-17 19:10:27 +00:00
|
|
|
this.state = {
|
|
|
|
uploads: []
|
|
|
|
};
|
2019-10-07 20:56:30 +00:00
|
|
|
this.init();
|
2018-07-17 19:10:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
componentDidMount() {
|
2019-10-07 20:56:30 +00:00
|
|
|
this.mounted = true;
|
2018-07-17 19:10:27 +00:00
|
|
|
}
|
|
|
|
|
2019-10-07 20:56:30 +00:00
|
|
|
componentWillUnmount() {
|
|
|
|
if (this.uploadsSubscription && this.uploadsSubscription.unsubscribe) {
|
|
|
|
this.uploadsSubscription.unsubscribe();
|
2018-12-21 10:55:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-07 20:56:30 +00:00
|
|
|
init = () => {
|
|
|
|
const { rid } = this.props;
|
2019-12-11 23:01:12 +00:00
|
|
|
if (!rid) { return; }
|
2019-10-07 20:56:30 +00:00
|
|
|
|
|
|
|
const db = database.active;
|
|
|
|
this.uploadsObservable = db.collections
|
|
|
|
.get('uploads')
|
|
|
|
.query(
|
|
|
|
Q.where('rid', rid)
|
|
|
|
)
|
|
|
|
.observeWithColumns(['progress', 'error']);
|
|
|
|
|
|
|
|
this.uploadsSubscription = this.uploadsObservable
|
|
|
|
.subscribe((uploads) => {
|
|
|
|
if (this.mounted) {
|
|
|
|
this.setState({ uploads });
|
|
|
|
} else {
|
|
|
|
this.state.uploads = uploads;
|
|
|
|
}
|
|
|
|
if (!this.ranInitialUploadCheck) {
|
|
|
|
this.uploadCheck();
|
|
|
|
}
|
|
|
|
});
|
2018-07-17 19:10:27 +00:00
|
|
|
}
|
|
|
|
|
2019-10-07 20:56:30 +00:00
|
|
|
uploadCheck = () => {
|
|
|
|
this.ranInitialUploadCheck = true;
|
|
|
|
const { uploads } = this.state;
|
|
|
|
uploads.forEach(async(u) => {
|
|
|
|
if (!RocketChat.isUploadActive(u.path)) {
|
|
|
|
try {
|
|
|
|
const db = database.active;
|
|
|
|
await db.action(async() => {
|
|
|
|
await u.update(() => {
|
|
|
|
u.error = true;
|
|
|
|
});
|
|
|
|
});
|
|
|
|
} catch (e) {
|
|
|
|
log(e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
deleteUpload = async(item) => {
|
2019-02-01 13:30:12 +00:00
|
|
|
try {
|
2019-10-07 20:56:30 +00:00
|
|
|
const db = database.active;
|
|
|
|
await db.action(async() => {
|
|
|
|
await item.destroyPermanently();
|
|
|
|
});
|
2019-02-01 13:30:12 +00:00
|
|
|
} catch (e) {
|
2019-09-03 19:27:57 +00:00
|
|
|
log(e);
|
2019-02-01 13:30:12 +00:00
|
|
|
}
|
2018-07-17 19:10:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
cancelUpload = async(item) => {
|
|
|
|
try {
|
2019-10-07 20:56:30 +00:00
|
|
|
await RocketChat.cancelUpload(item);
|
2018-07-17 19:10:27 +00:00
|
|
|
} catch (e) {
|
2019-09-03 19:27:57 +00:00
|
|
|
log(e);
|
2018-07-17 19:10:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
tryAgain = async(item) => {
|
2019-07-29 20:44:39 +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 {
|
2019-10-07 20:56:30 +00:00
|
|
|
const db = database.active;
|
|
|
|
await db.action(async() => {
|
|
|
|
await item.update(() => {
|
|
|
|
item.error = false;
|
|
|
|
});
|
2018-07-17 19:10:27 +00:00
|
|
|
});
|
2019-07-29 20:44:39 +00:00
|
|
|
await RocketChat.sendFileMessage(rid, item, undefined, server, user);
|
2018-07-17 19:10:27 +00:00
|
|
|
} catch (e) {
|
2019-09-03 19:27:57 +00:00
|
|
|
log(e);
|
2018-07-17 19:10:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
renderItemContent = (item) => {
|
2019-12-11 23:01:12 +00:00
|
|
|
const { window, theme } = this.props;
|
2018-09-25 19:28:42 +00:00
|
|
|
|
2018-07-17 19:10:27 +00:00
|
|
|
if (!item.error) {
|
|
|
|
return (
|
|
|
|
[
|
|
|
|
<View key='row' style={styles.row}>
|
2019-12-11 23:01:12 +00:00
|
|
|
<CustomIcon name='file-generic' size={20} color={themes[theme].auxiliaryText} />
|
|
|
|
<Text style={[styles.descriptionContainer, styles.descriptionText, { color: themes[theme].auxiliaryText }]} ellipsizeMode='tail' numberOfLines={1}>
|
2018-07-17 19:10:27 +00:00
|
|
|
{I18n.t('Uploading')} {item.name}
|
|
|
|
</Text>
|
2019-12-11 23:01:12 +00:00
|
|
|
<CustomIcon name='cross' size={20} color={themes[theme].auxiliaryText} onPress={() => this.cancelUpload(item)} />
|
2018-07-17 19:10:27 +00:00
|
|
|
</View>,
|
2019-12-11 23:01:12 +00:00
|
|
|
<View key='progress' style={[styles.progress, { width: (window.width * item.progress) / 100, backgroundColor: themes[theme].tintColor }]} />
|
2018-07-17 19:10:27 +00:00
|
|
|
]
|
|
|
|
);
|
|
|
|
}
|
|
|
|
return (
|
|
|
|
<View style={styles.row}>
|
2019-12-11 23:01:12 +00:00
|
|
|
<CustomIcon name='warning' size={20} color={themes[theme].dangerColor} />
|
2018-07-17 19:10:27 +00:00
|
|
|
<View style={styles.descriptionContainer}>
|
2019-12-11 23:01:12 +00:00
|
|
|
<Text style={[styles.descriptionText, { color: themes[theme].auxiliaryText }]}>{I18n.t('Error_uploading')} {item.name}</Text>
|
2018-07-17 19:10:27 +00:00
|
|
|
<TouchableOpacity onPress={() => this.tryAgain(item)}>
|
2019-12-11 23:01:12 +00:00
|
|
|
<Text style={[styles.tryAgainButtonText, { color: themes[theme].tintColor }]}>{I18n.t('Try_again')}</Text>
|
2018-07-17 19:10:27 +00:00
|
|
|
</TouchableOpacity>
|
|
|
|
</View>
|
2019-12-11 23:01:12 +00:00
|
|
|
<CustomIcon name='cross' size={20} color={themes[theme].auxiliaryText} onPress={() => this.deleteUpload(item)} />
|
2018-07-17 19:10:27 +00:00
|
|
|
</View>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2019-10-07 20:56:30 +00:00
|
|
|
// TODO: transform into stateless and update based on its own observable changes
|
2019-12-11 23:01:12 +00:00
|
|
|
renderItem = (item, index) => {
|
|
|
|
const { theme } = this.props;
|
|
|
|
|
|
|
|
return (
|
|
|
|
<View
|
|
|
|
key={item.path}
|
|
|
|
style={[
|
|
|
|
styles.item,
|
|
|
|
index !== 0 ? { marginTop: 10 } : {},
|
|
|
|
{
|
|
|
|
backgroundColor: themes[theme].chatComponentBackground,
|
|
|
|
borderColor: themes[theme].borderColor
|
|
|
|
}
|
|
|
|
]}
|
|
|
|
>
|
|
|
|
{this.renderItemContent(item)}
|
|
|
|
</View>
|
|
|
|
);
|
|
|
|
}
|
2018-07-17 19:10:27 +00:00
|
|
|
|
|
|
|
render() {
|
|
|
|
const { uploads } = this.state;
|
|
|
|
return (
|
|
|
|
<ScrollView style={styles.container}>
|
|
|
|
{uploads.map((item, i) => this.renderItem(item, i))}
|
|
|
|
</ScrollView>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
2019-08-20 19:24:22 +00:00
|
|
|
|
2019-12-11 23:01:12 +00:00
|
|
|
export default responsive(withTheme(UploadProgress));
|