-
Notifications
You must be signed in to change notification settings - Fork 0
/
App.tsx
173 lines (159 loc) · 4.47 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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
import { useEffect, useState } from 'react';
import {
StyleSheet,
Text,
View,
NativeModules,
SafeAreaView,
Linking,
Platform,
ToastAndroid,
FlatList,
RefreshControl
} from 'react-native';
import AppModal from './components/AppModal'
import LoaderComponent from './components/Loader'
import Icons from './components/Icons';
function App(): React.JSX.Element {
const [showPermissionModal, setShowPermissionModal] = useState(false)
const [isLoading, setIsLoading] = useState(true);
const [refreshing, setRefreshing] = useState(false);
const [healthData, setHealthData] = useState<any>([]);
const loadPermissions = () => {
NativeModules.HealthInfo.authorizeHealthKit((value: boolean) => {
if (value == true) {
handleGetHealthKitData()
} else {
setShowPermissionModal(true)
}
})
}
const handleAskForPermission = () => {
NativeModules.HealthInfo.authorizeHealthKit((value: boolean) => {
if (value == true) {
handleGetHealthKitData()
} else {
// permission has been denied on initial app load, user will need to update in settings.
// Navigate to settings to update manually.
if (Platform.OS == 'ios') {
Linking.openURL('app-settings:')
} else {
ToastAndroid.show('Update permissions in settings', ToastAndroid.LONG)
}
}
})
}
const handleGetHealthKitData = () => {
setHealthData([])
setShowPermissionModal(false)
setIsLoading(false)
NativeModules.HealthInfo.getHeartRate((value: { status: boolean }) => {
if (value.status == true) {
setHealthData((prevHealthData:any) => [...prevHealthData, value]);
}
})
NativeModules.HealthInfo.getBodyTemperature((value: { status: boolean }) => {
if (value.status == true) {
setHealthData((prevHealthData:any) => [...prevHealthData, value]);
}
})
NativeModules.HealthInfo.getOxygenSaturation((value: { status: boolean }) => {
if (value.status == true) {
setHealthData((prevHealthData:any) => [...prevHealthData, value]);
}
})
NativeModules.HealthInfo.getBloodPressureSystolic((value: { status: boolean }) => {
if (value.status == true) {
setHealthData((prevHealthData:any) => [...prevHealthData, value]);
}
})
}
const handleRefresh = () => {
setRefreshing(true);
handleGetHealthKitData()
setRefreshing(false)
}
useEffect(() => {
loadPermissions()
}, [])
return (
<View style={styles.container}>
<SafeAreaView>
<LoaderComponent isLoading={isLoading} />
<View style={styles.header}>
<Text style={styles.appTitle}>Health Tracker App</Text>
</View>
<FlatList
data={healthData}
keyExtractor={(item) => item.name}
ListEmptyComponent={<EmptyStateMessage />}
renderItem={({ item }) => (
<View style={styles.dataItem}>
<Icons identifier={item.identifier} />
<View style={styles.dataText}>
<Text style={styles.dataItemName}>{item.name}</Text>
<Text style={styles.dataItemDuration}>({item.recordDuration})</Text>
<Text style={styles.dataItemValue}>
{item.latestValue} {item.unit}
</Text>
</View>
</View>
)}
refreshControl={<RefreshControl refreshing={refreshing} onRefresh={handleRefresh} />}
/>
<AppModal
showPermissionModal={showPermissionModal}
onButtonPressed={() => handleAskForPermission()}
/>
</SafeAreaView>
</View>
);
}
const EmptyStateMessage = () => {
return (
<Text style={styles.emptyStateMessage}>No health data available</Text>
);
}
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: '#f4f4f4',
},
header: {
backgroundColor: '#3498db',
padding: 15,
alignItems: 'center',
},
appTitle: {
color: 'white',
fontSize: 20,
fontWeight: 'bold',
},
dataItem: {
flexDirection: 'row',
alignItems: 'center',
padding: 15,
borderBottomWidth: 1,
borderBottomColor: '#ddd',
},
dataText: {
marginLeft: 15,
},
dataItemName: {
fontSize: 16,
fontWeight: 'bold',
},
dataItemDuration: {
fontSize: 12,
color: '#888',
},
dataItemValue: {
fontSize: 14,
},
emptyStateMessage: {
textAlign: 'center',
fontSize: 18,
marginTop: 20,
},
});
export default App;