This repository has been archived by the owner on Oct 19, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
App.tsx
773 lines (738 loc) · 29.3 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
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
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
import './global';
import 'intl-pluralrules';
import { newKitFromWeb3 } from '@celo/contractkit';
import { fontMap } from '@impact-market/ui-kit';
import AsyncStorage from '@react-native-async-storage/async-storage';
import NetInfo from '@react-native-community/netinfo';
import {
NavigationContainer,
DefaultTheme as NavigationDefaultTheme,
} from '@react-navigation/native';
import BigNumber from 'bignumber.js';
import Button from 'components/core/Button';
import Card from 'components/core/Card';
import DownloadSvg from 'components/svg/DownloadSvg';
import NoConnectionSvg from 'components/svg/NoConnectionSvg';
import AppLoading from 'expo-app-loading';
import { Asset } from 'expo-asset';
import Constants from 'expo-constants';
import * as Device from 'expo-device';
import * as Analytics from 'expo-firebase-analytics';
import * as Font from 'expo-font';
import * as Linking from 'expo-linking';
import * as Localization from 'expo-localization';
import * as Notifications from 'expo-notifications';
import * as SplashScreen from 'expo-splash-screen';
import {
STORAGE_USER_ADDRESS,
STORAGE_USER_PHONE_NUMBER,
} from 'helpers/constants';
import {
setAppExchangeRatesAction,
setAppSuspectWrongDateTime,
setCeloKit,
setPushNotificationListeners,
} from 'helpers/redux/actions/app';
import { setPushNotificationsToken } from 'helpers/redux/actions/auth';
import { resetUserApp, setUserLanguage } from 'helpers/redux/actions/user';
import rootSagas from 'helpers/redux/sagas';
import { isReadyRef, navigationRef } from 'helpers/rootNavigation';
import moment from 'moment';
import React from 'react';
import {
Image,
View,
LogBox,
StatusBar,
Dimensions,
Platform,
} from 'react-native';
import FlashMessage from 'react-native-flash-message';
import {
DefaultTheme,
Provider as PaperProvider,
configureFonts,
Text,
IconButton,
Modal,
Portal,
Paragraph,
Headline,
} from 'react-native-paper';
import { Host } from 'react-native-portalize';
import { Provider } from 'react-redux';
import { createStore, applyMiddleware } from 'redux';
import {
offlineMiddleware,
suspendSaga,
consumeActionMiddleware,
} from 'redux-offline-queue';
import createSagaMiddleware from 'redux-saga';
import { gt as semverGt, gte as semverGte } from 'semver';
import * as Sentry from 'sentry-expo';
import CacheStore from 'services/cacheStore';
import { startNotificationsListeners } from 'services/pushNotifications';
import FAQ from 'views/faq';
import Web3 from 'web3';
import config from './config';
import i18n, { loadi18n, supportedLanguages } from './src/assets/i18n';
import { welcomeUser } from './src/helpers';
import combinedReducer from './src/helpers/redux/reducers';
import Navigator from './src/navigator';
import Api from './src/services/api';
import { registerForPushNotifications } from './src/services/pushNotifications';
import { ipctColors } from './src/styles';
if (__DEV__) {
import('./ReactotronConfig').then(() =>
console.log('Reactotron Configured')
);
}
BigNumber.config({ EXPONENTIAL_AT: [-7, 30] });
const kit = newKitFromWeb3(new Web3(config.jsonRpc));
const middlewares = [];
const sagaMiddleware = createSagaMiddleware();
middlewares.push(offlineMiddleware({}));
middlewares.push(suspendSaga(sagaMiddleware));
middlewares.push(consumeActionMiddleware());
const store = createStore(combinedReducer, applyMiddleware(...middlewares));
sagaMiddleware.run(rootSagas);
const fonts = {
regular: {
fontFamily: 'Gelion-Regular',
},
medium: {
fontFamily: 'Gelion-Medium',
},
light: {
fontFamily: 'Gelion-Light',
},
thin: {
fontFamily: 'Gelion-Thin',
},
};
const theme = {
...DefaultTheme,
roundness: 4,
colors: {
...DefaultTheme.colors,
primary: ipctColors.blueRibbon,
text: ipctColors.almostBlack,
},
fonts: configureFonts({
default: fonts,
ios: fonts,
android: fonts,
}),
};
const navigationTheme = {
...NavigationDefaultTheme,
colors: {
...NavigationDefaultTheme.colors,
primary: ipctColors.blueRibbon,
background: '#ffffff',
},
};
LogBox.ignoreLogs([
'No DSN provided, backend will not do anything',
"The provided value 'moz-chunked-arraybuffer' is not a valid 'responseType'.",
"The provided value 'ms-stream' is not a valid 'responseType'.",
'Firebase Analytics is not available in the Expo client.',
]);
Notifications.setNotificationHandler({
handleNotification: async () => ({
shouldShowAlert: true,
shouldPlaySound: false,
shouldSetBadge: false,
}),
});
// Construct a new instrumentation instance. This is needed to communicate between the integration and React
const routingInstrumentation =
new Sentry.Native.ReactNavigationV5Instrumentation();
Sentry.init({
// release: 'impactmarket@' + process.env.REACT_APP_RELEASE_VERSION, // https://docs.sentry.io/product/sentry-basics/guides/integrate-frontend/upload-source-maps/
environment:
process.env.SENTRY_ENVIRONMENT !== undefined
? process.env.SENTRY_ENVIRONMENT
: 'dev',
dsn: process.env.EXPO_SENTRY_DNS,
enableInExpoDevelopment: false,
debug: false,
integrations: [
new Sentry.Native.ReactNativeTracing({
routingInstrumentation,
// ... other options
}),
],
// sampleRate: 0.1,
// tracesSampleRate: 0.1,
tracesSampler: (samplingContext) => {
// Examine provided context data (including parent decision, if any) along
// with anything in the global namespace to compute the sample rate or
// sampling decision for this transaction
// if ipct-activity is defined, send all error to sentry
if (
samplingContext.transactionContext.tags &&
samplingContext.transactionContext.tags['ipct-activity'] !==
undefined
) {
return 0.5;
}
return 0.1;
},
});
const prefix = Linking.makeUrl('/');
interface IAppState {
isSplashReady: boolean;
isAppReady: boolean;
// loggedIn is not used anywhere, only forces update!
loggedIn: boolean;
testnetWarningOpen: boolean;
infoUserNewAppVersion: boolean;
blockUserToUpdateApp: boolean;
netAvailable: boolean;
}
class App extends React.Component<any, IAppState> {
private currentRouteName: string | undefined = '';
private linking = {
prefixes: [prefix],
};
constructor(props: any) {
super(props);
this.state = {
isSplashReady: false,
isAppReady: false,
loggedIn: false,
testnetWarningOpen: false,
infoUserNewAppVersion: false,
blockUserToUpdateApp: false,
netAvailable: true,
};
}
componentDidMount = () => {
const currentLoggedIn = store.getState().user.wallet.address.length > 0;
if (currentLoggedIn) {
setPushNotificationListeners(
startNotificationsListeners(kit, store.dispatch)
);
}
if (config.testnet) {
// TODO:
// eslint-disable-next-line react/no-did-mount-set-state
this.setState({ testnetWarningOpen: true });
setTimeout(
() => this.setState({ testnetWarningOpen: false }),
5000
);
}
//
const { width, height } = Dimensions.get('screen');
Analytics.setUserProperties({
screen_dimensions: `${width}x${height}`,
os_version: `${Device.osName.slice(0, 1).toLowerCase()}${
Device.osVersion
}`, // to separate android and ios
});
};
componentWillUnmount = () => {
const listeners = store.getState().app.notificationsListeners;
if (listeners) {
Notifications.removeNotificationSubscription(
listeners.notificationReceivedListener
);
Notifications.removeNotificationSubscription(
listeners.notificationResponseReceivedListener
);
}
};
handleUpdateClick = () => {
const androidURL =
'https://play.google.com/store/apps/details?id=com.impactmarket.mobile';
const iosURL = 'https://apps.apple.com/app/impactmarket/id1530870911';
if (Device.osName === 'iOS') {
Linking.openURL(iosURL);
} else {
Linking.openURL(androidURL);
}
};
render() {
const {
isAppReady,
isSplashReady,
testnetWarningOpen,
infoUserNewAppVersion,
blockUserToUpdateApp,
netAvailable,
} = this.state;
if (!isSplashReady) {
return (
<AppLoading
startAsync={this._cacheSplashResourcesAsync}
onFinish={() => this.setState({ isSplashReady: true })}
onError={console.warn}
autoHideSplash={false}
/>
);
}
if (!netAvailable) {
return (
<PaperProvider theme={theme}>
<Portal>
<Modal visible dismissable={false}>
<Card
style={{
marginHorizontal: 20,
paddingVertical: 43,
}}
>
<Card.Content
style={{
alignItems: 'center',
}}
>
<NoConnectionSvg />
<Paragraph
style={{
fontFamily: 'Gelion-Regular',
fontSize: 21,
lineHeight: 25,
color: ipctColors.almostBlack,
width: '70%',
textAlign: 'center',
}}
>
{i18n.t('generic.offline')}
</Paragraph>
</Card.Content>
</Card>
</Modal>
</Portal>
</PaperProvider>
);
}
if (infoUserNewAppVersion) {
return (
<PaperProvider theme={theme}>
<Portal>
<Modal visible dismissable={false}>
<Card style={{ marginHorizontal: 20 }}>
<Card.Content>
<View
style={{
alignItems: 'center',
}}
>
<DownloadSvg />
<Headline
style={{
fontFamily: 'Gelion-Regular',
fontSize: 24,
lineHeight: 24,
textAlign: 'center',
color: ipctColors.almostBlack,
marginVertical: 16,
}}
>
{i18n.t(
'generic.newVersionAvailable'
)}
</Headline>
<Paragraph
style={{
fontFamily: 'Gelion-Regular',
fontSize: 16,
lineHeight: 19,
color: ipctColors.almostBlack,
textAlign: 'center',
}}
>
{i18n.t(
'generic.newVersionAvailableMessage'
)}
</Paragraph>
</View>
<Button
modeType="default"
style={{
marginTop: 20,
marginHorizontal: 5,
}}
bold
onPress={this.handleUpdateClick}
>
{i18n.t('generic.update')}
</Button>
{!blockUserToUpdateApp && (
<Button
modeType="gray"
style={{
marginTop: 11,
marginHorizontal: 5,
}}
onPress={() =>
this.setState({
infoUserNewAppVersion:
false,
})
}
>
{i18n.t('generic.skip')}
</Button>
)}
</Card.Content>
</Card>
</Modal>
</Portal>
</PaperProvider>
);
}
if (!isAppReady) {
return (
<View
style={{
flex: 1,
alignItems: 'center',
flexDirection: 'row',
backgroundColor: ipctColors.blueRibbon,
}}
>
<Image
style={{ flex: 1, resizeMode: 'cover' }}
source={require('./src/assets/images/splash.png')}
onLoad={this._cacheResourcesAsync}
/>
</View>
);
}
const testnetWarningView = (
<View
style={{
backgroundColor: '#45c7ff',
width: '100%',
paddingTop: 30,
paddingBottom: 10,
paddingHorizontal: 20,
position: 'absolute',
zIndex: testnetWarningOpen ? 1 : -1,
flex: 1,
flexDirection: 'row',
alignItems: 'center',
}}
>
<Text style={{ textAlign: 'center', width: '80%' }}>
{i18n.t('generic.testnetWarning')}
</Text>
<IconButton
style={{ width: '10%' }}
icon="close-circle-outline"
size={20}
onPress={() => this.setState({ testnetWarningOpen: false })}
/>
</View>
);
return (
<PaperProvider theme={theme}>
<Provider store={store}>
<StatusBar
backgroundColor="rgba(0, 0, 0, 0.2)"
translucent
/>
<FlashMessage
position={{
top:
Platform.OS === 'android'
? StatusBar.currentHeight
: 50,
left: 0,
right: 0,
}}
floating
/>
{config.testnet && testnetWarningView}
<NavigationContainer
theme={navigationTheme}
linking={this.linking}
onReady={() => {
const currentRouteName =
navigationRef.current?.getCurrentRoute()?.name;
if (currentRouteName !== undefined) {
Analytics.setCurrentScreen(currentRouteName);
}
routingInstrumentation.registerNavigationContainer(
navigationRef
);
// Save the current route name for later comparision
this.currentRouteName = currentRouteName;
(isReadyRef.current as any) = true; // https://github.com/DefinitelyTyped/DefinitelyTyped/issues/31065
}}
onStateChange={() => {
const previousRouteName = this.currentRouteName;
const currentRouteName =
navigationRef.current?.getCurrentRoute()?.name;
if (
previousRouteName !== currentRouteName &&
currentRouteName !== undefined
) {
// The line below uses the expo-firebase-analytics tracker
// https://docs.expo.io/versions/latest/sdk/firebase-analytics/
// Change this line to use another Mobile analytics SDK
Analytics.setCurrentScreen(currentRouteName);
}
// Save the current route name for later comparision
this.currentRouteName = currentRouteName;
}}
ref={navigationRef}
>
<Host>
<Navigator />
<FAQ />
</Host>
</NavigationContainer>
</Provider>
</PaperProvider>
);
}
_cacheSplashResourcesAsync = async () => {
const images = [require('./src/assets/images/splash.png')];
const cacheImages = images.map((image) => {
return Asset.fromModule(image).downloadAsync();
});
await Promise.all(cacheImages);
};
_cacheResourcesAsync = async () => {
try {
await Font.loadAsync(fontMap);
await Font.loadAsync({
// Load a font `Montserrat` from a static resource
// Montserrat: require('assets/fonts/Montserrat.ttf'),
// Any string can be used as the fontFamily name. Here we use an object to provide more control
'Gelion-SemiBold': {
uri: require('./src/assets/fonts/FontGelion/Gelion-SemiBold.ttf'),
},
'Gelion-Bold': {
uri: require('./src/assets/fonts/FontGelion/Gelion-Bold.ttf'),
},
'Gelion-Regular': {
uri: require('./src/assets/fonts/FontGelion/Gelion-Regular.ttf'),
},
'Gelion-Medium': {
uri: require('./src/assets/fonts/FontGelion/Gelion-Medium.ttf'),
},
'Gelion-Light': {
uri: require('./src/assets/fonts/FontGelion/Gelion-Light.ttf'),
},
'Gelion-Thin': {
uri: require('./src/assets/fonts/FontGelion/Gelion-Thin.ttf'),
},
'Inter-Thin': {
uri: require('./src/assets/fonts/Inter/Inter-Thin.ttf'),
},
'Inter-SemiBold': {
uri: require('./src/assets/fonts/Inter/Inter-SemiBold.ttf'),
},
'Inter-Regular': {
uri: require('./src/assets/fonts/Inter/Inter-Regular.ttf'),
},
'Inter-Medium': {
uri: require('./src/assets/fonts/Inter/Inter-Medium.ttf'),
},
'Inter-Light': {
uri: require('./src/assets/fonts/Inter/Inter-Light.ttf'),
},
'Inter-ExtraLight': {
uri: require('./src/assets/fonts/Inter/Inter-ExtraLight.ttf'),
},
'Inter-ExtraBold': {
uri: require('./src/assets/fonts/Inter/Inter-ExtraBold.ttf'),
},
'Inter-Bold': {
uri: require('./src/assets/fonts/Inter/Inter-Bold.ttf'),
},
'Inter-Black': {
uri: require('./src/assets/fonts/Inter/Inter-Black.ttf'),
},
'Manrope-Bold': {
uri: require('./src/assets/fonts/Manrope/Manrope-Bold.ttf'),
},
'Manrope-ExtraBold': {
uri: require('./src/assets/fonts/Manrope/Manrope-ExtraBold.ttf'),
},
'Manrope-ExtraLight': {
uri: require('./src/assets/fonts/Manrope/Manrope-ExtraLight.ttf'),
},
'Manrope-Light': {
uri: require('./src/assets/fonts/Manrope/Manrope-Light.ttf'),
},
'Manrope-Medium': {
uri: require('./src/assets/fonts/Manrope/Manrope-Medium.ttf'),
},
'Manrope-Regular': {
uri: require('./src/assets/fonts/Manrope/Manrope-Regular.ttf'),
},
'Manrope-SemiBold': {
uri: require('./src/assets/fonts/Manrope/Manrope-SemiBold.ttf'),
},
});
// wait to load langiages
await loadi18n;
let language = Localization.locale;
if (language.includes('-')) {
language = language.substr(0, language.indexOf('-'));
} else if (language.includes('_')) {
language = language.substr(0, language.indexOf('_'));
}
if (!supportedLanguages.includes(language)) {
language = 'en';
}
i18n.changeLanguage(language);
store.dispatch(setUserLanguage(language));
const netState = await NetInfo.fetch();
if (!netState.isConnected) {
this.setState({ netAvailable: false });
return;
}
await this._checkForNewVersion();
await this._authUser();
this.setState({ isAppReady: true });
// just at the end, so when we hide, app is ready!
SplashScreen.hideAsync();
} catch (e) {
Sentry.Native.captureException(e);
return (
<PaperProvider theme={theme}>
<Portal>
<Modal visible dismissable={false}>
<Card
style={{
marginHorizontal: 20,
paddingVertical: 43,
}}
>
<Card.Content
style={{
alignItems: 'center',
}}
>
<Paragraph
style={{
fontFamily: 'Gelion-Regular',
fontSize: 21,
lineHeight: 25,
color: ipctColors.almostBlack,
width: '70%',
textAlign: 'center',
}}
>
{i18n.t('errors.loadingApp')}
</Paragraph>
</Card.Content>
</Card>
</Modal>
</Portal>
</PaperProvider>
);
}
};
_checkForNewVersion = async () => {
// this should not be built this way,
// wee need instead an udp library and a NTP server.
const preTime = new Date();
const version = await Api.system.getMobileVersion();
const postTime = new Date();
if (version === undefined) {
// TODO: error loading app, reload
return;
}
const requestDiff = moment(preTime).diff(postTime);
const timeDiff =
new Date(postTime.getTime() - requestDiff / 2).getTime() -
version.timestamp;
// 10 seconds
if (timeDiff < -10000 || timeDiff > 10000) {
store.dispatch(setAppSuspectWrongDateTime(true, timeDiff));
}
let manifestVersion = '';
if (Constants.manifest2 !== null) {
manifestVersion = Constants.manifest2.runtimeVersion;
} else if (
Constants.manifest !== null &&
Constants.manifest.version !== undefined
) {
manifestVersion = Constants.manifest.version;
}
if (manifestVersion.length > 0) {
let lastVersionFromCache = await CacheStore.getLastVersion();
if (lastVersionFromCache === null) {
lastVersionFromCache = manifestVersion;
}
if (!semverGte(manifestVersion, version.minimal)) {
// id the user does not have the minimal required version
// block until update
this.setState({
blockUserToUpdateApp: true,
infoUserNewAppVersion: true,
});
} else if (semverGt(version.latest, lastVersionFromCache)) {
// if there's a new version
// warn only once
await CacheStore.cacheLastVersion(version.latest);
this.setState({ infoUserNewAppVersion: true });
}
}
};
_authUser = async () => {
let address: string | null = '';
let phoneNumber: string | null = '';
let loggedIn = false;
try {
store.dispatch(setCeloKit(kit));
address = await AsyncStorage.getItem(STORAGE_USER_ADDRESS);
phoneNumber = await AsyncStorage.getItem(STORAGE_USER_PHONE_NUMBER);
const exchangeRates = await Api.system.getExchangeRate();
const userRates: { [key: string]: number } = {};
Object.assign(
userRates,
...exchangeRates.map((y) => ({ [y.currency]: y.rate }))
);
store.dispatch(setAppExchangeRatesAction(userRates));
if (address !== null && phoneNumber !== null) {
const pushNotificationToken =
await registerForPushNotifications();
if (pushNotificationToken) {
store.dispatch(
setPushNotificationsToken(pushNotificationToken)
);
setPushNotificationListeners(
startNotificationsListeners(kit, store.dispatch)
);
}
const userWelcome = await Api.user.welcome(
pushNotificationToken
);
if (userWelcome !== undefined) {
const userMetadata = await CacheStore.getUser();
if (userMetadata === null) {
await AsyncStorage.clear();
store.dispatch(resetUserApp());
return;
}
await welcomeUser(
address,
phoneNumber,
userWelcome,
userRates,
kit,
store.dispatch,
userMetadata
);
loggedIn = true;
}
}
this.setState({
loggedIn,
});
} catch (e) {
Sentry.Native.captureException(e);
}
};
}
export default Sentry.Native.withProfiler(App, { name: 'App' });