-
Notifications
You must be signed in to change notification settings - Fork 0
/
App.tsx
90 lines (77 loc) · 2.5 KB
/
App.tsx
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
import * as Font from "expo-font";
import * as Location from "expo-location";
import { LocationObject } from "expo-location";
import * as SplashScreen from "expo-splash-screen";
import React, { createContext, useCallback, useEffect, useState } from "react";
import AnimatedLoader from "react-native-animated-loader";
import { Text, View, StyleSheet } from "react-native";
import Main from "./src/screens/Main";
import Center from "./src/utils/Center";
import createEmptyLocation from "./src/utils/LocationUtils";
const App = () => {
const [isLoadingComplete, setLoadingComplete] = useState(false);
const [loadingText, setLoadingText] = useState("Uygulama yükleniyor!");
const [location, setLocation] = useState<LocationObject>(
createEmptyLocation()
);
const [locationErrorMessage, setLocationErrorMessage] = useState<string>("");
useEffect(() => {
const load = async () => {
try {
await loadResourcesAsync();
await loadInitialLocation(setLocation, setLocationErrorMessage);
await handleFinishLoading(setLoadingComplete);
} catch (error) {
console.warn(error);
} finally {
setLoadingComplete(true);
}
};
load();
}, []);
const onLayoutRootView = useCallback(async () => {
if (isLoadingComplete) {
await SplashScreen.hideAsync();
}
}, [isLoadingComplete]);
if (!isLoadingComplete) {
return (
<AnimatedLoader
visible={!isLoadingComplete}
overlayColor="rgba(255,255,255,0.75)"
animationStyle={styles.lottie}
speed={1}
source={require("./assets/animations/loading.json")}
>
<Text>{loadingText}</Text>
</AnimatedLoader>
);
}
return <Main initialLocation={location} />;
};
const styles = StyleSheet.create({
lottie: {
width: 256,
height: 256,
},
});
const loadResourcesAsync = async () => {
await Promise.all([Font.loadAsync({})]);
};
const handleFinishLoading = async (
setLoadingComplete: React.Dispatch<React.SetStateAction<boolean>>
) => {
setLoadingComplete(true);
};
const loadInitialLocation = async (
setLocation: React.Dispatch<React.SetStateAction<LocationObject>>,
setLocationErrorMessage: React.Dispatch<React.SetStateAction<string>>
) => {
let { status } = await Location.getBackgroundPermissionsAsync();
if (status !== "granted") {
setLocationErrorMessage("Permission to access location was denied");
}
let location = await Location.getCurrentPositionAsync({});
setLocation(location);
};
export default App;