forked from alicedapp/AliceX
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathApp.js
348 lines (313 loc) · 10.1 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
/**
* @format
* This is the ExampleMaps Registrar for Alice
*/
import React, { Component } from 'react';
import {
AppState,
AsyncStorage,
Dimensions,
Image,
Linking,
View,
SafeAreaView,
} from 'react-native';
import {
createAppContainer,
createMaterialTopTabNavigator,
createStackNavigator,
createSwitchNavigator
} from 'react-navigation';
import CodePush from "react-native-code-push";
import MapboxGL from '@react-native-mapbox-gl/maps';
import { isIphoneX } from 'react-native-iphone-x-helper'
import env from './env.json';
const {AppRegistry, ...MiniDapps} = require('./src/Apps/AppRegistry'); // AppRegistry is required
import {Settings, Wallet} from './src/AliceSDK/Web3'
import DappsScreen from './src/AliceCore/Screens/DappsScreen';
import Dashboard from './src/AliceCore/Screens/Dashboard';
import NavigatorService, {navigate} from './src/AliceCore/Utils/navigationWrapper';
import {NavigationBar} from "./src/AliceCore/Components";
import {challengedPOI} from "./src/Apps/Foam/utils";
import firebase from 'react-native-firebase';
const { height, width } = Dimensions.get('window');
MapboxGL.setAccessToken(env.mapbox);
GLOBAL.XMLHttpRequest = GLOBAL.originalXMLHttpRequest || GLOBAL.XMLHttpRequest;
const AppTabNavigator = createMaterialTopTabNavigator({
DappsScreen: {
screen: DappsScreen,
navigationOptions: {
tabBarLabel: 'Home',
tabBarIcon: ({ focused }) => (
focused ? <Image source={require('./src/AliceCore/Assets/dapps-tab-selected.png')} style={{resizeMode: 'contain', width: 42}}/>
: <Image source={require('./src/AliceCore/Assets/dapps-tab.png')} style={{resizeMode: 'contain', width: 42}}/>
)
}
},
Dashboard: {
screen: Dashboard,
navigationOptions: {
tabBarLabel: 'Dashboard',
tabBarIcon: ({ focused }) => (
focused ? <Image source={require('./src/AliceCore/Assets/dashboard-selected.png')} style={{resizeMode: 'contain', width: 42}}/>
: <Image source={require('./src/AliceCore/Assets/dashboard-tab.png')} style={{resizeMode: 'contain', width: 42}}/>
)
}
}
}, {
initialRouteName: 'Dashboard',
order: ['DappsScreen', 'Dashboard',],
tabBarPosition: 'bottom',
animationEnabled: true,
header: {
style: {
elevation: 0,
shadowOpacity: 0,
}
},
tabBarOptions: {
showLabel: false,
backgroundColor: 'white',
indicatorStyle: {
backgroundColor: 'transparent',
},
labelStyle: {},
allowFontScaling: true,
activeTintColor: '#000',
inactiveTintColor: '#fff',
style: {
elevation: 0,
shadowOpacity: 0,
backgroundColor: 'transparent',
borderTopWidth: 0,
position: 'absolute',
left: 0,
right: 0,
bottom: 40,
},
showIcon: true
},
});
const SwitchNavigator = createSwitchNavigator(MiniDapps);
const MainApp = createStackNavigator({
DappsScreen: { screen: DappsScreen },
// DappsScreen: { screen: MiniDapps.DAOstack },
...MiniDapps,
}, {
headerMode: 'none',
});
//Switch to go back to react native main screen
// export const AliceMain = createAppContainer(MainApp);
export const AliceMain = createAppContainer(SwitchNavigator);
class App extends Component {
constructor(props) {
super(props);
this.state = {
wallet: '',
network: '',
rotation: '',
networkColor: '',
darkMode: '',
walletconnect: '',
orientation: '',
deeplink: '',
currentRoute: 'DappsScreen',
appState: AppState.currentState,
};
}
componentDidMount() {
this.getOrientation();
this.getNetwork();
if (this.props.navigationRoute) {
navigate(this.props.navigationRoute);
}
this.checkPermission();
AppState.addEventListener('change', this._handleAppStateChange);
const aliceEventEmitter = Wallet.aliceEvent()
aliceEventEmitter.addListener(
"aliceEvent",
(event) => {
console.log('EVENT TRIGGERED: ', event)
if (event.address) {
console.log('WALLET INFO: ', event, event.address);
this.setState({ wallet: event.address});
}
if (event.network) {
const parsedEvent = JSON.parse(event.network);
console.log('NETWORK CHANGED: ', parsedEvent);
this.setState({network: parsedEvent.name, networkColor: parsedEvent.color});
}
if (event.orientation) {
console.log('ROTATION CHANGED: ', event, event.orientation);
this.setState({ orientation: event.orientation});
}
if (event.deeplink) {
console.log('DEEP LINK: ', event, event.deeplink);
this.handleOpenURL(event.deeplink);
this.setState({ deeplink: event.deeplink});
}
if (event.walletconnect) {
console.log('WALLET CONNECT: ', event, event.walletconnect);
this.setState({ walletconnect: event.walletconnect});
}
if (event.isDarkMode) {
console.log('DARK MODE: ', event, event.isDarkMode);
this.setState({ darkMode: event.isDarkMode});
}
}
);
}
//1
async checkPermission() {
const enabled = await firebase.messaging().hasPermission();
if (enabled) {
this.getToken();
} else {
this.requestPermission();
}
}
//3
async getToken() {
let fcmToken = await AsyncStorage.getItem('fcmToken');
console.log('FIREBASE TOKEN: ', fcmToken);
if (!fcmToken) {
fcmToken = await firebase.messaging().getToken();
if (fcmToken) {
// user has a device token
console.log('FIREBASE TOKEN: ', fcmToken);
// await AsyncStorage.setItem('fcmToken', fcmToken);
}
}
}
//2
async requestPermission() {
try {
await firebase.messaging().requestPermission();
// User has authorised
this.getToken();
} catch (error) {
// User has rejected permissions
console.log('permission rejected');
}
}
componentWillUnmount() {
AppState.removeEventListener('change', this._handleAppStateChange);
}
_handleAppStateChange = (nextAppState) => {
console.log('APPSTATE: ', nextAppState)
if (this.state.appState.match(/inactive|background/) && nextAppState === 'active') {
console.log('App has come to the foreground!')
}
this.setState({appState: nextAppState});
}
handleOpenURL(event) {
const route = event.replace(/.*?:\/\//g, '');
const {dapp, data} = JSON.parse(decodeURIComponent(route.substring(3)));
navigate(dapp, {data});
}
getNetwork = async () => {
const networkInfo = await Wallet.getNetwork();
this.setState({network: networkInfo.name, networkColor: networkInfo.color});
};
componentWillUnmount() {
}
onReceived(notification) {
console.log("Notification received: ", notification);
}
onOpened(openResult) {
console.log('Message: ', openResult.notification.payload.body);
if (openResult.notification.payload.title === "FOAM") {
navigate('FoamMap', {poi: challengedPOI});
}
if (openResult.notification.payload.title === "E2E") {
navigate('E2E');
}
if (openResult.notification.payload.title === "VotezUp") {
navigate('BridgeWater');
}
if (openResult.notification.payload.title === "CheezeWizards") {
navigate('CheezeWizards/WizardScreen', {
notificationChallenge: {
affinity: 3,
ascending: false,
ascensionOpponent: 0,
challengeId: "_71eemyysk",
currentDuel: "0x0000000000000000000000000000000000000000000000000000000000000000",
id: "6144",
maxPower: 117022711285813,
molded: false,
nonce: 4,
owner: "0xB45B74aDE7973AD25eC91F64c64aEC07d26F386C",
power: 56340035804596,
ready: true,
},
wizard: {
affinity: 1,
ascending: false,
ascensionOpponent: 0,
challengeId: "_m9ol8etji",
currentDuel: "0x0000000000000000000000000000000000000000000000000000000000000000",
id: "6090",
maxPower: 70000000000000,
molded: false,
nonce: 2,
owner: "0xA1b02d8c67b0FDCF4E379855868DeB470E169cfB",
power: 70000000000000,
ready: true,
}
});
}
}
onIds(device) {
console.log('Device info: ', device);
}
getOrientation = async () => {
try {
console.log('ORIENTATION: ', await Settings.getOrientation());
} catch(e) {
console.log(e);
}
};
getActiveRouteName = (navigationState) => {
if (!navigationState) {
return null;
}
const route = navigationState.routes[navigationState.index];
// dive into nested navigators
if (route.routes) {
return this.getActiveRouteName(route);
}
return route.routeName;
}
render() {
return (
<View style={{flex: 1}}>
{this.state.network !== 'main' && <View style={{ backgroundColor: this.state.networkColor, position: 'absolute', width, top:0, height: isIphoneX() ? 32 : 20, zIndex: 1}}/>}
{this.state.currentRoute !== "DappsScreen" && <NavigationBar/>}
<AliceMain
ref={navigatorRef => {
NavigatorService.setContainer(navigatorRef);
}}
onNavigationStateChange={(prevState, currentState, action) => {
const currentRouteName = this.getActiveRouteName(currentState);
const previousRouteName = this.getActiveRouteName(prevState);
console.log('CURRENT ROUTE: ', currentRouteName);
console.log('CURRENT ROUTE: ', previousRouteName);
console.log('CURRENT ROUTE STATE: ', this.state.currentRoute);
if (previousRouteName !== currentRouteName) {
this.setState({ currentRoute: currentRouteName });
// the line below uses the @react-native-firebase/analytics tracker
// change the tracker here to use other Mobile analytics SDK.
// analytics().setCurrentScreen(currentRouteName, currentRouteName);
}
}}
>
</AliceMain>
</View>
);
}
}
export default CodePush({
checkFrequency: CodePush.CheckFrequency.ON_APP_RESUME,
installMode: CodePush.InstallMode.ON_NEXT_RESUME,
})(App);