ApsMode enum - refactor of secureValueOf

This commit is contained in:
piotrek 2022-12-11 23:07:37 +01:00
parent 5485792801
commit 6205c9b893
6 changed files with 7 additions and 13 deletions

View file

@ -8,12 +8,6 @@ enum class ApsMode() {
companion object { companion object {
fun secureValueOf(stringValue: String): ApsMode { fun fromString(stringValue: String?) = values().firstOrNull { it.name == (stringValue?.uppercase() ?: UNDEFINED) }
return try {
valueOf(stringValue.uppercase())
} catch (e: IllegalArgumentException) {
UNDEFINED
}
}
} }
} }

View file

@ -183,7 +183,7 @@ class LoopPlugin @Inject constructor(
get() { get() {
val closedLoopEnabled = constraintChecker.isClosedLoopAllowed() val closedLoopEnabled = constraintChecker.isClosedLoopAllowed()
val maxIobAllowed = constraintChecker.getMaxIOBAllowed().value() val maxIobAllowed = constraintChecker.getMaxIOBAllowed().value()
val apsMode = ApsMode.secureValueOf(sp.getString(info.nightscout.core.utils.R.string.key_aps_mode, ApsMode.OPEN.name)) val apsMode = ApsMode.fromString(sp.getString(info.nightscout.core.utils.R.string.key_aps_mode, ApsMode.OPEN.name))
val pump = activePlugin.activePump val pump = activePlugin.activePump
var isLGS = false var isLGS = false
if (!isSuspended && !pump.isSuspended()) if (closedLoopEnabled.value()) if (maxIobAllowed == HardLimits.MAX_IOB_LGS || apsMode == ApsMode.LGS) isLGS = true if (!isSuspended && !pump.isSuspended()) if (closedLoopEnabled.value()) if (maxIobAllowed == HardLimits.MAX_IOB_LGS || apsMode == ApsMode.LGS) isLGS = true

View file

@ -16,7 +16,7 @@ class Objective6(injector: HasAndroidInjector) : Objective(injector, "maxiob", R
tasks.add(MinimumDurationTask(this, T.days(1).msecs())) tasks.add(MinimumDurationTask(this, T.days(1).msecs()))
tasks.add( tasks.add(
object : Task(this, R.string.closedmodeenabled) { object : Task(this, R.string.closedmodeenabled) {
override fun isCompleted(): Boolean = ApsMode.secureValueOf(sp.getString(info.nightscout.core.utils.R.string.key_aps_mode, ApsMode.OPEN.name)) == ApsMode.CLOSED override fun isCompleted(): Boolean = ApsMode.fromString(sp.getString(info.nightscout.core.utils.R.string.key_aps_mode, ApsMode.OPEN.name)) == ApsMode.CLOSED
}) })
tasks.add( tasks.add(
object : Task(this, R.string.maxiobset) { object : Task(this, R.string.maxiobset) {

View file

@ -68,7 +68,7 @@ class SafetyPlugin @Inject constructor(
} }
override fun isClosedLoopAllowed(value: Constraint<Boolean>): Constraint<Boolean> { override fun isClosedLoopAllowed(value: Constraint<Boolean>): Constraint<Boolean> {
val mode = ApsMode.secureValueOf(sp.getString(info.nightscout.core.utils.R.string.key_aps_mode, ApsMode.OPEN.name)) val mode = ApsMode.fromString(sp.getString(info.nightscout.core.utils.R.string.key_aps_mode, ApsMode.OPEN.name))
if (mode == ApsMode.OPEN) value.set(aapsLogger, false, rh.gs(R.string.closedmodedisabledinpreferences), this) if (mode == ApsMode.OPEN) value.set(aapsLogger, false, rh.gs(R.string.closedmodedisabledinpreferences), this)
if (!config.isEngineeringModeOrRelease()) { if (!config.isEngineeringModeOrRelease()) {
if (value.value()) { if (value.value()) {
@ -160,7 +160,7 @@ class SafetyPlugin @Inject constructor(
} }
override fun applyMaxIOBConstraints(maxIob: Constraint<Double>): Constraint<Double> { override fun applyMaxIOBConstraints(maxIob: Constraint<Double>): Constraint<Double> {
val apsMode = ApsMode.secureValueOf(sp.getString(info.nightscout.core.utils.R.string.key_aps_mode, ApsMode.OPEN.name)) val apsMode = ApsMode.fromString(sp.getString(info.nightscout.core.utils.R.string.key_aps_mode, ApsMode.OPEN.name))
if (apsMode == ApsMode.LGS) maxIob.setIfSmaller(aapsLogger, HardLimits.MAX_IOB_LGS, rh.gs(info.nightscout.core.ui.R.string.limiting_iob, HardLimits.MAX_IOB_LGS, rh.gs(info.nightscout.core.ui.R.string.lowglucosesuspend)), this) if (apsMode == ApsMode.LGS) maxIob.setIfSmaller(aapsLogger, HardLimits.MAX_IOB_LGS, rh.gs(info.nightscout.core.ui.R.string.limiting_iob, HardLimits.MAX_IOB_LGS, rh.gs(info.nightscout.core.ui.R.string.lowglucosesuspend)), this)
return maxIob return maxIob
} }

View file

@ -1284,7 +1284,7 @@ class SmsCommunicatorPlugin @Inject constructor(
} }
private fun getApsModeText(): String = private fun getApsModeText(): String =
when (ApsMode.secureValueOf(sp.getString(info.nightscout.core.utils.R.string.key_aps_mode, ApsMode.OPEN.name))) { when (ApsMode.fromString(sp.getString(info.nightscout.core.utils.R.string.key_aps_mode, ApsMode.OPEN.name))) {
ApsMode.OPEN -> rh.gs(info.nightscout.core.ui.R.string.openloop) ApsMode.OPEN -> rh.gs(info.nightscout.core.ui.R.string.openloop)
ApsMode.CLOSED -> rh.gs(info.nightscout.core.ui.R.string.closedloop) ApsMode.CLOSED -> rh.gs(info.nightscout.core.ui.R.string.closedloop)
ApsMode.LGS -> rh.gs(info.nightscout.core.ui.R.string.lowglucosesuspend) ApsMode.LGS -> rh.gs(info.nightscout.core.ui.R.string.lowglucosesuspend)

View file

@ -162,7 +162,7 @@ class LoopDialog : DaggerDialogFragment() {
val closedLoopAllowed = constraintChecker.isClosedLoopAllowed(Constraint(true)) val closedLoopAllowed = constraintChecker.isClosedLoopAllowed(Constraint(true))
val closedLoopAllowed2 = activePlugin.activeObjectives?.isAccomplished(Objectives.MAXIOB_OBJECTIVE) ?: false val closedLoopAllowed2 = activePlugin.activeObjectives?.isAccomplished(Objectives.MAXIOB_OBJECTIVE) ?: false
val lgsEnabled = constraintChecker.isLgsAllowed(Constraint(true)) val lgsEnabled = constraintChecker.isLgsAllowed(Constraint(true))
val apsMode = ApsMode.secureValueOf(sp.getString(info.nightscout.core.utils.R.string.key_aps_mode, ApsMode.OPEN.name)) val apsMode = ApsMode.fromString(sp.getString(info.nightscout.core.utils.R.string.key_aps_mode, ApsMode.OPEN.name))
val pump = activePlugin.activePump val pump = activePlugin.activePump
binding.overviewDisconnect15m.visibility = pumpDescription.tempDurationStep15mAllowed.toVisibility() binding.overviewDisconnect15m.visibility = pumpDescription.tempDurationStep15mAllowed.toVisibility()