Skip to content

Commit

Permalink
Improve Execute Logic for Flexible HealthKit Query Execution (#20)
Browse files Browse the repository at this point in the history
# Improve Execute Logic for Flexible HealthKit Query Execution

## ♻️ Current situation & Problem
- Executions that are performed after the initial configuration were
never fully activated.


## ⚙️ Release Notes 
- Improves the execution of HealthKit queries.


## 📝 Code of Conduct & Contributing Guidelines 

By submitting creating this pull request, you agree to follow our [Code
of
Conduct](https://github.com/StanfordSpezi/.github/blob/main/CODE_OF_CONDUCT.md)
and [Contributing
Guidelines](https://github.com/StanfordSpezi/.github/blob/main/CONTRIBUTING.md):
- [x] I agree to follow the [Code of
Conduct](https://github.com/StanfordSpezi/.github/blob/main/CODE_OF_CONDUCT.md)
and [Contributing
Guidelines](https://github.com/StanfordSpezi/.github/blob/main/CONTRIBUTING.md).
  • Loading branch information
PSchmiedmayer authored Mar 11, 2024
1 parent 3562808 commit 1e9cb5a
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -74,27 +74,23 @@ final class HealthKitSampleDataSource: HealthKitDataSource {
}


func askedForAuthorization() {
func askedForAuthorization() async {
guard askedForAuthorization(for: sampleType) && !deliverySetting.isManual && !active else {
return
}

Task {
await triggerManualDataSourceCollection()
}
await triggerManualDataSourceCollection()
}

func startAutomaticDataCollection() {
func startAutomaticDataCollection() async {
guard askedForAuthorization(for: sampleType) else {
return
}

switch deliverySetting {
case let .anchorQuery(startSetting, _) where startSetting == .automatic,
let .background(startSetting, _) where startSetting == .automatic:
Task {
await triggerManualDataSourceCollection()
}
await triggerManualDataSourceCollection()
default:
break
}
Expand Down
25 changes: 13 additions & 12 deletions Sources/SpeziHealthKit/HealthKit.swift
Original file line number Diff line number Diff line change
Expand Up @@ -70,14 +70,13 @@ import SwiftUI
public final class HealthKit: Module, EnvironmentAccessible, DefaultInitializable {
@ObservationIgnored @StandardActor private var standard: any HealthKitConstraint
private let healthStore: HKHealthStore
private var initialHealthKitDataSourceDescriptions: [HealthKitDataSourceDescription] = []
private var healthKitDataSourceDescriptions: [HealthKitDataSourceDescription] = []
@ObservationIgnored private lazy var healthKitComponents: [any HealthKitDataSource] = {
healthKitDataSourceDescriptions
.flatMap { $0.dataSources(healthStore: healthStore, standard: standard) }
}()
@ObservationIgnored private var healthKitComponents: [any HealthKitDataSource] = []


private var healthKitSampleTypes: Set<HKSampleType> {
healthKitDataSourceDescriptions.reduce(into: Set()) {
(initialHealthKitDataSourceDescriptions + healthKitDataSourceDescriptions).reduce(into: Set()) {
$0 = $0.union($1.sampleTypes)
}
}
Expand Down Expand Up @@ -111,8 +110,7 @@ public final class HealthKit: Module, EnvironmentAccessible, DefaultInitializabl
@HealthKitDataSourceDescriptionBuilder _ healthKitDataSourceDescriptions: () -> [HealthKitDataSourceDescription]
) {
self.init()

self.healthKitDataSourceDescriptions = healthKitDataSourceDescriptions()
self.initialHealthKitDataSourceDescriptions = healthKitDataSourceDescriptions()
}

public init() {
Expand All @@ -133,8 +131,8 @@ public final class HealthKit: Module, EnvironmentAccessible, DefaultInitializabl


public func configure() {
for healthKitComponent in healthKitComponents {
healthKitComponent.startAutomaticDataCollection()
for healthKitDataSourceDescription in initialHealthKitDataSourceDescriptions {
execute(healthKitDataSourceDescription)
}
}

Expand All @@ -151,16 +149,19 @@ public final class HealthKit: Module, EnvironmentAccessible, DefaultInitializabl
alreadyRequestedSampleTypes = healthKitSampleTypesIdentifiers

for healthKitComponent in healthKitComponents {
// reads the above userDefault!
healthKitComponent.askedForAuthorization()
await healthKitComponent.askedForAuthorization()
}
}

public func execute(_ healthKitDataSourceDescription: HealthKitDataSourceDescription) {
healthKitDataSourceDescriptions.append(healthKitDataSourceDescription)
let dataSources = healthKitDataSourceDescription.dataSources(healthStore: healthStore, standard: standard)

for dataSource in dataSources {
dataSource.startAutomaticDataCollection()
healthKitComponents.append(dataSource)
Task {
await dataSource.startAutomaticDataCollection()
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@ import SwiftUI
/// Requirement for every HealthKit Data Source.
public protocol HealthKitDataSource {
/// Called after the used was asked for authorization.
func askedForAuthorization()
func askedForAuthorization() async
/// Called to trigger the manual data collection.
func triggerManualDataSourceCollection() async
/// Called to start the automatic data collection.
func startAutomaticDataCollection()
func startAutomaticDataCollection() async
}


Expand Down

0 comments on commit 1e9cb5a

Please sign in to comment.