-
Notifications
You must be signed in to change notification settings - Fork 75
/
Copy pathApp.tsx
97 lines (85 loc) · 2.99 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
import { ApolloProvider, useMutation } from '@apollo/react-hooks';
import { GoogleSignin } from '@react-native-community/google-signin';
import React, { useContext, useEffect } from 'react';
import { StatusBar, StyleSheet } from 'react-native';
import codePush from 'react-native-code-push';
import FlashMessage from 'react-native-flash-message';
import { SafeAreaView } from 'react-navigation';
import Config from './app/config';
import { Errors, PollIntervals } from './app/constants';
import { AppContext, AppContextProvider } from './app/context';
import client from './app/graphql/client';
import { MUTATION_LAST_SEEN } from './app/graphql/mutation';
import AppNavigator from './app/navigation';
import { ThemeStatic, Typography } from './app/theme';
import { DynamicStatusBar } from './app/theme/Colors';
import { ThemeColors } from './app/types/theme';
import { crashlytics } from './app/utils/firebase';
import { loadThemeType } from './app/utils/storage';
import { computeUnreadMessages } from './app/utils/shared';
const { webClientId } = Config;
GoogleSignin.configure({
webClientId,
forceConsentPrompt: true
});
const SafeAreaApp = () => {
const { user, theme, themeType, toggleTheme, updateUnreadMessages } = useContext(AppContext);
const { barStyle, backgroundColor } = DynamicStatusBar[themeType];
const [updateLastSeen] = useMutation(MUTATION_LAST_SEEN);
const initializeTheme = async () => {
try {
const themeType = await loadThemeType();
toggleTheme(themeType);
} catch ({ message }) {
crashlytics.recordCustomError(Errors.LOAD_THEME, message);
}
};
useEffect(() => {
initializeTheme();
}, []);
useEffect(() => {
setInterval(async () => {
if (user.id) {
try {
const { data: { updateLastSeen: { chats } } } = await updateLastSeen({ variables: { userId: user.id } });
const unreadMessages = computeUnreadMessages(chats, user.id);
updateUnreadMessages(unreadMessages);
} catch ({ message }) {
crashlytics.recordCustomError(Errors.UPDATE_LAST_SEEN, message);
}
}
}, PollIntervals.lastSeen);
}, [user.id]);
return (
<SafeAreaView style={styles(theme).container}>
<StatusBar animated barStyle={barStyle} backgroundColor={backgroundColor} />
<AppNavigator />
<FlashMessage titleStyle={styles().flashMessageTitle} floating position='bottom' />
</SafeAreaView>
);
};
const App = () => {
return (
<ApolloProvider client={client}>
<AppContextProvider>
<SafeAreaApp />
</AppContextProvider>
</ApolloProvider>
);
};
const styles = (theme = {} as ThemeColors) => StyleSheet.create({
container: {
flex: 1,
backgroundColor: theme.base
},
flashMessageTitle: {
...Typography.FontWeights.Light,
...Typography.FontSizes.Body,
color: ThemeStatic.white
}
});
const CodepushApp = codePush({
deploymentKey: Config.codepush.production,
checkFrequency: codePush.CheckFrequency.ON_APP_START
})(App);
export default CodepushApp;