-
Notifications
You must be signed in to change notification settings - Fork 0
/
App.js
145 lines (122 loc) · 3.96 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
/*
Author: Ted Jenks
React-Native App.js (entry point of application)
*/
import 'react-native-gesture-handler';
//------------------------------------------------------------------------------
/* IMPORTS */
// React imports
import React, {Component} from 'react';
import ReactNative, { Alert, LogBox, PermissionsAndroid } from "react-native";
// Third party imports
import * as Keychain from 'react-native-keychain';
import {NavigationContainer} from '@react-navigation/native';
import {initialize} from 'react-native-wifi-p2p';
import NetInfo from "@react-native-community/netinfo";
// Local imports
import {IdentityManager} from './app/src/tools/identityManager';
import ExistingUser from './app/src/components/existingUser/existingUser';
import NewUser from './app/src/components/newUser/newUser';
import LoadingPage from './app/src/components/generic/loadingPage';
//,
LogBox.ignoreAllLogs(); //Ignore all logs
//------------------------------------------------------------------------------
/* BODY */
class App extends Component {
state = {
newUser: null,
failConnect:false,
};
constructor() {
super();
this.handleRefresh().catch(e => console.log(e));
const unsubscribe = NetInfo.addEventListener(state => {
if(!state.isConnected){
this.noConnectionAlert();
this.setState({failConnect: true});
}
});
}
async componentDidMount() {
// this.handleRefresh().catch(e => console.log(e));
try {
await initialize();
// since it's required in Android >= 6.0#
const granted = await PermissionsAndroid.request(
PermissionsAndroid.PERMISSIONS.ACCESS_FINE_LOCATION,
{
title: 'Access to wi-fi P2P mode',
message: 'ACCESS_FINE_LOCATION',
},
);
console.log(
granted === PermissionsAndroid.RESULTS.GRANTED
? 'You can use the p2p mode'
: 'Permission denied: p2p mode will not work',
);
} catch (e) {
console.error('P2P error: ', e);
}
}
noConnectionAlert = () =>
Alert.alert(
'NO INTERNET CONNECTION',
'This app requires an internet connection. Please connect to the internet and restart app to continue.',
[],
);
handleRefresh = async () => {
const identityManager = new IdentityManager();
// check if the user has an existing account set up
try {
const identity = await identityManager.getID();
console.log(
'Identity information found in App.js: ',
JSON.stringify(identity).substring(0, 300),
);
if (identity == null) {
setTimeout(() => this.setState({newUser: true}), 750);
return;
} else {
setTimeout(() => this.setState({newUser: false}), 750);
return;
}
} catch (e) {
console.log(e);
}
};
handleDelete = async () => {
await this.setState({newUser: true});
setTimeout(async () => {
// wait for navigation to unmount
try {
let identityManger = new IdentityManager(); // clear personal details in Realm
const queryResult = await identityManger.getID();
await identityManger.deleteAll();
Keychain.resetGenericPassword(); // clear BC account info
} catch (e) {
console.log(e);
}
}, 2000);
};
handleSubmit = () => {
this.setState({newUser: false});
this.handleRefresh().catch(e => console.log(e));
};
displayContent = () => {
if (this.state.newUser && !this.state.failConnect) {
return (
<NewUser onSubmit={this.handleSubmit} onRefresh={this.handleRefresh} onDelete={this.handleDelete}/>
);
} else if (this.state.newUser === false && !this.state.failConnect) {
return <ExistingUser onDelete={this.handleDelete} />;
} else {
return <LoadingPage />;
}
};
render() {
return <NavigationContainer>{this.displayContent()}</NavigationContainer>;
}
}
//------------------------------------------------------------------------------
/* EXPORTS */
export default App;