-
Notifications
You must be signed in to change notification settings - Fork 5
/
Screen.js
131 lines (121 loc) · 3.7 KB
/
Screen.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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
import {View, StyleSheet} from 'react-native';
import React, {useState} from 'react';
import Card from './Card';
import PlaceholderBackCards from './PlaceholderBackCards';
import Question from './Question';
import PowerIndicators from './PowerIndicators';
import PlaceholderBackStaticCard from './PlaceholderBackStaticCard';
import StartButton from './StartButton';
import useGeneratedCards from './useGeneratedCards';
export default function AnimatedStyleUpdateExample() {
const {getCardByIndex} = useGeneratedCards();
const [currentCard, setCurrentCard] = useState({});
const [currentCardIndex, setCurrentCardIndex] = useState(0);
const [currentMood, setCurrentMood] = useState({happy: [], sad: []});
const [showStartButton, setShowStartButton] = useState(true);
const [showAnimatedReverseCard, setShowAnimatedReverseCard] = useState(false);
const [showReverseCard, setShowReverseCard] = useState(false);
const [showCard, setShowCard] = useState(false);
const [showQuestion, setShowQuestion] = useState(false);
// TODO refactor those settimeouts
const showNextCard = (timeout) => {
setTimeout(() => {
setShowCard(true);
setTimeout(() => {
setShowQuestion(true);
}, 100);
}, timeout);
};
const onStartGame = () => {
setCurrentCard(getCardByIndex(currentCardIndex));
setCurrentCardIndex(currentCardIndex + 1);
setTimeout(() => {
setShowStartButton(false);
setShowAnimatedReverseCard(true);
}, 500);
setTimeout(() => {
setShowReverseCard(true);
setTimeout(() => {
setShowAnimatedReverseCard(false);
}, 100);
}, 2000);
showNextCard(2500);
};
const onChooseLeftAnswer = () => {
setCurrentMood(currentCard.onLeft);
createNewCard();
setTimeout(() => {
setCurrentMood({happy: [], sad: []});
}, 200);
};
const onChooseRightAnswer = () => {
setCurrentMood(currentCard.onRight);
createNewCard();
setTimeout(() => {
setCurrentMood({happy: [], sad: []});
}, 200);
};
const createNewCard = () => {
setShowQuestion(false);
setTimeout(() => {
// let it fly away in peace for 300 ms
setCurrentCard(getCardByIndex(currentCardIndex));
setCurrentCardIndex(currentCardIndex + 1);
setShowCard(false);
}, 300);
showNextCard(700);
};
return (
<View style={styles.wrapper}>
<View style={styles.topWrapper}>
<PowerIndicators currentMood={currentMood} />
</View>
<View style={styles.questionWrapper}>
<Question question={currentCard.question} showQuestion={showQuestion} />
</View>
<View style={styles.cardWrapper}>
{showStartButton && <StartButton onPress={onStartGame} />}
{showAnimatedReverseCard && <PlaceholderBackCards />}
{showReverseCard && <PlaceholderBackStaticCard />}
{showCard && (
<Card
onChooseLeftAnswer={onChooseLeftAnswer}
onChooseRightAnswer={onChooseRightAnswer}
leftText={currentCard.leftText}
rightText={currentCard.rightText}
image={currentCard.image}
backgroundColor={currentCard.background}
/>
)}
</View>
<View style={styles.nameWrapper} />
</View>
);
}
const styles = StyleSheet.create({
wrapper: {
flex: 1,
flexDirection: 'column',
alignItems: 'center',
},
cardWrapper: {
height: 240,
justifyContent: 'center',
alignItems: 'center',
},
questionWrapper: {
height: 100,
justifyContent: 'center',
alignItems: 'center',
},
nameWrapper: {
height: 100,
justifyContent: 'center',
alignItems: 'center',
},
topWrapper: {
width: '100%',
height: 200,
backgroundColor: '#ccc',
},
});