Repository navigation
Expand file tree
/
Copy pathApp.js
More file actions
243 lines (234 loc) · 11.2 KB
/
Copy pathApp.js
File metadata and controls
243 lines (234 loc) · 11.2 KB
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
import React, { useState, useEffect } from "react";
import AsyncStorage from "@react-native-async-storage/async-storage";
// import navagiation
import { NavigationContainer } from "@react-navigation/native";
import { createStackNavigator } from "@react-navigation/stack";
// import bottom tab navigation
import { createBottomTabNavigator } from "@react-navigation/bottom-tabs";
// import icons
import { Ionicons } from "@expo/vector-icons";
import { FontAwesome } from "@expo/vector-icons";
import { FontAwesome5 } from "@expo/vector-icons";
// import containers
import HomeScreen from "./containers/HomeScreen";
import ProfileScreen from "./containers/ProfileScreen";
import SignInScreen from "./containers/SignInScreen";
import SignUpScreen from "./containers/SignUpScreen";
import SettingsScreen from "./containers/SettingsScreen";
import MapScreen from "./containers/MapScreen";
import RoomScreen from "./containers/RoomScreen";
// import components
import Logo from "./components/Logo";
const Tab = createBottomTabNavigator();
const Stack = createStackNavigator();
export default function App() {
const [isLoading, setIsLoading] = useState(true);
const [userToken, setUserToken] = useState(null);
const [userId, setUserId] = useState(null);
// state for id room
const [roomId, setRoomId] = useState("");
// save or remove userToken
const setToken = async (token) => {
if (token) {
console.log("userToken=>", token);
AsyncStorage.setItem("userToken", token);
setUserToken(token);
} else {
AsyncStorage.removeItem("userToken");
setUserToken(null);
}
};
// save or remove userId
const setId = async (id) => {
if (id) {
console.log("userId=>", id);
AsyncStorage.setItem("userId", id);
setUserId(id);
} else {
AsyncStorage.removeItem("userId");
setUserId(null);
}
};
useEffect(() => {
// Fetch the token from storage then navigate to our appropriate place
const bootstrapAsync = async () => {
// We should also handle error for production apps
const userToken = await AsyncStorage.getItem("userToken");
// This will switch to the App screen or Auth screen and this loading
// screen will be unmounted and thrown away.
setIsLoading(false);
setUserToken(userToken);
};
bootstrapAsync();
}, []);
return (
<NavigationContainer>
{isLoading ? null : userToken === null ? ( // We haven't finished checking for the token yet
// No token found, user isn't signed in
<Stack.Navigator screenOptions={{ headerShown: false }}>
<Stack.Screen
name="SignIn"
options={{ animationEnabled: false }}
>
{() => (
<SignInScreen setToken={setToken} setId={setId} />
)}
</Stack.Screen>
<Stack.Screen name="SignUp">
{() => (
<SignUpScreen setToken={setToken} setId={setId} />
)}
</Stack.Screen>
</Stack.Navigator>
) : (
// User is signed in
<Stack.Navigator
screenOptions={{
headerTitle: (props) => <Logo {...props} size={30} />,
headerTitleAlign: "center",
}}
>
<Stack.Screen
name="Tab"
options={{
headerShown: false,
animationEnabled: false,
}}
>
{() => (
<Tab.Navigator
tabBarOptions={{
activeTintColor: "tomato",
inactiveTintColor: "gray",
}}
>
<Tab.Screen
name="Home"
options={{
tabBarLabel: "Home",
tabBarIcon: ({ color, size }) => (
<Ionicons
name={"ios-home"}
size={size}
color={color}
/>
),
}}
>
{() => (
<Stack.Navigator
// headerMode={false}
screenOptions={{
headerTitle: () => (
<Logo size={30} />
),
headerTitleAlign: "center",
}}
>
<Stack.Screen
name="Home"
component={HomeScreen}
></Stack.Screen>
<Stack.Screen name="Room">
{(props) => (
<RoomScreen
{...props}
setRoomId={setRoomId}
roomId={roomId}
/>
)}
</Stack.Screen>
</Stack.Navigator>
)}
</Tab.Screen>
<Tab.Screen
name="Map"
options={{
tabBarLabel: "Around me",
tabBarIcon: ({ color, size }) => (
<FontAwesome
name="map-marker"
size={size}
color={color}
/>
),
}}
>
{() => (
<Stack.Navigator
screenOptions={{
headerTitle: (props) => (
<Logo
{...props}
size={30}
/>
),
headerTitleAlign: "center",
}}
>
<Stack.Screen name="Map">
{(props) => (
<MapScreen
{...props}
setRoomId={setRoomId}
roomId={roomId}
/>
)}
</Stack.Screen>
</Stack.Navigator>
)}
</Tab.Screen>
<Tab.Screen
name="Profile"
options={{
tabBarLabel: "Profile",
tabBarIcon: ({ color, size }) => (
<FontAwesome5
name="user-alt"
size={size}
color={color}
/>
),
}}
>
{() => (
<Stack.Navigator
screenOptions={{
headerTitle: (props) => (
<Logo
{...props}
size={30}
/>
),
headerTitleAlign: "center",
}}
>
<Stack.Screen
name="Profile"
options={{
headerTitleAlign: "center",
title: "Profile",
tabBarLabel: "Profile",
}}
>
{(props) => (
<ProfileScreen
{...props}
setToken={setToken}
setId={setId}
token={userToken}
userId={userId}
/>
)}
</Stack.Screen>
</Stack.Navigator>
)}
</Tab.Screen>
</Tab.Navigator>
)}
</Stack.Screen>
</Stack.Navigator>
)}
</NavigationContainer>
);
}