-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathApp.tsx
175 lines (162 loc) · 5.62 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
import React, { useContext, useEffect, useState } from "react";
import { StatusBar, StyleSheet, Text } from "react-native";
import "react-native-reanimated";
import { createBottomTabNavigator } from "@react-navigation/bottom-tabs";
import { createNativeStackNavigator } from "@react-navigation/native-stack";
import { DefaultTheme, NavigationContainer } from "@react-navigation/native";
import auth from "@react-native-firebase/auth";
import { AuthContext } from "./contexts/AuthContext";
import { SocketContext } from "./contexts/SocketContext";
import { getAllConversations, getOptionsInfo, getProfile, getStorage, postAuthInfo, setStorage } from "./utils";
import { ConversationInterface, RootStackParamList } from "./utils/interfaces";
import AntDesign from "react-native-vector-icons/AntDesign";
import MaterialIcons from "react-native-vector-icons/MaterialIcons";
import Feather from "react-native-vector-icons/Feather";
import { darkColor, mainColor } from "./utils/components";
import Home from "./screens/Home";
import Filter from "./screens/Filter";
import Conversations from "./screens/Conversations";
import Chat from "./screens/Chat";
import Profile from "./screens/Profile";
import ProfileEdit from "./screens/ProfileEdit";
import Login from "./screens/Login";
import Loading from "./screens/Loading";
const Tab = createBottomTabNavigator();
const Stack = createNativeStackNavigator<RootStackParamList>();
const Theme = {
...DefaultTheme,
colors: {
...DefaultTheme.colors,
background: darkColor,
},
};
const HomeNavigation = () => {
return (
<Stack.Navigator
screenOptions={({ route }) => ({
headerTitleAlign: "center",
headerStyle: {
backgroundColor: darkColor,
},
headerBackTitleVisible: false,
headerTintColor: mainColor,
headerShadowVisible: false,
})}>
<Stack.Screen name="Home" component={Home} />
<Stack.Screen name="Filter" component={Filter} />
</Stack.Navigator>
);
};
const ProfileNavigation = () => {
return (
<Stack.Navigator
screenOptions={({ route }) => ({
headerTitleAlign: "center",
headerStyle: {
backgroundColor: darkColor,
},
headerBackTitleVisible: false,
headerTintColor: mainColor,
headerShadowVisible: false,
})}>
<Stack.Screen name="Profile" component={Profile} />
<Stack.Screen name="ProfileEdit" component={ProfileEdit} />
</Stack.Navigator>
);
};
const TabNavigation = () => {
return (
<Tab.Navigator
screenOptions={({ route }) => ({
headerShown: true,
headerShadowVisible: false,
headerTitleAlign: "center",
headerStyle: {
backgroundColor: darkColor,
},
tabBarShowLabel: true,
tabBarLabelStyle: {
marginBottom: 3,
fontSize: 12,
fontWeight: "bold",
},
tabBarActiveTintColor: mainColor,
tabBarIcon: ({ focused }) => {
switch (route.name) {
case "HomeNavigation":
return <Feather name="home" size={25} color={`${focused ? "#e4bb4a" : "#fffbf57f"}`} />;
case "Conversations":
return (
<MaterialIcons name="messenger-outline" size={25} color={`${focused ? "#e4bb4a" : "#fffbf57f"}`} />
);
case "ProfileNavigation":
return <AntDesign name="user" size={25} color={`${focused ? "#e4bb4a" : "#fffbf57f"}`} />;
}
},
tabBarStyle: {
backgroundColor: "black",
},
})}>
<Tab.Screen name="HomeNavigation" component={HomeNavigation} options={{ headerShown: false, title: "Home" }} />
<Tab.Screen name="Conversations" component={Conversations} />
<Tab.Screen
name="ProfileNavigation"
component={ProfileNavigation}
options={{ headerShown: false, title: "Profile" }}
/>
</Tab.Navigator>
);
};
const App = () => {
const { userInfo, setUserInfo, setOptionsInfo } = useContext(AuthContext);
const { setConversations, socketConnected } = useContext(SocketContext);
const [loggedIn, setLoggedIn] = useState<boolean | null>(null);
useEffect(() => {
// get user login status
(async () => {
let value = await getStorage("uid");
if (!value) setLoggedIn(false);
})();
// firebase auth
return auth().onAuthStateChanged(async user => {
if (user) {
await setStorage("uid", user.uid);
setConversations!(
(await getAllConversations()).sort((prev: ConversationInterface, next: ConversationInterface) => {
return next.timestamp - prev.timestamp;
}),
);
postAuthInfo().then(async () => {
setUserInfo!(await getProfile());
});
setOptionsInfo!(await getOptionsInfo());
} else {
setLoggedIn(false);
setUserInfo!({});
setOptionsInfo!({});
}
});
}, []);
useEffect(() => {
// Check if userInfo exists & socketConnected equals true
// If so, user if logged in
if (Object.keys(userInfo).length && socketConnected) setLoggedIn(true);
}, [userInfo, socketConnected]);
if (loggedIn === null) return <Loading />;
if (!loggedIn) return <Login />;
return (
<NavigationContainer theme={Theme}>
<StatusBar barStyle="light-content" />
<Stack.Navigator>
<Stack.Screen name="TabNavigation" component={TabNavigation} options={{ headerShown: false }} />
<Stack.Screen name="Chat" component={Chat} />
</Stack.Navigator>
</NavigationContainer>
);
};
export default App;
export const DefaultStyles = StyleSheet.create({
Container: {
display: "flex",
},
});