forked from BluezoneGlobal/bluezone-app
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathApp.js
443 lines (405 loc) · 14.5 KB
/
App.js
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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
/*
* @Project Bluezone
* @Author Bluezone Global (contact@bluezone.ai)
* @Createdate 04/26/2020, 16:36
*
* This file is part of Bluezone (https://bluezone.ai)
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
import React from 'react';
import 'react-native-gesture-handler';
import {NavigationContainer} from '@react-navigation/native';
import {createStackNavigator} from '@react-navigation/stack';
// Components
import LoadingScreen from './app/main/components/LoadingScreen';
import Home from './app/main/components/MainScreen';
import Welcome from './app/main/components/WelcomeScreen';
import WatchScan from './app/main/components/WatchScanScreen';
import HistoryScan from './app/main/components/HistoryScanScreen';
import NotifyDetail from './app/main/components/NotifyDetail';
import Invite from './app/main/components/InviteScreen';
import PhoneNumberRegisterScreen from './app/main/components/PhoneNumberRegisterScreen';
import PhoneNumberVerifyOTPScreen from './app/main/components/PhoneNumberVerifyOTPScreen';
import HistoryUploadedByOTPScreen from './app/main/components/HistoryUploadedByOTPScreen';
import PageView from './app/main/components/PageViewScreen';
import DetailNew from './app/main/components/DetailNewScreen';
import ViewLog from './app/main/components/ViewLog';
import DownloadLatestVersionScreen from './app/main/components/DownloadLatestVersionScreen';
import Introduction from './app/main/components/IntroductionScreen';
import StartUse from './app/main/components/StartScreen';
import RegisterInfomation from './app/main/components/RegisterInfomationScreen';
import ContactHistory from './app/main/components/ContactHistoryScreen';
// import ScanScreen from './app/main/components/ScanScreen';
import FAQScreen from './app/main/components/FAQScreen';
import ContextProvider from './LanguageContext';
import LanguageProvider from './app/base/LanguageProvider';
import {translationMessages} from './app/i18n';
import {remoteMessageListener} from './app/core/push';
import decorateMainAppStart from './app/main/decorateMainAppStart';
import {navigationRef, navigate} from './RootNavigation';
import {registerMessageHandler} from './app/core/fcm';
// Api
import {registerResourceLanguageChange} from './app/core/language';
const isLastStepOfWizard = name => {
return wizard.indexOf(name) + 1 === wizard.length;
};
const nextStepName = name => {
if (wizard.indexOf(name) + 1 >= wizard.length) {
throw new Error('nexStepName: Khong ton tai buoc tiep theo');
}
return wizard[wizard.indexOf(name) + 1];
};
const prevStepName = name => {
if (wizard.indexOf(name) - 1 < 0) {
throw new Error('prevStepName: Khong ton tai buoc tiep theo');
}
return wizard[wizard.indexOf(name) - 1];
};
// Const
import {NOTIFICATION_TYPE} from './app/const/notification';
const LOADING_SCREEN_NAME = 'LoadingScreen';
const INTRODUTION_WIZARD_NAME = 'IntrodutionWizard';
const PHONE_NUMBER_REGISTER_WIZARD_NAME = 'PhoneNumberRegisterWizard';
const PHONE_NUMBER_VERITY_OTP_WIZARD_NAME = 'PhoneNumberVerifyOTPWizard';
const REGISTER_INFOMATION_WIZARD_NAME = 'RegisterInfomationWizard';
const START_USE_WIZARD_NAME = 'StartUseWizard';
const WELCOME_WIZARD_NAME = 'WelcomeWizard';
let wizard = [
LOADING_SCREEN_NAME,
INTRODUTION_WIZARD_NAME,
PHONE_NUMBER_REGISTER_WIZARD_NAME,
PHONE_NUMBER_VERITY_OTP_WIZARD_NAME,
REGISTER_INFOMATION_WIZARD_NAME,
START_USE_WIZARD_NAME,
];
const LOADING_INITIAL_ROUTE = LOADING_SCREEN_NAME;
const MAIN_INITIAL_ROUTE = 'Home';
const WELCOME_INITIAL_ROUTE = WELCOME_WIZARD_NAME;
import {
registerNotificationOpened,
registerInitialNotification,
removeDeliveredNotification,
} from './app/core/fcm';
import {getIsFirstLoading, setIsFirstLoading} from './app/core/storage';
// Components
const HomeScreen = decorateMainAppStart(Home);
const Stack = createStackNavigator();
class App extends React.Component {
constructor(props) {
super(props);
this.state = {
loading: true,
isHome: false,
isFirstLoading: '',
translationMessagesState: translationMessages,
};
this.onResourceLanguageChange = this.onResourceLanguageChange.bind(this);
this.onChangeStackHome = this.onChangeStackHome.bind(this);
registerResourceLanguageChange(this.onResourceLanguageChange);
this.LoadingProps = propsComponent => (
<LoadingScreen
name={LOADING_SCREEN_NAME}
onFinished={this.handleFinishedWork}
{...propsComponent}
/>
);
this.IntroducationProps = propsComponent => (
<Introduction
name={INTRODUTION_WIZARD_NAME}
onFinished={this.handleFinishedWork}
{...propsComponent}
/>
);
this.PhoneNumberRegisterProps = propsComponent => (
<PhoneNumberRegisterScreen
name={PHONE_NUMBER_REGISTER_WIZARD_NAME}
onFinished={this.handleFinishedWork}
{...propsComponent}
/>
);
this.PhoneNumberVerifyOTPProps = propsComponent => (
<PhoneNumberVerifyOTPScreen
name={PHONE_NUMBER_VERITY_OTP_WIZARD_NAME}
onFinished={this.handleFinishedWork}
{...propsComponent}
/>
);
this.RegisterInfomationProps = propsComponent => (
<RegisterInfomation
name={REGISTER_INFOMATION_WIZARD_NAME}
onFinished={this.handleFinishedWork}
{...propsComponent}
/>
);
this.StartUseProps = propsComponent => (
<StartUse
name={START_USE_WIZARD_NAME}
onFinished={this.handleFinishedWork}
{...propsComponent}
/>
);
this.WelcomeProps = propsComponent => (
<Welcome
name={WELCOME_WIZARD_NAME}
onFinished={this.onChangeStackHome}
{...propsComponent}
/>
);
this.HomeProps = propsComponent => (
<HomeScreen name={MAIN_INITIAL_ROUTE} {...propsComponent} />
);
}
async componentDidMount() {
// Check trạng thái lần đầu tiên vào app => hien: Dang khoi tao. Lan sau: Dang dong bo
const isFirstLoading = await getIsFirstLoading();
this.setState({isFirstLoading: isFirstLoading === null});
if (isFirstLoading === null) {
setIsFirstLoading(false);
}
this.removeNotificationOpenedListener = registerNotificationOpened(
this.onNotificationOpened,
);
// Check whether an initial notification is available
registerInitialNotification(this.onNotificationOpened);
this.removeMessageListener = registerMessageHandler(async notifyObj => {
// TODO can sua ve dung dev trong server.js het => Khong duoc, __DEV__ nay chi dung cho debugger thoi
if (__DEV__) {
alert(JSON.stringify(notifyObj));
}
await remoteMessageListener(notifyObj);
});
}
componentWillUnmount() {
this.removeNotificationOpenedListener &&
this.removeNotificationOpenedListener();
this.removeMessageListener && this.removeMessageListener();
}
/**
* Xu ly khi 1 buoc trong wizard ket thuc
* @param name
* @param result la ket qua cua buoc truoc co the chuyen cho buoc sau. VD: PhoneNumberRegister tra ve result = {phoneNumber: '0988888888'}
* @param gotoMainScreen
* @param goBack
*/
handleFinishedWork = (
name = '',
result = {},
gotoMainScreen = false,
goBack = false,
) => {
const {isFirstLoading, loading} = this.state;
console.log(
'handleFinishedWork',
name,
gotoMainScreen,
goBack,
loading,
isFirstLoading,
);
if (goBack) {
navigate(prevStepName(name));
return;
}
// Neu khong phai la lan dau vao app hoac khong the hoan thanh 1 buoc trong wizard hoac la buoc cua wizard thi chuyen luon vao giao dien chinh.
if (!isFirstLoading || isLastStepOfWizard(name)) {
this.setState({loading: false});
return;
}
// Trường hợp lấy TokenFirebase === null thì check wizard còn 3 màn hình.
if (gotoMainScreen) {
wizard = [
LOADING_SCREEN_NAME,
INTRODUTION_WIZARD_NAME,
START_USE_WIZARD_NAME,
];
name =
name !== LOADING_SCREEN_NAME
? INTRODUTION_WIZARD_NAME
: LOADING_SCREEN_NAME;
}
navigate(nextStepName(name), result);
};
onChangeStackHome() {
this.setState({isHome: true});
}
onResourceLanguageChange(resource) {
this.setState({translationMessagesState: resource});
}
onNotificationOpened = remoteMessage => {
const {loading} = this.state;
console.log('onNotificationOpened');
if (!remoteMessage) {
return;
}
const obj = remoteMessage.notification;
if (
(obj && obj.data._group === 'INFO') ||
(obj && obj.data._group === NOTIFICATION_TYPE.SEND_SHORT_NEWS)
) {
navigate('NotifyDetail', {
item: {
title: obj.title,
bigText: obj.body,
timestamp: obj.data.timestamp,
text: obj.data.text,
largeIcon: obj.data.largeIcon,
},
});
} else if (obj && obj.data._group === 'MOBILE') {
if (loading) {
navigate(PHONE_NUMBER_REGISTER_WIZARD_NAME);
} else {
navigate('PhoneNumberRegisterScreen');
}
} else if (
obj &&
(obj.data._group === NOTIFICATION_TYPE.SEND_URL_NEW ||
obj.data._group === NOTIFICATION_TYPE.SEND_HTML_NEWS)
) {
if (obj.data._group === NOTIFICATION_TYPE.SEND_HTML_NEWS) {
navigate('DetailNew', {
item: {
title: obj.title,
data: obj?.data?.data,
},
});
} else {
navigate('PageView', {
item: {
title: obj.title,
data: obj?.data?.data,
},
});
}
}
// getNotifications().cancelNotification(remoteMessage.notification._notificationId);
removeDeliveredNotification(remoteMessage.notification._notificationId);
console.log(
'Notification caused app to open from background state:',
remoteMessage.notification._notificationId,
);
};
render() {
const {translationMessagesState, loading, isHome} = this.state;
return (
<ContextProvider>
<LanguageProvider messages={translationMessagesState}>
<NavigationContainer ref={navigationRef}>
{loading ? (
<Stack.Navigator
id="auth"
headerMode="none"
mode="card"
initialRouteName={LOADING_INITIAL_ROUTE}>
<Stack.Screen
name={LOADING_INITIAL_ROUTE}
component={this.LoadingProps}
/>
<Stack.Screen
name={INTRODUTION_WIZARD_NAME}
component={this.IntroducationProps}
/>
<Stack.Screen
name={PHONE_NUMBER_REGISTER_WIZARD_NAME}
component={this.PhoneNumberRegisterProps}
/>
<Stack.Screen
name={PHONE_NUMBER_VERITY_OTP_WIZARD_NAME}
component={this.PhoneNumberVerifyOTPProps}
/>
<Stack.Screen
name={REGISTER_INFOMATION_WIZARD_NAME}
component={this.RegisterInfomationProps}
/>
<Stack.Screen
name={START_USE_WIZARD_NAME}
component={this.StartUseProps}
/>
<Stack.Screen
path="NotifyDetail"
name="NotifyDetail"
component={NotifyDetail}
/>
</Stack.Navigator>
) : !isHome ? (
<Stack.Navigator
id="Welcome"
headerMode="none"
mode="card"
initialRouteName={WELCOME_INITIAL_ROUTE}>
<Stack.Screen
name={WELCOME_INITIAL_ROUTE}
component={this.WelcomeProps}
/>
</Stack.Navigator>
) : (
<Stack.Navigator
id="home"
headerMode="none"
mode="card"
initialRouteName={MAIN_INITIAL_ROUTE}>
<Stack.Screen
name={MAIN_INITIAL_ROUTE}
component={this.HomeProps}
/>
<Stack.Screen name="WatchScan" component={WatchScan} />
<Stack.Screen name="HistoryScan" component={HistoryScan} />
<Stack.Screen
name="NotifyDetail"
component={NotifyDetail}
path="NotifyDetail"
/>
<Stack.Screen name="Invite" component={Invite} />
<Stack.Screen
name="PhoneNumberRegisterScreen"
component={PhoneNumberRegisterScreen}
/>
<Stack.Screen
name="PhoneNumberVerifyOTPScreen"
component={PhoneNumberVerifyOTPScreen}
/>
<Stack.Screen
name="RegisterInfomation"
component={RegisterInfomation}
/>
<Stack.Screen name="PageView" component={PageView} />
<Stack.Screen name="DetailNew" component={DetailNew} />
<Stack.Screen
name="HistoryUploadedByOTP"
component={HistoryUploadedByOTPScreen}
/>
<Stack.Screen name="ViewLog" component={ViewLog} />
<Stack.Screen
name="DownloadLatestVersionScreen"
component={DownloadLatestVersionScreen}
/>
<Stack.Screen
name="ContactHistory"
component={ContactHistory}
/>
{/*<Stack.Screen name="ScanScreen" component={ScanScreen} />*/}
<Stack.Screen name="Welcome" component={Welcome} />
<Stack.Screen name="FAQScreen">
{props => <FAQScreen {...props} showBack={true} />}
</Stack.Screen>
</Stack.Navigator>
)}
</NavigationContainer>
</LanguageProvider>
</ContextProvider>
);
}
}
export default App;