forked from nightscout/Trio
-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move dynamic ISF settings out from "OpenAPS" to "Extra Features" section
- Loading branch information
Showing
11 changed files
with
209 additions
and
78 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
enum Dynamic { | ||
enum Config {} | ||
} | ||
|
||
protocol DynamicProvider: Provider {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
extension Dynamic { | ||
final class Provider: BaseProvider, DynamicProvider {} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
import SwiftUI | ||
|
||
extension Dynamic { | ||
final class StateModel: BaseStateModel<Provider> { | ||
@Injected() var settings: SettingsManager! | ||
@Injected() var storage: FileStorage! | ||
|
||
@Published var useNewFormula: Bool = false | ||
@Published var enableDynamicCR: Bool = false | ||
@Published var sigmoid: Bool = false | ||
@Published var adjustmentFactor: Decimal = 0.5 | ||
@Published var weightPercentage: Decimal = 0.65 | ||
@Published var tddAdjBasal: Bool = false | ||
@Published var threshold_setting: Decimal = 65 | ||
@Published var unit: GlucoseUnits = .mmolL | ||
|
||
var preferences: Preferences { | ||
settingsManager.preferences | ||
} | ||
|
||
override func subscribe() { | ||
unit = settingsManager.settings.units | ||
useNewFormula = settings.preferences.useNewFormula | ||
enableDynamicCR = settings.preferences.enableDynamicCR | ||
sigmoid = settings.preferences.sigmoid | ||
adjustmentFactor = settings.preferences.adjustmentFactor | ||
weightPercentage = settings.preferences.weightPercentage | ||
tddAdjBasal = settings.preferences.tddAdjBasal | ||
|
||
if unit == .mmolL { | ||
threshold_setting = settings.preferences.threshold_setting.asMmolL | ||
} else { | ||
threshold_setting = settings.preferences.threshold_setting | ||
} | ||
} | ||
|
||
var unChanged: Bool { | ||
preferences.enableDynamicCR == enableDynamicCR && | ||
preferences.adjustmentFactor == adjustmentFactor && | ||
preferences.sigmoid == sigmoid && | ||
preferences.tddAdjBasal == tddAdjBasal && | ||
preferences.threshold_setting == convertBack(threshold_setting) && | ||
preferences.useNewFormula == useNewFormula && | ||
preferences.weightPercentage == weightPercentage | ||
} | ||
|
||
func convertBack(_ glucose: Decimal) -> Decimal { | ||
if unit == .mmolL { | ||
return glucose.asMgdL | ||
} | ||
return glucose | ||
} | ||
|
||
func saveIfChanged() { | ||
if !unChanged { | ||
var newSettings = storage.retrieve(OpenAPS.Settings.preferences, as: Preferences.self) ?? Preferences() | ||
newSettings.enableDynamicCR = enableDynamicCR | ||
newSettings.adjustmentFactor = adjustmentFactor | ||
newSettings.sigmoid = sigmoid | ||
newSettings.tddAdjBasal = tddAdjBasal | ||
newSettings.threshold_setting = convertBack(threshold_setting) | ||
newSettings.useNewFormula = useNewFormula | ||
newSettings.weightPercentage = weightPercentage | ||
newSettings.timestamp = Date() | ||
storage.save(newSettings, as: OpenAPS.Settings.preferences) | ||
} | ||
} | ||
} | ||
} |
89 changes: 89 additions & 0 deletions
89
FreeAPS/Sources/Modules/Dynamic/View/DynamicRootView.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
import SwiftUI | ||
import Swinject | ||
|
||
extension Dynamic { | ||
struct RootView: BaseView { | ||
let resolver: Resolver | ||
@StateObject var state = StateModel() | ||
|
||
private var conversionFormatter: NumberFormatter { | ||
let formatter = NumberFormatter() | ||
formatter.numberStyle = .decimal | ||
formatter.maximumFractionDigits = 1 | ||
|
||
return formatter | ||
} | ||
|
||
private var formatter: NumberFormatter { | ||
let formatter = NumberFormatter() | ||
formatter.numberStyle = .decimal | ||
return formatter | ||
} | ||
|
||
private var glucoseFormatter: NumberFormatter { | ||
let formatter = NumberFormatter() | ||
formatter.numberStyle = .decimal | ||
if state.unit == .mmolL { | ||
formatter.maximumFractionDigits = 1 | ||
} else { formatter.maximumFractionDigits = 0 } | ||
formatter.roundingMode = .halfUp | ||
return formatter | ||
} | ||
|
||
var body: some View { | ||
Form { | ||
Section { | ||
HStack { | ||
Toggle("Activate Dynamic Sensitivity (ISF)", isOn: $state.useNewFormula) | ||
} | ||
if state.useNewFormula { | ||
HStack { | ||
Toggle("Activate Dynamic Carb Ratio (CR)", isOn: $state.enableDynamicCR) | ||
} | ||
} | ||
} header: { Text("Enable") } | ||
|
||
if state.useNewFormula { | ||
Section { | ||
HStack { | ||
Toggle("Use Sigmoid Formula", isOn: $state.sigmoid) | ||
} | ||
} header: { Text("Formula") } | ||
|
||
Section { | ||
HStack { | ||
Text("Adjustment Factor") | ||
Spacer() | ||
DecimalTextField("0", value: $state.adjustmentFactor, formatter: formatter) | ||
} | ||
|
||
HStack { | ||
Text("Weighted Average of TDD. Weight of past 24 hours:") | ||
Spacer() | ||
DecimalTextField("0", value: $state.weightPercentage, formatter: formatter) | ||
} | ||
|
||
HStack { | ||
Toggle("Adjust basal", isOn: $state.tddAdjBasal) | ||
} | ||
} header: { Text("Settings") } | ||
|
||
Section { | ||
HStack { | ||
Text("Threshold Setting") | ||
Spacer() | ||
DecimalTextField("0", value: $state.threshold_setting, formatter: glucoseFormatter) | ||
Text(state.unit.rawValue) | ||
} | ||
} header: { Text("Safety") } | ||
} | ||
} | ||
.onAppear(perform: configureView) | ||
.navigationBarTitle("Dynamic ISF") | ||
.navigationBarTitleDisplayMode(.automatic) | ||
.onDisappear { | ||
state.saveIfChanged() | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.