-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHealthdata.tsx
236 lines (223 loc) · 6.9 KB
/
Healthdata.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
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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
// HealthData.tsx
import React, {useEffect, useState} from 'react';
import {View, Text, TouchableOpacity, TextInput, Button} from 'react-native';
import AppleHealthKit, {
HealthKitPermissions,
HealthValue,
} from 'react-native-health';
import {
heightPercentageToDP as hp,
widthPercentageToDP as wp,
} from 'react-native-responsive-screen';
interface HealthDataProps {
onUpdateData: () => void;
heartRate: any;
stepCount: any;
activeEngy: any;
bloodSat: any;
setHeartRate: (value: any) => void;
setStepCount: (value: any) => void;
setActiveEngy: (value: any) => void;
setBloodSat: (value: any) => void;
}
const HealthData: React.FC<HealthDataProps> = ({
onUpdateData,
heartRate,
stepCount,
activeEngy,
bloodSat,
setHeartRate,
setStepCount,
setActiveEngy,
setBloodSat,
}) => {
const [heartRateInput, setHeartRateInput] = useState<string>('');
const [stepCountInput, setStepCountInput] = useState<string>('');
const [activeEngyInput, setActiveEngyInput] = useState<string>('');
const permissions = {
permissions: {
read: [
AppleHealthKit.Constants.Permissions.HeartRate,
AppleHealthKit.Constants.Permissions.Steps,
AppleHealthKit.Constants.Permissions.Workout,
AppleHealthKit.Constants.Permissions.OxygenSaturation,
],
write: [
AppleHealthKit.Constants.Permissions.HeartRate,
AppleHealthKit.Constants.Permissions.Steps,
AppleHealthKit.Constants.Permissions.Workout,
AppleHealthKit.Constants.Permissions.OxygenSaturation,
// ... other write permissions
],
},
} as HealthKitPermissions;
useEffect(() => {
// Initialize HealthKit and request permissions
AppleHealthKit.initHealthKit(permissions, (error: string) => {
if (error) {
console.log('Error initializing HealthKit:', error);
} else {
console.log('HealthKit permissions granted.');
// You can now save data to HealthKit.
syncData();
}
});
}, []);
const syncData = () => {
const updatedHeartRate = parseFloat(heartRateInput);
if (!isNaN(updatedHeartRate)) {
setHeartRate(updatedHeartRate);
// Update Heart Rate data in Apple HealthKit
const heartRateData = {
value: updatedHeartRate, // Updated heart rate value
date: new Date().toISOString(), // Current date and time
};
AppleHealthKit.saveHeartRateSample(heartRateData, (error: string) => {
if (!error) {
console.log('Heart Rate data updated in Apple HealthKit.');
} else {
console.error('Error updating Heart Rate data:', error);
}
});
}
const updatedStepCount = parseFloat(stepCountInput);
if (!isNaN(updatedStepCount)) {
setStepCount(updatedStepCount);
// console.log('this is step rate: ', updatedStepCount);
// Update Step Count data in Apple HealthKit
const stepCountData = {
value: updatedStepCount, // Updated step count value
startDate: new Date().toISOString(), // Current date and time
endDate: new Date().toISOString(), // Current date and time
};
AppleHealthKit.saveSteps(stepCountData, (error: string) => {
if (!error) {
console.log('Step Count data updated in Apple HealthKit.');
} else {
console.error('Error updating Step Count data:', error);
}
});
}
const updatedActiveEngy = parseFloat(activeEngyInput);
if (!isNaN(updatedActiveEngy)) {
setActiveEngy(updatedActiveEngy);
// console.log('this is active energy rate: ', updatedActiveEngy);
const activeEnergyData = {
energyBurned: Number(updatedActiveEngy), // Updated active energy burned value
type: String('Walking'), // See HealthActivity Enum,
energyBurnedUnit: 'calories',
distance: 50, // In Distance unit
distanceUnit: 'meter',
startDate: new Date(2023, 11, 5).toISOString(),
endDate: new Date(2023, 11, 6, 12, 42, 5).toISOString(), // Adjusted endDate
};
AppleHealthKit.saveWorkout(activeEnergyData, (error: string) => {
if (!error) {
console.log('Active Energy Burned data updated in Apple HealthKit.');
} else {
console.error('Error updating Active Energy Burned data:', error);
}
});
}
// Trigger data refresh
onUpdateData();
};
return (
<View
style={{
flex: 1,
justifyContent: 'center',
alignItems: 'center',
backgroundColor: '#fafafa',
}}>
<View
style={{
height: hp(10),
width: wp(80),
marginVertical: 10,
padding: 10,
justifyContent: 'space-between',
borderWidth: 1,
borderRadius: 10,
flexDirection: 'column',
}}>
<View>
<Text style={{fontSize: 20, fontWeight: 'bold'}}>
Heart Rate (BPM)
</Text>
<TextInput
placeholder="Enter heart rate"
onChangeText={text => setHeartRateInput(text)}
keyboardType="numeric"
/>
</View>
</View>
<View
style={{
height: hp(10),
width: wp(80),
marginVertical: 10,
padding: 10,
justifyContent: 'space-between',
borderWidth: 1,
borderRadius: 10,
}}>
<Text style={{fontSize: 20, fontWeight: 'bold'}}>Step Count</Text>
<TextInput
placeholder="Enter step count"
onChangeText={text => setStepCountInput(text)}
keyboardType="numeric"
/>
</View>
<View
style={{
height: hp(14),
width: wp(80),
marginVertical: 10,
padding: 10,
justifyContent: 'space-between',
borderWidth: 1,
borderRadius: 10,
}}>
<Text style={{fontSize: 20, fontWeight: 'bold'}}>
Active Energy Burned {'\n'}(calories)
</Text>
<TextInput
placeholder="Enter active energy burned"
onChangeText={text => setActiveEngyInput(text)}
keyboardType="numeric"
/>
</View>
<View
style={{
height: hp(10),
width: wp(80),
marginVertical: 10,
padding: 10,
justifyContent: 'space-between',
borderWidth: 1,
borderRadius: 10,
}}>
<Text style={{fontSize: 20, fontWeight: 'bold'}}>
Blood Saturation Level
</Text>
<View>
<Text>{bloodSat}</Text>
</View>
</View>
<TouchableOpacity
onPress={syncData}
style={{
borderRadius: 10,
height: 50,
width: 100,
backgroundColor: 'lightblue',
justifyContent: 'center',
alignItems: 'center',
}}>
<Text style={{fontWeight: 'bold'}}>Sync Now!</Text>
</TouchableOpacity>
</View>
);
};
export default HealthData;