Skip to content
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
21 changes: 10 additions & 11 deletions Common/Models/WatchContext.swift
Original file line number Diff line number Diff line change
Expand Up @@ -56,14 +56,14 @@ final class WatchContext: NSObject, RawRepresentable {
if let unitString = rawValue["gu"] as? String {
let unit = HKUnit(from: unitString)
preferredGlucoseUnit = unit
}

if let glucoseValue = rawValue["gv"] as? Double {
glucose = HKQuantity(unit: unit, doubleValue: glucoseValue)
}
if let glucoseValue = rawValue["gv"] as? Double {
glucose = HKQuantity(unit: preferredGlucoseUnit ?? .milligramsPerDeciliter(), doubleValue: glucoseValue)
}

if let glucoseValue = rawValue["egv"] as? Double {
eventualGlucose = HKQuantity(unit: unit, doubleValue: glucoseValue)
}
if let glucoseValue = rawValue["egv"] as? Double {
eventualGlucose = HKQuantity(unit: preferredGlucoseUnit ?? .milligramsPerDeciliter(), doubleValue: glucoseValue)
}

glucoseTrendRawValue = rawValue["gt"] as? Int
Expand Down Expand Up @@ -102,11 +102,10 @@ final class WatchContext: NSObject, RawRepresentable {
raw["bp"] = batteryPercentage
raw["cob"] = COB

if let unit = preferredGlucoseUnit {
raw["egv"] = eventualGlucose?.doubleValue(for: unit)
raw["gu"] = unit.unitString
raw["gv"] = glucose?.doubleValue(for: unit)
}
let unit = preferredGlucoseUnit ?? .milligramsPerDeciliter()
raw["egv"] = eventualGlucose?.doubleValue(for: unit)
raw["gu"] = preferredGlucoseUnit?.unitString
raw["gv"] = glucose?.doubleValue(for: unit)

raw["gt"] = glucoseTrendRawValue
raw["gd"] = glucoseDate
Expand Down
18 changes: 16 additions & 2 deletions WatchApp Extension/ExtensionDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

import WatchConnectivity
import WatchKit
import HealthKit
import os
import UserNotifications

Expand Down Expand Up @@ -152,10 +153,23 @@ final class ExtensionDelegate: NSObject, WKExtensionDelegate {
}
}

private lazy var healthStore = HKHealthStore()

fileprivate func updateContext(_ data: [String: Any]) {
if let context = WatchContext(rawValue: data as WatchContext.RawValue) {
DispatchQueue.main.async {
self.lastContext = context
if context.preferredGlucoseUnit == nil {
let type = HKQuantityType.quantityType(forIdentifier: .bloodGlucose)!
healthStore.preferredUnits(for: [type]) { (units, error) in
context.preferredGlucoseUnit = units[type]

DispatchQueue.main.async {
self.lastContext = context
}
}
} else {
DispatchQueue.main.async {
self.lastContext = context
}
}
}
}
Expand Down