-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathApp.js
More file actions
182 lines (159 loc) · 5.96 KB
/
Copy pathApp.js
File metadata and controls
182 lines (159 loc) · 5.96 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
import { Provider } from "react-redux";
import { store, persistor } from "./redux/Store";
import "react-native-gesture-handler";
import Toast from "react-native-toast-message";
import { PersistGate } from "redux-persist/integration/react";
import { useState, useEffect, useRef } from "react";
import * as Font from "expo-font";
import * as SplashScreen from "expo-splash-screen";
import Ionicons from "@expo/vector-icons/Ionicons";
import { QueryClient, QueryClientProvider } from "@tanstack/react-query";
import { useDispatch, useSelector } from "react-redux";
// Hosts the <Toast> instance with a safe-area-derived offset; `Toast.show()`
// still comes from the library import above.
import AppToast from "./Toast/AppToast";
import Navigator from "./navigation/navigator";
import { navigateSafely } from "./navigation/rootNavigation";
import * as Updates from "expo-updates";
import { SafeAreaProvider } from "react-native-safe-area-context";
import UpdateBanner from "./components/UpdateBanner";
import ThemedStatusBar from "./components/common/ThemedStatusBar";
import OfflineBanner from "./components/common/OfflineBanner";
import AutoAttendanceBootstrap from "./components/AutoAttendanceBootstrap";
import OfflineAttendanceBootstrap from "./components/OfflineAttendanceBootstrap";
import { selectIsLoggedIn } from "./redux/Slices/AuthSlice";
import {
initializeFcm,
registerBackgroundMessageHandler,
clearFcmRegistration,
} from "./services/notifications/fcm.service";
import { registerSessionCleanupHandler } from "./services/api/apiClient";
import { clearOfflineAttendance } from "./services/offline/AttendanceQueueService";
import { hydrate as hydrateAppearance } from "./settings/appearance";
// TEMPORARY: New Home Experience experiment — remove with the feature.
import { hydrate as hydrateHomeExperience } from "./settings/homeExperience";
import { hydrate as hydrateOfflineSyncAlerts } from "./settings/offlineSyncAlerts";
function cacheFonts(fonts) {
return fonts.map((font) => Font.loadAsync(font));
}
const queryClient = new QueryClient();
registerBackgroundMessageHandler();
// Forced logout (session expiry) reuses the same teardown as manual logout: the
// FCM registration, and the offline attendance queue plus its cached rules. A
// session that expires mid-outage would otherwise leave queued punches behind to
// sync under the next user's token.
registerSessionCleanupHandler(async () => {
await Promise.allSettled([clearFcmRegistration(), clearOfflineAttendance()]);
});
const getForegroundToastType = (type) => {
if (typeof type !== "string") {
return "notificationToast";
}
return type.toLowerCase() === "announcement"
? "announcementToast"
: "notificationToast";
};
function FcmBootstrap() {
const dispatch = useDispatch();
const isLoggedIn = useSelector(selectIsLoggedIn);
const teardownRef = useRef(() => {});
useEffect(() => {
let cancelled = false;
const setupFcm = async () => {
teardownRef.current();
teardownRef.current = () => {};
if (!isLoggedIn) {
return;
}
const teardown = await initializeFcm({
dispatch,
onForegroundNotification: ({ title, body, type }) => {
Toast.show({
type: getForegroundToastType(type),
text1: title,
text2: body,
onPress: () => {
Toast.hide();
navigateSafely("Notifications");
},
autoHide: true,
visibilityTime: 3500,
});
},
});
if (cancelled) {
teardown();
return;
}
teardownRef.current = teardown;
};
setupFcm();
return () => {
cancelled = true;
teardownRef.current();
teardownRef.current = () => {};
};
}, [dispatch, isLoggedIn]);
return null;
}
export default function App() {
const [appReady, setAppReady] = useState(false);
useEffect(() => {
const loadResourcesAndDataAsync = async () => {
try {
SplashScreen.preventAutoHideAsync();
// Load fonts
const IconAssets = cacheFonts([Ionicons.font]);
await Promise.all([...IconAssets]);
// Both must resolve before the navigator mounts. Appearance decides the
// palette of the first paint; the Home variant decides which screen
// component mounts, and getting it wrong remounts Home and fires its
// focus effects (and their network calls) twice.
await Promise.all([hydrateAppearance(), hydrateHomeExperience()]);
// Not awaited with the two above: the banner is an overlay, so the worst
// case is one frame before the preference lands. Blocking the splash on
// it would be the worse trade.
hydrateOfflineSyncAlerts().catch(() => {});
if (!__DEV__ && Updates.isEnabled) {
try {
const update = await Updates.checkForUpdateAsync();
if (update.isAvailable) {
await Updates.fetchUpdateAsync();
await Updates.reloadAsync();
}
} catch (error) {
// Ignore OTA check failures during startup and continue booting.
}
}
} catch (error) {
} finally {
setAppReady(true);
SplashScreen.hideAsync();
}
};
loadResourcesAndDataAsync();
}, []);
if (!appReady) {
return null;
}
return (
<SafeAreaProvider>
<Provider store={store}>
<PersistGate persistor={persistor} loading={null}>
<QueryClientProvider client={queryClient}>
<FcmBootstrap />
<AutoAttendanceBootstrap />
<OfflineAttendanceBootstrap />
<Navigator />
{/* Above the navigator so it floats over any screen, below AppToast
so a transient toast still wins the top of the screen. */}
<OfflineBanner />
<UpdateBanner />
<ThemedStatusBar />
<AppToast />
</QueryClientProvider>
</PersistGate>
</Provider>
</SafeAreaProvider>
);
}