-
Notifications
You must be signed in to change notification settings - Fork 34
/
AppInitManager.ts
243 lines (207 loc) · 7.01 KB
/
AppInitManager.ts
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
import { Linking, Alert } from 'react-native';
// import ShareMenu from 'react-native-share-menu';
import * as Sentry from '@sentry/react-native';
import * as SplashScreen from 'expo-splash-screen';
import * as Clipboard from 'expo-clipboard';
// import receiveShare from './src/common/services/receive-share.service';
import {
IS_ANDROID_OSS,
IS_IOS,
IS_TENANT,
IS_TENANT_PREVIEW,
} from './src/config/Config';
import { updateFeatureFlags } from 'ExperimentsProvider';
import checkTOS from '~/tos/checkTOS';
import { queryClient } from '~/services';
import { updateCustomNavigation } from '~/modules/navigation/service/custom-navigation.service';
import serviceProvider from '~/services/serviceProvider';
/**
* App initialization manager
*/
export class AppInitManager {
initialized = false;
navReady: boolean = false;
/**
* Initialize services without waiting for the promises
*/
async initializeServices() {
serviceProvider.socket.init();
// init block list service
serviceProvider.resolve('blockList').init();
// init in feed notices service
serviceProvider.resolve('inFeedNotices').init();
// init video player service
serviceProvider.resolve('videoPlayer').init();
// On app login (runs if the user login or if it is already logged in)
serviceProvider.session.onLogin(this.onLogin);
//on app logout
serviceProvider.session.onLogout(this.onLogout);
serviceProvider.resolve('openURL').init();
// update custom navigation data
updateCustomNavigation();
serviceProvider.resolve('storeRating').track('appSession');
try {
serviceProvider.log.info('[App] init session');
const token = await serviceProvider.session.init();
if (!token) {
// update settings and init growthbook
this.updateMindsConfigAndInitFeatureFlags();
serviceProvider.log.info('[App] there is no active session');
SplashScreen.hideAsync();
} else {
serviceProvider.log.info('[App] session initialized');
}
} catch (err) {
serviceProvider.log.exception('[App] Error initializing the app', err);
if (err instanceof Error) {
console.error('INIT ERROR', err.stack);
Alert.alert(
'Error',
'There was an error initializing the app.\n Do you want to copy the stack trace.',
[
{
text: 'Yes',
onPress: () => {
if (err instanceof Error) {
Clipboard.setStringAsync(err.stack || '');
}
},
},
{ text: 'No' },
],
{ cancelable: false },
);
}
}
}
updateMindsConfigAndInitFeatureFlags() {
// init with current cached data
updateFeatureFlags();
const afterUpdate = () => {
// if it changed we initialize growth book again
updateFeatureFlags();
// Check Terms of service
checkTOS();
};
// Update the config
serviceProvider.config.update().then(afterUpdate);
}
/**
* Handle session logout
*/
onLogout = () => {
// clear app badge
const pushService = serviceProvider.resolve('push');
pushService.setBadgeCount(0);
pushService.clearNotifications();
serviceProvider.resolve('translation').purgeLanguagesCache();
updateFeatureFlags();
serviceProvider.resolve('boostedContent').clear();
serviceProvider.resolve('portraitBoostedContent').clear();
queryClient.clear();
};
/**
* Handle session login
*/
onLogin = async () => {
const user = serviceProvider.session.getUser();
// update settings for this user and init growthbook
this.updateMindsConfigAndInitFeatureFlags();
// load boosted content
if (!IS_TENANT) {
serviceProvider.resolve('boostedContent').load();
serviceProvider.resolve('portraitBoostedContent').load();
}
Sentry.configureScope(scope => {
scope.setUser({ id: user.guid });
});
// register device token into backend on login
serviceProvider.resolve('push').registerToken();
// OSS check update
if (IS_ANDROID_OSS) {
setTimeout(async () => {
const user = serviceProvider.session.getUser();
serviceProvider.resolve('update').checkUpdate(!user.canary);
}, 5000);
}
if (IS_IOS) {
setTimeout(() => {
const {
requestTrackingPermission,
} = require('~/modules/tracking-transparency/tracking-transparency.service');
requestTrackingPermission();
}, 3000);
}
// if the navigator is ready, handle initial navigation (this is needed when the user lands on the welcome screen)
if (this.navReady) {
// when the experiment is enabled, we don't want to navigate to the initial screen because the navigation is done after the email verification.
this.initialNavigationHandling();
}
};
navigateToInitialScreen() {
if (serviceProvider.session.initialScreen) {
serviceProvider.log.info(
'[App] navigating to initial screen: ' +
serviceProvider.session.initialScreen,
);
serviceProvider.navigation.navigate(
serviceProvider.session.initialScreen,
{
initial: true,
...serviceProvider.session.initialScreenParams,
},
);
}
serviceProvider.session.setInitialScreen('');
}
async initialNavigationHandling() {
// ensure we run it once
if (this.initialized) {
return;
}
this.initialized = true;
console.log('[App] initial Navigation Handling');
try {
const deepLinkUrl = (await Linking.getInitialURL()) || '';
// handle deep link (if the app is opened by one)
if (deepLinkUrl) {
setTimeout(() => {
serviceProvider.resolve('deepLinks').navigate(deepLinkUrl, true);
}, 300);
}
// handle initial notifications (if the app is opened by tap on one)
serviceProvider.resolve('push').handleInitialNotification();
// handle initial shared content`
// ShareMenu.getInitialShare(receiveShare.handle);
if (serviceProvider.session.recoveryCodeUsed) {
serviceProvider.session.setRecoveryCodeUsed(false);
serviceProvider.navigation.navigate('RecoveryCodeUsedScreen');
}
SplashScreen.hideAsync();
} catch (err) {
SplashScreen.hideAsync();
serviceProvider.log.exception(err);
}
}
/**
* Run the session logic when the navigator is ready
*/
onNavigatorReady = async () => {
this.navReady = true;
// Emit first state change
serviceProvider.navigation.onStateChange();
// if the user is already logged in, handle initial navigation
if (serviceProvider.session.userLoggedIn) {
this.initialNavigationHandling();
} else if (IS_TENANT_PREVIEW) {
const deepLinkUrl = (await Linking.getInitialURL()) || '';
if (deepLinkUrl) {
setTimeout(() => {
serviceProvider.resolve('deepLinks').navigate(deepLinkUrl);
}, 300);
}
}
};
}
const appInitManagerInstace = new AppInitManager();
export default appInitManagerInstace;