-
Notifications
You must be signed in to change notification settings - Fork 0
/
App.js
142 lines (133 loc) · 3.59 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
/**
* Sample React Native App
* https://github.com/facebook/react-native
*
* @format
* @flow
*/
import React, { Component } from 'react';
import {
SafeAreaView,
View,
StatusBar,
AsyncStorage,
NetInfo,
Text
} from 'react-native';
import { createStackNavigator, createAppContainer } from "react-navigation";
import SplashScreen from "./screens/Splash/SplashComponent";
import ExpenseListScreen from "./screens/ExpenseList/ExpenseListComponent";
import SettingScreen from "./screens/ExpenseSetting/ExpenseSettingComponent";
const stackNavigator = createStackNavigator({
ExpenseList: {
screen: ExpenseListScreen,
navigationOptions: {
title: "Expense List",
}
},
Setting: {
screen: SettingScreen,
title: "Setting",
navigationOptions: {
title: "Setting",
}
},
},
{
initialRouteName: "ExpenseList",
/* The header config from HomeScreen is now here */
defaultNavigationOptions: {
headerStyle: {
backgroundColor: "#f4511e"
},
headerTintColor: "#fff",
headerTitleStyle: {
//fontFamily: "Montserrat-Bold",
fontSize: 20
}
}
}
)
const AppContainer = createAppContainer(stackNavigator);
export default class App extends Component {
constructor(props) {
super(props);
this.state = {
isLoading: true,
isConnected: true,
};
this.setResetUserDefault()
}
componentDidMount() {
// Preload data from an external API
// Preload data using AsyncStorage
setTimeout(() => {
this.setState({ isLoading: false });
this.checkInternetConnectivity();
}, 5000);
}
checkInternetConnectivity() {
// Get the network state once on each fresh launch:
NetInfo.getConnectionInfo().then(result => {
console.log("Connection type", result.type);
console.log("Is connected?", result.isConnected);
const connected = (result.type == "none") ? false : true
this.setState({
isConnected: connected
})
});
//Subscribe to network state updates:
// Subscribe
NetInfo.addEventListener("connectionChange", result => {
console.log("Connection type", result.type);
console.log("Is connected?", result.isConnected);
const connected = (result.type == "none") ? false : true
this.setState({
isConnected: connected
})
})
}
// To reset the categories and their records
async setResetUserDefault() {
try {
const datetime = new Date()
const currentmonth = datetime.getMonth().toString()
var oldmonth = await AsyncStorage.getItem("month")
if (oldmonth == undefined || oldmonth == null || oldmonth.length == 0) {
await AsyncStorage.setItem("month", currentmonth)
} else {
if (parseInt(currentmonth) != parseInt(oldmonth)) {
await AsyncStorage.removeItem("default_categories")
await AsyncStorage.setItem("month", currentmonth)
}
}
} catch (error) {
console.log(error.message)
}
}
render() {
if (this.state.isLoading) {
return <SplashScreen />
}
if (this.state.isConnected == true) {
return <AppContainer />
} else {
// To get a view with "No internet title"
return (
<View style={{
flex: 1,
justifyContent: "center",
padding: 10,
backgroundColor: "pink",
alignItems: "center"
}}>
<Text style={{
fontWeight: "bold",
color: "black",
textAlign: "center"
}}> No internet connectivity. Check your device settings. </Text>
</View>
)
}
}
}