-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathApp.js
More file actions
200 lines (180 loc) · 5.19 KB
/
Copy pathApp.js
File metadata and controls
200 lines (180 loc) · 5.19 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
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
192
193
194
195
196
197
198
199
200
import React, { useState, useEffect } from 'react';
import {
View,
Text,
StyleSheet,
Platform,
TouchableOpacity,
Alert,
ScrollView,
} from 'react-native';
import Roam from 'roam-reactnative';
import { request, PERMISSIONS, RESULTS } from 'react-native-permissions';
const App = () => {
const [locationData, setLocationData] = useState(null);
const requestIOSLocationAlwaysPermission = async () => {
try {
if (Platform.OS !== 'ios') {
Alert.alert('iOS Only', 'This permission check is for iOS devices.');
return;
}
// Step 1: Request "When In Use" permission first
const whenInUse = await request(PERMISSIONS.IOS.LOCATION_WHEN_IN_USE);
if (whenInUse === RESULTS.GRANTED) {
// Step 2: Request "Always" permission
const always = await request(PERMISSIONS.IOS.LOCATION_ALWAYS);
if (always === RESULTS.GRANTED) {
Alert.alert('✅ Permission Granted', 'Always allow location access is enabled.');
} else if (always === RESULTS.BLOCKED) {
Alert.alert(
'⚠️ Permission Blocked',
'Please enable “Always Allow” in iPhone Settings > Privacy > Location Services.'
);
openSettings();
} else {
Alert.alert(
'⚠️ Permission Denied',
'“Always Allow” permission not granted. Please update in Settings.'
);
}
} else {
Alert.alert(
'⚠️ Permission Denied',
'Please allow location access to use this feature.'
);
}
} catch (error) {
console.warn('Permission error:', error);
}
};
useEffect(() => {
const listener = Roam.startListener('location', (location) => {
console.log('📍 Location received:', location);
setLocationData(location);
});
return () => Roam.stopListener('location');
}, []);
const startTracking = async () => {
Roam.batchProcess(true, 0);
Roam.startTracking();
console.log('Tracking started');
};
const stopTracking = () => {
Roam.stopTracking();
console.log('Tracking stopped');
};
const locationItem = Array.isArray(locationData)
? locationData[0]
: locationData || {};
const location = locationItem.location || {};
const otherDetails = { ...locationItem };
delete otherDetails.location;
return (
<View style={styles.container}>
<ScrollView
contentContainerStyle={styles.scrollContainer}
showsVerticalScrollIndicator={false}
>
{/* Request Permission Button */}
<TouchableOpacity onPress={requestIOSLocationAlwaysPermission} style={styles.button}>
<Text style={styles.buttonText}>Request Permission</Text>
</TouchableOpacity>
{/* Start / Stop Tracking Buttons */}
<View style={styles.buttonRow}>
<TouchableOpacity onPress={startTracking} style={styles.button}>
<Text style={styles.buttonText}>Start Tracking</Text>
</TouchableOpacity>
<TouchableOpacity onPress={stopTracking} style={[styles.button, styles.stop]}>
<Text style={styles.buttonText}>Stop Tracking</Text>
</TouchableOpacity>
</View>
{/* Location + Device Info */}
<View style={styles.dataBox}>
<Text style={styles.title}>📍 Location Data</Text>
{Object.keys(location).length > 0 ? (
Object.entries(location).map(([key, value]) => (
<Text key={key} style={styles.dataText}>
{key}: {String(value)}
</Text>
))
) : (
<Text style={styles.emptyText}>No location data available</Text>
)}
<View style={styles.divider} />
<Text style={styles.title}>📡 Device Info</Text>
{Object.keys(otherDetails).length > 0 ? (
Object.entries(otherDetails).map(([key, value]) => (
<Text key={key} style={styles.dataText}>
{key}: {String(value)}
</Text>
))
) : (
<Text style={styles.emptyText}>No device info available</Text>
)}
</View>
</ScrollView>
</View>
);
};
export default App;
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: '#f9f9f9',
},
scrollContainer: {
padding: 20,
alignItems: 'center',
paddingBottom: 40,
},
button: {
backgroundColor: '#007AFF',
paddingVertical: 14,
paddingHorizontal: 24,
borderRadius: 10,
marginVertical: 10,
},
stop: {
backgroundColor: '#FF3B30',
},
buttonText: {
color: '#fff',
fontSize: 16,
fontWeight: '600',
},
buttonRow: {
flexDirection: 'row',
justifyContent: 'center',
marginTop: 10,
},
dataBox: {
backgroundColor: '#fff',
borderRadius: 10,
padding: 16,
width: '100%',
marginTop: 20,
elevation: 2,
shadowColor: '#000',
shadowOpacity: 0.1,
shadowRadius: 5,
shadowOffset: { width: 0, height: 2 },
},
title: {
fontWeight: '700',
fontSize: 16,
marginBottom: 8,
},
dataText: {
color: '#333',
marginBottom: 4,
},
emptyText: {
color: '#666',
fontStyle: 'italic',
},
divider: {
borderBottomWidth: 1,
borderColor: '#ddd',
marginVertical: 10,
},
});