From 2077671761e9efb212bdec38a090123cb35e44e1 Mon Sep 17 00:00:00 2001 From: Danish Ahmed Mirza <77742477+try-catch-stack@users.noreply.github.com> Date: Wed, 4 May 2022 01:45:16 +0530 Subject: [PATCH] Chore: Migrate containers/Loading to `reanimated` v2 and Hooks (#4073) * Chore : Clean `Loading` component and migrate to reanimated v2 * Remove theme prop * Use colors from useTheme and PixelRatio for image Co-authored-by: Gleidson Daniel Silva --- app/containers/Loading.tsx | 167 ++++++++++------------------------ app/views/ShareView/index.tsx | 2 +- 2 files changed, 51 insertions(+), 118 deletions(-) diff --git a/app/containers/Loading.tsx b/app/containers/Loading.tsx index c457b0401..be7e68513 100644 --- a/app/containers/Loading.tsx +++ b/app/containers/Loading.tsx @@ -1,8 +1,17 @@ -import React from 'react'; -import { Animated, Modal, StyleSheet, View } from 'react-native'; +import React, { useEffect } from 'react'; +import { Modal, StyleSheet, View, PixelRatio } from 'react-native'; +import Animated, { + cancelAnimation, + Extrapolate, + interpolate, + useAnimatedStyle, + useSharedValue, + withRepeat, + withSequence, + withTiming +} from 'react-native-reanimated'; -import { TSupportedThemes, withTheme } from '../theme'; -import { themes } from '../lib/constants'; +import { useTheme } from '../theme'; const styles = StyleSheet.create({ container: { @@ -11,130 +20,54 @@ const styles = StyleSheet.create({ justifyContent: 'center' }, image: { - width: 100, - height: 100, + width: PixelRatio.get() * 40, + height: PixelRatio.get() * 40, resizeMode: 'contain' } }); interface ILoadingProps { visible: boolean; - theme?: TSupportedThemes; } -interface ILoadingState { - scale: Animated.Value; - opacity: Animated.Value; -} - -class Loading extends React.PureComponent { - state = { - scale: new Animated.Value(1), - opacity: new Animated.Value(0) - }; - - private opacityAnimation?: Animated.CompositeAnimation; - - private scaleAnimation?: Animated.CompositeAnimation; - - componentDidMount() { - const { opacity, scale } = this.state; - const { visible } = this.props; - - this.opacityAnimation = Animated.timing(opacity, { - toValue: 1, - duration: 200, - useNativeDriver: true - }); - this.scaleAnimation = Animated.loop( - Animated.sequence([ - Animated.timing(scale, { - toValue: 0, - duration: 1000, - useNativeDriver: true - }), - Animated.timing(scale, { - toValue: 1, - duration: 1000, - useNativeDriver: true - }) - ]) - ); +const Loading = ({ visible }: ILoadingProps): React.ReactElement => { + const opacity = useSharedValue(0); + const scale = useSharedValue(1); + const { colors } = useTheme(); + useEffect(() => { if (visible) { - this.startAnimations(); + opacity.value = withTiming(1, { + duration: 200 + }); + scale.value = withRepeat(withSequence(withTiming(0, { duration: 1000 }), withTiming(1, { duration: 1000 })), -1); } - } + return () => { + cancelAnimation(scale); + }; + }, [opacity, scale, visible]); - componentDidUpdate(prevProps: ILoadingProps) { - const { visible } = this.props; - if (visible && visible !== prevProps.visible) { - this.startAnimations(); - } - } + const animatedOpacity = useAnimatedStyle(() => ({ + opacity: interpolate(opacity.value, [0, 1], [0, colors.backdropOpacity], Extrapolate.CLAMP) + })); + const animatedScale = useAnimatedStyle(() => ({ transform: [{ scale: interpolate(scale.value, [0, 0.5, 1], [1, 1.1, 1]) }] })); - componentWillUnmount() { - if (this.opacityAnimation && this.opacityAnimation.stop) { - this.opacityAnimation.stop(); - } - if (this.scaleAnimation && this.scaleAnimation.stop) { - this.scaleAnimation.stop(); - } - } + return ( + {}}> + + + + + + ); +}; - startAnimations() { - if (this.opacityAnimation && this.opacityAnimation.start) { - this.opacityAnimation.start(); - } - if (this.scaleAnimation && this.scaleAnimation.start) { - this.scaleAnimation.start(); - } - } - - render() { - const { opacity, scale } = this.state; - const { visible, theme } = this.props; - - const scaleAnimation = scale.interpolate({ - inputRange: [0, 0.5, 1], - outputRange: [1, 1.1, 1] - }); - - const opacityAnimation = opacity.interpolate({ - inputRange: [0, 1], - outputRange: [0, themes[theme!].backdropOpacity], - extrapolate: 'clamp' - }); - - return ( - {}}> - - - - - - ); - } -} - -export default withTheme(Loading); +export default Loading; diff --git a/app/views/ShareView/index.tsx b/app/views/ShareView/index.tsx index e98e11628..5dbd9c2e9 100644 --- a/app/views/ShareView/index.tsx +++ b/app/views/ShareView/index.tsx @@ -361,7 +361,7 @@ class ShareView extends Component { {this.renderContent()} - + ); }