-
Notifications
You must be signed in to change notification settings - Fork 0
/
App.tsx
244 lines (216 loc) · 7.33 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
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
import { createNativeStackNavigator } from "@react-navigation/native-stack";
import { SafeAreaProvider } from "react-native-safe-area-context";
import { useContext, useEffect, useState } from "react";
import AsyncStorage from "@react-native-async-storage/async-storage";
import { BackHandler, StyleSheet, useColorScheme } from "react-native";
import { NavigationContainer } from "@react-navigation/native";
import * as SplashScreen from "expo-splash-screen";
import {
MD3LightTheme as DefaultTheme,
PaperProvider,
} from "react-native-paper";
import * as Location from "expo-location";
// GOOGLE SIGN IN IMPORTS
import * as Google from "expo-auth-session/providers/google";
import * as WebBrowser from "expo-web-browser";
import {
GoogleAuthProvider,
getAuth,
onAuthStateChanged,
signInWithCredential,
} from "firebase/auth";
const auth = getAuth();
// Screens
import Auth from "./src/screens/Auth";
import Login from "./src/screens/Login";
import Register from "./src/screens/Register";
import Onboarding from "./src/screens/Onboarding";
import Profile from "./src/screens/Profile";
import NeighbourhoodDetail from "./src/screens/NeighbourhoodDetail";
import AddReport from "./src/screens/AddReport";
import Alert from "./src/screens/Alert";
import { RootStackParamList } from "./src/types/RootStackParamList";
import { colors } from "./src/utils/colors";
import Home from "./src/screens/Home";
import { UserLocationContext } from "./src/context/user-location.context";
import { UserContext, UserProvider } from "./src/context/user.context";
import { customDarkTheme } from "./src/utils/theme";
import { OAUTH_CLIENT_KEY_ANDROID, OAUTH_CLIENT_KEY_IOS } from "./config/oauth";
import { getStorage } from 'firebase/storage';
const Stack = createNativeStackNavigator<RootStackParamList>();
WebBrowser.maybeCompleteAuthSession();
const App = () => {
const {onboarded, getStorage} = useContext(UserContext);
console.log(onboarded);
const [userInfo, setUserInfo] = useState();
const [request, response, promptAsync] = Google.useAuthRequest({
iosClientId: OAUTH_CLIENT_KEY_IOS,
androidClientId: OAUTH_CLIENT_KEY_ANDROID,
});
useEffect(() => {
if (response?.type === "success") {
const { id_token } = response.params;
const credential = GoogleAuthProvider.credential(id_token);
// Handle the sign-in
}
}, [response]);
const [location, setLocation] = useState<any>(null);
const [errorMsg, setErrorMsg] = useState<string>("");
const [onboardingCompleted, setOnboardingCompleted] = useState<any>();
// loading the fonts onto the app
// UseEffect for splash screen
useEffect(() => {
async function prepare() {
await SplashScreen.preventAutoHideAsync();
}
prepare();
}, []);
// useEffect(() => {
// checkOnboarding();
// }, []);
const handleBackButton = () => {
BackHandler.exitApp();
return true;
};
// handle functionality when a user presses back (ANDROID ONLY)
useEffect(() => {
const backHandler = BackHandler.addEventListener(
"hardwareBackPress",
handleBackButton
);
return () => {
backHandler.remove();
};
}, []);
//fetch users location
useEffect(() => {
(async () => {
let { status } = await Location.requestForegroundPermissionsAsync();
if (status !== "granted") {
setErrorMsg("Permission to access location was denied");
return;
}
let location = await Location.getCurrentPositionAsync({});
setLocation(location);
})();
}, []);
let text = "Waiting..";
if (errorMsg) {
text = errorMsg;
} else if (location) {
text = JSON.stringify(location);
}
SplashScreen.hideAsync();
const darkTheme = {
...customDarkTheme,
};
const theme = {
...DefaultTheme,
myOwnProperty: true,
colors: {
...DefaultTheme.colors,
},
};
const colorScheme = useColorScheme();
const customTheme = colorScheme === "dark" ? darkTheme : darkTheme;
//checked if the user has completed the onboarding
// const checkOnboarding = async () => {
// try {
// const value: any = await AsyncStorage.getItem("@onboardingCompleted");
// if (value == true) {
// setOnboardingCompleted(true);
// }
// console.log("onboarding completed: ", onboardingCompleted);
// } catch (error) {
// console.log("Error @checkOnboarding: ", error);
// }
// };
return (
<PaperProvider theme={customTheme}>
<SafeAreaProvider>
<UserLocationContext.Provider value={{ location, setLocation }}>
<UserProvider>
<NavigationContainer>
<Stack.Navigator
initialRouteName={!onboarded ? "Onboarding" : "Auth"}
screenOptions={{ headerShown: false }}
>
<Stack.Screen
name="Auth"
component={Auth}
options={{
gestureEnabled: false, // Disable swipe-back gesture for this screen
}}
/>
<Stack.Screen
name="Login"
component={Login}
options={{
gestureEnabled: false, // Disable swipe-back gesture for this screen
}}
/>
<Stack.Screen
name="Register"
options={{
gestureEnabled: false,
}}
>
{(props) => <Register {...props} promptAsync={promptAsync} />}
</Stack.Screen>
<Stack.Screen
name="Onboarding"
component={Onboarding}
options={{
gestureEnabled: false, // Disable swipe-back gesture for this screen
}}
/>
<Stack.Screen
name="Profile"
component={Profile}
options={{
gestureEnabled: false, // Disable swipe-back gesture for this screen
}}
/>
<Stack.Screen
name="Detail"
component={NeighbourhoodDetail}
options={{
gestureEnabled: false, // Disable swipe-back gesture for this screen
}}
/>
<Stack.Screen
name="Report"
component={AddReport}
options={{
gestureEnabled: false, // Disable swipe-back gesture for this screen
}}
/>
<Stack.Screen
name="Alert"
component={Alert}
options={{
gestureEnabled: false, // Disable swipe-back gesture for this screen
}}
/>
<Stack.Screen
name="Home"
component={Home}
options={{
gestureEnabled: false, // Disable swipe-back gesture for this screen
}}
/>
{/* Add more screens here */}
</Stack.Navigator>
</NavigationContainer>
</UserProvider>
</UserLocationContext.Provider>
</SafeAreaProvider>
</PaperProvider>
);
};
const styles = StyleSheet.create({
container: {
flex: 1,
},
});
export default App;