-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathApp.js
391 lines (370 loc) · 12.2 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
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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
import React from "react";
import {
LogBox,
StatusBar,
Pressable,
Animated,
View,
Dimensions,
} from "react-native";
import { NavigationContainer, DefaultTheme } from "@react-navigation/native";
import { createNativeStackNavigator } from "@react-navigation/native-stack";
import { createBottomTabNavigator } from "@react-navigation/bottom-tabs";
import WelcomeScreen from "./screens/WelcomeScreen";
import VerificationCodeScreen from "./screens/VerificationCodeScreen";
import AnimatedSplash from "react-native-animated-splash-screen";
import OAuthAddSteps from "./screens/OAuthAdditionalStep";
import * as RootNavigation from "./utils/RootNavigation";
import { useAuth } from "./hooks/useAuth";
import HomeScreen from "./screens/Home";
import firebase from "firebase/compat/app";
import AsyncStorage from "@react-native-async-storage/async-storage";
import AddProfilePicture from "./screens/AddProfilePicture";
import { TailwindProvider } from "tailwindcss-react-native";
import AvatarSelected from "./screens/AvatarSelected";
import { CropAvatar, cropViewRef } from "./screens/CropAvatar";
import DefaultHeader from "./components/DefaultHeader";
import { firebaseConfig } from "./firebase";
import Ionicons from "react-native-vector-icons/Ionicons";
import MainHeader from "./components/MainHeader";
import SideMenu from "@chakrahq/react-native-side-menu";
import MainSideMenu from "./components/MainSideMenu";
import styles from "./assets/stylesheet/styles";
import { enableFreeze } from "react-native-screens";
enableFreeze(true);
firebase.initializeApp(firebaseConfig);
const db = firebase.firestore();
const MyTheme = {
...DefaultTheme,
colors: {
...DefaultTheme.colors,
primary: "#10A7FF",
background: "white",
},
};
const window = Dimensions.get("window");
export default function App() {
const [isSplashReady, setIsSplashReady] = React.useState(false);
const [isAddNeeded, setIsAddNeeded] = React.useState(null);
const [isAvatarUpdateNeeded, setIsAvatarUpdateNeeded] = React.useState(null);
const [isNewUser, setIsNewUser] = React.useState(null);
const Stack = createNativeStackNavigator();
const { isSignedIn, signOutUser } = useAuth();
// const isSignedIn = true;
// const signOutUser = () => {};
StatusBar.setBarStyle("dark-content", true);
LogBox.ignoreLogs([
"Warning: Can't perform a React state update on an unmounted component. This is a no-op, but it indicates a memory leak in your application. To fix, cancel all subscriptions and asynchronous tasks in a useEffect cleanup function.",
]);
const animatedValue1 = new Animated.Value(1);
const animatedValue2 = new Animated.Value(0);
const check = async () => {
if (
(await AsyncStorage.getItem("user-more")) != null &&
(await AsyncStorage.getItem("user-more")) != "null"
) {
try {
db.collection("users")
.doc(JSON.parse(await AsyncStorage.getItem("user-more")).uid)
.get()
.then((res) => {
setIsAddNeeded(res.data().additionalSteps);
setIsAvatarUpdateNeeded(res.data().needToUpdateAvatar);
})
.catch((err) => {
setIsAddNeeded(true);
setIsAvatarUpdateNeeded(true);
console.log(err);
});
} catch (error) {
console.log(error);
signOutUser();
}
} else {
console.log("no user-more");
}
console.log("im a check function");
};
React.useEffect(() => {
async function check() {
if (
(await AsyncStorage.getItem("new-user")) != null &&
(await AsyncStorage.getItem("new-user")) != "null"
) {
setIsNewUser(JSON.parse(await AsyncStorage.getItem("new-user")));
console.log(
"is new user: " + JSON.parse(await AsyncStorage.getItem("new-user"))
);
}
}
check();
console.log("im a check effect");
}, [isNewUser]);
React.useLayoutEffect(() => {
check();
console.log("\n\nFirst Load\t" + new Date() + "\n====================");
console.log("isSIGNEDIN: " + isSignedIn);
console.log("isAddNeeded: " + isAddNeeded);
console.log("is new user: ", isNewUser);
console.log("isAvatarUpdateNeeded: " + isAvatarUpdateNeeded);
setTimeout(() => {
setIsSplashReady(true);
}, 1000);
if (isSignedIn == true) {
console.log("user is logged in");
if (isAddNeeded == true) {
console.log("additional steps needed");
RootNavigation.navigate("OAuthAdditionalSteps");
}
if (isAvatarUpdateNeeded == true && isAddNeeded === false) {
RootNavigation.dispatch({
index: 2,
routes: [{ name: "Main" }, { name: "ChangeAvatar" }],
});
} else if (isAvatarUpdateNeeded === false && isAddNeeded === false) {
RootNavigation.dispatch({
index: 1,
routes: [{ name: "Main" }],
});
}
} else {
console.log("user is not logged in");
}
console.log("im a layout effect");
}, [isSignedIn, isAddNeeded, isAvatarUpdateNeeded]);
const ModalStack = createNativeStackNavigator();
const ChangeAvatar = () => (
<ModalStack.Navigator>
<ModalStack.Screen
name="AddProfilePicture"
component={AddProfilePicture}
options={{
headerShown: false,
presentation: "fullScreenModal",
}}
/>
<ModalStack.Screen
name="AvatarSelected"
component={AvatarSelected}
options={{
presentation: "card",
}}
/>
<ModalStack.Screen
name="CropAvatar"
component={CropAvatar}
options={{
presentation: "fullScreenModal",
headerShown: true,
header: () => {
return (
<DefaultHeader
title="Chỉnh sửa ảnh"
rightActionTitle={"Xong"}
onPressGoBack={RootNavigation.goBack}
onPressRightAction={() =>
cropViewRef.current?.saveImage(true, 90)
}
/>
);
},
}}
/>
</ModalStack.Navigator>
);
const SignIn = () => (
<ModalStack.Navigator>
<ModalStack.Screen
name="Welcome"
component={WelcomeScreen}
options={{
presentation: "fullScreenModal",
headerShown: false,
}}
/>
<ModalStack.Screen
name="VerificationCode"
component={VerificationCodeScreen}
options={{
headerShown: false,
presentation: "card",
}}
/>
</ModalStack.Navigator>
);
const HomeScr = () => (
<HomeScreen
overlay={
<Animated.View
pointerEvents={"box-none"}
style={[
styles.fullStretch,
{
opacity: animatedValue2,
},
]}
></Animated.View>
}
/>
);
const MainScreen = ({ navigation }) => {
const [currentScreen, setCurrentScreen] = React.useState("HomeScreen");
const [setting, setSetting] = React.useState(false);
const Tab = createBottomTabNavigator();
const menu = <MainSideMenu />;
return (
<SideMenu
menu={menu}
menuPosition="right"
isOpen={setting}
onChange={(isOpen) => setSetting(isOpen)}
onSliding={(value) => {
animatedValue1.setValue(1 - value);
animatedValue2.setValue(0.3 * value);
}}
openMenuOffset={300}
edgeHitWidth={window.width}
>
<Tab.Navigator
screenOptions={({ route }) => ({
tabBarIcon: ({ focused, color, size }) => {
let iconName;
if (route.name === "Home") {
iconName = focused ? "albums" : "albums-outline";
} else if (route.name === "Newsfeed") {
iconName = focused ? "newspaper" : "newspaper-outline";
} else if (route.name === "Chat") {
iconName = focused ? "chatbubble" : "chatbubble-outline";
} else if (route.name === "Notifications") {
iconName = focused ? "notifications" : "notifications-outline";
} else if (route.name === "Menu") {
iconName = focused ? "menu" : "menu-outline";
}
// You can return any component that you like here!
return <Ionicons name={iconName} size={size} color={color} />;
},
tabBarButton: (props) => (
<Pressable
{...props}
onPress={(e) => {
if (route.params.currentScreen == currentScreen) {
return;
} else {
// console.log(currentScreen);
navigation.navigate(route.name, {
previous_screen: currentScreen,
});
setCurrentScreen(route.params.currentScreen);
}
// console.log(route.params.currentScreen);
}}
/>
),
tabBarStyle: {
borderTopColor: "#B3B3B3",
// backgroundColor: "transparent",
// position: "absolute",
position: "absolute",
},
tabBarLabelStyle: {
fontWeight: "500",
},
// tabBarBackground: () => (
// <BlurView tint="light" intensity={1000} style={{ flex: 1 }} />
// ),
})}
>
<Tab.Screen
name="Home"
component={HomeScr}
initialParams={{
currentScreen: "HomeScreen",
}}
options={{
title: "Khám phá",
headerShown: true,
headerBackButtonMenuEnabled: false,
headerTransparent: true,
header: () => {
return (
<MainHeader
title={"Khám phá"}
haveBottomBorder={false}
setSetting={setSetting}
animatedValue={animatedValue1}
overlayValue={animatedValue2}
/>
);
},
}}
/>
</Tab.Navigator>
</SideMenu>
);
};
return (
<>
<AnimatedSplash
translucent={true}
isLoaded={isSplashReady}
logoImage={require("./assets/splash.png")}
backgroundColor={"#C8E6E8"}
logoHeight={150}
logoWidth={150}
>
<NavigationContainer ref={RootNavigation.navigationRef} theme={MyTheme}>
<TailwindProvider>
<StatusBar translucent backgroundColor="transparent" />
<Stack.Navigator
screenOptions={{
cardStyle: { backgroundColor: "#fff" },
}}
>
{isSignedIn == true ? (
<>
<Stack.Screen
options={{
title: "Trang chính",
headerShown: false,
gestureEnabled: false,
animation: "none",
}}
name="Main"
component={MainScreen}
/>
<Stack.Screen
name="ChangeAvatar"
component={ChangeAvatar}
options={{
headerShown: false,
presentation: "fullScreenModal",
}}
/>
<Stack.Screen
name="OAuthAdditionalSteps"
component={OAuthAddSteps}
options={{
headerShown: false,
gestureEnabled: false,
}}
/>
</>
) : (
<>
<Stack.Screen
name="SignIn"
component={SignIn}
options={{
headerShown: false,
presentation: "fullScreenModal",
}}
/>
</>
)}
</Stack.Navigator>
</TailwindProvider>
</NavigationContainer>
</AnimatedSplash>
</>
);
}