This repository was archived by the owner on Mar 9, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathApp.js
More file actions
144 lines (125 loc) · 5.16 KB
/
App.js
File metadata and controls
144 lines (125 loc) · 5.16 KB
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
import Expo from 'expo';
import React, { Component } from 'react';
import { NavigationActions } from 'react-navigation';
import { AppRegistry, NetInfo, Alert, KeyboardAvoidingView, View, AsyncStorage, BackHandler } from 'react-native';
import { _currencies } from './build/src/config/Data';
import Root from './build/src/config/Router';
export default class App extends Component {
constructor(props, state) {
super(props, state);
this.state = {
currentScreen: 'Login'
}
}
render() {
return (
<Root
onNavigationStateChange={(prevState, currentState) => {
const currentScreen = this.getCurrentRouteName(currentState);
const prevScreen = this.getCurrentRouteName(prevState);
if (prevScreen !== currentScreen) {
this.setState({ currentScreen });
}
}}
ref={nav => { this.navigator = nav; }}
/>
);
}
componentDidMount() {
console.disableYellowBox = true;
Expo.ScreenOrientation.allow(Expo.ScreenOrientation.Orientation.PORTRAIT);
NetInfo.isConnected.addEventListener('connectionChange', this.handleConnectivityChange);
BackHandler.addEventListener('hardwareBackPress', this.onBackPress);
}
componentWillUnmount() {
NetInfo.isConnected.removeEventListener('connectionChange', this.handleConnectivityChange);
BackHandler.removeEventListener('hardwareBackPress', this.onBackPress);
}
getCurrentRouteName(navigationState) {
if (!navigationState) {
return null;
}
const route = navigationState.routes[navigationState.index];
// dive into nested navigators
if (route.routes) {
return this.getCurrentRouteName(route);
}
return route.routeName;
}
onBackPress = () => {
if (this.state.currentScreen === 'Converter' || this.state.currentScreen === 'Settings') {
this.navigator.dispatch(NavigationActions.navigate({ routeName: 'Home' }));
return true;
}
if (this.state.currentScreen === 'Login' || this.state.currentScreen === 'ExpenseFeed' || this.state.currentScreen === 'GroupFeed') {
Alert.alert('Warning', 'Do you really want to close the application?', [
{ text: 'Cancel', style: 'cancel' },
{ text: 'OK', onPress: () => BackHandler.exitApp() }
],
{ onDismiss: () => undefined }
);
return true;
}
this.navigator.dispatch(NavigationActions.back());
return true;
}
handleConnectivityChange = (isConnected) => {
console.log('You are ' + (isConnected ? 'online' : 'offline'));
if (isConnected) {
AsyncStorage.getItem('currencies')
.then((value) => {
let currencies, latest;
if (value) {
let parsed = JSON.parse(value);
currencies = parsed.currencies;
latest = parsed.latest;
}
let date = new Date();
let today = date.getFullYear() + '-' + ('0' + (date.getMonth() + 1)).slice(-2) + '-' + ('0' + date.getDate()).slice(-2);
if (latest && latest === today) {
console.log('currencies up to date');
return;
}
this.updateExchangeRates('EUR', today);
AsyncStorage.getItem('defaultCurrency').then((value) => {
if (!value) {
AsyncStorage.setItem('defaultCurrency', JSON.stringify({ name: 'Euro', tag: 'EUR', rate: 1, symbol: '€' }));
}
});
});
}
}
async updateExchangeRates(base, today) {
console.log('updating currencies');
let headers = {
'Content-Type': 'application/json',
'Cache-Control': 'no-cache'
};
fetch('http://api.fixer.io/latest', { method: 'GET', headers: headers, body: null })
.then((resp) => resp.json())
.then((data) => {
if (data.rates) {
let key;
for (key in data.rates) {
_currencies[key].rate = data.rates[key];
_currencies[key].base = base;
_currencies[key].date = today;
}
_currencies[base].rate = 1;
_currencies[base].base = base;
_currencies[base].date = today;
let result = {
latest: today,
rates: _currencies
};
AsyncStorage.setItem('currencies', JSON.stringify(result));
} else {
throw 'Mathias';
}
})
.catch((error) => {
console.log(error);
});
}
}
AppRegistry.registerComponent('Karavaan', () => App);