diff --git a/Sources/SpeziHealthKit/CollectSample/HealthKitSampleDataSource.swift b/Sources/SpeziHealthKit/CollectSample/HealthKitSampleDataSource.swift index 5638f8d..bb95e74 100644 --- a/Sources/SpeziHealthKit/CollectSample/HealthKitSampleDataSource.swift +++ b/Sources/SpeziHealthKit/CollectSample/HealthKitSampleDataSource.swift @@ -74,17 +74,15 @@ 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 } @@ -92,9 +90,7 @@ final class HealthKitSampleDataSource: HealthKitDataSource { switch deliverySetting { case let .anchorQuery(startSetting, _) where startSetting == .automatic, let .background(startSetting, _) where startSetting == .automatic: - Task { - await triggerManualDataSourceCollection() - } + await triggerManualDataSourceCollection() default: break } diff --git a/Sources/SpeziHealthKit/HealthKit.swift b/Sources/SpeziHealthKit/HealthKit.swift index 83b680b..01e2019 100644 --- a/Sources/SpeziHealthKit/HealthKit.swift +++ b/Sources/SpeziHealthKit/HealthKit.swift @@ -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 { - healthKitDataSourceDescriptions.reduce(into: Set()) { + (initialHealthKitDataSourceDescriptions + healthKitDataSourceDescriptions).reduce(into: Set()) { $0 = $0.union($1.sampleTypes) } } @@ -111,8 +110,7 @@ public final class HealthKit: Module, EnvironmentAccessible, DefaultInitializabl @HealthKitDataSourceDescriptionBuilder _ healthKitDataSourceDescriptions: () -> [HealthKitDataSourceDescription] ) { self.init() - - self.healthKitDataSourceDescriptions = healthKitDataSourceDescriptions() + self.initialHealthKitDataSourceDescriptions = healthKitDataSourceDescriptions() } public init() { @@ -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) } } @@ -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() + } } } diff --git a/Sources/SpeziHealthKit/HealthKitDataSource/HealthKitDataSource.swift b/Sources/SpeziHealthKit/HealthKitDataSource/HealthKitDataSource.swift index 68fd51f..346c486 100644 --- a/Sources/SpeziHealthKit/HealthKitDataSource/HealthKitDataSource.swift +++ b/Sources/SpeziHealthKit/HealthKitDataSource/HealthKitDataSource.swift @@ -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 }