-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathApp.js
125 lines (106 loc) · 3.67 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
import React from 'react';
import { View, StyleSheet, AppState } from 'react-native';
import AppNavigator from './src/navigation/AppNavigator';
import LoadingScreen from './src/screens/LoadingScreen';
import DatabaseManager from './src/manager/DatabaseManager';
import LanguageManager from './src/manager/LanguageManager';
import GlutonManager from './src/manager/GlutonManager';
import GearManager from './src/manager/GearManager';
import UploadManager from './src/manager/UploadManager';
import NotificationManager from './src/manager/NotificationManager';
import UsernameDialog from './src/components/UsernameDialog';
import { } from 'react-native-dotenv';
export default class App extends React.Component {
state = {
isSplashReady: false,
isAppReady: false,
appState: AppState.currentState
}
componentDidMount() {
DatabaseManager.getInstance().loadSettings(null,
(_, error) => { alert("error loading settings" + JSON.stringify(error)); },
(_, {rows: { _array }}) => {
let settings = {};
for (var i in _array) {
settings[_array[i].name] = JSON.parse(_array[i].objData);
}
this.initApplication(settings);
}
);
AppState.addEventListener('change', this._handleAppStateChange);
}
componentWillUnmount() {
AppState.removeEventListener('change', this._handleAppStateChange);
console.log("unmounting App")
}
_handleAppStateChange = (nextAppState) => {
if (
nextAppState.match(/inactive|background/) &&
this.state.appState === 'active'
) {
this.uploadFreshData();
console.log('App is going to background!');
}
this.setState({appState: nextAppState});
};
initApplication(settings) {
LanguageManager.getInstance().setLanguage(settings.language);
NotificationManager.getInstance(); //just to show the user the notification permission screen.
GlutonManager.getInstance().setBuddy(settings.nickname);
GearManager.getInstance().setWsHost(settings.wsHost);
GearManager.getInstance().setGearHost(settings.gearHost);
GearManager.getInstance().connect();
this.uploadFreshData();
this.setState({
isSplashReady: true,
hasUserId: !!settings.userId,
userId: settings.userId
});
setTimeout(() => this.setState({isAppReady: true}), 3000);
}
handleNewUsername = (userName) => {
console.log("Saving username:" + JSON.stringify(userName));
DatabaseManager.getInstance().saveSettings('userId', userName, (error) => {alert(error)}, null);
this.setState({
hasUserId : true,
userId: userName})
};
getUploadServiceAuthToken = () => this.state.userId
uploadFreshData() {
token = this.getUploadServiceAuthToken()
//TODO get new auth token
if (token) {
UploadManager.getInstance().setToken(token);
DatabaseManager.getInstance().fetchUnrecordedData((_, error) => console.error(error), (_, data) => {
UploadManager.getInstance().uploadData(data, () => { DatabaseManager.getInstance().updateLastRecorded(); });
});
}
}
render() {
return (
<View style={styles.container}>
{this.state.isSplashReady == false
? null
: this.state.hasUserId
? <AppNavigator/>
: <UsernameDialog onUsername ={this.handleNewUsername}/>
}
<LoadingScreen hide={this.state.isAppReady} style={styles.loading}/>
</View>
);
}
}
const styles = StyleSheet.create({
container: {
width: '100%',
height: '100%',
},
loading: {
position: 'absolute',
width: '100%',
height: '100%',
backgroundColor: 'white',
alignItems: 'center',
justifyContent: 'center',
},
});