Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Benlrichards/workout route data #1077

Draft
wants to merge 5 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -792,6 +792,38 @@ class HealthPlugin(private var channel: MethodChannel? = null) :
totalSteps += stepRec.count
}


val routeLocations = mutableListOf<Map<String, Any>>()


Log.d("FLUTTER_HEALTH", "record.exerciseRouteResult: ${record.exerciseRouteResult}")
when (val exerciseRouteResult = record.exerciseRouteResult) {

is ExerciseRouteResult.Data ->
{
val locations = exerciseRouteResult.exerciseRoute.route.orEmpty()
for (location in locations) {
val locationMap = mapOf(
"latitude" to location.latitude as Any,
"longitude" to location.longitude as Any,
"altitude" to location.altitude as Any,
"timestamp" to location.time.toEpochMilli() as Any
)
routeLocations.add(locationMap)
}
}
is ExerciseRouteResult.ConsentRequired -> {
Log.d("FLUTTER_HEALTH", "Consent required")
}
is ExerciseRouteResult.NoData -> {
Log.d("FLUTTER_HEALTH", "No data")
}
else -> Unit
}




// val metadata = (rec as Record).metadata
// Add final datapoint
healthConnectData.add(
Expand Down Expand Up @@ -845,6 +877,7 @@ class HealthPlugin(private var channel: MethodChannel? = null) :
record.metadata
.dataOrigin
.packageName,
"routeLocations" to routeLocations,
),
)
}
Expand Down
20 changes: 14 additions & 6 deletions packages/health/example/ios/Runner.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,6 @@
TargetAttributes = {
97C146ED1CF9000F007C117D = {
CreatedOnToolsVersion = 7.3.1;
DevelopmentTeam = 59TCTNUBMQ;
LastSwiftMigration = 0910;
ProvisioningStyle = Automatic;
SystemCapabilities = {
Expand Down Expand Up @@ -386,20 +385,23 @@
CODE_SIGN_IDENTITY = "Apple Development";
CODE_SIGN_STYLE = Automatic;
CURRENT_PROJECT_VERSION = "$(FLUTTER_BUILD_NUMBER)";
DEVELOPMENT_TEAM = 59TCTNUBMQ;
DEVELOPMENT_TEAM = V9MZ5W578T;
ENABLE_BITCODE = NO;
FRAMEWORK_SEARCH_PATHS = (
"$(inherited)",
"$(PROJECT_DIR)/Flutter",
);
INFOPLIST_FILE = Runner/Info.plist;
IPHONEOS_DEPLOYMENT_TARGET = 13.0;
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks";
LD_RUNPATH_SEARCH_PATHS = (
"$(inherited)",
"@executable_path/Frameworks",
);
LIBRARY_SEARCH_PATHS = (
"$(inherited)",
"$(PROJECT_DIR)/Flutter",
);
PRODUCT_BUNDLE_IDENTIFIER = dk.cachet.example;
PRODUCT_BUNDLE_IDENTIFIER = "dk.cachet.example-benlrichards";
PRODUCT_NAME = "$(TARGET_NAME)";
PROVISIONING_PROFILE_SPECIFIER = "";
SWIFT_OBJC_BRIDGING_HEADER = "Runner/Runner-Bridging-Header.h";
Expand Down Expand Up @@ -531,7 +533,10 @@
);
INFOPLIST_FILE = Runner/Info.plist;
IPHONEOS_DEPLOYMENT_TARGET = 13.0;
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks";
LD_RUNPATH_SEARCH_PATHS = (
"$(inherited)",
"@executable_path/Frameworks",
);
LIBRARY_SEARCH_PATHS = (
"$(inherited)",
"$(PROJECT_DIR)/Flutter",
Expand Down Expand Up @@ -564,7 +569,10 @@
);
INFOPLIST_FILE = Runner/Info.plist;
IPHONEOS_DEPLOYMENT_TARGET = 13.0;
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks";
LD_RUNPATH_SEARCH_PATHS = (
"$(inherited)",
"@executable_path/Frameworks",
);
LIBRARY_SEARCH_PATHS = (
"$(inherited)",
"$(PROJECT_DIR)/Flutter",
Expand Down
4 changes: 2 additions & 2 deletions packages/health/example/ios/Runner/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@
<string>We will sync your data with the Apple Health app to give you better insights</string>
<key>NSHealthUpdateUsageDescription</key>
<string>We will sync your data with the Apple Health app to give you better insights</string>
<key>UIApplicationSupportsIndirectInputEvents</key>
<true/>
<key>UILaunchStoryboardName</key>
<string>LaunchScreen</string>
<key>UIMainStoryboardFile</key>
Expand All @@ -47,7 +49,5 @@
</array>
<key>UIViewControllerBasedStatusBarAppearance</key>
<false/>
<key>UIApplicationSupportsIndirectInputEvents</key>
<true/>
</dict>
</plist>
80 changes: 77 additions & 3 deletions packages/health/ios/Classes/SwiftHealthPlugin.swift
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import Flutter
import HealthKit
import UIKit
import CoreLocation

enum RecordingMethod: Int {
case unknown = 0 // RECORDING_METHOD_UNKNOWN (not supported on iOS)
Expand Down Expand Up @@ -922,8 +923,13 @@ public class SwiftHealthPlugin: NSObject, FlutterPlugin {

case let (samplesWorkout as [HKWorkout]) as Any:

let dictionaries = samplesWorkout.map { sample -> NSDictionary in
return [
var dictionaries = [[String: Any]]()
let dispatchGroup = DispatchGroup()

for sample in samplesWorkout {
dispatchGroup.enter()
print("sample.workoutActivityType, \(sample.workoutActivityType)")
var workoutDict: [String: Any] = [
"uuid": "\(sample.uuid)",
"workoutActivityType": workoutActivityTypeMap.first(where: {
$0.value == sample.workoutActivityType
Expand All @@ -941,9 +947,65 @@ public class SwiftHealthPlugin: NSObject, FlutterPlugin {
"total_distance": sample.totalDistance != nil ? Int(sample.totalDistance!.doubleValue(for: HKUnit.meter())) : 0,
"total_energy_burned": sample.totalEnergyBurned != nil ? Int(sample.totalEnergyBurned!.doubleValue(for: HKUnit.kilocalorie())) : 0
]

// Fetch associated route data
let workoutPredicate = HKQuery.predicateForObjects(from: sample)
let routeQuery = HKSampleQuery(sampleType: HKSeriesType.workoutRoute(), predicate: workoutPredicate, limit: HKObjectQueryNoLimit, sortDescriptors: nil) { (query, routeSamplesOrNil, error) in
if let error = error {
print("Error fetching route data: \(error.localizedDescription)")
// Continue without route data
dictionaries.append(workoutDict)
dispatchGroup.leave()
return
}
guard let routeSamples = routeSamplesOrNil as? [HKWorkoutRoute], !routeSamples.isEmpty else {
// No route data
dictionaries.append(workoutDict)
dispatchGroup.leave()
return
}

var routeLocations = [[String: Any]]()
let routeDispatchGroup = DispatchGroup()

for routeSample in routeSamples {
routeDispatchGroup.enter()
var locations = [CLLocation]()
let locationQuery = HKWorkoutRouteQuery(route: routeSample) { (query, locationData, done, error) in
if let error = error {
print("Error fetching locations: \(error.localizedDescription)")
routeDispatchGroup.leave()
return
}
if let locationData = locationData {
locations.append(contentsOf: locationData)
}
if done {
let locationDicts = locations.map { loc in
return [
"latitude": loc.coordinate.latitude,
"longitude": loc.coordinate.longitude,
"altitude": loc.altitude,
"timestamp": Int(loc.timestamp.timeIntervalSince1970 * 1000)
]
}
routeLocations.append(contentsOf: locationDicts)
routeDispatchGroup.leave()
}
}
self.healthStore.execute(locationQuery)
}

routeDispatchGroup.notify(queue: .main) {
workoutDict["route"] = routeLocations
dictionaries.append(workoutDict)
dispatchGroup.leave()
}
}
self.healthStore.execute(routeQuery)
}

DispatchQueue.main.async {
dispatchGroup.notify(queue: DispatchQueue.main) {
result(dictionaries)
}

Expand Down Expand Up @@ -1376,6 +1438,9 @@ public class SwiftHealthPlugin: NSObject, FlutterPlugin {
workoutActivityTypeMap["KICKBOXING"] = .kickboxing
workoutActivityTypeMap["MARTIAL_ARTS"] = .martialArts
workoutActivityTypeMap["TAI_CHI"] = .taiChi
if #available(iOS 17.0, *) {
workoutActivityTypeMap["UNDERWATER_DIVING"] = .underwaterDiving
}
workoutActivityTypeMap["WRESTLING"] = .wrestling
workoutActivityTypeMap["OTHER"] = .other
nutritionList = [
Expand Down Expand Up @@ -1619,6 +1684,13 @@ public class SwiftHealthPlugin: NSObject, FlutterPlugin {
}

func getWorkoutType(type: HKWorkoutActivityType) -> String {

if #available(iOS 17.0, *) {
if type == .underwaterDiving {
return "underwaterDiving"
}
}

switch type {
case .americanFootball:
return "americanFootball"
Expand Down Expand Up @@ -1732,6 +1804,8 @@ public class SwiftHealthPlugin: NSObject, FlutterPlugin {
return "waterSports"
case .wrestling:
return "wrestling"


case .yoga:
return "yoga"
case .barre:
Expand Down
14 changes: 7 additions & 7 deletions packages/health/lib/health.dart
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
library health;
library;

import 'dart:async';
import 'dart:collection';
import 'dart:io' show Platform;

import 'package:carp_serializable/carp_serializable.dart';
import 'package:json_annotation/json_annotation.dart';
import 'package:device_info_plus/device_info_plus.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/services.dart';
import 'package:json_annotation/json_annotation.dart';

part 'src/heath_data_types.dart';
part 'health.g.dart';
part 'health.json.dart';
part 'src/functions.dart';
part 'src/health_data_point.dart';
part 'src/health_value_types.dart';
part 'src/health_plugin.dart';
part 'src/health_value_types.dart';
part 'src/heath_data_types.dart';
part 'src/route_point.dart';
part 'src/workout_summary.dart';

part 'health.g.dart';
part 'health.json.dart';
56 changes: 49 additions & 7 deletions packages/health/lib/health.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions packages/health/lib/src/health_plugin.dart
Original file line number Diff line number Diff line change
Expand Up @@ -1254,6 +1254,7 @@ class Health {
HealthWorkoutActivityType.WHEELCHAIR_RUN_PACE,
HealthWorkoutActivityType.WHEELCHAIR_WALK_PACE,
HealthWorkoutActivityType.WRESTLING,
HealthWorkoutActivityType.UNDERWATER_DIVING,
HealthWorkoutActivityType.YOGA,
HealthWorkoutActivityType.SWIMMING_OPEN_WATER,
HealthWorkoutActivityType.SWIMMING_POOL,
Expand Down
2 changes: 1 addition & 1 deletion packages/health/lib/src/heath_data_types.dart
Original file line number Diff line number Diff line change
Expand Up @@ -527,7 +527,7 @@ enum HealthWorkoutActivityType {
WHEELCHAIR_RUN_PACE,
WHEELCHAIR_WALK_PACE,
WRESTLING,

UNDERWATER_DIVING,
// Android only
BIKING_STATIONARY,
CALISTHENICS,
Expand Down
Loading