@@ -139,7 +139,7 @@ final class WatchDataManager: NSObject {
139139 }
140140 }
141141
142- private func createWatchContext( _ completion: @escaping ( _ context: WatchContext ) -> Void ) {
142+ private func createWatchContext( recommendingBolusFor potentialCarbEntry : NewCarbEntry ? = nil , _ completion: @escaping ( _ context: WatchContext ) -> Void ) {
143143 let loopManager = deviceManager. loopManager!
144144
145145 let glucose = loopManager. glucoseStore. latestGlucose
@@ -160,6 +160,11 @@ final class WatchDataManager: NSObject {
160160 if let trend = self . deviceManager. cgmManager? . sensorState? . trendType {
161161 context. glucoseTrendRawValue = trend. rawValue
162162 }
163+
164+ if let potentialCarbEntry = potentialCarbEntry {
165+ context. potentialCarbEntry = potentialCarbEntry
166+ context. recommendedBolusDoseConsideringPotentialCarbEntry = try ? state. recommendBolus ( consideringPotentialCarbEntry: potentialCarbEntry, replacingCarbEntry: nil ) ? . amount
167+ }
163168
164169 if let glucose = glucose {
165170 updateGroup. enter ( )
@@ -201,21 +206,36 @@ final class WatchDataManager: NSObject {
201206 }
202207 }
203208
204- private func addCarbEntryFromWatchMessage( _ message: [ String : Any ] , completionHandler: ( ( _ error: Error ? ) -> Void ) ? = nil ) {
205- if let carbEntry = CarbEntryUserInfo ( rawValue: message) ? . carbEntry {
206- deviceManager. loopManager. addCarbEntryAndRecommendBolus ( carbEntry) { ( result) in
209+ private func addCarbEntryAndBolusFromWatchMessage( _ message: [ String : Any ] ) {
210+ guard let bolus = SetBolusUserInfo ( rawValue: message as SetBolusUserInfo . RawValue ) else {
211+ log. error ( " Could not enact bolus from from unknown message: %{public}@ " , String ( describing: message) )
212+ return
213+ }
214+
215+ func enactBolus( ) {
216+ guard bolus. value > 0 else { return }
217+ self . deviceManager. enactBolus ( units: bolus. value, at: bolus. startDate) { ( error) in
218+ if error == nil {
219+ self . deviceManager. analyticsServicesManager. didSetBolusFromWatch ( bolus. value)
220+ }
221+
222+ // When we've successfully started the bolus, send a new context with our new prediction
223+ self . sendWatchContextIfNeeded ( )
224+ }
225+ }
226+
227+ if let carbEntry = bolus. carbEntry {
228+ deviceManager. loopManager. addCarbEntry ( carbEntry) { ( result) in
207229 switch result {
208230 case . success:
209231 self . deviceManager. analyticsServicesManager. didAddCarbsFromWatch ( )
210- completionHandler ? ( nil )
232+ enactBolus ( )
211233 case . failure( let error) :
212234 self . log. error ( " %{public}@ " , String ( describing: error) )
213- completionHandler ? ( error)
214235 }
215236 }
216237 } else {
217- log. error ( " Could not add carb entry from unknown message: %{public}@ " , String ( describing: message) )
218- completionHandler ? ( nil )
238+ enactBolus ( )
219239 }
220240 }
221241}
@@ -224,25 +244,18 @@ final class WatchDataManager: NSObject {
224244extension WatchDataManager : WCSessionDelegate {
225245 func session( _ session: WCSession , didReceiveMessage message: [ String : Any ] , replyHandler: @escaping ( [ String : Any ] ) -> Void ) {
226246 switch message [ " name " ] as? String {
227- case CarbEntryUserInfo . name? :
228- addCarbEntryFromWatchMessage ( message) { ( _) in
229- self . createWatchContext { ( context) in
230- // Send back the updated prediction and recommended bolus
247+ case PotentialCarbEntryUserInfo . name? :
248+ if let potentialCarbEntry = PotentialCarbEntryUserInfo ( rawValue: message) ? . carbEntry {
249+ self . createWatchContext ( recommendingBolusFor: potentialCarbEntry) { ( context) in
231250 replyHandler ( context. rawValue)
232251 }
252+ } else {
253+ log. error ( " Could not recommend bolus from from unknown message: %{public}@ " , String ( describing: message) )
254+ replyHandler ( [ : ] )
233255 }
234256 case SetBolusUserInfo . name? :
235- // Start the bolus and reply when it's successfully requested
236- if let bolus = SetBolusUserInfo ( rawValue: message as SetBolusUserInfo . RawValue ) {
237- self . deviceManager. enactBolus ( units: bolus. value, at: bolus. startDate) { ( error) in
238- if error == nil {
239- self . deviceManager. analyticsServicesManager. didSetBolusFromWatch ( bolus. value)
240- }
241-
242- // When we've successfully started the bolus, send a new context with our new prediction
243- self . sendWatchContextIfNeeded ( )
244- }
245- }
257+ // Add carbs if applicable; start the bolus and reply when it's successfully requested
258+ addCarbEntryAndBolusFromWatchMessage ( message)
246259
247260 // Reply immediately
248261 replyHandler ( [ : ] )
@@ -282,7 +295,7 @@ extension WatchDataManager: WCSessionDelegate {
282295 }
283296
284297 func session( _ session: WCSession , didReceiveUserInfo userInfo: [ String : Any ] ) {
285- addCarbEntryFromWatchMessage ( userInfo)
298+ assertionFailure ( " We currently don't expect any userInfo messages transferred from the watch side " )
286299 }
287300
288301 func session( _ session: WCSession , activationDidCompleteWith activationState: WCSessionActivationState , error: Error ? ) {
0 commit comments