From f13d7ed631de0f95ed537998a08649e95bf3ea2d Mon Sep 17 00:00:00 2001
From: Diego Mello <diegolmello@gmail.com>
Date: Fri, 16 Nov 2018 09:07:13 -0200
Subject: [PATCH] DDP Connection badge animation changed (#533)

---
 app/containers/ConnectionBadge.js | 66 ++++++++-----------------------
 1 file changed, 17 insertions(+), 49 deletions(-)

diff --git a/app/containers/ConnectionBadge.js b/app/containers/ConnectionBadge.js
index 680aa7e0c..c357e980a 100644
--- a/app/containers/ConnectionBadge.js
+++ b/app/containers/ConnectionBadge.js
@@ -6,6 +6,7 @@ import { connect } from 'react-redux';
 import PropTypes from 'prop-types';
 
 import I18n from '../i18n';
+import debounce from '../utils/debounce';
 
 const styles = StyleSheet.create({
 	container: {
@@ -49,62 +50,20 @@ class ConnectionBadge extends Component {
 	static propTypes = {
 		connecting: PropTypes.bool,
 		connected: PropTypes.bool,
-		disconnected: PropTypes.bool
+		disconnected: PropTypes.bool // eslint-disable-line
 	}
 
 	constructor(props) {
 		super(props);
-		this.state = {
-			visible: false
-		};
 		this.animatedValue = new Animated.Value(0);
 	}
 
-	componentDidMount() {
-		const { connecting, disconnected } = this.props;
-		if (connecting || disconnected) {
-			this.animate(1);
-		}
-		this.timeout = setTimeout(() => {
-			const { connected } = this.props;
-			if (connected) {
-				this.timeout = setTimeout(() => {
-					this.animatedValue.stopAnimation();
-					this.animate(0);
-				}, 1000);
-			}
-		}, 1000);
+	componentDidUpdate() {
+		this.show();
 	}
 
-	componentDidUpdate(prevProps) {
-		const { visible } = this.state;
-		const { connecting, connected, disconnected } = this.props;
-
-		if ((connecting && connecting !== prevProps.connecting) || (disconnected && disconnected !== prevProps.disconnected)) {
-			if (!visible) {
-				this.animatedValue.stopAnimation();
-				this.animate(1);
-			}
-		} else if (connected && connected !== prevProps.connected) {
-			if (visible) {
-				if (this.timeout) {
-					clearTimeout(this.timeout);
-				}
-				this.timeout = setTimeout(() => {
-					this.animatedValue.stopAnimation();
-					this.animate(0);
-				}, 1000);
-			}
-		}
-	}
-
-	componentWillUnmount() {
-		if (this.timeout) {
-			clearTimeout(this.timeout);
-		}
-	}
-
-	animate = (toValue) => {
+	// eslint-disable-next-line react/sort-comp
+	animate = debounce((toValue) => {
 		Animated.timing(
 			this.animatedValue,
 			{
@@ -113,8 +72,17 @@ class ConnectionBadge extends Component {
 				easing: Easing.ease,
 				useNativeDriver: true
 			},
-		).start(() => this.setState({ visible: toValue === 1 }));
-	}
+		).start(() => {
+			if (toValue === 1) {
+				if (this.timeout) {
+					clearTimeout(this.timeout);
+				}
+				this.timeout = setTimeout(() => {
+					this.hide();
+				}, 1000);
+			}
+		});
+	}, 300);
 
 	show = () => {
 		this.animate(1);