From 7d536c02bc8f57b45845aa4c4d5649f305e93eb0 Mon Sep 17 00:00:00 2001 From: Nathan Racklyeft Date: Tue, 30 Aug 2016 21:20:00 -0700 Subject: [PATCH] Provide bolus amount suggestions for all watch boluses, and honor max bolus. Progress toward #30 --- Loop/Managers/WatchDataManager.swift | 4 +++- .../Controllers/BolusInterfaceController.swift | 18 +++++++++++++----- .../StatusInterfaceController.swift | 2 +- .../Models/BolusSuggestionUserInfo.swift | 13 +++++++++++-- WatchApp Extension/Models/WatchContext.swift | 11 ++++++++++- 5 files changed, 38 insertions(+), 10 deletions(-) diff --git a/Loop/Managers/WatchDataManager.swift b/Loop/Managers/WatchDataManager.swift index 4d589e5ccd..157102cd73 100644 --- a/Loop/Managers/WatchDataManager.swift +++ b/Loop/Managers/WatchDataManager.swift @@ -103,6 +103,7 @@ final class WatchDataManager: NSObject, WCSessionDelegate { let glucose = deviceDataManager.glucoseStore?.latestGlucose let reservoir = deviceDataManager.doseStore.lastReservoirValue + let maxBolus = deviceDataManager.maximumBolus deviceDataManager.loopManager.getLoopStatus { (predictedGlucose, recommendedTempBasal, lastTempBasal, lastLoopCompleted, insulinOnBoard, error) in @@ -115,6 +116,7 @@ final class WatchDataManager: NSObject, WCSessionDelegate { context.loopLastRunDate = lastLoopCompleted context.recommendedBolusDose = units + context.maxBolus = maxBolus if let trend = self.deviceDataManager.sensorInfo?.trendType { context.glucoseTrend = trend @@ -155,7 +157,7 @@ final class WatchDataManager: NSObject, WCSessionDelegate { switch message["name"] as? String { case CarbEntryUserInfo.name?: addCarbEntryFromWatchMessage(message) { (units) in - replyHandler(BolusSuggestionUserInfo(recommendedBolus: units ?? 0).rawValue) + replyHandler(BolusSuggestionUserInfo(recommendedBolus: units ?? 0, maxBolus: self.deviceDataManager.maximumBolus).rawValue) } case SetBolusUserInfo.name?: if let bolus = SetBolusUserInfo(rawValue: message) { diff --git a/WatchApp Extension/Controllers/BolusInterfaceController.swift b/WatchApp Extension/Controllers/BolusInterfaceController.swift index 9dcb80ff13..bd930bd0a9 100644 --- a/WatchApp Extension/Controllers/BolusInterfaceController.swift +++ b/WatchApp Extension/Controllers/BolusInterfaceController.swift @@ -27,8 +27,6 @@ final class BolusInterfaceController: WKInterfaceController, IdentifiableClass { } } - private var maxBolusValue: Double = 15 - private func pickerValueFromBolusValue(bolusValue: Double) -> Int { switch bolusValue { case let bolus where bolus > 10: @@ -71,15 +69,25 @@ final class BolusInterfaceController: WKInterfaceController, IdentifiableClass { super.awakeWithContext(context) let maxPickerValue: Int + var maxBolusValue: Double = 15 let pickerValue: Int if let context = context as? BolusSuggestionUserInfo { - maxPickerValue = pickerValueFromBolusValue(context.recommendedBolus) + let recommendedBolus = context.recommendedBolus + + if let maxBolus = context.maxBolus { + maxBolusValue = maxBolus + } else if recommendedBolus > 0 { + maxBolusValue = recommendedBolus + } + + maxPickerValue = pickerValueFromBolusValue(maxBolusValue) + let recommendedPickerValue = pickerValueFromBolusValue(recommendedBolus) maxBolusValue = bolusValueFromPickerValue(maxPickerValue) - pickerValue = Int(Double(maxPickerValue) * 0.75) + pickerValue = Int(Double(recommendedPickerValue) * 0.75) bolusValue = bolusValueFromPickerValue(pickerValue) - if let valueString = formatter.stringFromNumber(maxBolusValue) { + if let valueString = formatter.stringFromNumber(recommendedBolus) { recommendedValueLabel.setText(String(format: NSLocalizedString("Rec: %@ U", comment: "The label and value showing the recommended bolus"), valueString).localizedUppercaseString) } } else { diff --git a/WatchApp Extension/Controllers/StatusInterfaceController.swift b/WatchApp Extension/Controllers/StatusInterfaceController.swift index 9971c8235e..726874fabe 100644 --- a/WatchApp Extension/Controllers/StatusInterfaceController.swift +++ b/WatchApp Extension/Controllers/StatusInterfaceController.swift @@ -81,7 +81,7 @@ final class StatusInterfaceController: ContextInterfaceController { } @IBAction func setBolus() { - presentControllerWithName(BolusInterfaceController.className, context: nil) + presentControllerWithName(BolusInterfaceController.className, context: dataManager.lastContextData?.bolusSuggestion) } } diff --git a/WatchApp Extension/Models/BolusSuggestionUserInfo.swift b/WatchApp Extension/Models/BolusSuggestionUserInfo.swift index 1f3ec7efbe..6e29c94ce1 100644 --- a/WatchApp Extension/Models/BolusSuggestionUserInfo.swift +++ b/WatchApp Extension/Models/BolusSuggestionUserInfo.swift @@ -11,9 +11,11 @@ import Foundation final class BolusSuggestionUserInfo: RawRepresentable { let recommendedBolus: Double + let maxBolus: Double? - init(recommendedBolus: Double) { + init(recommendedBolus: Double, maxBolus: Double? = nil) { self.recommendedBolus = recommendedBolus + self.maxBolus = maxBolus } // MARK: - RawRepresentable @@ -30,13 +32,20 @@ final class BolusSuggestionUserInfo: RawRepresentable { } self.recommendedBolus = recommendedBolus + self.maxBolus = rawValue["mb"] as? Double } var rawValue: RawValue { - return [ + var raw: RawValue = [ "v": self.dynamicType.version, "name": BolusSuggestionUserInfo.name, "br": recommendedBolus ] + + if let maxBolus = maxBolus { + raw["mb"] = maxBolus + } + + return raw } } diff --git a/WatchApp Extension/Models/WatchContext.swift b/WatchApp Extension/Models/WatchContext.swift index 21e18ad6c8..b6b9e2efe5 100644 --- a/WatchApp Extension/Models/WatchContext.swift +++ b/WatchApp Extension/Models/WatchContext.swift @@ -13,9 +13,10 @@ import HealthKit final class WatchContext: NSObject, RawRepresentable { typealias RawValue = [String: AnyObject] - private let version = 2 + private let version = 3 var preferredGlucoseUnit: HKUnit? + var maxBolus: Double? var glucose: HKQuantity? var glucoseTrend: GlucoseTrend? @@ -27,6 +28,12 @@ final class WatchContext: NSObject, RawRepresentable { var lastNetTempBasalDate: NSDate? var recommendedBolusDose: Double? + var bolusSuggestion: BolusSuggestionUserInfo? { + guard let recommended = recommendedBolusDose else { return nil } + + return BolusSuggestionUserInfo(recommendedBolus: recommended, maxBolus: maxBolus) + } + var COB: Double? var IOB: Double? var reservoir: Double? @@ -72,6 +79,7 @@ final class WatchContext: NSObject, RawRepresentable { lastNetTempBasalDate = rawValue["bad"] as? NSDate recommendedBolusDose = rawValue["rbo"] as? Double COB = rawValue["cob"] as? Double + maxBolus = rawValue["mb"] as? Double } var rawValue: RawValue { @@ -94,6 +102,7 @@ final class WatchContext: NSObject, RawRepresentable { raw["gd"] = glucoseDate raw["iob"] = IOB raw["ld"] = loopLastRunDate + raw["mb"] = maxBolus raw["r"] = reservoir raw["rbo"] = recommendedBolusDose raw["rp"] = reservoirPercentage