Skip to content

fix: Ensure all OCKOutcomeValues properties are always up-to-date #26

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

Merged
merged 2 commits into from
May 10, 2025
Merged
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 @@ -188,7 +188,7 @@ extension OCKHealthKitPassthroughStore {

case .cumulative:
intersectingSamples.forEach { sample in
updatedEvent = invalidateCumulativeOutcome(addedSampleID: sample.id, event: updatedEvent)
updatedEvent = invalidateCumulativeOutcome(addedSample: sample, event: updatedEvent)
}
}
} else {
Expand Down Expand Up @@ -386,10 +386,11 @@ extension OCKHealthKitPassthroughStore {
/// that will affect the sum value. We do store the sample ID for the new sample, but set the outcome
/// value to -1 to indicate that the sum needs to be recomputed.
private func invalidateCumulativeOutcome(
addedSampleID: UUID,
addedSample: any Sample,
event: Event
) -> Event {

let addedSampleID = addedSample.id
var updatedEvent = event

// Create an outcome if one doesn't already exist
Expand All @@ -401,8 +402,18 @@ extension OCKHealthKitPassthroughStore {
event.outcome?.healthKitUUIDs.first != nil
else {

let value = -1
let units = event.task.healthKitLinkage.unit?.unitString
let outcomeValue = OCKOutcomeValue(-1, units: units)
let outcomeValue = OCKOutcomeValue(
value,
units: units,
createdDate: addedSample.dateInterval.start,
endDate: addedSample.dateInterval.end,
kind: nil,
sourceRevision: addedSample.sourceRevision,
device: addedSample.device,
metadata: addedSample.metadata
)

updatedEvent.outcome?.values.append(outcomeValue)
updatedEvent.outcome?.healthKitUUIDs.append([addedSampleID])
Expand All @@ -412,6 +423,12 @@ extension OCKHealthKitPassthroughStore {

// Invalidate the first outcome value
updatedEvent.outcome?.values[0].value = -1
updatedEvent.outcome?.values[0].createdDate = addedSample.dateInterval.start
updatedEvent.outcome?.values[0].dateInterval?.start = addedSample.dateInterval.start
updatedEvent.outcome?.values[0].dateInterval?.end = addedSample.dateInterval.end
updatedEvent.outcome?.values[0].sourceRevision = addedSample.sourceRevision
updatedEvent.outcome?.values[0].device = addedSample.device
updatedEvent.outcome?.values[0].metadata = addedSample.metadata

// Track the new sample ID
updatedEvent.outcome?.healthKitUUIDs[0].append(addedSampleID)
Expand Down Expand Up @@ -659,7 +676,9 @@ extension OCKHealthKitPassthroughStore {
updatedEvent.outcome = event.outcome ?? makeOutcome(for: event)

let units = event.task.healthKitLinkage.unit?.unitString
let outcomeValue = OCKOutcomeValue(newSum, units: units)
var outcomeValue = event.outcome?.values.first ?? OCKOutcomeValue(newSum, units: units)
outcomeValue.value = newSum
outcomeValue.units = units

updatedEvent.outcome!.values = [outcomeValue]

Expand Down
8 changes: 4 additions & 4 deletions CareKitStore/CareKitStore/Structs/OCKOutcomeValue.swift
Original file line number Diff line number Diff line change
Expand Up @@ -70,16 +70,16 @@ public struct OCKOutcomeValue: Codable, Equatable, CustomStringConvertible {
public var createdDate = Date()

/// The start date and end date when this value represents a HealthKit sample.
public private(set) var dateInterval: DateInterval?
public internal(set) var dateInterval: DateInterval?

/// An object indicating the source when this value represents a HealthKit sample.
public private(set) var sourceRevision: OCKSourceRevision?
public internal(set) var sourceRevision: OCKSourceRevision?

/// A device that generates data for HealthKit when this value represents a HealthKit sample.
public private(set) var device: OCKDevice?
public internal(set) var device: OCKDevice?

/// The metadata when this value represents a HealthKit sample.
public private(set) var metadata: [String: String]?
public internal(set) var metadata: [String: String]?

/// The underlying value.
public var value: OCKOutcomeValueUnderlyingType
Expand Down
Loading