-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathApp.js
117 lines (105 loc) · 3.21 KB
/
App.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
import React, { useState, useEffect } from "react";
import { View, Alert, Text } from "react-native";
import styles from "./App.styles";
import ImageMultipleChoiceQuestion from "./src/components/ImageMultipleChoiceQuestion";
import OpenEndedQuestion from "./src/components/OpenEndedQuestion";
// import questions from "./assets/data/imageMulatipleChoiceQuestions";
// import questions from "./assets/data/openEndedQuestions";
import questions from "./assets/data/allQuestions";
import HeaderBar from "./src/components/HeaderBar";
import AsyncStorage from "@react-native-async-storage/async-storage";
import FillInTheBlank from "./src/components/FillInTheBlank";
const App = () => {
const [currentQuestionIndex, setcurrentQuestionIndex] = useState(0);
const [currentQuestion, setCurrentQuestion] = useState(
questions[currentQuestionIndex]
);
const [lives, setLives] = useState(5);
const [hasLoaded, setHasLoaded] = useState(false);
useEffect(() => {
if (currentQuestionIndex >= questions.length) {
Alert.alert("You Won");
setLives(5);
setcurrentQuestionIndex(0);
} else {
setCurrentQuestion(questions[currentQuestionIndex]);
}
}, [currentQuestionIndex]);
useEffect(() => {
loadData();
}, []);
useEffect(() => {
if (hasLoaded) saveData();
}, [lives, currentQuestionIndex, hasLoaded]);
const onCorrect = () => {
setcurrentQuestionIndex(currentQuestionIndex + 1);
};
const reset = () => {
setLives(5);
setcurrentQuestionIndex(0);
};
const onWrong = () => {
if (lives <= 1) {
setLives(0);
Alert.alert("Game Over !", "Try Again", [
{
text: "Try again",
onPress: reset,
},
]);
} else {
Alert.alert("Wrong !");
setLives(lives - 1);
}
};
const saveData = async () => {
await AsyncStorage.setItem("lives", lives.toString());
await AsyncStorage.setItem(
"currentQuestionIndex",
currentQuestionIndex.toString()
);
};
const loadData = async () => {
const loadedLives = await AsyncStorage.getItem("lives");
if (loadedLives) setLives(parseInt(loadedLives));
const loadedCurrentQuestionIndex = await AsyncStorage.getItem(
"currentQuestionIndex"
);
if (loadedCurrentQuestionIndex)
setcurrentQuestionIndex(parseInt(loadedCurrentQuestionIndex));
setHasLoaded(true);
};
if (!hasLoaded) {
return <Text style={styles.root}>Loading......</Text>;
}
return (
<View style={styles.root}>
<HeaderBar
progress={currentQuestionIndex / questions.length}
lives={lives}
/>
{currentQuestion.type === "IMAGE_MULTIPLE_CHOICE" && (
<ImageMultipleChoiceQuestion
question={currentQuestion}
onCorrect={onCorrect}
onWrong={onWrong}
/>
)}
{currentQuestion.type === "OPEN_ENDED" && (
<OpenEndedQuestion
question={currentQuestion}
onCorrect={onCorrect}
onWrong={onWrong}
/>
)}
{currentQuestion.type === "FILL_IN_THE_BLANK" && (
<FillInTheBlank
question={currentQuestion}
onCorrect={onCorrect}
onWrong={onWrong}
/>
)}
</View>
);
};
export default App;