-
Notifications
You must be signed in to change notification settings - Fork 0
/
App.tsx
137 lines (127 loc) · 4.35 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
import * as React from 'react';
import { StyleSheet, View, Text, Button } from 'react-native';
import {
CMAuthorizationStatus,
authorizationStatus,
isStepCountingAvailable,
isDistanceAvailable,
isFloorCountingAvailable,
isPaceAvailable,
isCadenceAvailable,
isPedometerEventTrackingAvailable,
type CMPedometerData,
startUpdates,
stopUpdates,
type CMPedometerEvent,
CMPedometerEventType,
startEventUpdates,
stopEventUpdates,
queryPedometerData,
} from 'react-native-cm-pedometer';
export default function App() {
const [status, setStatus] = React.useState<
CMAuthorizationStatus | undefined
>();
const [isStepAvail, setStepAvail] = React.useState<boolean | undefined>();
const [isDistAvail, setDistAvail] = React.useState<boolean | undefined>();
const [isFloorAvail, setFloorAvail] = React.useState<boolean | undefined>();
const [isPaceAvail, setPaceAvail] = React.useState<boolean | undefined>();
const [isCadenceAvail, setCadenceAvail] = React.useState<
boolean | undefined
>();
const [isEventAvail, setEventAvail] = React.useState<boolean | undefined>();
React.useEffect(() => {
authorizationStatus().then(setStatus);
isStepCountingAvailable().then(setStepAvail);
isDistanceAvailable().then(setDistAvail);
isFloorCountingAvailable().then(setFloorAvail);
isPaceAvailable().then(setPaceAvail);
isCadenceAvailable().then(setCadenceAvail);
isPedometerEventTrackingAvailable().then(setEventAvail);
return () => {
stopUpdates();
};
}, []);
const [error, setError] = React.useState<Error | undefined>();
const [isDataStarted, setDataStarted] = React.useState<boolean>(false);
const [startDate, setStartDate] = React.useState<Date>(new Date());
const [data, setData] = React.useState<CMPedometerData | undefined>();
function onPressData() {
const now = new Date();
if (isDataStarted) {
stopUpdates();
queryPedometerData(startDate, now).then(setData);
} else {
setStartDate(now);
startUpdates(now, (newError, newData) => {
setError(newError);
setData(newData);
});
}
setDataStarted(!isDataStarted);
}
const [isEventStarted, setEventStarted] = React.useState<boolean>(false);
const [event, setEvent] = React.useState<CMPedometerEvent | undefined>();
function onPressEvent() {
if (isEventStarted) {
stopEventUpdates();
} else {
startEventUpdates((newError, newEvent) => {
setError(newError);
setEvent(newEvent);
});
}
setEventStarted(!isEventStarted);
}
return (
<View style={styles.container}>
<Text>
Authorization Status:{' '}
{status !== undefined && CMAuthorizationStatus[status]}
</Text>
<Text>Is Step Counting Available: {isStepAvail?.toString()}</Text>
<Text>Is Distance Available: {isDistAvail?.toString()}</Text>
<Text>Is Floor Counting Available: {isFloorAvail?.toString()}</Text>
<Text>Is Pace Available: {isPaceAvail?.toString()}</Text>
<Text>Is Cadence Available: {isCadenceAvail?.toString()}</Text>
<Text>
Is Pedometer Event Tracking Available: {isEventAvail?.toString()}
</Text>
<Text>Error: {error?.message}</Text>
<Button
onPress={onPressData}
title={isDataStarted ? 'Stop Data Updates' : 'Start Data Updates'}
/>
<Text>Start Date: {data?.startDate.toLocaleString()}</Text>
<Text>End Date: {data?.endDate.toLocaleString()}</Text>
<Text>Number of Steps: {data?.numberOfSteps}</Text>
<Text>Distance: {data?.distance}</Text>
<Text>Current Cadence: {data?.currentCadence}</Text>
<Text>Current Pace: {data?.currentPace}</Text>
<Text>Average Active Pace: {data?.averageActivePace}</Text>
<Text>Floors Ascended: {data?.floorsAscended}</Text>
<Text>Floors Descended: {data?.floorsDescended}</Text>
<Button
onPress={onPressEvent}
title={isEventStarted ? 'Stop Event Updates' : 'Start Event Updates'}
/>
<Text>Event date: {event?.date.toLocaleString()}</Text>
<Text>
Event type:{' '}
{event?.type !== undefined && CMPedometerEventType[event.type]}
</Text>
</View>
);
}
const styles = StyleSheet.create({
container: {
flex: 1,
alignItems: 'center',
justifyContent: 'center',
},
box: {
width: 60,
height: 60,
marginVertical: 20,
},
});