Skip to content

Commit

Permalink
Advanced TT
Browse files Browse the repository at this point in the history
  • Loading branch information
mountrcg committed May 26, 2024
1 parent a9b65bb commit 7974cc2
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 40 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -98,17 +98,15 @@ extension AddTempTarget {
return
}
var lowTarget = low
if units == .mmolL {
lowTarget = Decimal(round(Double(lowTarget.asMgdL)))
}
let highTarget = lowTarget

if viewPercantage {
lowTarget = Decimal(round(Double(computeTarget())))
hbt = computeHBT()
saveSettings = true
}
var highTarget = lowTarget

if units == .mmolL, !viewPercantage {
lowTarget = Decimal(round(Double(lowTarget.asMgdL)))
highTarget = lowTarget
}

let entry = TempTarget(
name: newPresetName.isEmpty ? TempTarget.custom : newPresetName,
Expand Down Expand Up @@ -189,5 +187,18 @@ extension AddTempTarget {
}
return Decimal(Double(target))
}

func computeHBT() -> Double {
let ratio = Decimal(percentage / 100)
let normalTarget: Decimal = 100
var target: Decimal = low
if units == .mmolL {
target = target.asMgdL }
var hbtcalc = Decimal(hbt)
if ratio != 1 {
hbtcalc = ((2 * ratio * normalTarget) - normalTarget - (ratio * target)) / (ratio - 1)
}
return round(Double(hbtcalc))
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,53 +34,59 @@ extension AddTempTarget {
}

HStack {
Text("Experimental")
Text("Advanced TT")
Toggle(isOn: $state.viewPercantage) {}.controlSize(.mini)
Image(systemName: "figure.highintensity.intervaltraining")
Image(systemName: "fork.knife")
}

if state.viewPercantage {
Section(
header: Text("")
header: Text("TT Effect on Insulin")
) {
VStack {
Slider(
value: $state.percentage,
in: 15 ...
min(Double(state.maxValue * 100), 200),
step: 1,
onEditingChanged: { editing in
isEditing = editing
}
)
HStack {
Text(NSLocalizedString("Target", comment: ""))
Spacer()
DecimalTextField(
"0",
value: $state.low,
formatter: formatter,
cleanInput: true
)
Text(state.units.rawValue).foregroundColor(.secondary)
}

if computeSliderLow() != computeSliderHigh() {
Text("\(state.percentage.formatted(.number)) % Insulin")
.foregroundColor(isEditing ? .orange : .blue)
.font(.largeTitle)
}
// Only display target slider when not 100 %
if state.percentage != 100 {
Divider()

Slider(
value: $state.hbt,
in: 101 ... 295,
step: 1
).accentColor(.green)

HStack {
Text(
(
state
.units == .mmolL ?
"\(state.computeTarget().asMmolL.formatted(.number.grouping(.never).rounded().precision(.fractionLength(1)))) mmol/L" :
"\(state.computeTarget().formatted(.number.grouping(.never).rounded().precision(.fractionLength(0)))) mg/dl"
)
+ NSLocalizedString(" Target Glucose", comment: "")
)
.foregroundColor(.green)
}
value: $state.percentage,
in: computeSliderLow() ... computeSliderHigh(),
step: 5
) {}
minimumValueLabel: { Text("\(computeSliderLow(), specifier: "%.0f")%") }
maximumValueLabel: { Text("\(computeSliderHigh(), specifier: "%.0f")%") }
onEditingChanged: { editing in
isEditing = editing }
Divider()
Text(
state
.units == .mgdL ?
"Half Basal Exercise Target at: \(state.computeHBT().formatted(.number)) mg/dl" :
"Half Basal Exercise Target at: \(state.computeHBT().asMmolL.formatted(.number.grouping(.never).rounded().precision(.fractionLength(1)))) mmol/L"
)
.foregroundColor(.secondary)
.font(.caption).italic()
} else {
Text(
"You have not enabled the proper Preferences to change sensitivity with chosen TempTarget. Verify Autosens Max > 1 & lowTT lowers Sens is on for lowTT's. For high TTs check highTT raises Sens is on (or Exercise Mode)!"
)
// .foregroundColor(.loopRed)
.font(.caption).italic()
.fixedSize(horizontal: false, vertical: true)
.multilineTextAlignment(.leading)
}
}
}
Expand Down Expand Up @@ -206,5 +212,29 @@ extension AddTempTarget {
}
}
}

func computeSliderLow() -> Double {
var minSens: Double = 15
var target = state.low
if state.units == .mmolL {
target = Decimal(round(Double(state.low.asMgdL))) }
if target == 0 { return minSens }
if target < 100 ||
(
!state.settingsManager.preferences.highTemptargetRaisesSensitivity && !state.settingsManager.preferences
.exerciseMode
) { minSens = 100 }
return minSens
}

func computeSliderHigh() -> Double {
var maxSens = Double(state.maxValue * 100)
var target = state.low
if target == 0 { return maxSens }
if state.units == .mmolL {
target = Decimal(round(Double(state.low.asMgdL))) }
if target > 100 || !state.settingsManager.preferences.lowTemptargetLowersSensitivity { maxSens = 100 }
return maxSens
}
}
}

0 comments on commit 7974cc2

Please sign in to comment.