Skip to content

Commit

Permalink
autoISF Config
Browse files Browse the repository at this point in the history
  • Loading branch information
mountrcg committed Nov 25, 2023
1 parent 923e7b2 commit 3cf4b4d
Show file tree
Hide file tree
Showing 3 changed files with 166 additions and 449 deletions.
76 changes: 35 additions & 41 deletions FreeAPS/Sources/Modules/AutoISF/AutoISFConfStateModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,32 +8,16 @@ extension AutoISFConf {

@Published var unit: GlucoseUnits = .mmolL
@Published var sections: [FieldSection] = []
@Published var autoisf: Bool = false

override func subscribe() {
unit = settingsManager.settings.units
preferences = provider.preferences
autoisf = settings.preferences.autoisf

// MARK: - autoISF fields

let autoisfConfig = [
Field(
displayName: "Enable autoISF",
type: .boolean(keypath: \.autoisf),
infoText: NSLocalizedString(
"Defaults to false. Adapt ISF when glucose is stuck at high levels, only works without COB.\n\nRead up on:\nhttps://github.com/ga-zelle/autoISF/tree/2.8.2",
comment: "Enable autoISF"
),
settable: self
),
Field(
displayName: "autoISF IOB Threshold",
type: .decimal(keypath: \.iobThreshold),
infoText: NSLocalizedString(
"Safety setting: Amount of IOB that if surpassed will prevent any further SMB's being administered. Default is 0, which disables the IOB threshold for SMB's. Advisable to set to 70% of maxIOB, can be meal dependant.",
comment: "autoISF IOB threshold"
),
settable: self
),
Field(
displayName: "Temp Targets toggle SMB for autoISF",
type: .boolean(keypath: \.enableSMBEvenOnOddOff),
Expand Down Expand Up @@ -73,6 +57,15 @@ extension AutoISFConf {
),
settable: self
),
Field(
displayName: "autoISF IOB Threshold",
type: .decimal(keypath: \.iobThreshold),
infoText: NSLocalizedString(
"Safety setting: Amount of IOB that if surpassed will prevent any further SMB's being administered. Default is 0, which disables the IOB threshold for SMB's. Advisable to set to 70% of maxIOB, can be meal dependant.",
comment: "autoISF IOB threshold"
),
settable: self
),
Field(
displayName: "autoISF Max",
type: .decimal(keypath: \.autoISFmax),
Expand Down Expand Up @@ -321,63 +314,64 @@ extension AutoISFConf {

sections = [
FieldSection(
displayName: NSLocalizedString("AutoISF main config", comment: "AutoISF main config"), fields: autoisfConfig
displayName: NSLocalizedString("Target Control", comment: "AutoISF control via Targets"),
fields: autoisfConfig
),
FieldSection(
displayName: NSLocalizedString(
"autoISF toggles & general Settings",
"Toggles & general Settings",
comment: "Switch on/off experimental stuff"
),
fields: xpmToogles
),
FieldSection(
displayName: NSLocalizedString(
"dura_ISF settings",
comment: "Experimental settings for high BG plateau based autoISF2.0"
"SMB Delivery Ratio settings",
comment: "Experimental settings for SMB increases autoISF 2.0"
),
fields: xpmDuraISF
fields: xpmSMB
),
FieldSection(
displayName: NSLocalizedString(
"bg_ISF settings",
comment: "Experimental settings for BG level based autoISF2.1"
"B30 settings",
comment: "AIMI B30 settings"
),
fields: xpmBGISF
fields: xpmB30
),
FieldSection(
displayName: NSLocalizedString(
"delta_ISF settings",
comment: "Experimental settings for BG delta based autoISF2.1"
"Acce-ISF settings",
comment: "Experimental settings for acceleration based autoISF 2.2"
),
fields: xpmDeltaISF
fields: xpmAcceISF
),
FieldSection(
displayName: NSLocalizedString(
"acce_ISF settings",
comment: "Experimental settings for acceleration based autoISF 2.2"
"BG-ISF settings",
comment: "Experimental settings for BG level based autoISF2.1"
),
fields: xpmAcceISF
fields: xpmBGISF
),
FieldSection(
displayName: NSLocalizedString(
"pp_ISF settings",
comment: "Experimental settings for postprandial based autoISF 2.2"
"Dura-ISF settings",
comment: "Experimental settings for high BG plateau based autoISF2.0"
),
fields: xpmPostPrandial
fields: xpmDuraISF
),
FieldSection(
displayName: NSLocalizedString(
"SMB Delivery Ratio settings",
comment: "Experimental settings for SMB increases autoISF 2.0"
"PP-ISF settings",
comment: "Experimental settings for postprandial based autoISF 2.2"
),
fields: xpmSMB
fields: xpmPostPrandial
),
FieldSection(
displayName: NSLocalizedString(
"B30 settings",
comment: "AIMI B30 settings"
"Delta-ISF settings",
comment: "Experimental settings for BG delta based autoISF2.1"
),
fields: xpmB30
fields: xpmDeltaISF
)
]
}
Expand Down
184 changes: 130 additions & 54 deletions FreeAPS/Sources/Modules/AutoISF/View/AutoISFConfRootView.swift
Original file line number Diff line number Diff line change
@@ -1,6 +1,61 @@
import SwiftUI
import Swinject

struct BulletList: View {
var withBorder: Bool = false
var toInfinity: Bool = true
var alignLeft: Bool = true
var borderWidth: CGFloat {
withBorder ? 1 : 0
}

var textFrameMaxWidth: CGFloat? {
toInfinity ? .infinity : nil
}

var textFrameAlignment: Alignment {
alignLeft ? .leading : .center
}

var listItems: [String]
var listItemSpacing: CGFloat? = nil
var bullet: String = ""
var bulletWidth: CGFloat? = nil
var bulletAlignment: Alignment = .leading

var body: some View {
VStack(
alignment: .leading,
spacing: listItemSpacing
) {
ForEach(listItems, id: \.self) { data in
HStack(alignment: .top) {
Text(bullet)
.frame(
width: bulletWidth,
alignment: bulletAlignment
)
.border(
Color.blue,
width: borderWidth
)
Text(data)
.frame(
maxWidth: textFrameMaxWidth,
alignment: textFrameAlignment
)
.border(
Color.orange,
width: borderWidth
)
}
}
}
.padding(2)
.border(.green, width: borderWidth)
}
}

extension AutoISFConf {
struct RootView: BaseView {
let resolver: Resolver
Expand All @@ -16,71 +71,77 @@ extension AutoISFConf {

var body: some View {
Form {
ForEach(state.sections.indexed(), id: \.1.id) { sectionIndex, section in
Section(header: Text(section.displayName)) {
ForEach(section.fields.indexed(), id: \.1.id) { fieldIndex, field in
HStack {
switch field.type {
case .boolean:
ZStack {
Button("", action: {
infoButtonPressed = InfoText(
description: field.infoText,
oref0Variable: field.displayName
)
})
Toggle(isOn: self.$state.sections[sectionIndex].fields[fieldIndex].boolValue) {
Section {
VStack(alignment: .leading, spacing: 15) {
HStack {
Toggle("Activate autoISF", isOn: $state.autoisf)
}
.padding(.bottom, 2)
if !state.autoisf {
VStack(alignment: .leading) {
Text(
"autoISF allows to adapt the insulin sensitivity factor (ISF) in the following scenarios of glucose behaviour:"
)
BulletList(
listItems:
[
"accelerating/decelerating blood glucose",
"blood glucose levels according to a predefined polygon, like a Sigmoid",
"postprandial (after meal) glucose rise",
"blood glucose plateaus above target"
],
listItemSpacing: 10
)
}
// .padding(10)
Text("It can also adapt SMB delivery settings.")
Text("Read up on it at:\nhttps://github.com/ga-zelle/autoISF/tree/2.8.2")
}
}
} header: { Text("Enable") }
if state.autoisf {
ForEach(state.sections.indexed(), id: \.1.id) { sectionIndex, section in
Section(header: Text(section.displayName)) {
ForEach(section.fields.indexed(), id: \.1.id) { fieldIndex, field in
HStack {
switch field.type {
case .boolean:
ZStack {
Button("", action: {
infoButtonPressed = InfoText(
description: field.infoText,
oref0Variable: field.displayName
)
})
Toggle(isOn: self.$state.sections[sectionIndex].fields[fieldIndex].boolValue) {
Text(field.displayName)
}
}
case .decimal:
ZStack {
Button("", action: {
infoButtonPressed = InfoText(
description: field.infoText,
oref0Variable: field.displayName
)
})
Text(field.displayName)
}
DecimalTextField(
"0",
value: self.$state.sections[sectionIndex].fields[fieldIndex].decimalValue,
formatter: formatter
)
}
case .decimal:
ZStack {
Button("", action: {
infoButtonPressed = InfoText(
description: field.infoText,
oref0Variable: field.displayName
)
})
Text(field.displayName)
}
DecimalTextField(
"0",
value: self.$state.sections[sectionIndex].fields[fieldIndex].decimalValue,
formatter: formatter
)
}
}
}
}
}
}
.onAppear(perform: configureView)
.navigationTitle("autoISF preferences")
.navigationTitle("autoISF Configuration")
.navigationBarTitleDisplayMode(.automatic)
.navigationBarItems(
trailing:
Button {
let lang = Locale.current.languageCode ?? "en"
if lang == "en" {
UIApplication.shared.open(
URL(
string: "https://openaps.readthedocs.io/en/latest/docs/While%20You%20Wait%20For%20Gear/preferences-and-safety-settings.html"
)!,
options: [:],
completionHandler: nil
)
} else {
UIApplication.shared.open(
URL(
string: "https://openaps-readthedocs-io.translate.goog/en/latest/docs/While%20You%20Wait%20For%20Gear/preferences-and-safety-settings.html?_x_tr_sl=en&_x_tr_tl=\(lang)&_x_tr_hl=\(lang)"
)!,
options: [:],
completionHandler: nil
)
}
}
label: { Image(systemName: "questionmark.circle") }
)
.alert(item: $infoButtonPressed) { infoButton in
Alert(
title: Text("\(infoButton.oref0Variable)"),
Expand All @@ -89,5 +150,20 @@ extension AutoISFConf {
)
}
}

func createParagraphAttribute(
tabStopLocation: CGFloat,
defaultTabInterval: CGFloat,
firstLineHeadIndent: CGFloat,
headIndent: CGFloat
) -> NSParagraphStyle {
let paragraphStyle: NSMutableParagraphStyle = NSParagraphStyle.default.mutableCopy() as! NSMutableParagraphStyle
let options: [NSTextTab.OptionKey: Any] = [:]
paragraphStyle.tabStops = [NSTextTab(textAlignment: .left, location: tabStopLocation, options: options)]
paragraphStyle.defaultTabInterval = defaultTabInterval
paragraphStyle.firstLineHeadIndent = firstLineHeadIndent
paragraphStyle.headIndent = headIndent
return paragraphStyle
}
}
}
Loading

0 comments on commit 3cf4b4d

Please sign in to comment.