-
Notifications
You must be signed in to change notification settings - Fork 0
/
App.js
82 lines (69 loc) · 2.17 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
import { StatusBar } from "expo-status-bar";
import { SafeAreaView, StyleSheet, Text, View } from "react-native";
import StartGameScreen from "./screens/StartGameScreen";
import GameOverScreen from "./screens/GameOverScreen";
import { LinearGradient } from "expo-linear-gradient";
import { act, useCallback, useState } from "react";
import GameScreen from "./screens/GameScreen";
import Colors from "./constants/colors";
import { useFonts } from "expo-font";
import * as SplashScreen from "expo-splash-screen";
SplashScreen.preventAutoHideAsync();
export default function App() {
const [userNumber, setUserNumber] = useState();
const [isGameOver, setIsGameOver] = useState(true);
const [guessRounds, setGuessRounds] = useState(0);
const [fontsLoaded, fontError] = useFonts({
"open-sans": require("./assets/fonts/OpenSans-Regular.ttf"),
"open-sans-bold": require("./assets/fonts/OpenSans-Bold.ttf"),
});
const onLayoutRootView = useCallback(async () => {
if (fontsLoaded || fontError) {
await SplashScreen.hideAsync();
}
}, [fontsLoaded, fontError]);
if (!fontsLoaded && !fontError) {
return null;
}
function pickedNumberHandler(pickedNumber) {
setUserNumber(pickedNumber);
setIsGameOver(false);
}
function gameOverHandler(numberOfRounds) {
setIsGameOver(true);
setGuessRounds(numberOfRounds);
}
function startNewGameHandler() {
setUserNumber(null);
setGuessRounds(0);
}
let activeScreen = <StartGameScreen onPickNumber={pickedNumberHandler} />;
if (userNumber && !isGameOver) {
activeScreen = (
<GameScreen userNumber={userNumber} onGameOver={gameOverHandler} />
);
}
if (isGameOver && userNumber) {
activeScreen = (
<GameOverScreen
userNumber={userNumber}
roundsNumber={guessRounds}
onSatrtNewGame={startNewGameHandler}
/>
);
}
return (
<LinearGradient
colors={[Colors.primary700, Colors.accent500]}
style={styles.rootScreen}
onLayout={onLayoutRootView}
>
<SafeAreaView style={styles.rootScreen}>{activeScreen}</SafeAreaView>
</LinearGradient>
);
}
const styles = StyleSheet.create({
rootScreen: {
flex: 1,
},
});