-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathSaveEventButton.js
109 lines (97 loc) · 2.55 KB
/
SaveEventButton.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
// @flow
import React from "react";
import { Animated, Easing, StyleSheet } from "react-native";
import ReactNativeHapticFeedback from "react-native-haptic-feedback";
import LottieView from "lottie-react-native";
import Touchable from "./Touchable";
import heartAnimationLight from "../../assets/animations/save-event-light.json";
import heartAnimationDark from "../../assets/animations/save-event-dark.json";
import text from "../constants/text";
type Props = {
active: boolean,
onDark: boolean,
onPress: boolean => void
};
type State = {
animating: boolean,
progress: ?Object
};
export default class SaveEventButton extends React.Component<Props, State> {
static defaultProps = {
active: false,
onDark: false
};
static getDerivedStateFromProps(nextProps: Props, prevState: State) {
if (!prevState.animating || !prevState.progress) {
const value = nextProps.active ? 1 : 0;
return {
progress: new Animated.Value(value),
animating: false
};
}
return null;
}
state = {
animating: false,
progress: null
};
shouldComponentUpdate(nextProps: Props) {
return (
this.props.active !== nextProps.active ||
this.props.onDark !== nextProps.onDark ||
this.props.onPress !== nextProps.onPress
);
}
componentWillUnmount() {
this.completeAnimation();
}
startAnimation = (value: number) => {
if (this.state.progress) {
Animated.timing(this.state.progress, {
toValue: value,
duration: value * 800,
easing: Easing.linear,
useNativeDriver: true
}).start(this.completeAnimation);
this.setState({ animating: true });
if (value) {
ReactNativeHapticFeedback.trigger("impactHeavy");
}
}
};
completeAnimation = () => {
if (this.state.animating) {
this.setState({ animating: false });
}
};
handlePress = () => {
this.startAnimation(!this.props.active ? 1 : 0);
this.props.onPress(!this.props.active);
};
render() {
const source = this.props.onDark ? heartAnimationLight : heartAnimationDark;
return (
<Touchable
accessibilityLabel={
this.props.active
? text.saveEventButtonUnSaveEvent
: text.saveEventButtonSaveEvent
}
onPress={this.handlePress}
style={styles.button}
>
<LottieView
progress={this.state.progress}
source={source}
loop={false}
/>
</Touchable>
);
}
}
const styles = StyleSheet.create({
button: {
width: 48,
height: 48
}
});