-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathApp.js
191 lines (181 loc) · 4.76 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
import React, {useState, useEffect} from 'react';
import {NavigationContainer} from '@react-navigation/native';
import RootNavigation from './src/navigations/RootNavigation';
import AuthNavigation from './src/navigations/AuthNavigation';
import {Provider} from 'react-redux';
import {AuthContext} from './src/components/context';
import SQLite from 'react-native-sqlite-storage';
import AsyncStorage from '@react-native-async-storage/async-storage';
import SplashScreen from './src/screens/SplashScreen';
import store from './src/redux/store';
import {StyleSheet, PermissionsAndroid, Text} from 'react-native';
export default function App() {
const [show_splash, showSplash] = useState(true);
const [userID, setUserID] = React.useState(null);
const saveUserID = async user_id => {
try {
await AsyncStorage.setItem('user_id', user_id);
setUserID(await AsyncStorage.getItem('user_id'));
} catch (e) {
console.log('error ::', e);
}
};
const requestWriteStoragePermission = async () => {
try {
const granted = await PermissionsAndroid.request(
PermissionsAndroid.PERMISSIONS.WRITE_EXTERNAL_STORAGE,
{
title: 'Write Storage Permission',
message:
'App needs access to your device storage to save the signature image.',
buttonPositive: 'OK',
buttonNegative: 'Cancel',
},
);
if (granted === PermissionsAndroid.RESULTS.GRANTED) {
await AsyncStorage.setItem(
'writeStoragePermission',
PermissionsAndroid.RESULTS.GRANTED,
);
return true; // Permission granted
} else {
await AsyncStorage.setItem(
'writeStoragePermission',
PermissionsAndroid.RESULTS.DENIED,
);
return false; // Permission denied
}
} catch (error) {
console.log('Error requesting write storage permission:', error);
}
};
const removeUserID = async () => {
try {
await AsyncStorage.removeItem('user_id');
setUserID(null);
} catch (e) {
console.log('error ::', e);
}
};
global.db = SQLite.openDatabase(
{
name: 'M3SDB.db',
createFromLocation: '~M3SDB.db',
location: 'Library',
readOnly: false,
},
success => {},
error => {
console.log('Error', error);
},
);
useEffect(() => {
const saveIp = async user_id => {
try {
await AsyncStorage.setItem('ip', 'bbda-103-231-92-41.ngrok-free.app');
// await AsyncStorage.setItem('ip', '192.168.177.107');
await AsyncStorage.setItem('port', '80');
} catch (e) {
console.log('error ::', e);
}
};
const timer = setTimeout(() => {
showSplash(false);
}, 3000);
requestWriteStoragePermission();
saveIp();
return () => clearTimeout(timer);
}, []);
useEffect(() => {
async function loadStoredUserID() {
try {
const storedUserID = await AsyncStorage.getItem('user_id');
if (storedUserID) {
setUserID(storedUserID);
}
} catch (e) {
console.log('Error loading user ID:', e);
}
}
loadStoredUserID();
}, []);
return (
<Provider store={store}>
<NavigationContainer>
{show_splash ? (
<SplashScreen />
) : userID == null ? (
<AuthContext.Provider value={{saveUserID, userID}}>
<AuthNavigation />
</AuthContext.Provider>
) : (
<AuthContext.Provider value={{removeUserID}}>
<RootNavigation />
</AuthContext.Provider>
)}
</NavigationContainer>
</Provider>
);
}
const styles = StyleSheet.create({
textInput: {
width: 200,
height: 40,
borderWidth: 1,
borderColor: '#000',
borderRadius: 5,
paddingHorizontal: 10,
},
sectionContainer: {
marginTop: 32,
paddingHorizontal: 24,
},
textInputContainer: {
position: 'absolute',
},
sectionTitle: {
fontSize: 24,
fontWeight: '600',
},
sectionDescription: {
marginTop: 8,
fontSize: 18,
fontWeight: '400',
},
highlight: {
fontWeight: '700',
},
container: {
flex: 1,
justifyContent: 'center',
alignItems: 'center',
backgroundColor: '#F5FCFF',
},
strokeColorButton: {
marginHorizontal: 2.5,
marginVertical: 8,
width: 30,
height: 30,
borderRadius: 15,
},
strokeWidthButton: {
marginHorizontal: 2.5,
marginVertical: 8,
width: 30,
height: 30,
borderRadius: 15,
justifyContent: 'center',
alignItems: 'center',
backgroundColor: '#39579A',
},
functionButton: {
marginHorizontal: 2.5,
marginVertical: 8,
height: 30,
width: 60,
backgroundColor: '#39579A',
justifyContent: 'center',
alignItems: 'center',
borderRadius: 5,
},
});