forked from yldio/react-native-wearables
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdata.android.js
43 lines (36 loc) · 1.08 KB
/
data.android.js
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
import Fitness, {
DataReadRequest,
FitnessOptions,
DataType,
TimeUnit
} from "react-native-google-fitness";
import { DATA_TYPES, ERRORS } from "./constants";
const typeMappings = {
[DATA_TYPES.heartRateBpm]: DataType.TYPE_HEART_RATE_BPM
};
const Data = {
Types: DATA_TYPES,
authorize(dataTypes) {
const options = new FitnessOptions.Builder();
dataTypes.forEach(dt => options.addDataType(typeMappings[dt]));
return Fitness.requestPermissions(options.build()).then(
() => undefined,
() => Promise.reject(new Error(ERRORS.failedInit))
);
},
read(dataType, options) {
const readRequest = new DataReadRequest.Builder()
.read_dataType(typeMappings[dataType])
.setTimeRange(+options.startDate, +options.endDate, TimeUnit.MILLISECONDS)
.build();
return Fitness.History.readData(readRequest).then(
result =>
result.dataSets.reduce((samples, dataset) => {
samples.push(...dataset);
return samples;
}, []),
() => Promise.reject(new Error(ERRORS.failedQuery))
);
}
};
export default Data;