Skip to content

Commit

Permalink
[CPA-62] Remove trendRateUnit from Glucose Store
Browse files Browse the repository at this point in the history
  • Loading branch information
Camji55 committed Jul 17, 2023
1 parent b82f96a commit 74430f2
Show file tree
Hide file tree
Showing 9 changed files with 7 additions and 23 deletions.
7 changes: 2 additions & 5 deletions LoopKit/GlucoseKit/CachedGlucoseObject+CoreDataClass.swift
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,6 @@ extension CachedGlucoseObject {
}
metadata[MetadataKeyGlucoseCondition] = condition?.rawValue
metadata[MetadataKeyGlucoseTrend] = trend?.symbol
metadata[MetadataKeyGlucoseTrendRateUnit] = trendRateUnit
metadata[MetadataKeyGlucoseTrendRateValue] = trendRateValue

return HKQuantitySample(
Expand All @@ -113,19 +112,17 @@ extension CachedGlucoseObject {

var trendRate: HKQuantity? {
get {
guard let trendRateUnit = trendRateUnit, let trendRateValue = trendRateValue else {
guard let trendRateValue = trendRateValue else {
return nil
}
return HKQuantity(unit: HKUnit(from: trendRateUnit), doubleValue: trendRateValue.doubleValue)
return HKQuantity(unit: .milligramsPerDeciliterPerMinute, doubleValue: trendRateValue.doubleValue)
}

set {
if let newValue = newValue {
let unit = HKUnit(from: unitString).unitDivided(by: .minute())
trendRateUnit = unit.unitString
trendRateValue = NSNumber(value: newValue.doubleValue(for: unit))
} else {
trendRateUnit = nil
trendRateValue = nil
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ extension CachedGlucoseObject {
@NSManaged public var primitiveDevice: Data?
@NSManaged public var primitiveCondition: String?
@NSManaged public var primitiveTrend: NSNumber?
@NSManaged public var trendRateUnit: String?
@NSManaged public var trendRateValue: NSNumber?
/// This is the date when this object is eligible for writing to HealthKit. For example, if it is required to delay writing
/// data to HealthKit, this date will be in the future. If the date is in the past, then it is written to HealthKit as soon as possible,
Expand All @@ -56,7 +55,6 @@ extension CachedGlucoseObject: Encodable {
try container.encodeIfPresent(device, forKey: .device)
try container.encodeIfPresent(condition, forKey: .condition)
try container.encodeIfPresent(trend, forKey: .trend)
try container.encodeIfPresent(trendRateUnit, forKey: .trendRateUnit)
try container.encodeIfPresent(trendRateValue?.doubleValue, forKey: .trendRateValue)
try container.encodeIfPresent(healthKitEligibleDate, forKey: .healthKitEligibleDate)
}
Expand All @@ -75,7 +73,6 @@ extension CachedGlucoseObject: Encodable {
case device
case condition
case trend
case trendRateUnit
case trendRateValue
case healthKitEligibleDate
}
Expand Down
6 changes: 2 additions & 4 deletions LoopKit/GlucoseKit/HKQuantitySample+GlucoseKit.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import HealthKit
let MetadataKeyGlucoseIsDisplayOnly = "com.loudnate.GlucoseKit.HKMetadataKey.GlucoseIsDisplayOnly"
let MetadataKeyGlucoseCondition = "com.LoopKit.GlucoseKit.HKMetadataKey.GlucoseCondition"
let MetadataKeyGlucoseTrend = "com.LoopKit.GlucoseKit.HKMetadataKey.GlucoseTrend"
let MetadataKeyGlucoseTrendRateUnit = "com.LoopKit.GlucoseKit.HKMetadataKey.GlucoseTrendRateUnit"
let MetadataKeyGlucoseTrendRateValue = "com.LoopKit.GlucoseKit.HKMetadataKey.GlucoseTrendRateValue"


Expand Down Expand Up @@ -44,10 +43,9 @@ extension HKQuantitySample: GlucoseSampleValue {
}

public var trendRate: HKQuantity? {
guard let unit = metadata?[MetadataKeyGlucoseTrendRateUnit] as? String,
let value = metadata?[MetadataKeyGlucoseTrendRateValue] as? Double else {
guard let value = metadata?[MetadataKeyGlucoseTrendRateValue] as? Double else {
return nil
}
return HKQuantity(unit: HKUnit(from: unit), doubleValue: value)
return HKQuantity(unit: .milligramsPerDeciliterPerMinute, doubleValue: value)
}
}
1 change: 0 additions & 1 deletion LoopKit/GlucoseKit/NewGlucoseSample.swift
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,6 @@ extension NewGlucoseSample {
metadata[MetadataKeyGlucoseCondition] = condition?.rawValue
metadata[MetadataKeyGlucoseTrend] = trend?.symbol
if let trendRate = trendRate {
metadata[MetadataKeyGlucoseTrendRateUnit] = HKUnit.milligramsPerDeciliterPerMinute.unitString
metadata[MetadataKeyGlucoseTrendRateValue] = trendRate.doubleValue(for: .milligramsPerDeciliterPerMinute)
}
if isDisplayOnly {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<model type="com.apple.IDECoreDataModeler.DataModel" documentVersion="1.0" lastSavedToolsVersion="21513" systemVersion="22D68" minimumToolsVersion="Automatic" sourceLanguage="Swift" userDefinedModelVersionIdentifier="">
<model type="com.apple.IDECoreDataModeler.DataModel" documentVersion="1.0" lastSavedToolsVersion="21754" systemVersion="22F82" minimumToolsVersion="Automatic" sourceLanguage="Swift" userDefinedModelVersionIdentifier="">
<entity name="CachedCarbObject" representedClassName="LoopKit.CachedCarbObject" syncable="YES">
<attribute name="absorptionTime" optional="YES" attributeType="Double" usesScalarValueType="NO"/>
<attribute name="addedDate" optional="YES" attributeType="Date" usesScalarValueType="NO"/>
Expand Down Expand Up @@ -29,7 +29,6 @@
<attribute name="syncIdentifier" optional="YES" attributeType="String"/>
<attribute name="syncVersion" optional="YES" attributeType="Integer 32" usesScalarValueType="YES"/>
<attribute name="trend" optional="YES" attributeType="Integer 16" defaultValueString="0" usesScalarValueType="YES"/>
<attribute name="trendRateUnit" optional="YES" attributeType="String"/>
<attribute name="trendRateValue" optional="YES" attributeType="Double" usesScalarValueType="YES"/>
<attribute name="unitString" attributeType="String"/>
<attribute name="uuid" optional="YES" attributeType="UUID" usesScalarValueType="NO"/>
Expand Down
4 changes: 0 additions & 4 deletions LoopKitHostedTests/Persistence/CachedGlucoseObjectTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ class CachedGlucoseObjectOperationsTests: PersistenceControllerTestCase {
XCTAssertEqual(object.wasUserEntered, false)
XCTAssertEqual(object.condition, .belowRange)
XCTAssertEqual(object.trend, .flat)
XCTAssertEqual(object.trendRateUnit, HKUnit.milligramsPerDeciliterPerMinute.unitString)
XCTAssertEqual(object.trendRateValue, 0.1)
XCTAssertEqual(object.modificationCounter, 1)
XCTAssertEqual(object.device, device)
Expand All @@ -60,7 +59,6 @@ class CachedGlucoseObjectOperationsTests: PersistenceControllerTestCase {
HKMetadataKeyWasUserEntered: true,
MetadataKeyGlucoseCondition: "belowRange",
MetadataKeyGlucoseTrend: "",
MetadataKeyGlucoseTrendRateUnit: HKUnit.milligramsPerDeciliterPerMinute.unitString,
MetadataKeyGlucoseTrendRateValue: 0.1
]
let quantitySample = HKQuantitySample(type: type, quantity: quantity, start: startDate, end: startDate, device: device, metadata: metadata)
Expand All @@ -77,7 +75,6 @@ class CachedGlucoseObjectOperationsTests: PersistenceControllerTestCase {
XCTAssertEqual(object.wasUserEntered, true)
XCTAssertEqual(object.condition, .belowRange)
XCTAssertEqual(object.trend, .flat)
XCTAssertEqual(object.trendRateUnit, HKUnit.milligramsPerDeciliterPerMinute.unitString)
XCTAssertEqual(object.trendRateValue, 0.1)
XCTAssertEqual(object.modificationCounter, 1)
XCTAssertEqual(object.device, device)
Expand All @@ -98,7 +95,6 @@ class CachedGlucoseObjectOperationsTests: PersistenceControllerTestCase {
HKMetadataKeyWasUserEntered: true,
MetadataKeyGlucoseCondition: "belowRange",
MetadataKeyGlucoseTrend: "",
MetadataKeyGlucoseTrendRateUnit: HKUnit.milligramsPerDeciliterPerMinute.unitString,
MetadataKeyGlucoseTrendRateValue: 0.1
]
let quantitySample = HKQuantitySample(type: type, quantity: quantity, start: startDate, end: startDate, device: device, metadata: metadata)
Expand Down
4 changes: 2 additions & 2 deletions LoopKitTests/GlucoseStoreTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -393,8 +393,8 @@ class GlucoseStoreCriticalEventLogExportTests: PersistenceControllerTestCase {
XCTAssertEqual(outputStream.string, """
[
{"isDisplayOnly":false,"modificationCounter":1,"provenanceIdentifier":"com.apple.dt.xctest.tool","startDate":"2100-01-02T03:08:00.000Z","syncIdentifier":"18CF3948-0B3D-4B12-8BFE-14986B0E6784","syncVersion":1,"unitString":"mg/dL","value":111,"wasUserEntered":false},
{"isDisplayOnly":false,"modificationCounter":3,"provenanceIdentifier":"com.apple.dt.xctest.tool","startDate":"2100-01-02T03:04:00.000Z","syncIdentifier":"2B03D96C-6F5D-4140-99CD-80C3E64D6010","syncVersion":3,"trend":3,"trendRateUnit":"mg/min·dL","trendRateValue":1,"unitString":"mg/dL","value":113,"wasUserEntered":false},
{"isDisplayOnly":false,"modificationCounter":4,"provenanceIdentifier":"com.apple.dt.xctest.tool","startDate":"2100-01-02T03:06:00.000Z","syncIdentifier":"FF1C4F01-3558-4FB2-957E-FA1522C4735E","syncVersion":4,"trend":3,"trendRateUnit":"mg/min·dL","trendRateValue":1,"unitString":"mg/dL","value":114,"wasUserEntered":false}
{"isDisplayOnly":false,"modificationCounter":3,"provenanceIdentifier":"com.apple.dt.xctest.tool","startDate":"2100-01-02T03:04:00.000Z","syncIdentifier":"2B03D96C-6F5D-4140-99CD-80C3E64D6010","syncVersion":3,"trend":3,"trendRateValue":1,"unitString":"mg/dL","value":113,"wasUserEntered":false},
{"isDisplayOnly":false,"modificationCounter":4,"provenanceIdentifier":"com.apple.dt.xctest.tool","startDate":"2100-01-02T03:06:00.000Z","syncIdentifier":"FF1C4F01-3558-4FB2-957E-FA1522C4735E","syncVersion":4,"trend":3,"trendRateValue":1,"unitString":"mg/dL","value":114,"wasUserEntered":false}
]
"""
)
Expand Down
1 change: 0 additions & 1 deletion LoopKitTests/NewGlucoseSampleTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ class NewGlucoseSampleTests: XCTestCase {
XCTAssertEqual(quantitySample.metadata?[HKMetadataKeySyncVersion] as? Int, 3)
XCTAssertEqual(quantitySample.metadata?[MetadataKeyGlucoseCondition] as? String, "aboveRange")
XCTAssertEqual(quantitySample.metadata?[MetadataKeyGlucoseTrend] as? String, GlucoseTrend.up.symbol)
XCTAssertEqual(quantitySample.metadata?[MetadataKeyGlucoseTrendRateUnit] as? String, HKUnit.milligramsPerDeciliterPerMinute.unitString)
XCTAssertEqual(quantitySample.metadata?[MetadataKeyGlucoseTrendRateValue] as? Double, 1.1)
XCTAssertNil(quantitySample.metadata?[MetadataKeyGlucoseIsDisplayOnly])
XCTAssertEqual(quantitySample.metadata?[HKMetadataKeyWasUserEntered] as? Bool, true)
Expand Down
1 change: 0 additions & 1 deletion LoopKitTests/StoredGlucoseSampleTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,6 @@ class StoredGlucoseSampleManagedObjectInitializerTests: PersistenceControllerTes
managedObject.device = device
managedObject.condition = .aboveRange
managedObject.trend = .downDownDown
managedObject.trendRateUnit = HKUnit.milligramsPerDeciliterPerMinute.unitString
managedObject.trendRateValue = -4.0
managedObject.healthKitEligibleDate = startDate.addingTimeInterval(.hours(3))
let sample = StoredGlucoseSample(managedObject: managedObject)
Expand Down

0 comments on commit 74430f2

Please sign in to comment.