-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathApp.tsx
288 lines (268 loc) · 9.57 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
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
import React, { useCallback, useEffect, useRef, useState } from "react";
import { StatusBar, View } from "react-native";
import {
NavigationContainer,
NavigationContainerRef,
StackActions,
} from "@react-navigation/native";
import { createBottomTabNavigator } from "@react-navigation/bottom-tabs";
import HomeStack from "./components/Home";
import ScheduleStack from "./components/Schedule";
import AnnouncementsStack from "./components/Announcements";
import InfoStack from "./components/Info";
import { colors, newColors } from "./global-styles";
import FlashMessage from "react-native-flash-message";
import {
getOSName,
getStoredJSON,
logAnalyticsEvent,
logAnalyticsScreenChange,
registerForPushNotificationsAsync,
storeJSON,
} from "./util";
import { WelcomeScreen } from "./components/Welcome";
import { CONFERENCE_ID } from "./api/api";
import { UserContext } from "./UserContext";
import { postRegisterPushNotifications } from "./api/user";
import { useSchedule } from "./api/schedule";
import { ScheduleContext } from "./ScheduleContext";
import { InfoContext } from "./InfoContext";
import * as Notifications from "expo-notifications";
import { Icon } from "react-native-elements";
import { useFonts } from "expo-font";
import * as Device from "expo-device";
import * as SplashScreen from "expo-splash-screen";
import { NotificationResponse } from "expo-notifications";
import { useInfo } from "./api/info";
// Keep the splash screen visible while we fetch resources
SplashScreen.preventAutoHideAsync();
// How to handle notifications when app is in foreground.
Notifications.setNotificationHandler({
handleNotification: async () => ({
shouldShowAlert: true,
shouldPlaySound: false,
shouldSetBadge: false,
}),
});
// Function to fire when a tab navigation event occurs.
// This is used to reset the navigation stack on a screen component
// when navigating to another screen component on Android, since that
// is the expected default behavior for the Android OS.
export const _tabNavigationListener = (props: any) => {
if (getOSName() !== "Android") return {};
const { navigation } = props;
return {
blur: (e: any) => {
const target = e.target;
const state = navigation.dangerouslyGetState();
const route = state.routes.find((r: any) => r.key === target);
// If we are leaving a tab that has its own stack navigation, then clear it.
if (route.state?.type === "stack" && route.state.routes?.length > 1) {
navigation.dispatch(StackActions.popToTop());
}
},
};
};
const Tab = createBottomTabNavigator();
export default function App() {
const [registeredConferenceID, setRegisteredConferenceID] =
useState<number>(0);
const [isReady, setIsReady] = useState<boolean>(false);
const { data, setData, status, setStatus } = useSchedule(null);
const {
data: infoData,
status: infoStatus,
setStatus: setInfoStatus,
} = useInfo([]);
const [notification, setNotification] = useState(null);
const navigationRef = useRef<NavigationContainerRef>(null);
const routeNameRef = useRef();
const [initialRouteName, setInitialRouteName] = useState("Home");
const lastNotificationResponse = Notifications.useLastNotificationResponse();
let [isFontsLoaded] = useFonts({
"Inter-400": require("./assets/fonts/Inter-Regular.ttf"),
"Inter-500": require("./assets/fonts/Inter-Medium.ttf"),
"Inter-600": require("./assets/fonts/Inter-SemiBold.ttf"),
"Inter-700": require("./assets/fonts/Inter-Bold.ttf"),
});
StatusBar.setBarStyle("light-content", true);
if (Device.brand != "Apple") {
StatusBar.setBackgroundColor("#00000000");
}
useEffect(() => {
// To handle when a notification is tapped.
_handleNotificationResponse(lastNotificationResponse);
}, [lastNotificationResponse]);
useEffect(() => {
(async () => {
const user = await getStoredJSON("user");
const id = user?.conference;
setRegisteredConferenceID(id ? id : 0);
setIsReady(true);
})();
// Fires when a notification is received when app is in foreground.
const subscription =
Notifications.addNotificationReceivedListener(_handleNotification);
return () => subscription.remove();
}, []);
const _handleNotification = (notification: any) => {
setNotification(notification);
};
const _handleNotificationResponse = (
response: NotificationResponse | undefined | null
) => {
if (response === undefined || response === null) return;
(async () => {
const lastNotificationDate = await getStoredJSON(
"last_notification_date"
);
if (
!lastNotificationDate ||
response.notification?.date > lastNotificationDate
) {
const notificationTitle =
response?.notification?.request?.content?.title || "";
logAnalyticsEvent("NotificationTapped", 0, notificationTitle);
if (navigationRef.current) {
navigationRef.current.navigate("Announcements");
} else {
setInitialRouteName("Announcements");
}
}
await storeJSON("last_notification_date", response.notification.date);
})();
};
const userRegistered = (deviceID: string, userName: string) => {
console.log(`user (${userName}) registered with ${deviceID}!`);
storeJSON("user", {
deviceID: deviceID,
name: userName,
conference: CONFERENCE_ID,
});
setRegisteredConferenceID(CONFERENCE_ID);
(async () => {
try {
const pushToken = await registerForPushNotificationsAsync();
await postRegisterPushNotifications({
expo_push_token: pushToken,
});
} catch (e) {
console.warn("Failed to register for push notifications!");
}
})();
};
const onLayoutRootView = useCallback(async () => {
if (isReady && isFontsLoaded) {
// This tells the splash screen to hide immediately! If we call this after
// `setAppIsReady`, then we may see a blank screen while the app is
// loading its initial state and rendering its first pixels. So instead,
// we hide the splash screen once we know the root view has already
// performed layout.
await SplashScreen.hideAsync();
}
}, [isReady, isFontsLoaded]);
if (!isReady || !isFontsLoaded) {
return null;
}
return (
<View
style={{ flex: 1, backgroundColor: colors.black }}
onLayout={onLayoutRootView}
>
<NavigationContainer
ref={navigationRef}
onReady={() => logAnalyticsScreenChange(initialRouteName)}
onStateChange={() => {
logAnalyticsScreenChange(
navigationRef.current?.getCurrentRoute()?.name
);
}}
>
{registeredConferenceID != CONFERENCE_ID ? (
<UserContext.Provider value={{ onUserRegistered: userRegistered }}>
<WelcomeScreen />
</UserContext.Provider>
) : (
<ScheduleContext.Provider
value={{
data: data,
status: status,
setData: setData,
setStatus: setStatus,
}}
>
<InfoContext.Provider
value={{
data: infoData,
status: infoStatus,
setStatus: setInfoStatus,
}}
>
<Tab.Navigator
screenOptions={({ route }) => ({
tabBarIcon: ({ focused, color, size }) => {
let iconName;
switch (route.name) {
case "Home":
iconName = "home";
break;
case "Schedule":
iconName = "calendar";
break;
case "Announcements":
iconName = "bell";
break;
default:
iconName = "ellipsis-h";
}
return (
<Icon
name={iconName}
size={size}
type="font-awesome-5"
color={color}
solid={focused}
/>
);
},
})}
tabBarOptions={{
activeTintColor: colors.lightBlue,
inactiveTintColor: colors.midGrey,
style: {
backgroundColor: newColors.darkGrey,
opacity: 0.9,
borderTopWidth: 0,
},
}}
initialRouteName={initialRouteName}
>
<Tab.Screen
name="Home"
component={HomeStack}
listeners={(props) => _tabNavigationListener({ ...props })}
/>
<Tab.Screen
name="Schedule"
component={ScheduleStack}
listeners={(props) => _tabNavigationListener({ ...props })}
/>
<Tab.Screen
name="Announcements"
component={AnnouncementsStack}
listeners={(props) => _tabNavigationListener({ ...props })}
/>
<Tab.Screen
name="More"
component={InfoStack}
listeners={(props) => _tabNavigationListener({ ...props })}
/>
</Tab.Navigator>
</InfoContext.Provider>
</ScheduleContext.Provider>
)}
<FlashMessage position="top" />
</NavigationContainer>
</View>
);
}