From dd43234a01a00b0f1e3207e322dd4b2ab2fee31d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matthew=20J=C3=B6rke?= Date: Tue, 9 Apr 2024 23:17:33 -0700 Subject: [PATCH] aggregate Progress across data sources --- .../BulkUpload/BulkUploadSampleDataSource.swift | 2 +- Sources/SpeziHealthKit/HealthKit.swift | 15 +++++++++++++++ 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/Sources/SpeziHealthKit/BulkUpload/BulkUploadSampleDataSource.swift b/Sources/SpeziHealthKit/BulkUpload/BulkUploadSampleDataSource.swift index a94f351..5378db5 100644 --- a/Sources/SpeziHealthKit/BulkUpload/BulkUploadSampleDataSource.swift +++ b/Sources/SpeziHealthKit/BulkUpload/BulkUploadSampleDataSource.swift @@ -39,7 +39,7 @@ final class BulkUploadSampleDataSource: HealthKitDataSource { } } - var progress: Progress { + public var progress: Progress { let progress = Progress(totalUnitCount: Int64(totalSamples)) progress.completedUnitCount = Int64(processedSamples) return progress diff --git a/Sources/SpeziHealthKit/HealthKit.swift b/Sources/SpeziHealthKit/HealthKit.swift index c066f09..295e38f 100644 --- a/Sources/SpeziHealthKit/HealthKit.swift +++ b/Sources/SpeziHealthKit/HealthKit.swift @@ -74,6 +74,21 @@ public final class HealthKit: Module, EnvironmentAccessible, DefaultInitializabl private var healthKitDataSourceDescriptions: [HealthKitDataSourceDescription] = [] @ObservationIgnored private var healthKitComponents: [any HealthKitDataSource] = [] + public var progress: Progress { + var totalUnitCount: Int64 = 0 + var completedUnitCount: Int64 = 0 + for dataSource in healthKitComponents { + if let bulkDataSource = dataSource as? BulkUploadSampleDataSource { + let individualProgress = bulkDataSource.progress + totalUnitCount += individualProgress.totalUnitCount + completedUnitCount += individualProgress.completedUnitCount + } + } + let macroProgress = Progress(totalUnitCount: totalUnitCount) + macroProgress.completedUnitCount = completedUnitCount + return macroProgress + } + private var healthKitSampleTypes: Set { (initialHealthKitDataSourceDescriptions + healthKitDataSourceDescriptions).reduce(into: Set()) {