Skip to content

Commit c93cea4

Browse files
author
Rick Pasetto
authored
Fix bug in BolusEntryView with "save without bolusing" (#211)
* Fix bug in BolusEntryView with "save without bolusing" This is a regression introduced by my recent changes (thanks, Darin, for finding it!) To make this a bit easier to reason about, I've moved a bunch of "helpers" into the ViewModel where it could be more easily unit tested. * fix indentation * PR Feedbck
1 parent a3319e3 commit c93cea4

File tree

2 files changed

+66
-49
lines changed

2 files changed

+66
-49
lines changed

Loop/View Models/BolusEntryViewModel.swift

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -601,3 +601,53 @@ final class BolusEntryViewModel: ObservableObject {
601601
extension BolusEntryViewModel.Alert: Identifiable {
602602
var id: Self { self }
603603
}
604+
605+
// MARK: Helpers
606+
extension BolusEntryViewModel {
607+
608+
var isManualGlucosePromptVisible: Bool {
609+
activeNotice == .staleGlucoseData && !isManualGlucoseEntryEnabled
610+
}
611+
612+
var isNoticeVisible: Bool {
613+
if activeNotice == nil {
614+
return false
615+
} else if activeNotice != .staleGlucoseData {
616+
return true
617+
} else {
618+
return !isManualGlucoseEntryEnabled
619+
}
620+
}
621+
622+
private var hasBolusEntryReadyToDeliver: Bool {
623+
enteredBolus.doubleValue(for: .internationalUnit()) != 0
624+
}
625+
626+
private var hasDataToSave: Bool {
627+
enteredManualGlucose != nil || potentialCarbEntry != nil
628+
}
629+
630+
enum ButtonChoice { case manualGlucoseEntry, actionButton }
631+
var primaryButton: ButtonChoice {
632+
if !isManualGlucosePromptVisible { return .actionButton }
633+
if hasBolusEntryReadyToDeliver { return .actionButton }
634+
return .manualGlucoseEntry
635+
}
636+
637+
enum ActionButtonAction {
638+
case saveWithoutBolusing
639+
case saveAndDeliver
640+
case enterBolus
641+
case deliver
642+
}
643+
644+
var actionButtonAction: ActionButtonAction {
645+
switch (hasDataToSave, hasBolusEntryReadyToDeliver) {
646+
case (true, true): return .saveAndDeliver
647+
case (true, false): return .saveWithoutBolusing
648+
case (false, true): return .deliver
649+
case (false, false): return .enterBolus
650+
}
651+
}
652+
}
653+

Loop/Views/BolusEntryView.swift

Lines changed: 16 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -308,13 +308,13 @@ struct BolusEntryView: View, HorizontalSizeClassOverride {
308308

309309
private var actionArea: some View {
310310
VStack(spacing: 0) {
311-
if isNoticeVisible {
311+
if viewModel.isNoticeVisible {
312312
warning(for: viewModel.activeNotice!)
313313
.padding([.top, .horizontal])
314314
.transition(AnyTransition.opacity.combined(with: .move(edge: .bottom)))
315315
}
316316

317-
if isManualGlucosePromptVisible {
317+
if viewModel.isManualGlucosePromptVisible {
318318
enterManualGlucoseButton
319319
.transition(AnyTransition.opacity.combined(with: .move(edge: .bottom)))
320320
}
@@ -325,20 +325,6 @@ struct BolusEntryView: View, HorizontalSizeClassOverride {
325325
.background(Color(.secondarySystemGroupedBackground).shadow(radius: 5))
326326
}
327327

328-
private var isNoticeVisible: Bool {
329-
if viewModel.activeNotice == nil {
330-
return false
331-
} else if viewModel.activeNotice != .staleGlucoseData {
332-
return true
333-
} else {
334-
return !viewModel.isManualGlucoseEntryEnabled
335-
}
336-
}
337-
338-
private var isManualGlucosePromptVisible: Bool {
339-
viewModel.activeNotice == .staleGlucoseData && !viewModel.isManualGlucoseEntryEnabled
340-
}
341-
342328
private func warning(for notice: BolusEntryViewModel.Notice) -> some View {
343329
switch notice {
344330
case .predictedGlucoseBelowSuspendThreshold(suspendThreshold: let suspendThreshold):
@@ -354,19 +340,7 @@ struct BolusEntryView: View, HorizontalSizeClassOverride {
354340
)
355341
}
356342
}
357-
358-
private var hasBolusEntryReadyToDeliver: Bool {
359-
return self.viewModel.enteredBolus.doubleValue(for: .internationalUnit()) != 0
360-
}
361-
362-
enum ButtonChoice { case manualGlucoseEntry, actionButton }
363-
364-
private var primaryButton: ButtonChoice {
365-
if !isManualGlucosePromptVisible { return .actionButton }
366-
if hasBolusEntryReadyToDeliver { return .actionButton }
367-
return .manualGlucoseEntry
368-
}
369-
343+
370344
private var enterManualGlucoseButton: some View {
371345
Button(
372346
action: {
@@ -376,43 +350,36 @@ struct BolusEntryView: View, HorizontalSizeClassOverride {
376350
},
377351
label: { Text("Enter Manual BG", comment: "Button text prompting manual glucose entry on bolus screen") }
378352
)
379-
.buttonStyle(ActionButtonStyle(primaryButton == .manualGlucoseEntry ? .primary : .secondary))
353+
.buttonStyle(ActionButtonStyle(viewModel.primaryButton == .manualGlucoseEntry ? .primary : .secondary))
380354
.padding([.top, .horizontal])
381355
}
382356

383357
private var actionButton: some View {
384-
Button(
358+
Button<Text>(
385359
action: {
386-
if !self.hasBolusEntryReadyToDeliver {
360+
if self.viewModel.actionButtonAction == .enterBolus {
387361
self.shouldBolusEntryBecomeFirstResponder = true
388362
} else {
389363
self.viewModel.saveAndDeliver(onSuccess: self.dismiss)
390364
}
391365
},
392366
label: {
393-
if hasDataToSave {
394-
if viewModel.enteredBolus.doubleValue(for: .internationalUnit()) == 0 {
395-
Text("Save without Bolusing", comment: "Button text to save carbs and/or manual glucose entry without a bolus")
396-
} else {
397-
Text("Save and Deliver", comment: "Button text to save carbs and/or manual glucose entry and deliver a bolus")
398-
}
399-
} else {
400-
if viewModel.enteredBolus.doubleValue(for: .internationalUnit()) == 0 {
401-
Text("Enter Bolus", comment: "Button text to begin entering a bolus")
402-
} else {
403-
Text("Deliver", comment: "Button text to deliver a bolus")
404-
}
367+
switch viewModel.actionButtonAction {
368+
case .saveWithoutBolusing:
369+
return Text("Save without Bolusing", comment: "Button text to save carbs and/or manual glucose entry without a bolus")
370+
case .saveAndDeliver:
371+
return Text("Save and Deliver", comment: "Button text to save carbs and/or manual glucose entry and deliver a bolus")
372+
case .enterBolus:
373+
return Text("Enter Bolus", comment: "Button text to begin entering a bolus")
374+
case .deliver:
375+
return Text("Deliver", comment: "Button text to deliver a bolus")
405376
}
406377
}
407378
)
408-
.buttonStyle(ActionButtonStyle(primaryButton == .actionButton ? .primary : .secondary))
379+
.buttonStyle(ActionButtonStyle(viewModel.primaryButton == .actionButton ? .primary : .secondary))
409380
.padding()
410381
}
411382

412-
private var hasDataToSave: Bool {
413-
viewModel.enteredManualGlucose != nil || viewModel.potentialCarbEntry != nil
414-
}
415-
416383
private func alert(for alert: BolusEntryViewModel.Alert) -> SwiftUI.Alert {
417384
switch alert {
418385
case .recommendationChanged:

0 commit comments

Comments
 (0)