-
Notifications
You must be signed in to change notification settings - Fork 1
/
App.js
142 lines (125 loc) · 3.94 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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
import React, { useEffect, useState } from "react";
import { StyleSheet, View } from "react-native";
import * as Font from "expo-font";
import AppLoading from "expo-app-loading";
import DrinksContext from "./context/drinks-context";
import InputScreen from "./screens/InputScreen";
import PlayScreen from "./screens/PlayScreen";
import StartScreen from "./screens/StartScreen";
import COLORS from "./constants/colors";
import {
initShot,
fetchShots,
initSettings,
defaultShots,
fetchSettings,
insertSettings,
} from "./database/sqlite";
// Return default font-family
const fetchFonts = () => {
return Font.loadAsync({
"assistant-regular": require("./assets/fonts/Assistant-SemiBold.ttf"),
"assistant-bold": require("./assets/fonts/Assistant-ExtraBold.ttf"),
});
};
export default function App() {
// Check if initial data has been loaded
// Used for fonts
const [dataLoaded, setDataLoaded] = useState(false);
// Manage currently active screen
const [content, setContent] = useState("StartScreen");
const [savedDrinks, setSavedDrinks] = useState([]);
const [savedWheel, setSavedWheel] = useState([]);
const [savedVibration, setSavedVibration] = useState([]);
// Handle a screen change
const changeScreenHandler = (selectedScreen) => {
setContent(selectedScreen);
};
// Initialize SQLite database
async function initData() {
try {
await initShot();
await initSettings();
await defaultShots();
} catch (err) {
console.log("Initializing database failed.");
console.log(err);
}
}
// Fetch data from sqlite database
// Save into context with the help of the useState savedShots
async function fetchData() {
try {
const fetch = await fetchShots();
const dataFetch = fetch.rows._array;
setSavedDrinks(dataFetch.reverse());
let status = await fetchSettings();
let dataStatus = status.rows._array;
if (!dataStatus.length) {
newStatus = await insertSettings(1, 1);
status = await fetchSettings();
dataStatus = status.rows._array;
}
setSavedWheel(dataStatus[0].wheel);
setSavedVibration(dataStatus[0].vibration);
console.log("Data fetched from database");
} catch (error) {
console.log("Fetching data from database failed");
console.log(error);
}
}
// Execute database loading
useEffect(() => {
async function loadDatabase() {
await initData();
await fetchData();
}
loadDatabase();
}, []);
// Set StartScreen as the initial active screen
// @props changeScreenHandler for screen to call if screen change needs to be executed
let activeScreen = <StartScreen onChangeScreen={changeScreenHandler} />;
// Alter currently active screen if content / screen was altered by changeScreenHandler
if (content !== "StartScreen") {
if (content === "PlayScreen") {
activeScreen = <PlayScreen onChangeScreen={changeScreenHandler} />;
} else if (content === "InputScreen") {
activeScreen = <InputScreen onChangeScreen={changeScreenHandler} />;
} else {
activeScreen = <StartScreen onChangeScreen={changeScreenHandler} />;
}
}
// Load initial data of the app
// Call fetchFonts to get default fonts
if (!dataLoaded) {
return (
<AppLoading
startAsync={fetchFonts}
onFinish={() => setDataLoaded(true)}
onError={(err) => console.warn(err)}
/>
);
}
return (
<DrinksContext.Provider
value={{
savedDrinks: savedDrinks,
setSavedDrinks: setSavedDrinks,
savedWheel: savedWheel,
setSavedWheel: setSavedWheel,
savedVibration: savedVibration,
setSavedVibration: setSavedVibration,
}}
>
<View style={styles.screen}>{activeScreen}</View>
</DrinksContext.Provider>
);
}
const styles = StyleSheet.create({
screen: {
backgroundColor: COLORS.primaryColor,
flex: 1,
alignItems: "center",
justifyContent: "center",
},
});