Skip to content

Commit

Permalink
feat: Make all HealthKit sample properties public (#20)
Browse files Browse the repository at this point in the history
* feat: Make all HealthKit sample properties public

* fix testing
  • Loading branch information
cbaker6 authored Mar 2, 2025
1 parent 9d16ede commit 72f1078
Show file tree
Hide file tree
Showing 8 changed files with 91 additions and 44 deletions.
24 changes: 11 additions & 13 deletions CareKitStore/CareKitStore/CoreData/OCKCDOutcomeValue.swift
Original file line number Diff line number Diff line change
Expand Up @@ -83,19 +83,17 @@ class OCKCDOutcomeValue: NSManagedObject {
var value: OCKOutcomeValue!

self.managedObjectContext!.performAndWait {
value = OCKOutcomeValue(self.value, units: units)
value.createdDate = createdDate
value.kind = kind
value.sourceRevision = sourceRevision?.makeValue()
value.device = device?.makeValue()
value.metadata = metadata
if let startDate = startDate,
let endDate = endDate {
value.dateInterval = DateInterval(
start: startDate,
end: endDate
)
}
value = OCKOutcomeValue(
self.value,
units: units,
createdDate: createdDate,
kind: kind,
sourceRevision: sourceRevision?.makeValue(),
device: device?.makeValue(),
metadata: metadata,
startDate: startDate,
endDate: endDate
)
}

return value
Expand Down
2 changes: 1 addition & 1 deletion CareKitStore/CareKitStore/HealthKit/OCKDevice.swift
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@

import HealthKit

struct OCKDevice: Codable, Hashable {
public struct OCKDevice: Codable, Hashable {

/**
The name of the receiver.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -247,15 +247,17 @@ extension OCKHealthKitPassthroughStore {
.quantity
.doubleValue(for: event.task.healthKitLinkage.unit)

var outcomeValue = OCKOutcomeValue(
let outcomeValue = OCKOutcomeValue(
doubleValue,
units: event.task.healthKitLinkage.unit.unitString
units: event.task.healthKitLinkage.unit.unitString,
createdDate: Date(),
kind: nil,
sourceRevision: sample.sourceRevision,
device: sample.device,
metadata: sample.metadata,
startDate: sample.dateInterval.start,
endDate: sample.dateInterval.end
)

outcomeValue.dateInterval = sample.dateInterval
outcomeValue.sourceRevision = sample.sourceRevision
outcomeValue.device = sample.device
outcomeValue.metadata = sample.metadata

var updatedEvent = event

Expand Down
9 changes: 8 additions & 1 deletion CareKitStore/CareKitStore/HealthKit/OCKSource.swift
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,15 @@

import HealthKit

struct OCKSource: Codable, Hashable {
public struct OCKSource: Codable, Hashable {
/**
The name of the source represented by the receiver. If the source is an app, then the name is the localized name of the app.
*/
var name: String

/**
The bundle identifier of the source represented by the receiver.
*/
var bundleIdentifier: String
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@

import HealthKit

struct OCKSourceRevision: Codable, Hashable {
public struct OCKSourceRevision: Codable, Hashable {
/**
The HKSource of the receiver.
*/
Expand Down
40 changes: 35 additions & 5 deletions CareKitStore/CareKitStore/Structs/OCKOutcomeValue.swift
Original file line number Diff line number Diff line change
Expand Up @@ -69,12 +69,17 @@ public struct OCKOutcomeValue: Codable, Equatable, CustomStringConvertible {
/// The date that this value was created.
public var createdDate = Date()

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

var sourceRevision: OCKSourceRevision?
var device: OCKDevice?
var metadata: [String: String]?
/// An object indicating the source when this value represents a HealthKit sample.
private(set) var sourceRevision: OCKSourceRevision?

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

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

/// The underlying value.
public var value: OCKOutcomeValueUnderlyingType
Expand Down Expand Up @@ -125,6 +130,31 @@ public struct OCKOutcomeValue: Codable, Equatable, CustomStringConvertible {
self.units = units
}

init(
_ value: OCKOutcomeValueUnderlyingType,
units: String? = nil,
createdDate: Date,
kind: String?,
sourceRevision: OCKSourceRevision?,
device: OCKDevice?,
metadata: [String: String]?,
startDate: Date?,
endDate: Date?
) {
self.init(value, units: units)
self.createdDate = createdDate
self.kind = kind
self.sourceRevision = sourceRevision
self.device = device
self.metadata = metadata
if let startDate = startDate,
let endDate = endDate {
self.dateInterval = DateInterval(
start: startDate,
end: endDate
)
}
}
/// Checks if two `OCKOutcomeValue`s have equal value properties, without checking their other properties.
private func hasSameValueAs(_ other: OCKOutcomeValue) -> Bool {
switch type {
Expand Down
16 changes: 10 additions & 6 deletions CareKitStore/CareKitStoreTests/OCKStore/TestStore+Outcomes.swift
Original file line number Diff line number Diff line change
Expand Up @@ -86,12 +86,16 @@ class TestStoreOutcomes: XCTestCase {
udiDeviceIdentifier: "udiDeviceIdentifier"
)
let metadata = ["key": "value"]
var value = OCKOutcomeValue(42)
value.kind = "number"
value.dateInterval = dateInterval
value.sourceRevision = sourceRevision
value.device = device
value.metadata = metadata
let value = OCKOutcomeValue(
42,
createdDate: Date(),
kind: "number",
sourceRevision: sourceRevision,
device: device,
metadata: metadata,
startDate: dateInterval.start,
endDate: dateInterval.end
)

var outcome = OCKOutcome(taskUUID: taskUUID, taskOccurrenceIndex: 0, values: [value])
outcome = try store.addOutcomeAndWait(outcome)
Expand Down
26 changes: 16 additions & 10 deletions CareKitStore/CareKitStoreTests/Structs/TestOutcomeValue.swift
Original file line number Diff line number Diff line change
Expand Up @@ -136,16 +136,10 @@ class TestOutcomeValue: XCTestCase {
}

func testCodingAllEntries() throws {
var value = OCKOutcomeValue(10)

// Value
value.kind = "whale"
value.units = "m/s"
value.createdDate = Date().addingTimeInterval(-200)
let createdDate = Date().addingTimeInterval(-200)
let startDate = Date().addingTimeInterval(-100)
let endDate = Date().addingTimeInterval(-50)
value.dateInterval = DateInterval(start: startDate, end: endDate)
value.sourceRevision = OCKSourceRevision(
let sourceRevision = OCKSourceRevision(
source: .init(
name: "name",
bundleIdentifier: "bundle"
Expand All @@ -158,7 +152,7 @@ class TestOutcomeValue: XCTestCase {
patchVersion: 2
)
)
value.device = OCKDevice(
let device = OCKDevice(
name: "deviceName",
manufacturer: "manufacturer",
model: "model",
Expand All @@ -168,7 +162,19 @@ class TestOutcomeValue: XCTestCase {
localIdentifier: "localIdentifier",
udiDeviceIdentifier: "udiDeviceIdentifier"
)
value.metadata = ["key": "value"]
let metadata = ["key": "value"]

let value = OCKOutcomeValue(
10,
units: "m/s",
createdDate: createdDate,
kind: "whale",
sourceRevision: sourceRevision,
device: device,
metadata: metadata,
startDate: startDate,
endDate: endDate
)

let encoded = try JSONEncoder().encode(value)
let decoded = try JSONDecoder().decode(OCKOutcomeValue.self, from: encoded)
Expand Down

0 comments on commit 72f1078

Please sign in to comment.