/
App.js
196 lines (170 loc) Β· 6.38 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
import analytics from '@segment/analytics-react-native';
import { get, last } from 'lodash';
import PropTypes from 'prop-types';
import nanoid from 'nanoid/non-secure';
import React, { Component } from 'react';
import {
AppRegistry,
AppState,
Linking,
} from 'react-native';
import CodePush from 'react-native-code-push';
import { REACT_APP_SEGMENT_API_WRITE_KEY } from 'react-native-dotenv';
import firebase from 'react-native-firebase';
import RNIOS11DeviceCheck from 'react-native-ios11-devicecheck';
import { useScreens } from 'react-native-screens';
import { connect, Provider } from 'react-redux';
import { compose, withProps } from 'recompact';
import { FlexItem } from './components/layout';
import OfflineBadge from './components/OfflineBadge';
import {
withWalletConnectConnections,
withWalletConnectOnSessionRequest,
} from './hoc';
import { registerTokenRefreshListener, saveFCMToken } from './model/firebase';
import * as keychain from './model/keychain';
import { Navigation } from './navigation';
import store from './redux/store';
import { requestsForTopic } from './redux/requests';
import Routes from './screens/Routes';
import { parseQueryParams } from './utils';
if (process.env.NODE_ENV === 'development') {
console.disableYellowBox = true;
}
useScreens(false);
class App extends Component {
static propTypes = {
appInitTimestamp: PropTypes.number,
requestsForTopic: PropTypes.func,
sortedWalletConnectors: PropTypes.arrayOf(PropTypes.object),
walletConnectClearTimestamp: PropTypes.func,
walletConnectOnSessionRequest: PropTypes.func,
walletConnectUpdateTimestamp: PropTypes.func,
};
state = { appState: AppState.currentState }
handleOpenLinkingURL = ({ url }) => {
Linking.canOpenURL(url).then((supported) => {
if (supported) {
const { uri, redirectUrl } = parseQueryParams(url);
const redirect = () => Linking.openURL(redirectUrl);
this.props.walletConnectOnSessionRequest(uri, redirect);
}
});
}
onPushNotificationOpened = (topic, autoOpened = false, fromLocal = false) => {
const { appInitTimestamp, requestsForTopic } = this.props;
const requests = requestsForTopic(topic);
if (requests && requests.length === 1) {
const request = requests[0];
const transactionTimestamp = get(request, 'displayDetails.timestampInMs');
const isNewTransaction = appInitTimestamp && (transactionTimestamp > appInitTimestamp);
if (!autoOpened || isNewTransaction) {
return Navigation.handleAction({
params: { autoOpened, transactionDetails: request },
routeName: 'ConfirmRequest',
});
}
}
if (fromLocal) {
return Navigation.handleAction({
params: { autoOpened, transactionDetails: last(requests) },
routeName: 'ConfirmRequest',
});
}
return Navigation.handleAction({ routeName: 'ProfileScreen' });
};
async componentDidMount() {
AppState.addEventListener('change', this.handleAppStateChange);
Linking.addEventListener('url', this.handleOpenLinkingURL);
await this.handleInitializeAnalytics();
firebase.notifications().getInitialNotification().then(notificationOpen => {
if (notificationOpen) {
const topic = get(notificationOpen, 'notification.data.topic');
this.onPushNotificationOpened(topic, false);
}
});
saveFCMToken();
this.onTokenRefreshListener = registerTokenRefreshListener();
// notification while app in foreground
this.notificationListener = firebase.notifications().onNotification(notification => {
const route = Navigation.getActiveRouteName();
if (route === 'ConfirmRequest') {
const localNotification = new firebase.notifications.Notification()
.setTitle(notification.title)
.setBody(notification.body)
.setData({ ...notification.data, fromLocal: true });
firebase.notifications().displayNotification(localNotification);
} else {
const topic = get(notification, 'data.topic');
this.onPushNotificationOpened(topic, true);
}
});
// notification opened from background
this.notificationOpenedListener = firebase.notifications().onNotificationOpened(notificationOpen => {
const topic = get(notificationOpen, 'notification.data.topic');
const fromLocal = get(notificationOpen, 'notification.data.fromLocal', false);
this.onPushNotificationOpened(topic, false, fromLocal);
});
}
handleInitializeAnalytics = async () => {
const storedIdentifier = await keychain.loadString('analyticsUserIdentifier');
if (!storedIdentifier) {
const identifier = await RNIOS11DeviceCheck.getToken()
.then((deviceId) => deviceId)
.catch(() => nanoid());
await keychain.saveString('analyticsUserIdentifier', identifier);
analytics.identify(identifier);
}
await analytics.setup(REACT_APP_SEGMENT_API_WRITE_KEY, {
ios: {
trackDeepLinks: true,
},
trackAppLifecycleEvents: true,
trackAttributionData: true,
});
}
handleAppStateChange = async (nextAppState) => {
if (nextAppState === 'active') {
this.props.walletConnectUpdateTimestamp();
await firebase.notifications().removeAllDeliveredNotifications();
}
if (nextAppState === 'background') {
this.props.walletConnectClearTimestamp();
}
this.setState({ appState: nextAppState });
}
componentWillUnmount() {
AppState.removeEventListener('change', this.handleAppStateChange);
Linking.removeEventListener('url', this.handleOpenLinkingURL);
this.notificationListener();
this.notificationOpenedListener();
this.onTokenRefreshListener();
}
handleNavigatorRef = (navigatorRef) => Navigation.setTopLevelNavigator(navigatorRef)
render = () => (
<Provider store={store}>
<FlexItem>
<OfflineBadge />
<Routes
ref={this.handleNavigatorRef}
/>
</FlexItem>
</Provider>
)
}
const AppWithRedux = compose(
withProps({ store }),
withWalletConnectConnections,
withWalletConnectOnSessionRequest,
connect(
({ walletconnect: { appInitTimestamp } }) => ({ appInitTimestamp }),
{
requestsForTopic,
},
),
)(App);
const AppWithCodePush = CodePush({
checkFrequency: CodePush.CheckFrequency.ON_APP_RESUME,
installMode: CodePush.InstallMode.ON_NEXT_RESUME,
})(AppWithRedux);
AppRegistry.registerComponent('Rainbow', () => AppWithCodePush);