Skip to content

Commit

Permalink
Allow the Execution of HealthKit Queries Outside of the Configuration (
Browse files Browse the repository at this point in the history
…#14)

# Allow the Execution of HealthKit Queries Outside of the Configuration


## ⚙️ Release Notes 
- Enables the execution of HealthKit queries outside of the
configuration
- Simplifies the logic in the HealthKit module, make information
private, and let the module conform to `DefaultInitializable`.



## 📝 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 Jan 10, 2024
1 parent 30e12a0 commit 49dda58
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ final class HealthKitSampleDataSource: HealthKitDataSource {
}
}

func willFinishLaunchingWithOptions(_ application: UIApplication, launchOptions: [UIApplication.LaunchOptionsKey: Any]) {
func willFinishLaunchingWithOptions() {
guard askedForAuthorization(for: sampleType) else {
return
}
Expand Down
41 changes: 29 additions & 12 deletions Sources/SpeziHealthKit/HealthKit.swift
Original file line number Diff line number Diff line change
Expand Up @@ -64,11 +64,11 @@ import SwiftUI
/// }
/// ```
@Observable
public final class HealthKit: Module, LifecycleHandler, EnvironmentAccessible {
@ObservationIgnored @StandardActor var standard: any HealthKitConstraint
let healthStore: HKHealthStore
let healthKitDataSourceDescriptions: [HealthKitDataSourceDescription]
@ObservationIgnored lazy var healthKitComponents: [any HealthKitDataSource] = {
public final class HealthKit: Module, LifecycleHandler, EnvironmentAccessible, DefaultInitializable {
@ObservationIgnored @StandardActor private var standard: any HealthKitConstraint
private let healthStore: HKHealthStore
private var healthKitDataSourceDescriptions: [HealthKitDataSourceDescription] = []
@ObservationIgnored private lazy var healthKitComponents: [any HealthKitDataSource] = {
healthKitDataSourceDescriptions
.flatMap { $0.dataSources(healthStore: healthStore, standard: standard) }
}()
Expand Down Expand Up @@ -104,9 +104,15 @@ public final class HealthKit: Module, LifecycleHandler, EnvironmentAccessible {
/// Creates a new instance of the ``HealthKit`` module.
/// - Parameters:
/// - healthKitDataSourceDescriptions: The ``HealthKitDataSourceDescription``s define what data is collected by the ``HealthKit`` module. You can, e.g., use ``CollectSample`` to collect a wide variety of `HKSampleTypes`.
public init(
public convenience init(
@HealthKitDataSourceDescriptionBuilder _ healthKitDataSourceDescriptions: () -> [HealthKitDataSourceDescription]
) {
self.init()

self.healthKitDataSourceDescriptions = healthKitDataSourceDescriptions()
}

public init() {
precondition(
HKHealthStore.isHealthDataAvailable(),
"""
Expand All @@ -119,11 +125,7 @@ public final class HealthKit: Module, LifecycleHandler, EnvironmentAccessible {
"""
)

let healthStore = HKHealthStore()
let healthKitDataSourceDescriptions = healthKitDataSourceDescriptions()

self.healthKitDataSourceDescriptions = healthKitDataSourceDescriptions
self.healthStore = healthStore
self.healthStore = HKHealthStore()
}


Expand All @@ -145,10 +147,25 @@ public final class HealthKit: Module, LifecycleHandler, EnvironmentAccessible {
}
}

public func execute(_ healthKitDataSourceDescription: HealthKitDataSourceDescription) {
healthKitDataSourceDescriptions.append(healthKitDataSourceDescription)
let dataSources = healthKitDataSourceDescription.dataSources(healthStore: healthStore, standard: standard)
for dataSource in dataSources {
dataSource.willFinishLaunchingWithOptions()
}
}

public func execute(@HealthKitDataSourceDescriptionBuilder _ healthKitDataSourceDescriptions: () -> [HealthKitDataSourceDescription]) {
for healthKitDataSourceDescription in healthKitDataSourceDescriptions() {
execute(healthKitDataSourceDescription)
}
}


@_documentation(visibility: internal)
public func willFinishLaunchingWithOptions(_ application: UIApplication, launchOptions: [UIApplication.LaunchOptionsKey: Any]) {
for healthKitComponent in healthKitComponents {
healthKitComponent.willFinishLaunchingWithOptions(application, launchOptions: launchOptions)
healthKitComponent.willFinishLaunchingWithOptions()
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,14 @@ import Spezi
import SwiftUI


public protocol HealthKitDataSource: LifecycleHandler {
/// Requirement for every HealthKit Data Source.
public protocol HealthKitDataSource {
/// Called after the used was asked for authorization.
func askedForAuthorization()
/// Called to trigger the manual data collection.
func triggerDataSourceCollection() async
/// Called when the application finished launching with options.
func willFinishLaunchingWithOptions()
}


Expand Down

0 comments on commit 49dda58

Please sign in to comment.