This repository has been archived by the owner on Dec 14, 2022. It is now read-only.
forked from maisieccino/ucl-assistant-app
-
Notifications
You must be signed in to change notification settings - Fork 4
/
App.tsx
171 lines (147 loc) · 4.51 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
import AppLoading from "expo-app-loading"
import { Asset } from "expo-asset"
import * as Font from "expo-font"
import * as Notifications from 'expo-notifications'
import PropTypes from "prop-types"
import React from "react"
import { Platform, StatusBar, View } from "react-native"
// import { enableScreens } from 'react-native-screens'
import { Provider } from "react-redux"
import { PersistGate } from "redux-persist/lib/integration/react"
import { QueryClient, QueryClientProvider } from "react-query"
import configureStore from "./configureStore"
import Colors from './constants/Colors'
import {
AnalyticsManager, AssetManager, ErrorManager, Warnings,
} from "./lib"
import RootNavigation from "./navigation/RootNavigation"
import { NotificationChannels } from "./redux/constants/notificationsConstants"
import Styles from "./styles/Containers"
const { persistor, store } = configureStore
const queryClient = new QueryClient()
ErrorManager.initialise()
// crashes LiveSeatingMapScreen, probably because of the large
// SVG base64 string
// unfortunately, this reduces navigation performance
// enableScreens()
Warnings.ignore()
interface Props {
skipLoadingScreen: boolean,
}
interface State {
isLoadingComplete: boolean,
store: any,
}
class App extends React.Component<Props, State> {
notificationSubscription = null
routeNameRef = null
navigationRef = null
static propTypes = {
skipLoadingScreen: PropTypes.bool,
}
static defaultProps = {
skipLoadingScreen: false,
}
static getActiveRouteName(navigationState): string {
if (!navigationState) {
return ``
}
const route = navigationState.routes[navigationState.index]
// dive into nested navigators
if (route.state) {
return App.getActiveRouteName(route.state)
}
return route.name
}
constructor(props) {
super(props)
this.state = {
isLoadingComplete: false,
store: {
persistor,
store,
},
}
}
componentDidMount(): void {
if (Platform.OS === `android`) {
Object.keys(NotificationChannels).forEach((key) => {
const channel = NotificationChannels[key]
Notifications.setNotificationChannelAsync(channel.id, channel.options)
})
}
this.notificationSubscription = Notifications.addNotificationReceivedListener(
this.handleNotification,
)
AnalyticsManager.initialise()
if (this.navigationRef !== null && this.navigationRef) {
const state = this.navigationRef.getRootState()
this.routeNameRef = App.getActiveRouteName(state)
}
}
componentWillUnmount(): void {
Notifications.removeNotificationSubscription(this.notificationSubscription)
}
loadResourcesAsync = async (): Promise<any> => Promise.all([
Asset.loadAsync(Object.values(AssetManager.undraw) as string[]),
Font.loadAsync({
...AssetManager.font,
}),
])
handleLoadingError = (error: Error): void => {
ErrorManager.captureError(error)
}
handleFinishLoading = (): void => {
this.setState({ isLoadingComplete: true })
}
handleNotification = (notification): void => console.log(
`Received notification`,
notification,
)
onNavigationStateChange = (currentState): void => {
const currentScreen = App.getActiveRouteName(currentState)
const prevScreen = this.routeNameRef
if (prevScreen !== currentScreen) {
AnalyticsManager.logScreenView(currentScreen)
}
this.routeNameRef = currentScreen
}
render(): React.ReactElement {
const { isLoadingComplete } = this.state
const { skipLoadingScreen } = this.props
if (!isLoadingComplete && !skipLoadingScreen) {
return (
<AppLoading
startAsync={this.loadResourcesAsync}
onError={this.handleLoadingError}
onFinish={this.handleFinishLoading}
/>
)
}
const {
store: {
store: stateStore, persistor: statePersistor,
},
} = this.state
return (
<Provider store={stateStore}>
<PersistGate persistor={statePersistor}>
<QueryClientProvider client={queryClient}>
<View style={Styles.app}>
<StatusBar
barStyle="dark-content"
hidden={false}
backgroundColor={Colors.pageBackground}
/>
<RootNavigation
ref={this.navigationRef}
onNavigationStateChange={this.onNavigationStateChange}
/>
</View>
</QueryClientProvider>
</PersistGate>
</Provider>
)
}
}
export default App