diff --git a/Loop/Managers/WatchDataManager.swift b/Loop/Managers/WatchDataManager.swift index a20a326050..4558ed2b09 100644 --- a/Loop/Managers/WatchDataManager.swift +++ b/Loop/Managers/WatchDataManager.swift @@ -128,6 +128,29 @@ final class WatchDataManager: NSObject, WCSessionDelegate { context.recommendedBolusDose = state.recommendedBolus?.recommendation.amount context.maxBolus = manager.settings.maximumBolus + let updateGroup = DispatchGroup() + + updateGroup.enter() + manager.doseStore.insulinOnBoard(at: Date()) {(result) in + // This function completes asynchronously, so below + // is a completion that returns a value after eventual + // function completion. + switch result { + case .success(let iobValue): + context.IOB = iobValue.value + case .failure: + context.IOB = nil + } + updateGroup.leave() + } + + if let cobValue = state.carbsOnBoard { + context.COB = cobValue.quantity.doubleValue(for: HKUnit.gram()) + } else { + // we expect state.carbsOnBoard to be nil if value is zero: + context.COB = 0.0 + } + if let glucoseTargetRangeSchedule = manager.settings.glucoseTargetRangeSchedule { if let override = glucoseTargetRangeSchedule.override { context.glucoseRangeScheduleOverride = GlucoseRangeScheduleOverrideUserInfo( @@ -146,7 +169,10 @@ final class WatchDataManager: NSObject, WCSessionDelegate { context.glucoseTrendRawValue = trend.rawValue } - completion(context) + updateGroup.notify(queue: DispatchQueue.global(qos: .background)) { + completion(context) + } + } } } diff --git a/WatchApp Extension/Controllers/StatusInterfaceController.swift b/WatchApp Extension/Controllers/StatusInterfaceController.swift index fafedbe369..d3a6d1c986 100644 --- a/WatchApp Extension/Controllers/StatusInterfaceController.swift +++ b/WatchApp Extension/Controllers/StatusInterfaceController.swift @@ -177,7 +177,46 @@ final class StatusInterfaceController: WKInterfaceController, ContextUpdatable { } // TODO: Other elements + let insulinFormatter: NumberFormatter = { + let numberFormatter = NumberFormatter() + + numberFormatter.numberStyle = .decimal + numberFormatter.minimumFractionDigits = 1 + numberFormatter.maximumFractionDigits = 1 + + return numberFormatter + }() + statusLabel.setHidden(true) + var statusLabelText = "" + + if let activeInsulin = context?.IOB, let valueStr = insulinFormatter.string(from:NSNumber(value:activeInsulin)) + { + statusLabelText = String(format: NSLocalizedString( + "IOB %1$@ U", + comment: "The subtitle format describing units of active insulin. (1: localized insulin value description)"), + valueStr) + } + + if let carbsOnBoard = context?.COB + { + let carbFormatter = NumberFormatter() + carbFormatter.numberStyle = .decimal + carbFormatter.maximumFractionDigits = 0 + let valueStr = carbFormatter.string(from:NSNumber(value:carbsOnBoard)) + + if statusLabelText != "" { // Not empty - add carriage return + statusLabelText += "\n" + } + statusLabelText += String(format: NSLocalizedString( + "COB %1$@ g", + comment: "The subtitle format describing grams of active carbs. (1: localized carb value description)"), + valueStr!) + } + + statusLabel.setText(statusLabelText) + statusLabel.setHidden(false) + } private func updateForOverrideContext(_ context: GlucoseRangeScheduleOverrideUserInfo.Context?) { diff --git a/WatchApp/Base.lproj/Interface.storyboard b/WatchApp/Base.lproj/Interface.storyboard index f2b392d588..b68a91eb73 100644 --- a/WatchApp/Base.lproj/Interface.storyboard +++ b/WatchApp/Base.lproj/Interface.storyboard @@ -1,12 +1,12 @@ - + - - + + @@ -149,16 +149,14 @@ -