| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398 |
- import SwiftUI
- import Swinject
- extension Bolus {
- // alternative bolus calc
- struct AlternativeBolusCalcRootView: BaseView {
- let resolver: Resolver
- let waitForSuggestion: Bool
- @ObservedObject var state: StateModel
- @State private var showInfo = false
- @State var insulinCalculated: Decimal = 0
- @Environment(\.colorScheme) var colorScheme
- private var formatter: NumberFormatter {
- let formatter = NumberFormatter()
- formatter.numberStyle = .decimal
- formatter.maximumFractionDigits = 2
- return formatter
- }
- private var fractionDigits: Int {
- if state.units == .mmolL {
- return 1
- } else { return 0 }
- }
- var body: some View {
- Form {
- Section {
- HStack {
- Text("Glucose")
- DecimalTextField(
- "0",
- value: Binding(
- get: {
- if state.units == .mmolL {
- return state.currentBG * 0.0555
- } else {
- return state.currentBG
- }
- },
- set: { newValue in
- if state.units == .mmolL {
- state.currentBG = newValue * 0.0555
- } else {
- state.currentBG = newValue
- }
- }
- ),
- formatter: formatter,
- autofocus: false,
- cleanInput: true
- )
- .onChange(of: state.currentBG) { newValue in
- if newValue > 500 {
- state.currentBG = 500 // ensure that user can not input more than 500 mg/dL
- }
- insulinCalculated = state.calculateInsulin()
- }
- Text(state.units.rawValue)
- .foregroundColor(.secondary)
- }
- .contentShape(Rectangle())
- HStack {
- Button(action: {
- showInfo.toggle()
- insulinCalculated = state.calculateInsulin()
- }, label: {
- Image(systemName: "info.circle")
- Text("Calculations")
- })
- .foregroundStyle(.blue)
- .font(.footnote)
- .buttonStyle(PlainButtonStyle())
- .frame(maxWidth: .infinity, alignment: .leading)
- if state.fattyMeals {
- Spacer()
- Toggle(isOn: $state.useFattyMealCorrectionFactor) {
- Text("Fatty Meal")
- }
- .toggleStyle(CheckboxToggleStyle())
- .font(.footnote)
- .onChange(of: state.useFattyMealCorrectionFactor) { _ in
- insulinCalculated = state.calculateInsulin()
- }
- }
- }
- }
- header: { Text("Values") }
- Section {
- HStack {
- Text("Recommended Bolus")
- Spacer()
- Text(
- formatter
- .string(from: Double(insulinCalculated) as NSNumber)!
- )
- let unit = NSLocalizedString(
- " U",
- comment: "Unit in number of units delivered (keep the space character!)"
- )
- Text(unit).foregroundColor(.secondary)
- }.contentShape(Rectangle())
- .onTapGesture {
- state.amount = insulinCalculated
- }
- if !state.waitForSuggestion {
- HStack {
- Text("Bolus")
- Spacer()
- DecimalTextField(
- "0",
- value: $state.amount,
- formatter: formatter,
- autofocus: false,
- cleanInput: true
- )
- Text(!(state.amount > state.maxBolus) ? "U" : "😵").foregroundColor(.secondary)
- }
- }
- }
- header: { Text("Bolus") }
- Section {
- Button(action: {
- state.add()
- }) {
- Text(!(state.amount > state.maxBolus) ? "Enact bolus" : "Max Bolus exceeded!")
- .frame(maxWidth: .infinity, alignment: .center)
- }
- .disabled(
- state.amount <= 0 || state.amount > state.maxBolus
- )
- }
- .onAppear {
- configureView {
- state.waitForSuggestionInitial = waitForSuggestion
- state.waitForSuggestion = waitForSuggestion
- }
- }
- .navigationTitle("Enact Bolus")
- .navigationBarTitleDisplayMode(.inline)
- .navigationBarItems(leading: Button("Close", action: state.hideModal))
- }
- .blur(radius: showInfo ? 3 : 0)
- .popup(isPresented: showInfo) {
- bolusInfoAlternativeCalculator
- }
- }
- // calculation showed in popup
- var bolusInfoAlternativeCalculator: some View {
- let unit = NSLocalizedString(
- " U",
- comment: "Unit in number of units delivered (keep the space character!)"
- )
- return VStack {
- VStack {
- VStack {
- HStack {
- Text("Calculations")
- .font(.title3)
- .fontWeight(.semibold)
- Spacer()
- }
- .padding(.vertical, 10)
- HStack {
- Text("Carb Ratio")
- .foregroundColor(.secondary)
- Spacer()
- Text(state.carbRatio.formatted())
- Text(NSLocalizedString(" g/U", comment: " grams per Unit"))
- .foregroundColor(.secondary)
- }
- HStack {
- Text("ISF")
- .foregroundColor(.secondary)
- Spacer()
- let isf = state.isf
- Text(isf.formatted())
- Text(state.units.rawValue + NSLocalizedString("/U", comment: "/Insulin unit"))
- .foregroundColor(.secondary)
- }
- HStack {
- Text("Target Glucose")
- .foregroundColor(.secondary)
- Spacer()
- let target = state.units == .mmolL ? state.target.asMmolL : state.target
- Text(target.formatted(.number.grouping(.never).rounded().precision(.fractionLength(fractionDigits))))
- Text(state.units.rawValue)
- .foregroundColor(.secondary)
- }
- HStack {
- Text("Basal")
- .foregroundColor(.secondary)
- Spacer()
- let basal = state.basal
- Text(basal.formatted())
- Text(NSLocalizedString(" U/h", comment: " Units per hour"))
- .foregroundColor(.secondary)
- }
- HStack {
- Text("Fraction")
- .foregroundColor(.secondary)
- Spacer()
- let fraction = state.fraction
- Text(fraction.formatted())
- }
- if state.useFattyMealCorrectionFactor {
- HStack {
- Text("Fatty Meal Factor")
- .foregroundColor(.orange)
- Spacer()
- let fraction = state.fattyMealFactor
- Text(fraction.formatted())
- .foregroundColor(.orange)
- }
- }
- }
- .padding()
- VStack {
- HStack {
- Text("Glucose")
- .foregroundColor(.secondary)
- Spacer()
- let glucose = state.units == .mmolL ? state.currentBG.asMmolL : state.currentBG
- Text(glucose.formatted(.number.grouping(.never).rounded().precision(.fractionLength(fractionDigits))))
- Text(state.units.rawValue)
- .foregroundColor(.secondary)
- Spacer()
- Image(systemName: "arrow.right")
- Spacer()
- let targetDifferenceInsulin = state.targetDifferenceInsulin
- // rounding
- let targetDifferenceInsulinAsDouble = NSDecimalNumber(decimal: targetDifferenceInsulin).doubleValue
- let roundedTargetDifferenceInsulin = Decimal(round(100 * targetDifferenceInsulinAsDouble) / 100)
- Text(roundedTargetDifferenceInsulin.formatted())
- Text(unit)
- .foregroundColor(.secondary)
- }
- HStack {
- Text("IOB")
- .foregroundColor(.secondary)
- Spacer()
- let iob = state.iob
- // rounding
- let iobAsDouble = NSDecimalNumber(decimal: iob).doubleValue
- let roundedIob = Decimal(round(100 * iobAsDouble) / 100)
- Text(roundedIob.formatted())
- Text(unit)
- .foregroundColor(.secondary)
- Spacer()
- Image(systemName: "arrow.right")
- Spacer()
- let iobCalc = state.iobInsulinReduction
- // rounding
- let iobCalcAsDouble = NSDecimalNumber(decimal: iobCalc).doubleValue
- let roundedIobCalc = Decimal(round(100 * iobCalcAsDouble) / 100)
- Text(roundedIobCalc.formatted())
- Text(unit).foregroundColor(.secondary)
- }
- HStack {
- Text("Trend")
- .foregroundColor(.secondary)
- Spacer()
- let trend = state.units == .mmolL ? state.deltaBG.asMmolL : state.deltaBG
- Text(trend.formatted(.number.grouping(.never).rounded().precision(.fractionLength(fractionDigits))))
- Text(state.units.rawValue).foregroundColor(.secondary)
- Spacer()
- Image(systemName: "arrow.right")
- Spacer()
- let trendInsulin = state.fifteenMinInsulin
- // rounding
- let trendInsulinAsDouble = NSDecimalNumber(decimal: trendInsulin).doubleValue
- let roundedTrendInsulin = Decimal(round(100 * trendInsulinAsDouble) / 100)
- Text(roundedTrendInsulin.formatted())
- Text(unit)
- .foregroundColor(.secondary)
- }
- HStack {
- Text("COB")
- .foregroundColor(.secondary)
- Spacer()
- let cob = state.cob
- Text(cob.formatted())
- let unitGrams = NSLocalizedString(" g", comment: "grams")
- Text(unitGrams).foregroundColor(.secondary)
- Spacer()
- Image(systemName: "arrow.right")
- Spacer()
- let insulinCob = state.wholeCobInsulin
- // rounding
- let insulinCobAsDouble = NSDecimalNumber(decimal: insulinCob).doubleValue
- let roundedInsulinCob = Decimal(round(100 * insulinCobAsDouble) / 100)
- Text(roundedInsulinCob.formatted())
- Text(unit)
- .foregroundColor(.secondary)
- }
- }
- .padding()
- Divider()
- .fontWeight(.bold)
- HStack {
- Text("Full Bolus")
- .foregroundColor(.secondary)
- Spacer()
- let insulin = state.roundedWholeCalc
- Text(insulin.formatted()).foregroundStyle(state.roundedWholeCalc < 0 ? Color.loopRed : Color.primary)
- Text(unit)
- .foregroundColor(.secondary)
- }
- .padding()
- Divider()
- .fontWeight(.bold)
- HStack {
- Text("Result")
- .fontWeight(.bold)
- Spacer()
- let fraction = state.fraction
- Text(fraction.formatted())
- Text(" x ")
- .foregroundColor(.secondary)
- // if fatty meal is chosen
- if state.useFattyMealCorrectionFactor {
- let fattyMealFactor = state.fattyMealFactor
- Text(fattyMealFactor.formatted())
- .foregroundColor(.orange)
- Text(" x ")
- .foregroundColor(.secondary)
- }
- let insulin = state.roundedWholeCalc
- Text(insulin.formatted()).foregroundStyle(state.roundedWholeCalc < 0 ? Color.loopRed : Color.primary)
- Text(unit)
- .foregroundColor(.secondary)
- Text(" = ")
- .foregroundColor(.secondary)
- let result = state.insulinCalculated
- // rounding
- let resultAsDouble = NSDecimalNumber(decimal: result).doubleValue
- let roundedResult = Decimal(round(100 * resultAsDouble) / 100)
- Text(roundedResult.formatted())
- .fontWeight(.bold)
- .font(.system(size: 16))
- .foregroundColor(.blue)
- Text(unit)
- .foregroundColor(.secondary)
- }
- .padding()
- }
- .padding(.top, 10)
- .padding(.bottom, 15)
- // Hide button
- VStack {
- Button { showInfo = false }
- label: {
- Text("OK")
- }
- .frame(maxWidth: .infinity, alignment: .center)
- .font(.system(size: 16))
- .fontWeight(.semibold)
- .foregroundColor(.blue)
- }
- .padding(.bottom, 20)
- }
- .font(.footnote)
- .background(
- RoundedRectangle(cornerRadius: 10, style: .continuous)
- .fill(Color(colorScheme == .dark ? UIColor.systemGray4 : UIColor.systemGray4).opacity(0.9))
- )
- }
- }
- }
|