-
Notifications
You must be signed in to change notification settings - Fork 3
/
App.tsx
170 lines (146 loc) · 5.2 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
import Constants, { AppOwnership } from "expo-constants";
import * as Device from "expo-device";
import * as Notifications from "expo-notifications";
import * as SecureStore from "expo-secure-store";
import { StatusBar } from "expo-status-bar";
import { checkForUpdateAsync, fetchUpdateAsync, reloadAsync } from "expo-updates";
import { apiFetcher, AppVersion, AuthProvider, useAuth, useRequest } from "lynbrook-app-api-hooks";
import React, { useCallback, useEffect, useRef, useState } from "react";
import { Alert, AppState, AppStateStatus, Linking, Platform, Text } from "react-native";
import "react-native-gesture-handler";
import { SafeAreaProvider } from "react-native-safe-area-context";
import * as semver from "semver";
import useSWR from "swr";
import { useSWRNativeRevalidate } from "swr-react-native";
import tw from "twrnc";
import FilledButton from "./components/FilledButton";
import Loading from "./components/Loading";
import Stack from "./components/Stack";
import useCachedResources from "./helpers/useCachedResources";
import Navigation from "./navigation";
const isActive = (x: AppStateStatus) => x === "active";
const NeedUpdate = () => (
<Stack style={tw`flex-1 justify-center p-8`} spacing={4} align="center">
<Text style={tw`text-lg font-bold`}>Update Required</Text>
<Text style={tw`text-base text-center`}>
Please download the latest update from the{" "}
{Platform.OS === "android" ? "Play Store" : "App Store"} in order to continue using the app.
</Text>
<FilledButton
onPress={() =>
Linking.openURL(
Platform.OS === "android"
? "https://play.google.com/store/apps/details?id=org.fuhsd.lhs.app"
: "https://apps.apple.com/us/app/lynbrook-high-school/id1530326385"
)
}
>
Open Store
</FilledButton>
</Stack>
);
const Root = () => {
const { token } = useAuth();
const { request } = useRequest();
useEffect(() => {
(async () => {
if (!token) return;
if (Device.isDevice) {
const { status } = await Notifications.requestPermissionsAsync();
if (status !== "granted") return;
const { data } = await Notifications.getExpoPushTokenAsync({
experienceId: "@lynbrookhs/lhs-app",
});
await request("POST", "/users/me/tokens/", { token: data });
}
if (Platform.OS === "android") {
await Notifications.setNotificationChannelAsync("default", {
name: "default",
importance: Notifications.AndroidImportance.MAX,
vibrationPattern: [0, 250, 250, 250],
lightColor: "#FF231F7C",
});
}
})();
}, [token]);
useEffect(() => {
const subscription = Notifications.addNotificationResponseReceivedListener((response) => {
console.log(response);
Linking.openURL("lhs://posts");
});
return () => subscription.remove();
});
return <Navigation />;
};
const App = () => {
const isLoadingComplete = useCachedResources();
const appState = useRef(AppState.currentState);
const [, setActive] = useState(appState.current === "active");
const {
data: appVersion,
error,
revalidate,
} = useSWR<AppVersion>("https://lynbrookasb.org/api/app_version/", apiFetcher());
const checkUpdate = useCallback(async () => {
revalidate();
try {
const check = await checkForUpdateAsync();
if (!check.isAvailable) return;
const update = await fetchUpdateAsync();
if (!update.isNew) return;
} catch {
return;
// In development mode or cannot communicate.
}
Alert.alert(
"Update Available",
"The app has been updated and will now reload.",
[{ text: "OK", onPress: reloadAsync }],
{ cancelable: false }
);
}, []);
const handleAppStateChange = (newState: AppStateStatus) => {
if (!isActive(appState.current) && isActive(newState)) checkUpdate();
appState.current = newState;
setActive(newState === "active");
};
useEffect(() => {
checkUpdate();
AppState.addEventListener("change", handleAppStateChange);
return () => AppState.removeEventListener("change", handleAppStateChange);
}, []);
if (!isLoadingComplete || !appVersion) return null;
if (!error && (Constants.appOwnership === AppOwnership.Standalone || !Constants.appOwnership)) {
const neededVersion = semver.coerce(appVersion[Platform.OS] ?? 0);
const currentVersion = semver.coerce(Constants.nativeBuildVersion);
if (neededVersion && currentVersion && semver.gt(neededVersion, currentVersion)) {
return <NeedUpdate />;
}
}
const loadToken = async () => {
try {
const token = await SecureStore.getItemAsync("token");
return token ?? undefined;
} catch (e) {
console.error(e);
}
};
const onTokenChange = async (token: string | undefined) => {
if (token) await SecureStore.setItemAsync("token", token);
else await SecureStore.deleteItemAsync("token");
};
return (
<SafeAreaProvider>
<AuthProvider
fallback={<Loading />}
loadToken={loadToken}
onTokenChange={onTokenChange}
afterRequest={useSWRNativeRevalidate}
>
<Root />
</AuthProvider>
<StatusBar />
</SafeAreaProvider>
);
};
export default App;