|
@@ -11,9 +11,12 @@ struct EditTempTargetForm: View {
|
|
|
@State private var target: Decimal
|
|
@State private var target: Decimal
|
|
|
@State private var duration: Decimal
|
|
@State private var duration: Decimal
|
|
|
@State private var date: Date
|
|
@State private var date: Date
|
|
|
|
|
+ @State private var halfBasalTarget: Decimal
|
|
|
|
|
+ @State private var percentage: Decimal
|
|
|
|
|
|
|
|
@State private var hasChanges = false
|
|
@State private var hasChanges = false
|
|
|
@State private var showAlert = false
|
|
@State private var showAlert = false
|
|
|
|
|
+ @State private var isUsingSlider = false
|
|
|
|
|
|
|
|
init(tempTargetToEdit: TempTargetStored, state: OverrideConfig.StateModel) {
|
|
init(tempTargetToEdit: TempTargetStored, state: OverrideConfig.StateModel) {
|
|
|
tempTarget = tempTargetToEdit
|
|
tempTarget = tempTargetToEdit
|
|
@@ -22,6 +25,16 @@ struct EditTempTargetForm: View {
|
|
|
_target = State(initialValue: tempTargetToEdit.target?.decimalValue ?? 0)
|
|
_target = State(initialValue: tempTargetToEdit.target?.decimalValue ?? 0)
|
|
|
_duration = State(initialValue: tempTargetToEdit.duration?.decimalValue ?? 0)
|
|
_duration = State(initialValue: tempTargetToEdit.duration?.decimalValue ?? 0)
|
|
|
_date = State(initialValue: tempTargetToEdit.date ?? Date())
|
|
_date = State(initialValue: tempTargetToEdit.date ?? Date())
|
|
|
|
|
+ _halfBasalTarget = State(initialValue: tempTargetToEdit.halfBasalTarget?.decimalValue ?? 160)
|
|
|
|
|
+
|
|
|
|
|
+ let normalTarget: Decimal = 100
|
|
|
|
|
+ if let halfBasal = tempTargetToEdit.halfBasalTarget?.decimalValue {
|
|
|
|
|
+ let target = state.units == .mgdL ? state.tempTargetTarget.asMgdL : state.tempTargetTarget
|
|
|
|
|
+ let ratio = (halfBasal - normalTarget + (normalTarget * target) / 2) / normalTarget
|
|
|
|
|
+ _percentage = State(initialValue: ratio * 100)
|
|
|
|
|
+ } else {
|
|
|
|
|
+ _percentage = State(initialValue: 100)
|
|
|
|
|
+ }
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
var color: LinearGradient {
|
|
var color: LinearGradient {
|
|
@@ -100,6 +113,63 @@ struct EditTempTargetForm: View {
|
|
|
}.listRowBackground(Color.chart)
|
|
}.listRowBackground(Color.chart)
|
|
|
|
|
|
|
|
Section {
|
|
Section {
|
|
|
|
|
+ VStack {
|
|
|
|
|
+ VStack {
|
|
|
|
|
+ Text("\(state.percentage.formatted(.number)) % Insulin")
|
|
|
|
|
+ .foregroundColor(isUsingSlider ? .orange : Color.tabBar)
|
|
|
|
|
+ .font(.largeTitle)
|
|
|
|
|
+
|
|
|
|
|
+ Slider(value: Binding(
|
|
|
|
|
+ get: {
|
|
|
|
|
+ Double(truncating: percentage as NSNumber) // Decimal in Double umwandeln
|
|
|
|
|
+ },
|
|
|
|
|
+ set: { newValue in
|
|
|
|
|
+ percentage = Decimal(newValue) // Den neuen Slider-Wert speichern
|
|
|
|
|
+ hasChanges = true
|
|
|
|
|
+
|
|
|
|
|
+ // Berechne das halfBasalTarget basierend auf dem neuen percentage-Wert
|
|
|
|
|
+ let ratio = Decimal(Int(percentage) / 100)
|
|
|
|
|
+ let normalTarget: Decimal = 100
|
|
|
|
|
+ var target: Decimal = state.tempTargetTarget
|
|
|
|
|
+ if state.units == .mmolL {
|
|
|
|
|
+ target = target.asMgdL
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ if ratio != 1 {
|
|
|
|
|
+ let hbtcalc = ((2 * ratio * normalTarget) - normalTarget - (ratio * target)) / (ratio - 1)
|
|
|
|
|
+ halfBasalTarget = hbtcalc
|
|
|
|
|
+ } else {
|
|
|
|
|
+ halfBasalTarget = normalTarget
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ ), in: Double(state.computeSliderLow()) ... Double(state.computeSliderHigh()), step: 5) {}
|
|
|
|
|
+ minimumValueLabel: {
|
|
|
|
|
+ Text("\(state.computeSliderLow(), specifier: "%.0f")%")
|
|
|
|
|
+ }
|
|
|
|
|
+ maximumValueLabel: {
|
|
|
|
|
+ Text("\(state.computeSliderHigh(), specifier: "%.0f")%")
|
|
|
|
|
+ }
|
|
|
|
|
+ onEditingChanged: { editing in
|
|
|
|
|
+ isUsingSlider = editing
|
|
|
|
|
+ state.halfBasalTarget = Decimal(state.computeHalfBasalTarget())
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ Divider()
|
|
|
|
|
+ Text(
|
|
|
|
|
+ state
|
|
|
|
|
+ .units == .mgdL ?
|
|
|
|
|
+ "Half Basal Exercise Target at: \(state.halfBasalTarget.formatted(.number.precision(.fractionLength(0)))) mg/dl" :
|
|
|
|
|
+ "Half Basal Exercise Target at: \(state.halfBasalTarget.asMmolL.formatted(.number.grouping(.never).rounded().precision(.fractionLength(1)))) mmol/L"
|
|
|
|
|
+ )
|
|
|
|
|
+ .foregroundColor(.secondary)
|
|
|
|
|
+ .font(.caption).italic()
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ } header: {
|
|
|
|
|
+ Text("% Insulin")
|
|
|
|
|
+ }.listRowBackground(Color.chart)
|
|
|
|
|
+
|
|
|
|
|
+ Section {
|
|
|
HStack {
|
|
HStack {
|
|
|
Text("Target")
|
|
Text("Target")
|
|
|
Spacer()
|
|
Spacer()
|
|
@@ -184,6 +254,7 @@ struct EditTempTargetForm: View {
|
|
|
tempTarget.duration = NSDecimalNumber(decimal: duration)
|
|
tempTarget.duration = NSDecimalNumber(decimal: duration)
|
|
|
tempTarget.date = date
|
|
tempTarget.date = date
|
|
|
tempTarget.isUploadedToNS = false
|
|
tempTarget.isUploadedToNS = false
|
|
|
|
|
+ tempTarget.halfBasalTarget = NSDecimalNumber(decimal: halfBasalTarget)
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
private func resetValues() {
|
|
private func resetValues() {
|