-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.tsx
169 lines (145 loc) · 4.4 KB
/
index.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
import { NativeEventEmitter, NativeModules, Platform } from 'react-native';
const LINKING_ERROR =
`The package 'react-native-cm-pedometer' doesn't seem to be linked. Make sure: \n\n` +
Platform.select({ ios: "- You have run 'pod install'\n", default: '' }) +
'- You rebuilt the app after installing the package\n' +
'- You are not using Expo Go\n';
const CmPedometer = NativeModules.CmPedometer
? NativeModules.CmPedometer
: new Proxy(
{},
{
get() {
throw new Error(LINKING_ERROR);
},
}
);
// Determining Pedometer Availability
export enum CMAuthorizationStatus {
notDetermined,
restricted,
denied,
authorized,
}
export function authorizationStatus(): Promise<CMAuthorizationStatus> {
return CmPedometer.authorizationStatus();
}
export function isStepCountingAvailable(): Promise<boolean> {
return CmPedometer.isStepCountingAvailable();
}
export function isDistanceAvailable(): Promise<boolean> {
return CmPedometer.isDistanceAvailable();
}
export function isFloorCountingAvailable(): Promise<boolean> {
return CmPedometer.isFloorCountingAvailable();
}
export function isPaceAvailable(): Promise<boolean> {
return CmPedometer.isPaceAvailable();
}
export function isCadenceAvailable(): Promise<boolean> {
return CmPedometer.isCadenceAvailable();
}
export function isPedometerEventTrackingAvailable(): Promise<boolean> {
return CmPedometer.isPedometerEventTrackingAvailable();
}
// Getting Live Pedometer Data
export interface CMPedometerData {
startDate: Date;
endDate: Date;
numberOfSteps: number;
distance: number | undefined | null;
averageActivePace: number | undefined | null;
currentPace: number | undefined | null;
currentCadence: number | undefined | null;
floorsAscended: number | undefined | null;
floorsDescended: number | undefined | null;
}
export enum CMPedometerEventType {
pause,
resume,
}
export interface CMPedometerEvent {
date: Date;
type: CMPedometerEventType;
}
export type CMPedometerHandler = (
error: Error | undefined,
data: CMPedometerData | undefined
) => void;
export type CMPedometerEventHandler = (
error: Error | undefined,
pedometerEvent: CMPedometerEvent
) => void;
const enum CmPedometerEvent {
onPedometerData = 'onPedometerData',
onPedometerEvent = 'onPedometerEvent',
}
const eventEmitter = new NativeEventEmitter(CmPedometer);
export function startUpdates(from: Date, handler: CMPedometerHandler): void {
eventEmitter.addListener(CmPedometerEvent.onPedometerData, (event) => {
let error: Error | undefined;
if (event.error) {
error = new Error(event.error);
}
let data: CMPedometerData | undefined;
if (event.data) {
data = {
...event.data,
startDate: new Date(event.data.startDate),
endDate: new Date(event.data.endDate),
} as CMPedometerData;
}
handler(error, data);
});
CmPedometer.startUpdates(from.toISOString());
}
export function stopUpdates(): void {
eventEmitter.removeAllListeners(CmPedometerEvent.onPedometerData);
CmPedometer.stopUpdates();
}
export function startEventUpdates(handler: CMPedometerEventHandler): void {
eventEmitter.addListener(CmPedometerEvent.onPedometerEvent, (event) => {
let error: Error | undefined;
if (event.error) {
error = new Error(event.error);
}
let pedometerEvent: CMPedometerEvent | undefined;
if (event.pedometerEvent) {
pedometerEvent = {
...event.pedometerEvent,
date: new Date(event.pedometerEvent.date),
} as CMPedometerEvent;
handler(error, pedometerEvent);
}
});
CmPedometer.startEventUpdates();
}
export function stopEventUpdates(): void {
eventEmitter.removeAllListeners(CmPedometerEvent.onPedometerEvent);
CmPedometer.stopEventUpdates();
}
// Fetching Historical Pedometer Data
export function queryPedometerData(
from: Date,
to: Date
): Promise<CMPedometerData> {
return new Promise((resolve, reject) => {
CmPedometer.queryPedometerData(
from.toISOString(),
to.toISOString(),
(error: string | undefined, data: any) => {
if (error) {
reject(new Error(error));
} else if (data) {
resolve({
...data,
startDate: new Date(data.startDate),
endDate: new Date(data.endDate),
} as CMPedometerData);
} else {
reject(new Error('An unexpected error has occurred.'));
}
}
);
});
}