2018-10-02 12:33:21 +00:00
|
|
|
import React, { Component } from 'react';
|
|
|
|
import {
|
2019-02-12 16:14:11 +00:00
|
|
|
Text, StyleSheet, ActivityIndicator, Animated, Easing
|
2018-10-02 12:33:21 +00:00
|
|
|
} from 'react-native';
|
|
|
|
import { connect } from 'react-redux';
|
|
|
|
import PropTypes from 'prop-types';
|
|
|
|
|
|
|
|
import I18n from '../i18n';
|
2018-11-16 11:07:13 +00:00
|
|
|
import debounce from '../utils/debounce';
|
2019-03-29 19:36:07 +00:00
|
|
|
import sharedStyles from '../views/Styles';
|
|
|
|
import {
|
2019-04-08 12:35:28 +00:00
|
|
|
COLOR_BACKGROUND_CONTAINER, COLOR_DANGER, COLOR_SUCCESS, COLOR_WHITE, COLOR_TEXT_DESCRIPTION
|
2019-03-29 19:36:07 +00:00
|
|
|
} from '../constants/colors';
|
2018-10-02 12:33:21 +00:00
|
|
|
|
|
|
|
const styles = StyleSheet.create({
|
|
|
|
container: {
|
|
|
|
width: '100%',
|
|
|
|
position: 'absolute',
|
|
|
|
top: 0,
|
|
|
|
height: 41,
|
2019-03-29 19:36:07 +00:00
|
|
|
backgroundColor: COLOR_BACKGROUND_CONTAINER,
|
2018-10-02 12:33:21 +00:00
|
|
|
flexDirection: 'row',
|
|
|
|
alignItems: 'center',
|
|
|
|
justifyContent: 'center',
|
|
|
|
elevation: 4
|
|
|
|
},
|
|
|
|
text: {
|
2019-03-29 19:36:07 +00:00
|
|
|
color: COLOR_WHITE,
|
2018-10-02 12:33:21 +00:00
|
|
|
fontSize: 15,
|
2019-03-29 19:36:07 +00:00
|
|
|
...sharedStyles.textRegular
|
2018-10-02 12:33:21 +00:00
|
|
|
},
|
|
|
|
textConnecting: {
|
2019-03-29 19:36:07 +00:00
|
|
|
...sharedStyles.textColorDescription
|
2018-10-02 12:33:21 +00:00
|
|
|
},
|
|
|
|
containerConnected: {
|
2019-03-29 19:36:07 +00:00
|
|
|
backgroundColor: COLOR_SUCCESS
|
2018-10-02 12:33:21 +00:00
|
|
|
},
|
|
|
|
containerOffline: {
|
2019-03-29 19:36:07 +00:00
|
|
|
backgroundColor: COLOR_DANGER
|
2018-10-02 12:33:21 +00:00
|
|
|
},
|
|
|
|
activityIndicator: {
|
|
|
|
marginRight: 15
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
const ANIMATION_DURATION = 300;
|
|
|
|
|
|
|
|
@connect(state => ({
|
|
|
|
connecting: state.meteor.connecting,
|
|
|
|
connected: state.meteor.connected,
|
|
|
|
disconnected: !state.meteor.connecting && !state.meteor.connected
|
|
|
|
}))
|
|
|
|
class ConnectionBadge extends Component {
|
|
|
|
static propTypes = {
|
|
|
|
connecting: PropTypes.bool,
|
|
|
|
connected: PropTypes.bool,
|
2019-02-12 16:14:11 +00:00
|
|
|
disconnected: PropTypes.bool
|
2018-10-02 12:33:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
constructor(props) {
|
|
|
|
super(props);
|
|
|
|
this.animatedValue = new Animated.Value(0);
|
2019-02-12 16:14:11 +00:00
|
|
|
if (props.connecting) {
|
|
|
|
this.show();
|
|
|
|
}
|
2018-10-02 12:33:21 +00:00
|
|
|
}
|
|
|
|
|
2018-11-16 11:07:13 +00:00
|
|
|
componentDidUpdate() {
|
2019-02-12 16:14:11 +00:00
|
|
|
const { connected, disconnected } = this.props;
|
|
|
|
this.show(connected || disconnected);
|
|
|
|
}
|
|
|
|
|
|
|
|
componentWillUnmount() {
|
|
|
|
if (this.timeout) {
|
|
|
|
clearTimeout(this.timeout);
|
|
|
|
}
|
2018-10-02 12:33:21 +00:00
|
|
|
}
|
|
|
|
|
2018-11-16 11:07:13 +00:00
|
|
|
// eslint-disable-next-line react/sort-comp
|
2019-02-12 16:14:11 +00:00
|
|
|
animate = debounce((toValue, autoHide) => {
|
2018-10-02 12:33:21 +00:00
|
|
|
Animated.timing(
|
|
|
|
this.animatedValue,
|
|
|
|
{
|
|
|
|
toValue,
|
|
|
|
duration: ANIMATION_DURATION,
|
|
|
|
easing: Easing.ease,
|
|
|
|
useNativeDriver: true
|
|
|
|
},
|
2018-11-16 11:07:13 +00:00
|
|
|
).start(() => {
|
2019-02-12 16:14:11 +00:00
|
|
|
if (toValue === 1 && autoHide) {
|
2018-11-16 11:07:13 +00:00
|
|
|
if (this.timeout) {
|
|
|
|
clearTimeout(this.timeout);
|
|
|
|
}
|
|
|
|
this.timeout = setTimeout(() => {
|
|
|
|
this.hide();
|
|
|
|
}, 1000);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}, 300);
|
2018-10-02 12:33:21 +00:00
|
|
|
|
2019-02-12 16:14:11 +00:00
|
|
|
show = (autoHide) => {
|
|
|
|
this.animate(1, autoHide);
|
2018-10-02 12:33:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
hide = () => {
|
|
|
|
this.animate(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
render() {
|
|
|
|
const { connecting, connected } = this.props;
|
|
|
|
|
|
|
|
const translateY = this.animatedValue.interpolate({
|
|
|
|
inputRange: [0, 1],
|
2018-10-17 16:30:00 +00:00
|
|
|
outputRange: [-42, 0]
|
2018-10-02 12:33:21 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
if (connecting) {
|
|
|
|
return (
|
2019-02-12 16:14:11 +00:00
|
|
|
<Animated.View style={[styles.container, { transform: [{ translateY }] }]}>
|
2019-04-08 12:35:28 +00:00
|
|
|
<ActivityIndicator color={COLOR_TEXT_DESCRIPTION} style={styles.activityIndicator} />
|
2019-02-12 16:14:11 +00:00
|
|
|
<Text style={[styles.text, styles.textConnecting]}>{I18n.t('Connecting')}</Text>
|
|
|
|
</Animated.View>
|
2018-10-02 12:33:21 +00:00
|
|
|
);
|
|
|
|
} else if (connected) {
|
|
|
|
return (
|
2019-02-12 16:14:11 +00:00
|
|
|
<Animated.View style={[styles.container, styles.containerConnected, { transform: [{ translateY }] }]}>
|
|
|
|
<Text style={styles.text}>{I18n.t('Connected')}</Text>
|
|
|
|
</Animated.View>
|
2018-10-02 12:33:21 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
2019-02-12 16:14:11 +00:00
|
|
|
<Animated.View style={[styles.container, styles.containerOffline, { transform: [{ translateY }] }]}>
|
|
|
|
<Text style={styles.text}>{I18n.t('Offline')}</Text>
|
|
|
|
</Animated.View>
|
2018-10-02 12:33:21 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export default ConnectionBadge;
|