Skip to content

Commit d831e1c

Browse files
Add sortOrder to query #10
1 parent b533bed commit d831e1c

File tree

6 files changed

+28
-13
lines changed

6 files changed

+28
-13
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,8 @@ this.healthData.query(
104104
endDate: new Date(), // now
105105
dataType: "steps", // equal to the 'name' property of 'HealthDataType'
106106
unit: "count", // make sure this is compatible with the 'dataType' (see below)
107-
aggregateBy: "day" // optional, one of: "hour", "day", "sourceAndDay"
107+
aggregateBy: "day", // optional, one of: "hour", "day", "sourceAndDay"
108+
sortOrder: "desc" // optional, default "asc"
108109
})
109110
.then(result => console.log(JSON.stringify(result)))
110111
.catch(error => this.resultToShow = error);

demo-ng/app/app.component.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,8 @@ export class AppComponent {
5050
endDate: new Date(), // now
5151
dataType: dataType,
5252
unit: unit,
53-
aggregateBy: aggregateBy
53+
aggregateBy: aggregateBy,
54+
sortOrder: "desc"
5455
})
5556
.then(result => {
5657
console.log(JSON.stringify(result));

src/health-data.android.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,6 @@ export class HealthData extends Common implements HealthDataApi {
7878
});
7979
}
8080

81-
// TODO how does Fit deal with unit conversion? mi <----> km, and such
8281
query(opts: QueryRequest): Promise<Array<ResponseItem>> {
8382
return new Promise((resolve, reject) => {
8483
try {
@@ -127,6 +126,12 @@ export class HealthData extends Common implements HealthDataApi {
127126
result = result.concat(this.dumpDataSet(readResult.getDataSets().get(index), opts));
128127
}
129128
}
129+
130+
// the result is sorted asc, so reverse in case that was requested
131+
if (opts.sortOrder === "desc") {
132+
result = result.reverse();
133+
}
134+
130135
return result;
131136
}
132137

src/health-data.common.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,18 @@ export interface HealthDataType {
1313

1414
export type AggregateBy = "hour" | "day" | "sourceAndDay";
1515

16+
export type SortOrder = "asc" | "desc";
17+
1618
export interface QueryRequest {
1719
startDate: Date;
1820
endDate: Date;
1921
dataType: string;
20-
aggregateBy?: AggregateBy;
2122
unit: string;
23+
aggregateBy?: AggregateBy;
24+
/**
25+
* Default "asc"
26+
*/
27+
sortOrder?: SortOrder;
2228
}
2329

2430
export interface ResponseItem {
@@ -32,6 +38,7 @@ export interface ResponseItem {
3238
source?: string;
3339
}
3440

41+
/*
3542
export interface ResultResponse {
3643
status: {
3744
action: string;
@@ -61,6 +68,7 @@ export function createResultResponse(action: string, message: string, type?: str
6168
export function createErrorResponse(action: string, description: string): ErrorResponse {
6269
return {action, description};
6370
}
71+
*/
6472

6573
export interface HealthDataApi {
6674
isAvailable(): Promise<boolean>;

src/health-data.ios.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ export class HealthData extends Common implements HealthDataApi {
6464
return new Promise((resolve, reject) => {
6565
let typeOfData = acceptableDataTypes[opts.dataType];
6666
if (quantityTypes[typeOfData] || categoryTypes[typeOfData]) {
67-
this.queryForQuantityOrCategoryData(typeOfData, opts.startDate, opts.endDate, opts.aggregateBy, opts.unit, (res, error) => {
67+
this.queryForQuantityOrCategoryData(typeOfData, opts, (res, error) => {
6868
if (error) {
6969
reject(error);
7070
} else {
@@ -92,16 +92,16 @@ export class HealthData extends Common implements HealthDataApi {
9292
}
9393
}
9494

95-
private queryForQuantityOrCategoryData(dataType: string, start: Date, end: Date, aggregateBy: AggregateBy, unitString: string, callback: (data: Array<ResponseItem>, error: string) => void) {
95+
private queryForQuantityOrCategoryData(dataType: string, opts: QueryRequest, callback: (data: Array<ResponseItem>, error: string) => void) {
9696
let objectType = this.resolveDataType(dataType);
9797

98-
const predicate = HKQuery.predicateForSamplesWithStartDateEndDateOptions(start, end, HKQueryOptions.StrictStartDate);
98+
const predicate = HKQuery.predicateForSamplesWithStartDateEndDateOptions(opts.startDate, opts.endDate, HKQueryOptions.StrictStartDate);
9999

100-
let endDateSortDescriptor = NSSortDescriptor.alloc().initWithKeyAscending(HKSampleSortIdentifierEndDate, true);
100+
let endDateSortDescriptor = NSSortDescriptor.alloc().initWithKeyAscending(HKSampleSortIdentifierEndDate, opts.sortOrder !== "desc");
101101
const sortBy = NSArray.arrayWithObject<NSSortDescriptor>(endDateSortDescriptor);
102102

103103
// note that passing an invalid 'unitString' will crash the app (can't catch that error either)
104-
const unit = HKUnit.unitFromString(unitString);
104+
const unit = HKUnit.unitFromString(opts.unit);
105105

106106
let query = HKSampleQuery.alloc().initWithSampleTypePredicateLimitSortDescriptorsResultsHandler(
107107
objectType, predicate, null, sortBy, (query: HKSampleQuery, listResults: NSArray<HKSample>, error: NSError) => {
@@ -114,7 +114,7 @@ export class HealthData extends Common implements HealthDataApi {
114114

115115
const resultItem = <ResponseItem>{
116116
source: source.name,
117-
unit: unitString,
117+
unit: opts.unit,
118118
start: startDate,
119119
end: endDate
120120
};
@@ -124,14 +124,14 @@ export class HealthData extends Common implements HealthDataApi {
124124
if ((<HKQuantitySample>sample).quantity.isCompatibleWithUnit(unit)) {
125125
resultItem.value = (<HKQuantitySample>sample).quantity.doubleValueForUnit(unit);
126126
} else {
127-
console.log("Incompatible unit passed: " + unitString + " (" + unit + ")");
127+
console.log("Incompatible unit passed: " + opts.unit + " (" + unit + ")");
128128
}
129129
}
130130

131131
parsedData.push(resultItem);
132132
}
133133

134-
callback(this.aggregate(parsedData, aggregateBy), null);
134+
callback(this.aggregate(parsedData, opts.aggregateBy), null);
135135
} else {
136136
console.dir(error);
137137
callback(null, error.localizedDescription);

src/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "nativescript-health-data",
3-
"version": "1.0.0",
3+
"version": "1.0.1",
44
"description": "Health Data plugin for Nativescript, using Google Fit and Apple HealthKit.",
55
"main": "health-data",
66
"typings": "index.d.ts",

0 commit comments

Comments
 (0)