@@ -64,7 +64,7 @@ export class HealthData extends Common implements HealthDataApi {
64
64
return new Promise ( ( resolve , reject ) => {
65
65
let typeOfData = acceptableDataTypes [ opts . dataType ] ;
66
66
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 ) => {
68
68
if ( error ) {
69
69
reject ( error ) ;
70
70
} else {
@@ -92,16 +92,16 @@ export class HealthData extends Common implements HealthDataApi {
92
92
}
93
93
}
94
94
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 ) {
96
96
let objectType = this . resolveDataType ( dataType ) ;
97
97
98
- const predicate = HKQuery . predicateForSamplesWithStartDateEndDateOptions ( start , end , HKQueryOptions . StrictStartDate ) ;
98
+ const predicate = HKQuery . predicateForSamplesWithStartDateEndDateOptions ( opts . startDate , opts . endDate , HKQueryOptions . StrictStartDate ) ;
99
99
100
- let endDateSortDescriptor = NSSortDescriptor . alloc ( ) . initWithKeyAscending ( HKSampleSortIdentifierEndDate , true ) ;
100
+ let endDateSortDescriptor = NSSortDescriptor . alloc ( ) . initWithKeyAscending ( HKSampleSortIdentifierEndDate , opts . sortOrder !== "desc" ) ;
101
101
const sortBy = NSArray . arrayWithObject < NSSortDescriptor > ( endDateSortDescriptor ) ;
102
102
103
103
// 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 ) ;
105
105
106
106
let query = HKSampleQuery . alloc ( ) . initWithSampleTypePredicateLimitSortDescriptorsResultsHandler (
107
107
objectType , predicate , null , sortBy , ( query : HKSampleQuery , listResults : NSArray < HKSample > , error : NSError ) => {
@@ -114,7 +114,7 @@ export class HealthData extends Common implements HealthDataApi {
114
114
115
115
const resultItem = < ResponseItem > {
116
116
source : source . name ,
117
- unit : unitString ,
117
+ unit : opts . unit ,
118
118
start : startDate ,
119
119
end : endDate
120
120
} ;
@@ -124,14 +124,14 @@ export class HealthData extends Common implements HealthDataApi {
124
124
if ( ( < HKQuantitySample > sample ) . quantity . isCompatibleWithUnit ( unit ) ) {
125
125
resultItem . value = ( < HKQuantitySample > sample ) . quantity . doubleValueForUnit ( unit ) ;
126
126
} else {
127
- console . log ( "Incompatible unit passed: " + unitString + " (" + unit + ")" ) ;
127
+ console . log ( "Incompatible unit passed: " + opts . unit + " (" + unit + ")" ) ;
128
128
}
129
129
}
130
130
131
131
parsedData . push ( resultItem ) ;
132
132
}
133
133
134
- callback ( this . aggregate ( parsedData , aggregateBy ) , null ) ;
134
+ callback ( this . aggregate ( parsedData , opts . aggregateBy ) , null ) ;
135
135
} else {
136
136
console . dir ( error ) ;
137
137
callback ( null , error . localizedDescription ) ;
0 commit comments