-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathApp.tsx
156 lines (143 loc) · 4.66 KB
/
App.tsx
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
import React, { useEffect, useState } from 'react';
import { Image, StyleSheet, View } from 'react-native';
import GetLocation from 'react-native-get-location';
import axios, { AxiosError, AxiosResponse } from 'axios';
import AsyncStorage from '@react-native-async-storage/async-storage';
import { allowScreenshots, preventScreenshots } from './src/ScreenshotModule';
import SpringPressable from './src/SpringPressable';
import { os, deviceName, macAddress, ipAddress, showToast, delay, imei, checkAndroidLocationPermission, checkIOSLocationPermission } from './src/utils';
import { baseURL } from './src/constants';
function App(): JSX.Element {
const [loading, setLoading] = useState(false);
const [isActivate, setIsActivate] = useState(false);
const [location, setLocation] = useState('');
const [screenshotId, setScreenshotId] = useState('');
const accessLocation = (): void => {
GetLocation.getCurrentPosition({ enableHighAccuracy: true, timeout: 60000, })
.then(loc => {
setLocation(`latitude: ${loc.latitude}, longitude: ${loc.longitude}`);
}).catch(error => {
const { code, message } = error;
const msg = `Location - ${code}, ${message}`;
showToast(msg);
});
}
useEffect(() => {
accessLocation();
async function getData() {
try {
const id = await AsyncStorage.getItem('id');
const status = await AsyncStorage.getItem('status');
if (id !== null && status !== null) {
setScreenshotId(id);
status === 'true'
? (preventScreenshots(), setIsActivate(true))
: (allowScreenshots(), setIsActivate(false))
}
} catch (e) {
showToast('async storege error reading data');
}
}
getData();
}, [])
const create = () => {
setLoading(true);
const url = `${baseURL}/screenshots`;
const params = {
"os": os, "device_name": deviceName,
"mac_address": macAddress, "imei": imei(),
"location": location, "public_ip_address": ipAddress, "status": true
}
axios.post(url, params)
.then(async (response: AxiosResponse) => {
await delay(1500);
await AsyncStorage.setItem('id', response.data?.data?._id);
await AsyncStorage.setItem('status', response.data?.data?.status.toString());
!isActivate ? preventScreenshots() : allowScreenshots();
setIsActivate(!isActivate);
setLoading(false);
}).catch(async (error: AxiosError | any) => {
await delay(1500);
setLoading(false);
if (error.response) {
error.response.status === 400
? showToast("invalid or missing parameters")
: showToast(error.response.data?.message);
}
else {
showToast(error.message);
}
});
};
const update = (status: boolean) => {
setLoading(true);
const url = `${baseURL}/screenshots/${screenshotId}`;
const params = { "status": status }
axios.put(url, params)
.then(async (response: AxiosResponse) => {
await delay(1500);
await AsyncStorage.setItem('status', response.data?.data?.status.toString());
!isActivate ? preventScreenshots() : allowScreenshots();
setIsActivate(!isActivate);
setLoading(false);
}).catch(async (error: AxiosError | any) => {
await delay(1500);
setLoading(false);
if (error.response) {
error.response.status === 400
? showToast("invalid or missing parameters")
: showToast(error.response.data?.message);
}
});
}
const toggleBtn = () => {
os === 'ios' ? checkIOSLocationPermission() : checkAndroidLocationPermission();
screenshotId === ''
? create()
: isActivate ? update(false) : update(true);
};
return (
<View style={styles.container}>
<View style={styles.flex1} />
<View style={styles.imageContainer}>
<Image style={styles.imageStyle} source={require('./src/assets/logo.png')} />
</View>
<View style={styles.buttonSection}>
<SpringPressable
loading={loading}
isActivate={isActivate}
onPress={toggleBtn}
/>
</View>
<View style={styles.flex1} />
</View>
);
}
const styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: 'center',
alignItems: 'center',
},
flex1: {
flex: 1,
},
imageContainer: {
flex: 2,
width: '100%',
justifyContent: 'center',
alignItems: 'center',
},
imageStyle: {
width: 100,
height: 100,
borderRadius: 10,
},
buttonSection: {
flex: 1,
width: '100%',
justifyContent: 'center',
alignItems: 'center',
},
});
export default App;