Skip to content

Commit

Permalink
Use Half Basal Target as a variable in new simplified Temp Targets
Browse files Browse the repository at this point in the history
  • Loading branch information
Jon-b-m committed Mar 18, 2023
1 parent 2c361fc commit ff21245
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ extension AddTempTarget {

func enact() {
let diff = Double(halfBasal - 100)
let lowTarget = Decimal(diff + 40 * (percentage / 100)) / (Decimal(percentage) / 100)
let multiplier = percentage - (diff * (percentage / 100))
let lowTarget = Decimal(diff + multiplier) / (Decimal(percentage) / 100)
let highTarget = lowTarget

let entry = TempTarget(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ extension AddTempTarget {
Section(
header: Text("Basal Insulin and Sensitivity ratio"),
footer: Text(
"A lower 'Half Basal Target' setting will lower the basal and raise the ISF earlier (at a lower target glucose)"
"A lower 'Half Basal Target' setting will reduce the basal and raise the ISF earlier, at a lower target glucose." +
" Your setting: \(state.halfBasal) mg/dl. Autosens.max limits the max endpoint (\(state.maxValue * 100) %)"
)
) {
VStack {
Expand All @@ -51,9 +52,8 @@ extension AddTempTarget {
"Target" +
(
state
.units == .mmolL ?
": \((Decimal(Double(state.halfBasal - 100) + 40 * (state.percentage / 100)) / (Decimal(state.percentage) / 100)).asMmolL.formatted(.number)) mmol/L" :
": \((Decimal(Double(state.halfBasal - 100) + 40 * (state.percentage / 100)) / (Decimal(state.percentage) / 100)).formatted(.number)) mg/dl"
.units == .mmolL ? ": \(computeTarget().asMmolL.formatted(.number)) mmol/L" :
": \(computeTarget().formatted(.number)) mg/dl"
)
).foregroundColor(.secondary).italic()
}
Expand Down Expand Up @@ -98,6 +98,18 @@ extension AddTempTarget {
.navigationBarItems(leading: Button("Close", action: state.hideModal))
}

func computeTarget() -> Decimal {
let ratio = min(Decimal(state.percentage / 100), state.maxValue)
let diff = Double(state.halfBasal - 100)
let multiplier = state.percentage - (diff * (state.percentage / 100))
var target = Decimal(diff + multiplier) / ratio

if (state.halfBasal + (state.halfBasal + target - 100)) <= 0 {
target = (state.halfBasal - 100 + (state.halfBasal - 100) * state.maxValue) / state.maxValue
}
return target
}

private func presetView(for preset: TempTarget) -> some View {
var low = preset.targetBottom
var high = preset.targetTop
Expand Down

0 comments on commit ff21245

Please sign in to comment.