Skip to content

Commit

Permalink
HBT Part two
Browse files Browse the repository at this point in the history
  • Loading branch information
Jon-b-m committed Mar 25, 2023
1 parent b755391 commit 56209dc
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 22 deletions.
4 changes: 2 additions & 2 deletions FreeAPS.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -2874,7 +2874,7 @@
buildSettings = {
APP_DISPLAY_NAME = "$(APP_DISPLAY_NAME)";
APP_GROUP_ID = "$(APP_GROUP_ID)";
ASSETCATALOG_COMPILER_APPICON_NAME = "$(APP_ICON)";
ASSETCATALOG_COMPILER_APPICON_NAME = iAPS_Loop_Text;
ASSETCATALOG_COMPILER_GLOBAL_ACCENT_COLOR_NAME = AccentColor;
BUNDLE_IDENTIFIER = "$(BUNDLE_IDENTIFIER)";
CODE_SIGN_ENTITLEMENTS = FreeAPS/Resources/FreeAPS.entitlements;
Expand Down Expand Up @@ -2941,7 +2941,7 @@
buildSettings = {
ALWAYS_EMBED_SWIFT_STANDARD_LIBRARIES = YES;
APP_DISPLAY_NAME = "$(APP_DISPLAY_NAME)";
ASSETCATALOG_COMPILER_APPICON_NAME = "$(APP_ICON)";
ASSETCATALOG_COMPILER_APPICON_NAME = iAPS_Loop_Text;
ASSETCATALOG_COMPILER_GLOBAL_ACCENT_COLOR_NAME = AccentColor;
BUNDLE_IDENTIFIER = "$(BUNDLE_IDENTIFIER)";
CLANG_CXX_LANGUAGE_STANDARD = "gnu++17";
Expand Down
2 changes: 1 addition & 1 deletion FreeAPS/Resources/javascript/bundle/determine-basal.js

Large diffs are not rendered by default.

10 changes: 7 additions & 3 deletions FreeAPS/Sources/APS/APSManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -722,6 +722,7 @@ final class BaseAPSManager: APSManager, Injectable {
var duration: Decimal = 0
var unlimited: Bool = false
var newDuration: Decimal = 0
var hbtSetting: Decimal = 160

if currentTDD > 0 {
let tenDaysAgo = Date().addingTimeInterval(-10.days.timeInterval)
Expand Down Expand Up @@ -774,9 +775,9 @@ final class BaseAPSManager: APSManager, Injectable {

isPercentageEnabled = booleanArray.first?.enabled ?? false
useOverride = overrideArray.first?.enabled ?? false

overridePercentage = Decimal(overrideArray.first?.percentage ?? 100)
unlimited = overrideArray.first?.indefinite ?? true
hbtSetting = Decimal(booleanArray.first?.hbt ?? 160)

if useOverride {
duration = (overrideArray.first?.duration ?? 0) as Decimal
Expand Down Expand Up @@ -808,7 +809,8 @@ final class BaseAPSManager: APSManager, Injectable {
overridePercentage: overridePercentage,
useOverride: useOverride,
duration: duration,
unlimited: unlimited
unlimited: unlimited,
hbt: hbtSetting
)
storage.save(averages, as: OpenAPS.Monitor.oref2_variables)

Expand All @@ -832,6 +834,7 @@ final class BaseAPSManager: APSManager, Injectable {
useOverride = overrideArray.first?.enabled ?? false
overridePercentage = Decimal(overrideArray.first?.percentage ?? 100)
unlimited = overrideArray.first?.indefinite ?? true
hbtSetting = Decimal(booleanArray.first?.hbt ?? 160)

if useOverride {
duration = (overrideArray.first?.duration ?? 0) as Decimal
Expand Down Expand Up @@ -863,7 +866,8 @@ final class BaseAPSManager: APSManager, Injectable {
overridePercentage: overridePercentage,
useOverride: useOverride,
duration: duration,
unlimited: unlimited
unlimited: unlimited,
hbt: hbtSetting
)
storage.save(averages, as: OpenAPS.Monitor.oref2_variables)
}
Expand Down
6 changes: 5 additions & 1 deletion FreeAPS/Sources/Models/Oref2_variables.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ struct Oref2_variables: JSON, Equatable {
var useOverride: Bool
var duration: Decimal
var unlimited: Bool
var hbt: Decimal

init(
average_total_data: Decimal,
Expand All @@ -20,7 +21,8 @@ struct Oref2_variables: JSON, Equatable {
overridePercentage: Decimal,
useOverride: Bool,
duration: Decimal,
unlimited: Bool
unlimited: Bool,
hbt: Decimal
) {
self.average_total_data = average_total_data
self.weightedAverage = weightedAverage
Expand All @@ -31,6 +33,7 @@ struct Oref2_variables: JSON, Equatable {
self.useOverride = useOverride
self.duration = duration
self.unlimited = unlimited
self.hbt = hbt
}
}

Expand All @@ -45,5 +48,6 @@ extension Oref2_variables {
case useOverride
case duration
case unlimited
case hbt
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,25 +14,23 @@ extension AddTempTarget {
@Published var presets: [TempTarget] = []
@Published var percentage = 100.0
@Published var maxValue: Decimal = 1.2
@Published var halfBasal: Decimal = 160
@Published var viewPercantage = false
@Published var hbt: Double = 160
@Published var saveSettings: Bool = false

private(set) var units: GlucoseUnits = .mmolL

override func subscribe() {
units = settingsManager.settings.units
presets = storage.presets()
maxValue = settingsManager.preferences.autosensMax
halfBasal = settingsManager.preferences.halfBasalExerciseTarget
}

func enact() {
var lowTarget = low

if viewPercantage {
var ratio = Decimal(percentage / 100)
// let hB = halfBasal
let hB = Decimal(hbt)
let c = hB - 100
var target = (c / ratio) - c + 100
Expand All @@ -43,6 +41,7 @@ extension AddTempTarget {
}
lowTarget = target
lowTarget = Decimal(round(Double(target)))
saveSettings = true
}
var highTarget = lowTarget

Expand Down Expand Up @@ -74,7 +73,6 @@ extension AddTempTarget {

if viewPercantage {
var ratio = Decimal(percentage / 100)
// let hB = halfBasal
let hB = Decimal(hbt)
let c = hB - 100
var target = (c / ratio) - c + 100
Expand All @@ -85,6 +83,7 @@ extension AddTempTarget {
}
lowTarget = target
lowTarget = Decimal(round(Double(target)))
saveSettings = true
}
var highTarget = lowTarget

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ extension AddTempTarget {

@FetchRequest(
entity: ViewPercentage.entity(),
sortDescriptors: [NSSortDescriptor(key: "enabled", ascending: false)]
sortDescriptors: [NSSortDescriptor(key: "date", ascending: false)]
) var isEnabledArray: FetchedResults<ViewPercentage>

@Environment(\.managedObjectContext) var moc
Expand All @@ -36,12 +36,17 @@ extension AddTempTarget {
}

Toggle(isOn: $state.viewPercantage) {
Text("Exercise / Pre Meal Slider")
HStack {
Text("Use Slider for")
Image(systemName: "figure.highintensity.intervaltraining")
Text("or")
Image(systemName: "fork.knife")
}
}

if state.viewPercantage {
Section(
header: Text("Effect of new TT on Basal and inversely on Sensitivity")
header: Text("Total Effect of TT on insulin (not CR)")
) {
VStack {
Slider(
Expand All @@ -65,16 +70,20 @@ extension AddTempTarget {
": \(computeTarget().asMmolL.formatted(.number.grouping(.never).rounded().precision(.fractionLength(1)))) mmol/L" :
": \(computeTarget().formatted(.number.grouping(.never).rounded().precision(.fractionLength(0)))) mg/dl"
)
).foregroundColor(.primary).italic()
) // .foregroundColor(.primary).italic()

Slider(
value: $state.hbt,
in: 120 ... 180,
step: 1
)
Text("Half Basal target setting: \(state.hbt.formatted(.number)) mg/dl")
.foregroundColor(.green)
.font(.caption)
Text(
state
.units == .mgdL ? "Half normal Basal at: \(state.hbt.formatted(.number)) mg/dl" :
"Half normal Basal at: \(state.hbt.asMmolL.formatted(.number.grouping(.never).rounded().precision(.fractionLength(1)))) mmol/L"
)
.foregroundColor(.green)
.font(.caption).italic()
}
}
} else {
Expand Down Expand Up @@ -133,13 +142,13 @@ extension AddTempTarget {
}
.onAppear {
configureView()
state.hbt = Double(state.halfBasal)
state.hbt = isEnabledArray.first?.hbt ?? 160
}
.navigationTitle("Enact Temp Target")
.navigationBarTitleDisplayMode(.automatic)
.navigationBarItems(leading: Button("Close", action: state.hideModal))
.onDisappear {
if state.viewPercantage {
if state.viewPercantage, state.saveSettings {
let isEnabledMoc = ViewPercentage(context: moc)
isEnabledMoc.enabled = true
isEnabledMoc.date = Date()
Expand All @@ -157,8 +166,6 @@ extension AddTempTarget {

func computeTarget() -> Decimal {
var ratio = Decimal(state.percentage / 100)

let hB = state.halfBasal
let c = Decimal(state.hbt - 100)
var target = (c / ratio) - c + 100

Expand Down

0 comments on commit 56209dc

Please sign in to comment.