-
Notifications
You must be signed in to change notification settings - Fork 0
/
App.js
97 lines (86 loc) · 2.51 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
import { useState, useEffect, useRef, useLayoutEffect } from "react";
import { StatusBar, AppState, LogBox, Alert, BackHandler } from "react-native";
import AppNavigator from "./config/navigations";
import { Provider as PaperProvider, DefaultTheme } from "react-native-paper";
import { enableScreens } from "react-native-screens";
import SplashScreen from "./pages/Splash";
enableScreens();
console.disableYellowBox = true;
const theme = {
...DefaultTheme,
roundness: 2,
colors: {
...DefaultTheme.colors,
primary: "#607D8B",
accent: "#f1c40f",
},
};
LogBox.ignoreAllLogs();
const App = () => {
const appState = useRef(AppState.currentState);
const [outOfApp, setOutOfApp] = useState(true);
const [appStateVisible, setAppStateVisible] = useState(appState.current);
useLayoutEffect(() => {
const subscription = AppState.addEventListener("change", (nextAppState) => {
if (
appState.current.match(/inactive|background/) &&
nextAppState === "active"
) {
console.log("App has come to the foreground!");
}
appState.current = nextAppState;
setAppStateVisible(appState.current);
appStateChangeListener(appState.current);
});
return () => {
subscription.remove();
};
}, []);
const appStateChangeListener = (val) => {
if (val == "active") {
setTimeout(() => {
setOutOfApp(false);
}, 1500);
//val will be "active" only when app is in foreground
// use navigation to navigate to Splashscreen
} else {
setOutOfApp(true);
}
};
// const expensiveValue = useMemo(() => [outOfApp]);
useEffect(() => {
const backAction = () => {
BackHandler.exitApp();
// Alert.alert("Hold on!", "Are you sure you want to close app?", [
// {
// text: "Cancel",
// onPress: () => null,
// style: "cancel",
// },
// { text: "YES", onPress: () => BackHandler.exitApp() },
// ]);
return true;
};
const backHandler = BackHandler.addEventListener(
"hardwareBackPress",
backAction
);
return () => backHandler.remove();
}, []);
if (outOfApp) {
return appStateVisible === "active" ? <SplashScreen /> : "";
} else {
return (
<PaperProvider theme={theme}>
<StatusBar
hidden={false}
translucent={true}
// backgroundColor={styles.headerColor}
barStyle="light-content"
/>
<AppNavigator />
</PaperProvider>
);
}
};
export default App;