| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724 |
- import Charts
- import CoreData
- import LoopKitUI
- import SwiftUI
- import Swinject
- extension Treatments {
- struct RootView: BaseView {
- enum FocusedField {
- case carbs
- case fat
- case protein
- case bolus
- }
- @FocusState private var focusedField: FocusedField?
- let resolver: Resolver
- @State var state = StateModel()
- @State private var showPresetSheet = false
- @State private var autofocus: Bool = true
- @State private var calculatorDetent = PresentationDetent.large
- @State private var pushed: Bool = false
- @State private var debounce: DispatchWorkItem?
- @State private var showFatProteinOrderBanner = false
- private enum Config {
- static let dividerHeight: CGFloat = 2
- static let spacing: CGFloat = 3
- }
- @Environment(\.colorScheme) var colorScheme
- @Environment(AppState.self) var appState
- private var formatter: NumberFormatter {
- let formatter = NumberFormatter()
- formatter.numberStyle = .decimal
- formatter.maximumIntegerDigits = 2
- formatter.maximumFractionDigits = 3
- return formatter
- }
- private var bolusProgressFormatter: NumberFormatter {
- let fractionDigits: Int = switch state.settingsManager.preferences.bolusIncrement {
- case 0.1: 1
- case 0.025: 3
- default: 2
- }
- let formatter = NumberFormatter()
- formatter.numberStyle = .decimal
- formatter.minimum = 0
- formatter.maximumFractionDigits = fractionDigits
- formatter.minimumFractionDigits = fractionDigits
- formatter.allowsFloats = true
- formatter.roundingIncrement = Double(state.settingsManager.preferences.bolusIncrement) as NSNumber
- return formatter
- }
- private var mealFormatter: NumberFormatter {
- let formatter = NumberFormatter()
- formatter.numberStyle = .decimal
- formatter.maximumIntegerDigits = 3
- formatter.maximumFractionDigits = 0
- return formatter
- }
- private var gluoseFormatter: NumberFormatter {
- let formatter = NumberFormatter()
- formatter.numberStyle = .decimal
- if state.units == .mmolL {
- formatter.maximumIntegerDigits = 2
- formatter.maximumFractionDigits = 1
- } else {
- formatter.maximumIntegerDigits = 3
- formatter.maximumFractionDigits = 0
- }
- return formatter
- }
- private var fractionDigits: Int {
- if state.units == .mmolL {
- return 1
- } else { return 0 }
- }
- /// Handles macro input (carb, fat, protein) in a debounced fashion.
- func handleDebouncedInput() {
- debounce?.cancel()
- debounce = DispatchWorkItem { [self] in
- Task {
- await state.updateForecasts()
- state.insulinCalculated = await state.calculateInsulin()
- }
- }
- if let debounce = debounce {
- DispatchQueue.main.asyncAfter(deadline: .now() + 0.35, execute: debounce)
- }
- }
- @ViewBuilder private func proteinAndFat() -> some View {
- HStack {
- HStack {
- Text("Fat")
- TextFieldWithToolBar(
- text: $state.fat,
- placeholder: "0",
- keyboardType: .numberPad,
- numberFormatter: mealFormatter,
- showArrows: true,
- previousTextField: { focusedField = previousField(from: .fat) },
- nextTextField: { focusedField = nextField(from: .fat) },
- unitsText: String(localized: "g", comment: "Units for carbs")
- )
- .focused($focusedField, equals: .fat)
- }
- Divider().foregroundStyle(.primary).fontWeight(.bold).frame(width: 10)
- HStack {
- Text("Protein")
- TextFieldWithToolBar(
- text: $state.protein,
- placeholder: "0",
- keyboardType: .numberPad,
- numberFormatter: mealFormatter,
- showArrows: true,
- previousTextField: { focusedField = previousField(from: .protein) },
- nextTextField: { focusedField = nextField(from: .protein) },
- unitsText: String(localized: "g", comment: "Units for carbs")
- )
- .focused($focusedField, equals: .protein)
- }
- }
- }
- @ViewBuilder private func carbsTextField() -> some View {
- HStack {
- Text("Carbs")
- Spacer()
- TextFieldWithToolBar(
- text: $state.carbs,
- placeholder: "0",
- keyboardType: .numberPad,
- numberFormatter: mealFormatter,
- showArrows: true,
- previousTextField: { focusedField = previousField(from: .carbs) },
- nextTextField: { focusedField = nextField(from: .carbs) },
- unitsText: String(localized: "g", comment: "Units for carbs")
- )
- .focused($focusedField, equals: .carbs)
- .onChange(of: state.carbs) {
- handleDebouncedInput()
- }
- }
- }
- /// Determines the next field to focus on based on the current focused field.
- ///
- /// This function handles the tab order navigation between input fields,
- /// taking into account whether fat/protein fields are visible based on user settings.
- ///
- /// - Parameter current: The currently focused field
- /// - Returns: The next field that should receive focus, or nil if there is no next field
- private func nextField(from current: FocusedField) -> FocusedField? {
- // If fat/protein fields are hidden, skip them in navigation
- let showFPU = state.useFPUconversion
- switch current {
- case .fat:
- return .bolus
- case .protein:
- return .fat
- case .carbs:
- return showFPU ? .protein : .bolus
- case .bolus:
- return .carbs
- }
- }
- /// Determines the previous field to focus on based on the current focused field.
- ///
- /// This function handles the reverse tab order navigation between input fields,
- /// taking into account whether fat/protein fields are visible based on user settings.
- ///
- /// - Parameter current: The currently focused field
- /// - Returns: The previous field that should receive focus, or nil if there is no previous field
- private func previousField(from current: FocusedField) -> FocusedField? {
- let showFPU = state.useFPUconversion
- switch current {
- case .fat:
- return .protein
- case .protein:
- return .carbs
- case .carbs:
- return .bolus
- case .bolus:
- return showFPU ? .fat : .carbs
- }
- }
- var body: some View {
- ZStack(alignment: .center) {
- VStack {
- List {
- Section {
- ForecastChart(state: state)
- .padding(.vertical)
- }.listRowBackground(Color.chart)
- Section {
- carbsTextField()
- if state.useFPUconversion {
- proteinAndFat()
- if showFatProteinOrderBanner {
- HStack {
- Image(systemName: "arrow.left.arrow.right")
- Text("The order of Fat and Protein inputs has changed.").font(.callout)
- Spacer()
- Button {
- PropertyPersistentFlags.shared.hasSeenFatProteinOrderChange = true
- withAnimation { showFatProteinOrderBanner = false }
- } label: {
- Image(systemName: "xmark.circle.fill")
- }
- .buttonStyle(.plain)
- }
- .listRowBackground(Color.orange.opacity(0.75))
- .transition(.opacity)
- }
- }
- // Time
- HStack {
- // Semi-hacky workaround to make sure the List renders the horizontal divider properly between the `Time` and `Note` rows within the Section
- HStack {
- Text("")
- Image(systemName: "clock").padding(.leading, -7)
- }
- Spacer()
- if !pushed {
- Button {
- pushed = true
- } label: { Text("Now") }.buttonStyle(.borderless).foregroundColor(.secondary)
- .padding(.trailing, 5)
- } else {
- Button { state.date = state.date.addingTimeInterval(-15.minutes.timeInterval) }
- label: { Image(systemName: "minus.circle") }.tint(.blue).buttonStyle(.borderless)
- DatePicker(
- "Time",
- selection: $state.date,
- displayedComponents: [.hourAndMinute]
- ).controlSize(.mini)
- .labelsHidden()
- .onChange(of: state.date) { _, _ in
- // Trigger simulation when date changes to update forecasts for backdated carbs
- Task {
- // `updateForecasts()` does update the `simulatedDetermination` of type `Determination?` var on the main thread, so I can use this to pass its cob value into the bolus calc manager
- await state.updateForecasts()
- state.insulinCalculated = await state.calculateInsulin()
- }
- }
- Button {
- state.date = state.date.addingTimeInterval(15.minutes.timeInterval)
- }
- label: { Image(systemName: "plus.circle") }.tint(.blue).buttonStyle(.borderless)
- }
- }
- // Notes
- HStack {
- Image(systemName: "square.and.pencil")
- TextFieldWithToolBarString(
- text: $state.note,
- placeholder: String(localized: "Note..."),
- maxLength: 25
- )
- }
- }.listRowBackground(Color.chart)
- Section {
- if state.fattyMeals || state.sweetMeals {
- HStack(spacing: 10) {
- if state.fattyMeals {
- Toggle(isOn: $state.useFattyMealCorrectionFactor) {
- Text("Reduced Bolus")
- }
- .toggleStyle(RadioButtonToggleStyle())
- .font(.footnote)
- .onChange(of: state.useFattyMealCorrectionFactor) {
- Task {
- state.insulinCalculated = await state.calculateInsulin()
- if state.useFattyMealCorrectionFactor {
- state.useSuperBolus = false
- }
- }
- }
- }
- if state.sweetMeals {
- Toggle(isOn: $state.useSuperBolus) {
- Text("Super Bolus")
- }
- .toggleStyle(RadioButtonToggleStyle())
- .font(.footnote)
- .onChange(of: state.useSuperBolus) {
- Task {
- state.insulinCalculated = await state.calculateInsulin()
- if state.useSuperBolus {
- state.useFattyMealCorrectionFactor = false
- }
- }
- }
- }
- }
- }
- HStack {
- HStack {
- Text("Recommendation")
- Button(action: {
- state.showInfo.toggle()
- }, label: {
- Image(systemName: "info.circle")
- })
- .foregroundStyle(.blue)
- .buttonStyle(PlainButtonStyle())
- }
- Spacer()
- Button {
- state.amount = state.insulinCalculated
- } label: {
- HStack {
- Text(
- formatter
- .string(from: Double(state.insulinCalculated) as NSNumber) ?? ""
- )
- Text(
- String(
- localized:
- " U",
- comment: "Unit in number of units delivered (keep the space character!)"
- )
- ).foregroundColor(.secondary)
- }
- }
- .disabled(state.insulinCalculated == 0 || state.amount == state.insulinCalculated)
- .buttonStyle(.bordered).padding(.trailing, -10)
- }
- HStack {
- Text("Bolus")
- Spacer()
- TextFieldWithToolBar(
- text: $state.amount,
- placeholder: "0",
- textColor: colorScheme == .dark ? .white : .blue,
- maxLength: 5,
- numberFormatter: formatter,
- showArrows: true,
- previousTextField: { focusedField = previousField(from: .bolus) },
- nextTextField: { focusedField = nextField(from: .bolus) },
- unitsText: String(localized: "U", comment: "Units for bolus amount")
- ).focused($focusedField, equals: .bolus)
- .onChange(of: state.amount) {
- Task {
- await state.updateForecasts()
- }
- }
- }
- HStack {
- Text("External Insulin")
- Spacer()
- Toggle("", isOn: $state.externalInsulin).toggleStyle(CheckboxToggleStyle())
- }
- }.listRowBackground(Color.chart)
- treatmentButton
- }
- .listSectionSpacing(sectionSpacing)
- }
- .blur(radius: state.isAwaitingDeterminationResult ? 5 : 0)
- if state.isAwaitingDeterminationResult {
- CustomProgressView(text: progressText.displayName)
- }
- }
- .padding(.top)
- .ignoresSafeArea(edges: .top)
- .scrollContentBackground(.hidden).background(appState.trioBackgroundColor(for: colorScheme))
- .blur(radius: state.showInfo ? 3 : 0)
- .navigationTitle("Treatments")
- .navigationBarTitleDisplayMode(.inline)
- .toolbar(content: {
- ToolbarItem(placement: .topBarLeading) {
- Button {
- state.hideModal()
- } label: {
- Text("Close")
- }
- }
- if state.displayPresets {
- ToolbarItem(placement: .topBarTrailing) {
- Button(action: {
- showPresetSheet = true
- }, label: {
- HStack {
- Text("Presets")
- Image(systemName: "plus")
- }
- })
- }
- }
- })
- .onAppear {
- configureView {
- state.isActive = true
- Task { @MainActor in
- state.insulinCalculated = await state.calculateInsulin()
- }
- if PropertyPersistentFlags.shared.hasSeenFatProteinOrderChange != true {
- showFatProteinOrderBanner = true
- }
- }
- }
- .onDisappear {
- state.isActive = false
- state.addButtonPressed = false
- // Cancel all Combine subscriptions and unregister State from broadcaster
- state.cleanupTreatmentState()
- }
- .sheet(isPresented: $state.showInfo) {
- PopupView(state: state)
- }
- .sheet(isPresented: $showPresetSheet, onDismiss: {
- showPresetSheet = false
- }) {
- MealPresetView(state: state)
- }
- .alert("Error while processing Treatment", isPresented: $state.showDeterminationFailureAlert) {
- Button("OK", role: .cancel) {
- state.hideModal()
- }
- } message: {
- Text("\(state.determinationFailureMessage)")
- }
- }
- var progressText: ProgressText {
- switch (state.amount > 0, state.carbs > 0) {
- case (true, true):
- return .updatingIOBandCOB
- case (false, true):
- return .updatingCOB
- case (true, false):
- return .updatingIOB
- default:
- return .updatingTreatments
- }
- }
- @State private var showConfirmDialogForBolusing = false
- private var bolusWarning: (shouldConfirm: Bool, warningMessage: String, color: Color) {
- let isGlucoseVeryLow = state.currentBG < 54
- let isForecastVeryLow = state.minPredBG < 54
- // Only warn when enacting a bolus via pump
- guard !state.externalInsulin, state.amount > 0 else {
- return (false, "", .primary)
- }
- let warningMessage = isGlucoseVeryLow ? String(localized: "Glucose is very low.") :
- isForecastVeryLow ? String(localized: "Glucose forecast is very low.") :
- ""
- let warningColor: Color = isGlucoseVeryLow ? .red : colorScheme == .dark ? .orange : .accentColor
- let shouldConfirm = state.confirmBolus && (isGlucoseVeryLow || isForecastVeryLow)
- return (shouldConfirm, warningMessage, warningColor)
- }
- var treatmentButton: some View {
- let shouldDisplayBolusProgress = state.isBolusInProgress && state.amount > 0 &&
- !state.externalInsulin && (state.carbs == 0 || state.fat == 0 || state.protein == 0)
- var treatmentButtonBackground = Color(.systemBlue)
- if limitExceeded {
- treatmentButtonBackground = Color(.systemRed)
- } else if disableTaskButton {
- treatmentButtonBackground = Color(.systemGray)
- }
- return Section {
- if shouldDisplayBolusProgress {
- bolusInProgressView
- .listRowBackground(Color.clear)
- .listRowInsets(EdgeInsets(top: 4, leading: 0, bottom: 4, trailing: 0))
- } else {
- Button {
- if bolusWarning.shouldConfirm {
- showConfirmDialogForBolusing = true
- } else {
- state.invokeTreatmentsTask()
- }
- } label: {
- HStack {
- taskButtonLabel
- }
- .font(.headline)
- .foregroundStyle(Color.white)
- .frame(maxWidth: .infinity, alignment: .center)
- .frame(height: 35)
- }
- .disabled(disableTaskButton)
- .listRowBackground(treatmentButtonBackground)
- .shadow(radius: 3)
- .clipShape(RoundedRectangle(cornerRadius: 8))
- .confirmationDialog(
- bolusWarning.warningMessage + " Bolus \(state.amount.description) U?",
- isPresented: $showConfirmDialogForBolusing,
- titleVisibility: .visible
- ) {
- Button("Cancel", role: .cancel) {}
- Button(
- bolusWarning.warningMessage
- .isEmpty ? String(localized: "Enact Bolus") : String(localized: "Ignore Warning and Enact Bolus"),
- role: bolusWarning.warningMessage.isEmpty ? nil : .destructive
- ) {
- state.invokeTreatmentsTask()
- }
- }
- }
- } header: {
- if !bolusWarning.warningMessage.isEmpty {
- Text(bolusWarning.warningMessage)
- .textCase(nil)
- .font(.subheadline)
- .foregroundColor(bolusWarning.color)
- .frame(maxWidth: .infinity, alignment: .center)
- .padding(.top, -22)
- }
- }
- }
- /// Card-style in-progress visualizer matching Home's `bolusView` look:
- /// insulin-tinted background, cross.vial.fill icon, "Bolusing" + "X of Y U" text,
- /// xmark.app cancel, gradient progress bar overlaid at the bottom.
- @ViewBuilder private var bolusInProgressView: some View {
- let progress = state.bolusProgress ?? 0
- let bolusTotal = state.lastPumpBolus?.bolus?.amount as Decimal?
- let bolusFraction = (bolusTotal ?? 0) * progress
- let bolusString: String = {
- guard let bolusTotal = bolusTotal else { return String(localized: "Bolus In Progress...") }
- return (bolusProgressFormatter.string(from: bolusFraction as NSNumber) ?? "0")
- + String(localized: " of ", comment: "Bolus string partial message: 'x U of y U' in home view")
- + (Formatter.decimalFormatterWithThreeFractionDigits.string(from: bolusTotal as NSNumber) ?? "0")
- + String(localized: " U", comment: "Insulin unit")
- }()
- ZStack {
- // background card
- RoundedRectangle(cornerRadius: 15)
- .fill(
- colorScheme == .dark
- ? Color(red: 0.03921568627, green: 0.133333333, blue: 0.2156862745)
- : Color.insulin.opacity(0.2)
- )
- .frame(height: 56)
- .shadow(
- color: colorScheme == .dark
- ? Color(red: 0.02745098039, green: 0.1098039216, blue: 0.1411764706)
- : Color.black.opacity(0.33),
- radius: 3
- )
- // bolus content
- HStack {
- Image(systemName: "cross.vial.fill")
- .font(.system(size: 25))
- Spacer()
- VStack {
- Text("Bolusing")
- .font(.subheadline)
- .frame(maxWidth: .infinity, alignment: .leading)
- Text(bolusString)
- .font(.caption)
- .frame(maxWidth: .infinity, alignment: .leading)
- }
- .padding(.leading, 5)
- Spacer()
- Button { state.cancelBolus() } label: {
- Image(systemName: "xmark.app")
- .font(.system(size: 25))
- }.tint(Color.tabBar)
- .buttonStyle(.borderless)
- .accessibilityLabel("Cancel bolus")
- }
- .padding(.horizontal, 10)
- .padding(.trailing, 8)
- }
- .padding(.horizontal, 10)
- .overlay(alignment: .bottom) {
- BolusProgressBar(progress: progress)
- .padding(.horizontal, 18)
- .padding(.bottom, 1)
- }
- .clipShape(RoundedRectangle(cornerRadius: 15))
- }
- private var taskButtonLabel: some View {
- if pumpBolusLimitExceeded {
- return Text("Max Bolus of \(state.maxBolus.description) U Exceeded")
- } else if externalBolusLimitExceeded {
- return Text("Max External Bolus of \(state.maxExternal.description) U Exceeded")
- } else if carbLimitExceeded {
- return Text("Max Carbs of \(state.maxCarbs.description) g Exceeded")
- } else if fatLimitExceeded {
- return Text("Max Fat of \(state.maxFat.description) g Exceeded")
- } else if proteinLimitExceeded {
- return Text("Max Protein of \(state.maxProtein.description) g Exceeded")
- }
- let hasInsulin = state.amount > 0
- let hasCarbs = state.carbs > 0
- let hasFatOrProtein = state.fat > 0 || state.protein > 0
- let bolusString = state.externalInsulin ? String(localized: "External Insulin") : String(localized: "Enact Bolus")
- // Note: when a pump bolus is in progress, the row is rendered by `bolusInProgressView`
- // (Home-style card), so this label's in-progress branch is intentionally absent.
- switch (hasInsulin, hasCarbs, hasFatOrProtein) {
- case (true, true, true):
- return Text("Log Meal and \(bolusString)")
- case (true, true, false):
- return Text("Log Carbs and \(bolusString)")
- case (true, false, true):
- return Text("Log FPU and \(bolusString)")
- case (true, false, false):
- return Text(state.externalInsulin ? String(localized: "Log External Insulin") : String(localized: "Enact Bolus"))
- case (false, true, true):
- return Text("Log Meal")
- case (false, true, false):
- return Text("Log Carbs")
- case (false, false, true):
- return Text("Log FPU")
- default:
- return Text("Continue Without Treatment")
- }
- }
- private var pumpBolusLimitExceeded: Bool {
- !state.externalInsulin && state.amount > state.maxBolus
- }
- private var externalBolusLimitExceeded: Bool {
- state.externalInsulin && state.amount > state.maxExternal
- }
- private var carbLimitExceeded: Bool {
- state.carbs > state.maxCarbs
- }
- private var fatLimitExceeded: Bool {
- state.fat > state.maxFat
- }
- private var proteinLimitExceeded: Bool {
- state.protein > state.maxProtein
- }
- private var limitExceeded: Bool {
- pumpBolusLimitExceeded || externalBolusLimitExceeded || carbLimitExceeded || fatLimitExceeded || proteinLimitExceeded
- }
- private var disableTaskButton: Bool {
- (
- state.isBolusInProgress && state
- .amount > 0 && !state.externalInsulin && (state.carbs == 0 || state.fat == 0 || state.protein == 0)
- ) || state
- .addButtonPressed || limitExceeded
- }
- }
- struct DividerDouble: View {
- var body: some View {
- VStack(spacing: 2) {
- Rectangle()
- .frame(height: 1)
- .foregroundColor(.gray.opacity(0.65))
- Rectangle()
- .frame(height: 1)
- .foregroundColor(.gray.opacity(0.65))
- }
- .frame(height: 4)
- .padding(.vertical)
- }
- }
- struct DividerCustom: View {
- var body: some View {
- Rectangle()
- .frame(height: 1)
- .foregroundColor(.gray.opacity(0.65))
- .padding(.vertical)
- }
- }
- }
|