Skip to content

Commit

Permalink
Merge pull request #1100 from iarata/health-12
Browse files Browse the repository at this point in the history
Iarata/health-12 base branch
  • Loading branch information
iarata authored Dec 18, 2024
2 parents 3784963 + 8aa68c4 commit d198e10
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 46 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -119,3 +119,5 @@ app.*.symbols
!/dev/ci/**/Gemfile.lock
packages/app_usage/example/.flutter-plugins-dependencies
packages/app_usage/example/.flutter-plugins-dependencies

.sdkmanrc
2 changes: 1 addition & 1 deletion packages/health/example/ios/Runner/AppDelegate.swift
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import UIKit
import Flutter

@UIApplicationMain
@main
@objc class AppDelegate: FlutterAppDelegate {
override func application(
_ application: UIApplication,
Expand Down
42 changes: 23 additions & 19 deletions packages/health/example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import 'package:carp_serializable/carp_serializable.dart';
void main() => runApp(HealthApp());

class HealthApp extends StatefulWidget {
const HealthApp({super.key});

@override
_HealthAppState createState() => _HealthAppState();
}
Expand Down Expand Up @@ -175,7 +177,9 @@ class _HealthAppState extends State<HealthApp> {
// filter out duplicates
_healthDataList = Health().removeDuplicates(_healthDataList);

_healthDataList.forEach((data) => debugPrint(toJsonString(data)));
for (var data in _healthDataList) {
debugPrint(toJsonString(data));
}

// update the UI to display the results
setState(() {
Expand Down Expand Up @@ -662,7 +666,7 @@ class _HealthAppState extends State<HealthApp> {
if (p.value is AudiogramHealthValue) {
return ListTile(
title: Text("${p.typeString}: ${p.value}"),
trailing: Text('${p.unitString}'),
trailing: Text(p.unitString),
subtitle: Text('${p.dateFrom} - ${p.dateTo}\n${p.recordingMethod}'),
);
}
Expand All @@ -671,7 +675,7 @@ class _HealthAppState extends State<HealthApp> {
title: Text(
"${p.typeString}: ${(p.value as WorkoutHealthValue).totalEnergyBurned} ${(p.value as WorkoutHealthValue).totalEnergyBurnedUnit?.name}"),
trailing: Text(
'${(p.value as WorkoutHealthValue).workoutActivityType.name}'),
(p.value as WorkoutHealthValue).workoutActivityType.name),
subtitle: Text('${p.dateFrom} - ${p.dateTo}\n${p.recordingMethod}'),
);
}
Expand All @@ -686,46 +690,46 @@ class _HealthAppState extends State<HealthApp> {
}
return ListTile(
title: Text("${p.typeString}: ${p.value}"),
trailing: Text('${p.unitString}'),
trailing: Text(p.unitString),
subtitle: Text('${p.dateFrom} - ${p.dateTo}\n${p.recordingMethod}'),
);
});

Widget _contentNoData = const Text('No Data to show');
final Widget _contentNoData = const Text('No Data to show');

Widget _contentNotFetched =
final Widget _contentNotFetched =
const Column(mainAxisAlignment: MainAxisAlignment.center, children: [
const Text("Press 'Auth' to get permissions to access health data."),
const Text("Press 'Fetch Dat' to get health data."),
const Text("Press 'Add Data' to add some random health data."),
const Text("Press 'Delete Data' to remove some random health data."),
Text("Press 'Auth' to get permissions to access health data."),
Text("Press 'Fetch Dat' to get health data."),
Text("Press 'Add Data' to add some random health data."),
Text("Press 'Delete Data' to remove some random health data."),
]);

Widget _authorized = const Text('Authorization granted!');
final Widget _authorized = const Text('Authorization granted!');

Widget _authorizationNotGranted = const Column(
final Widget _authorizationNotGranted = const Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
const Text('Authorization not given.'),
const Text(
Text('Authorization not given.'),
Text(
'For Google Health Connect please check if you have added the right permissions and services to the manifest file.'),
const Text('For Apple Health check your permissions in Apple Health.'),
Text('For Apple Health check your permissions in Apple Health.'),
],
);

Widget _contentHealthConnectStatus = const Text(
'No status, click getHealthConnectSdkStatus to get the status.');

Widget _dataAdded = const Text('Data points inserted successfully.');
final Widget _dataAdded = const Text('Data points inserted successfully.');

Widget _dataDeleted = const Text('Data points deleted successfully.');
final Widget _dataDeleted = const Text('Data points deleted successfully.');

Widget get _stepsFetched => Text('Total number of steps: $_nofSteps.');

Widget _dataNotAdded =
final Widget _dataNotAdded =
const Text('Failed to add data.\nDo you have permissions to add data?');

Widget _dataNotDeleted = const Text('Failed to delete data');
final Widget _dataNotDeleted = const Text('Failed to delete data');

Widget get _content => switch (_state) {
AppState.DATA_READY => _contentDataReady,
Expand Down
14 changes: 7 additions & 7 deletions packages/health/example/lib/util.dart
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,13 @@ const List<HealthDataType> dataTypesIOS = [
HealthDataType.HEADACHE_UNSPECIFIED,

// note that a phone cannot write these ECG-based types - only read them
HealthDataType.ELECTROCARDIOGRAM,
HealthDataType.HIGH_HEART_RATE_EVENT,
HealthDataType.IRREGULAR_HEART_RATE_EVENT,
HealthDataType.LOW_HEART_RATE_EVENT,
HealthDataType.RESTING_HEART_RATE,
HealthDataType.WALKING_HEART_RATE,
HealthDataType.ATRIAL_FIBRILLATION_BURDEN,
// HealthDataType.ELECTROCARDIOGRAM,
// HealthDataType.HIGH_HEART_RATE_EVENT,
// HealthDataType.IRREGULAR_HEART_RATE_EVENT,
// HealthDataType.LOW_HEART_RATE_EVENT,
// HealthDataType.RESTING_HEART_RATE,
// HealthDataType.WALKING_HEART_RATE,
// HealthDataType.ATRIAL_FIBRILLATION_BURDEN,

HealthDataType.NUTRITION,
HealthDataType.GENDER,
Expand Down
41 changes: 22 additions & 19 deletions packages/health/ios/Classes/SwiftHealthPlugin.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1171,33 +1171,36 @@ public class SwiftHealthPlugin: NSObject, FlutterPlugin {
predicate = NSCompoundPredicate(type: .and, subpredicates: [predicate, manualPredicate])
}

let query = HKStatisticsQuery(
// TODO: [NOTE] Computational heavy
let query = HKStatisticsCollectionQuery(
quantityType: sampleType,
quantitySamplePredicate: predicate,
options: .cumulativeSum
) { query, queryResult, error in

guard let queryResult = queryResult else {
options: .cumulativeSum,
anchorDate: dateFrom,
intervalComponents: DateComponents(day: 1)
)
query.initialResultsHandler = { query, results, error in
guard let results = results else {
let error = error! as NSError
print("Error getting total steps in interval \(error.localizedDescription)")

DispatchQueue.main.async {
result(nil)
}
return
}
var steps = 0.0

if let quantity = queryResult.sumQuantity() {
let unit = HKUnit.count()
steps = quantity.doubleValue(for: unit)
}

let totalSteps = Int(steps)
DispatchQueue.main.async {
result(totalSteps)
}
}

var totalSteps = 0.0
results.enumerateStatistics(from: dateFrom, to: dateTo) { statistics, stop in
if let quantity = statistics.sumQuantity() {
let unit = HKUnit.count()
totalSteps += quantity.doubleValue(for: unit)
}
}

DispatchQueue.main.async {
result(Int(totalSteps))
}
}

HKHealthStore().execute(query)
Expand Down

0 comments on commit d198e10

Please sign in to comment.