2018-04-24 19:34:03 +00:00
|
|
|
import React from 'react';
|
2021-09-13 20:41:05 +00:00
|
|
|
import { Animated, Modal, StyleSheet, View } from 'react-native';
|
|
|
|
|
2021-08-16 21:14:56 +00:00
|
|
|
import { withTheme } from '../theme';
|
2022-04-07 14:10:03 +00:00
|
|
|
import { themes } from '../lib/constants';
|
2018-04-24 19:34:03 +00:00
|
|
|
|
|
|
|
const styles = StyleSheet.create({
|
|
|
|
container: {
|
|
|
|
flex: 1,
|
|
|
|
alignItems: 'center',
|
2021-08-16 21:14:56 +00:00
|
|
|
justifyContent: 'center'
|
2018-04-24 19:34:03 +00:00
|
|
|
},
|
|
|
|
image: {
|
|
|
|
width: 100,
|
|
|
|
height: 100,
|
|
|
|
resizeMode: 'contain'
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2021-09-13 20:41:05 +00:00
|
|
|
interface ILoadingProps {
|
|
|
|
visible: boolean;
|
2022-01-17 16:10:39 +00:00
|
|
|
theme?: string;
|
2021-09-13 20:41:05 +00:00
|
|
|
}
|
2018-04-24 19:34:03 +00:00
|
|
|
|
2022-03-18 15:02:04 +00:00
|
|
|
interface ILoadingState {
|
|
|
|
scale: Animated.Value;
|
|
|
|
opacity: Animated.Value;
|
|
|
|
}
|
|
|
|
|
|
|
|
class Loading extends React.PureComponent<ILoadingProps, ILoadingState> {
|
2018-04-24 19:34:03 +00:00
|
|
|
state = {
|
|
|
|
scale: new Animated.Value(1),
|
|
|
|
opacity: new Animated.Value(0)
|
2021-09-13 20:41:05 +00:00
|
|
|
};
|
|
|
|
|
2022-03-18 15:02:04 +00:00
|
|
|
private opacityAnimation?: Animated.CompositeAnimation;
|
2021-09-13 20:41:05 +00:00
|
|
|
|
2022-03-18 15:02:04 +00:00
|
|
|
private scaleAnimation?: Animated.CompositeAnimation;
|
2018-04-24 19:34:03 +00:00
|
|
|
|
|
|
|
componentDidMount() {
|
2018-09-25 19:28:42 +00:00
|
|
|
const { opacity, scale } = this.state;
|
|
|
|
const { visible } = this.props;
|
|
|
|
|
2021-09-13 20:41:05 +00:00
|
|
|
this.opacityAnimation = Animated.timing(opacity, {
|
|
|
|
toValue: 1,
|
|
|
|
duration: 200,
|
|
|
|
useNativeDriver: true
|
|
|
|
});
|
|
|
|
this.scaleAnimation = Animated.loop(
|
|
|
|
Animated.sequence([
|
|
|
|
Animated.timing(scale, {
|
2018-04-24 19:34:03 +00:00
|
|
|
toValue: 0,
|
|
|
|
duration: 1000,
|
|
|
|
useNativeDriver: true
|
2021-09-13 20:41:05 +00:00
|
|
|
}),
|
|
|
|
Animated.timing(scale, {
|
2018-04-24 19:34:03 +00:00
|
|
|
toValue: 1,
|
|
|
|
duration: 1000,
|
|
|
|
useNativeDriver: true
|
2021-09-13 20:41:05 +00:00
|
|
|
})
|
|
|
|
])
|
|
|
|
);
|
2018-04-24 19:34:03 +00:00
|
|
|
|
2018-09-25 19:28:42 +00:00
|
|
|
if (visible) {
|
2018-04-24 19:34:03 +00:00
|
|
|
this.startAnimations();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-18 15:02:04 +00:00
|
|
|
componentDidUpdate(prevProps: ILoadingProps) {
|
2018-09-25 19:28:42 +00:00
|
|
|
const { visible } = this.props;
|
|
|
|
if (visible && visible !== prevProps.visible) {
|
2018-04-24 19:34:03 +00:00
|
|
|
this.startAnimations();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
componentWillUnmount() {
|
2018-08-01 19:35:06 +00:00
|
|
|
if (this.opacityAnimation && this.opacityAnimation.stop) {
|
|
|
|
this.opacityAnimation.stop();
|
|
|
|
}
|
|
|
|
if (this.scaleAnimation && this.scaleAnimation.stop) {
|
|
|
|
this.scaleAnimation.stop();
|
|
|
|
}
|
2018-04-24 19:34:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
startAnimations() {
|
2018-08-01 19:35:06 +00:00
|
|
|
if (this.opacityAnimation && this.opacityAnimation.start) {
|
|
|
|
this.opacityAnimation.start();
|
|
|
|
}
|
|
|
|
if (this.scaleAnimation && this.scaleAnimation.start) {
|
|
|
|
this.scaleAnimation.start();
|
|
|
|
}
|
2018-04-24 19:34:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
render() {
|
2018-09-25 19:28:42 +00:00
|
|
|
const { opacity, scale } = this.state;
|
2021-08-16 21:14:56 +00:00
|
|
|
const { visible, theme } = this.props;
|
2018-09-25 19:28:42 +00:00
|
|
|
|
|
|
|
const scaleAnimation = scale.interpolate({
|
2018-04-24 19:34:03 +00:00
|
|
|
inputRange: [0, 0.5, 1],
|
|
|
|
outputRange: [1, 1.1, 1]
|
|
|
|
});
|
2021-08-16 21:14:56 +00:00
|
|
|
|
|
|
|
const opacityAnimation = opacity.interpolate({
|
|
|
|
inputRange: [0, 1],
|
2022-01-17 16:10:39 +00:00
|
|
|
outputRange: [0, themes[theme!].backdropOpacity],
|
2021-08-16 21:14:56 +00:00
|
|
|
extrapolate: 'clamp'
|
|
|
|
});
|
|
|
|
|
2018-04-24 19:34:03 +00:00
|
|
|
return (
|
2021-09-13 20:41:05 +00:00
|
|
|
<Modal visible={visible} transparent onRequestClose={() => {}}>
|
|
|
|
<View style={styles.container} testID='loading'>
|
2021-08-16 21:14:56 +00:00
|
|
|
<Animated.View
|
2021-09-13 20:41:05 +00:00
|
|
|
style={[
|
|
|
|
{
|
2022-03-18 15:02:04 +00:00
|
|
|
...StyleSheet.absoluteFillObject,
|
2022-01-17 16:10:39 +00:00
|
|
|
backgroundColor: themes[theme!].backdropColor,
|
2021-09-13 20:41:05 +00:00
|
|
|
opacity: opacityAnimation
|
|
|
|
}
|
|
|
|
]}
|
2021-08-16 21:14:56 +00:00
|
|
|
/>
|
2018-04-24 19:34:03 +00:00
|
|
|
<Animated.Image
|
|
|
|
source={require('../static/images/logo.png')}
|
2021-09-13 20:41:05 +00:00
|
|
|
style={[
|
|
|
|
styles.image,
|
|
|
|
{
|
|
|
|
transform: [
|
|
|
|
{
|
|
|
|
scale: scaleAnimation
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
|
|
|
]}
|
2018-04-24 19:34:03 +00:00
|
|
|
/>
|
|
|
|
</View>
|
|
|
|
</Modal>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
2021-08-16 21:14:56 +00:00
|
|
|
|
2021-09-13 20:41:05 +00:00
|
|
|
export default withTheme(Loading);
|