Skip to content

Commit 5954841

Browse files
authored
Merge pull request #54 from shilangyu/patch-1
Update README samples
2 parents db6a1e9 + 41b8933 commit 5954841

File tree

1 file changed

+72
-54
lines changed

1 file changed

+72
-54
lines changed

README.md

Lines changed: 72 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -70,22 +70,21 @@ For this method you need to specify the strings identifiers of types you want to
7070

7171
In the example, we want to read data for all **QuantityTypes** and write data for **QuantityType.stepCount**:
7272
```dart
73-
void requestAuthorization() async {
74-
try {
75-
final readTypes = <String>[];
76-
readTypes.addAll(QuantityType.values.map((e) => e.identifier));
77-
final writeTypes = <String>[
78-
QuantityType.stepCount.identifier,
79-
];
80-
final isRequested =
81-
await HealthKitReporter.requestAuthorization(readTypes, writeTypes);
82-
if (isRequested) {
83-
// read data/write data/observe data
84-
}
85-
} catch (e) {
86-
print(e);
73+
Future<void> requestAuthorization() async {
74+
try {
75+
final readTypes = QuantityType.values.map((e) => e.identifier).toList();
76+
final writeTypes = <String>[
77+
QuantityType.stepCount.identifier,
78+
];
79+
final isRequested =
80+
await HealthKitReporter.requestAuthorization(readTypes, writeTypes);
81+
if (isRequested) {
82+
// read data/write data/observe data
8783
}
84+
} catch (e) {
85+
print(e);
8886
}
87+
}
8988
```
9089

9190
**Warning: Please keep in mind, Apple Health Kit does not let anyone to know if reading permissions were provided.**
@@ -99,38 +98,52 @@ See [Authorization status](https://developer.apple.com/documentation/healthkit/h
9998
After authorization, you can try to start reading data.
10099

101100
```dart
102-
void read(bool isRequested) async {
103-
if (isRequested) {
104-
final preferredUnits =
105-
await HealthKitReporter.preferredUnits([QuantityType.stepCount]);
106-
preferredUnits.forEach((preferredUnit) async {
107-
print('preferredUnit: ${preferredUnit.identifier}');
108-
final type = QuantityTypeFactory.from(preferredUnit.identifier);
109-
final quantities = await HealthKitReporter.quantityQuery(
110-
type, preferredUnit, _predicate);
111-
print('quantity: ${quantities.map((e) => e.map).toList()}');
112-
final statistics = await HealthKitReporter.statisticsQuery(
113-
type, preferredUnit, _predicate);
114-
print('statistics: ${statistics.map}');
115-
});
116-
final characteristics = await HealthKitReporter.characteristicsQuery();
117-
print('characteristics: ${characteristics.map}');
118-
final categories = await HealthKitReporter.categoryQuery(
119-
CategoryType.sleepAnalysis, _predicate);
120-
print('categories: ${categories.map((e) => e.map).toList()}');
121-
final samples = await HealthKitReporter.sampleQuery(
122-
QuantityType.stepCount.identifier, _predicate);
123-
print('samples: ${samples.map((e) => e.map).toList()}');
124-
final sources = await HealthKitReporter.sourceQuery(
125-
QuantityType.stepCount.identifier, _predicate);
126-
print('sources: ${sources.map((e) => e.map).toList()}');
127-
final correlations = await HealthKitReporter.correlationQuery(
128-
CorrelationType.bloodPressure.identifier, _predicate);
129-
print('correlations: ${correlations.map((e) => e.map).toList()}');
130-
} else {
131-
print('error isRequested: $isRequested');
101+
Future<void> read(bool isRequested) async {
102+
if (isRequested) {
103+
final preferredUnits =
104+
await HealthKitReporter.preferredUnits([QuantityType.stepCount]);
105+
for (final preferredUnit in preferredUnits) {
106+
print('preferredUnit: ${preferredUnit.identifier}');
107+
final type = QuantityTypeFactory.from(preferredUnit.identifier);
108+
final quantities = await HealthKitReporter.quantityQuery(
109+
type,
110+
preferredUnit.unit,
111+
_predicate,
112+
);
113+
print('quantity: ${quantities.map((e) => e.map).toList()}');
114+
final statistics = await HealthKitReporter.statisticsQuery(
115+
type,
116+
preferredUnit.unit,
117+
_predicate,
118+
);
119+
print('statistics: ${statistics.map}');
132120
}
121+
final characteristics = await HealthKitReporter.characteristicsQuery();
122+
print('characteristics: ${characteristics.map}');
123+
final categories = await HealthKitReporter.categoryQuery(
124+
CategoryType.sleepAnalysis,
125+
_predicate,
126+
);
127+
print('categories: ${categories.map((e) => e.map).toList()}');
128+
final samples = await HealthKitReporter.sampleQuery(
129+
QuantityType.stepCount.identifier,
130+
_predicate,
131+
);
132+
print('samples: ${samples.map((e) => e.map).toList()}');
133+
final sources = await HealthKitReporter.sourceQuery(
134+
QuantityType.stepCount.identifier,
135+
_predicate,
136+
);
137+
print('sources: ${sources.map((e) => e.map).toList()}');
138+
final correlations = await HealthKitReporter.correlationQuery(
139+
CorrelationType.bloodPressure.identifier,
140+
_predicate,
141+
);
142+
print('correlations: ${correlations.map((e) => e.map).toList()}');
143+
} else {
144+
print('error isRequested: $isRequested');
133145
}
146+
}
134147
```
135148

136149
In the example above, there is a call of **preferredUnits** function. You can provide identifiers to get preferred units for them and eventually receive properly calculated values from queries. The units will be chosen automatically based on you current localization. This is only required for **QuantityTypes**. If you will try to provide invalid unit for a type, you will get an error.
@@ -214,18 +227,23 @@ Try simple **observerQuery** to get notifications if something is changed.
214227
This call is a subscription for EventChannel of the plugin, so don't forget to cancel it as soon as you don't need it anymore.
215228

216229
```dart
217-
void observerQuery() async {
218-
final identifier = QuantityType.stepCount.identifier;
219-
final sub = HealthKitReporter.observerQuery(identifier, _predicate,
220-
onUpdate: (identifier) async {
230+
Future<void> observerQuery() async {
231+
final identifier = QuantityType.stepCount.identifier;
232+
final sub = HealthKitReporter.observerQuery(
233+
[identifier],
234+
_predicate,
235+
onUpdate: (identifier) async {
221236
print('Updates for observerQuerySub');
222237
print(identifier);
223-
});
224-
print('observerQuerySub: $sub');
225-
final isSet = await HealthKitReporter.enableBackgroundDelivery(
226-
identifier, UpdateFrequency.immediate);
227-
print('enableBackgroundDelivery: $isSet');
228-
}
238+
},
239+
);
240+
print('observerQuerySub: $sub');
241+
final isSet = await HealthKitReporter.enableBackgroundDelivery(
242+
identifier,
243+
UpdateFrequency.immediate,
244+
);
245+
print('enableBackgroundDelivery: $isSet');
246+
}
229247
```
230248

231249
According to [Observing Query](https://developer.apple.com/documentation/healthkit/hkobserverquery) and [Background Delivery](https://developer.apple.com/documentation/healthkit/hkhealthstore/1614175-enablebackgrounddelivery)

0 commit comments

Comments
 (0)