keepyourmouthshut/Rider App/App.js

118 lines
3.6 KiB
JavaScript
Raw Normal View History

2023-03-09 09:15:59 +00:00
import { ApolloProvider } from "@apollo/react-hooks";
import AsyncStorage from "@react-native-async-storage/async-storage";
import * as Font from "expo-font";
import * as Notifications from "expo-notifications";
import * as SplashScreen from "expo-splash-screen";
import React, { useEffect, useState } from "react";
import { Platform, StatusBar } from "react-native";
import FlashMessage from "react-native-flash-message";
import i18n from "./i18n";
import setupApolloClient from "./src/apollo/index";
import { AuthContext } from "./src/context/auth";
import { ConfigurationProvider } from "./src/context/configuration";
import AppContainer from "./src/routes/index";
2022-12-21 10:01:12 +00:00
export default function App() {
2023-03-09 09:15:59 +00:00
const [fontLoaded, setFontLoaded] = useState(false);
const [client, setClient] = useState(null);
const [token, setToken] = useState(false);
const [appIsReady, setAppIsReady] = useState(false);
2022-12-21 10:01:12 +00:00
useEffect(() => {
2023-03-09 09:15:59 +00:00
(async () => {
const token = await AsyncStorage.getItem("rider-token");
if (token) setToken(token);
setAppIsReady(true);
})();
}, []);
2022-12-21 10:01:12 +00:00
useEffect(() => {
2023-03-09 09:15:59 +00:00
(async () => {
2022-12-21 10:01:12 +00:00
try {
2023-03-09 09:15:59 +00:00
await SplashScreen.preventAutoHideAsync();
2022-12-21 10:01:12 +00:00
} catch (e) {
2023-03-09 09:15:59 +00:00
console.log(e);
2022-12-21 10:01:12 +00:00
}
2023-03-09 09:15:59 +00:00
})();
loadData();
}, []);
2022-12-21 10:01:12 +00:00
2023-03-09 09:15:59 +00:00
const setTokenAsync = async (token) => {
await AsyncStorage.setItem("rider-token", token);
setToken(token);
};
2022-12-21 10:01:12 +00:00
const logout = async () => {
try {
2023-03-09 09:15:59 +00:00
await AsyncStorage.removeItem("rider-token");
setToken(null);
2022-12-21 10:01:12 +00:00
} catch (e) {
2023-03-09 09:15:59 +00:00
console.log("Logout Error: ", e);
2022-12-21 10:01:12 +00:00
}
2023-03-09 09:15:59 +00:00
};
2022-12-21 10:01:12 +00:00
async function loadData() {
2023-03-09 09:15:59 +00:00
await i18n.initAsync();
2022-12-21 10:01:12 +00:00
await Font.loadAsync({
2023-03-09 09:15:59 +00:00
MuseoSans300: require("./assets/font/MuseoSans/MuseoSans300.ttf"),
MuseoSans500: require("./assets/font/MuseoSans/MuseoSans500.ttf"),
MuseoSans700: require("./assets/font/MuseoSans/MuseoSans700.ttf"),
icomoon: require("./assets/font/icomoon.ttf"),
});
const client = await setupApolloClient();
await permissionForPushNotificationsAsync();
setClient(client);
setFontLoaded(true);
await SplashScreen.hideAsync();
2022-12-21 10:01:12 +00:00
}
async function permissionForPushNotificationsAsync() {
2023-03-09 09:15:59 +00:00
const {
status: existingStatus,
} = await Notifications.getPermissionsAsync();
let finalStatus = existingStatus;
2022-12-21 10:01:12 +00:00
// only ask if permissions have not already been determined, because
// iOS won't necessarily prompt the user a second time.
2023-03-09 09:15:59 +00:00
if (existingStatus !== "granted") {
2022-12-21 10:01:12 +00:00
// Android remote notification permissions are granted during the app
// install, so this will only ask on iOS
2023-03-09 09:15:59 +00:00
const { status } = await Notifications.requestPermissionsAsync();
finalStatus = status;
2022-12-21 10:01:12 +00:00
}
// Stop here if the user did not grant permissions
2023-03-09 09:15:59 +00:00
if (finalStatus !== "granted") {
return;
2022-12-21 10:01:12 +00:00
}
2023-03-09 09:15:59 +00:00
if (Platform.OS === "android") {
Notifications.setNotificationChannelAsync("default", {
name: "default",
2022-12-21 10:01:12 +00:00
sound: true,
2023-03-09 09:15:59 +00:00
priority: "max",
2022-12-21 10:01:12 +00:00
importance: Notifications.AndroidImportance.HIGH,
2023-03-09 09:15:59 +00:00
vibrate: [0, 250, 250, 250],
});
2022-12-21 10:01:12 +00:00
}
}
if (fontLoaded && client && appIsReady) {
return (
<ApolloProvider client={client}>
<StatusBar
translucent
2023-03-09 09:15:59 +00:00
backgroundColor={"transparent"}
2022-12-21 10:01:12 +00:00
barStyle="dark-content"
/>
<ConfigurationProvider>
<AuthContext.Provider value={{ token, setTokenAsync, logout }}>
<AppContainer />
</AuthContext.Provider>
</ConfigurationProvider>
<FlashMessage duration={2000} position="center" />
</ApolloProvider>
2023-03-09 09:15:59 +00:00
);
2022-12-21 10:01:12 +00:00
}
2023-03-09 09:15:59 +00:00
return null;
2022-12-21 10:01:12 +00:00
}