|
|
@@ -10,15 +10,15 @@ extension Bolus {
|
|
|
|
|
|
@StateObject var state: StateModel
|
|
|
|
|
|
- @State private var showInfo = false
|
|
|
+ @State private var showInfo: Bool = false
|
|
|
@State private var showAlert = false
|
|
|
@State private var autofocus: Bool = true
|
|
|
@State private var calculatorDetent = PresentationDetent.medium
|
|
|
- @State var pushed = false
|
|
|
- @State var isPromptPresented = false
|
|
|
- @State var dish: String = ""
|
|
|
- @State var saved = false
|
|
|
- @State var isCalculating: Bool = false
|
|
|
+ @State private var pushed: Bool = false
|
|
|
+ @State private var isPromptPresented: Bool = false
|
|
|
+ @State private var dish: String = ""
|
|
|
+ @State private var saved: Bool = false
|
|
|
+ @State private var debounce: DispatchWorkItem?
|
|
|
|
|
|
@Environment(\.managedObjectContext) var moc
|
|
|
|
|
|
@@ -81,7 +81,18 @@ extension Bolus {
|
|
|
}
|
|
|
|
|
|
private var empty: Bool {
|
|
|
- state.carbs <= 0 && state.fat <= 0 && state.protein <= 0
|
|
|
+ state.useFPUconversion ? (state.carbs <= 0 && state.fat <= 0 && state.protein <= 0) : (state.carbs <= 0)
|
|
|
+ }
|
|
|
+
|
|
|
+ /// Handles macro input (carb, fat, protein) in a debounced fashion.
|
|
|
+ func handleDebouncedInput() {
|
|
|
+ debounce?.cancel()
|
|
|
+ debounce = DispatchWorkItem { [self] in
|
|
|
+ state.insulinCalculated = state.calculateInsulin()
|
|
|
+ }
|
|
|
+ if let debounce = debounce {
|
|
|
+ DispatchQueue.main.asyncAfter(deadline: .now() + 0.35, execute: debounce)
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
private var presetPopover: some View {
|
|
|
@@ -277,7 +288,11 @@ extension Bolus {
|
|
|
formatter: formatter,
|
|
|
autofocus: false,
|
|
|
cleanInput: true
|
|
|
- )
|
|
|
+ ).onChange(of: state.carbs) { _ in
|
|
|
+ if state.carbs > 0 {
|
|
|
+ handleDebouncedInput()
|
|
|
+ }
|
|
|
+ }
|
|
|
Text("g").foregroundColor(.secondary)
|
|
|
}
|
|
|
|
|
|
@@ -327,27 +342,6 @@ extension Bolus {
|
|
|
.popover(isPresented: $isPromptPresented) {
|
|
|
presetPopover
|
|
|
}
|
|
|
-
|
|
|
- HStack {
|
|
|
- Spacer()
|
|
|
- Button {
|
|
|
- isCalculating = true
|
|
|
- state.insulinCalculated = state.calculateInsulin()
|
|
|
-
|
|
|
- DispatchQueue.main.asyncAfter(deadline: .now() + 0.3) {
|
|
|
- isCalculating = false
|
|
|
- }
|
|
|
- }
|
|
|
- label: {
|
|
|
- if !isCalculating {
|
|
|
- Text("Calculate")
|
|
|
- } else {
|
|
|
- ProgressView().progressViewStyle(CircularProgressViewStyle())
|
|
|
- }
|
|
|
- }.disabled(empty)
|
|
|
-
|
|
|
- Spacer()
|
|
|
- }
|
|
|
}.listRowBackground(Color.chart)
|
|
|
|
|
|
if state.displayPresets {
|