From 30af551cbdac0ecd02f7015bf09e841824a21db8 Mon Sep 17 00:00:00 2001 From: Milos Kozak Date: Thu, 21 Oct 2021 09:45:34 +0200 Subject: [PATCH] Toast for error in profile --- .../profile/local/LocalProfileFragment.kt | 4 +- .../profile/local/LocalProfilePlugin.kt | 62 +++++++++++++++---- app/src/main/res/values/strings.xml | 5 ++ 3 files changed, 57 insertions(+), 14 deletions(-) diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/profile/local/LocalProfileFragment.kt b/app/src/main/java/info/nightscout/androidaps/plugins/profile/local/LocalProfileFragment.kt index 2413655124..1675d4986b 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/profile/local/LocalProfileFragment.kt +++ b/app/src/main/java/info/nightscout/androidaps/plugins/profile/local/LocalProfileFragment.kt @@ -221,7 +221,7 @@ class LocalProfileFragment : DaggerFragment() { } binding.save.setOnClickListener { - if (!localProfilePlugin.isValidEditState()) { + if (!localProfilePlugin.isValidEditState(activity)) { return@setOnClickListener //Should not happen as saveButton should not be visible if not valid } uel.log(Action.STORE_PROFILE, Sources.LocalProfile, ValueWithUnit.SimpleString(localProfilePlugin.currentProfile()?.name @@ -261,7 +261,7 @@ class LocalProfileFragment : DaggerFragment() { private fun updateGUI() { if (_binding == null) return - val isValid = localProfilePlugin.isValidEditState() + val isValid = localProfilePlugin.isValidEditState(activity) val isEdited = localProfilePlugin.isEdited if (isValid) { this.view?.setBackgroundColor(resourceHelper.gc(R.color.ok_background)) diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/profile/local/LocalProfilePlugin.kt b/app/src/main/java/info/nightscout/androidaps/plugins/profile/local/LocalProfilePlugin.kt index 32253598c4..9edee2294e 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/profile/local/LocalProfilePlugin.kt +++ b/app/src/main/java/info/nightscout/androidaps/plugins/profile/local/LocalProfilePlugin.kt @@ -24,6 +24,7 @@ import info.nightscout.androidaps.utils.DateUtil import info.nightscout.androidaps.utils.DecimalFormatter import info.nightscout.androidaps.utils.HardLimits import info.nightscout.androidaps.utils.JsonHelper +import info.nightscout.androidaps.utils.ToastUtils import info.nightscout.androidaps.utils.alertDialogs.OKDialog import info.nightscout.androidaps.utils.resources.ResourceHelper import info.nightscout.androidaps.utils.sharedPreferences.SP @@ -104,25 +105,62 @@ class LocalProfilePlugin @Inject constructor( fun currentProfile(): SingleProfile? = if (numOfProfiles > 0 && currentProfileIndex < numOfProfiles) profiles[currentProfileIndex] else null @Synchronized - fun isValidEditState(): Boolean { + fun isValidEditState(activity: FragmentActivity?): Boolean { val pumpDescription = activePlugin.activePump.pumpDescription with(profiles[currentProfileIndex]) { - if (dia < hardLimits.minDia() || dia > hardLimits.maxDia()) return false - if (name.isNullOrEmpty()) return false - if (blockFromJsonArray(ic, dateUtil)?.any { it.amount < hardLimits.minIC() || it.amount > hardLimits.maxIC() } != false) return false - if (blockFromJsonArray(basal, dateUtil)?.any { it.amount < pumpDescription.basalMinimumRate || it.amount > 10.0 } != false) return false + if (dia < hardLimits.minDia() || dia > hardLimits.maxDia()) { + ToastUtils.errorToast(activity,resourceHelper.gs(R.string.value_out_of_hard_limits, resourceHelper.gs(info.nightscout.androidaps.core.R.string.profile_dia), dia)) + return false + } + if (name.isNullOrEmpty()){ + ToastUtils.errorToast(activity,resourceHelper.gs(R.string.missing_profile_name)) + return false + } + if (blockFromJsonArray(ic, dateUtil)?.any { it.amount < hardLimits.minIC() || it.amount > hardLimits.maxIC() } != false) { + ToastUtils.errorToast(activity,resourceHelper.gs(R.string.error_in_ic_values)) + return false + } val low = blockFromJsonArray(targetLow, dateUtil) val high = blockFromJsonArray(targetHigh, dateUtil) if (profileFunction.getUnits() == GlucoseUnit.MGDL) { - if (low?.any { it.amount < HardLimits.VERY_HARD_LIMIT_TARGET_BG[0].toDouble() || it.amount > HardLimits.VERY_HARD_LIMIT_TARGET_BG[1].toDouble() } != false) return false - if (high?.any { it.amount < HardLimits.VERY_HARD_LIMIT_TARGET_BG[0].toDouble() || it.amount > HardLimits.VERY_HARD_LIMIT_TARGET_BG[1].toDouble() } != false) return false - if (blockFromJsonArray(isf, dateUtil)?.any { it.amount < HardLimits.MIN_ISF || it.amount > HardLimits.MAX_ISF } != false) return false + if (blockFromJsonArray(isf, dateUtil)?.any { it.amount < HardLimits.MIN_ISF || it.amount > HardLimits.MAX_ISF } != false) { + ToastUtils.errorToast(activity,resourceHelper.gs(R.string.error_in_isf_values)) + return false + } + if (blockFromJsonArray(basal, dateUtil)?.any { it.amount < pumpDescription.basalMinimumRate || it.amount > 10.0 } != false) { + ToastUtils.errorToast(activity,resourceHelper.gs(R.string.error_in_basal_values)) + return false + } + if (low?.any { it.amount < HardLimits.VERY_HARD_LIMIT_TARGET_BG[0].toDouble() || it.amount > HardLimits.VERY_HARD_LIMIT_TARGET_BG[1].toDouble() } != false) { + ToastUtils.errorToast(activity,resourceHelper.gs(R.string.error_in_target_values)) + return false + } + if (high?.any { it.amount < HardLimits.VERY_HARD_LIMIT_TARGET_BG[0].toDouble() || it.amount > HardLimits.VERY_HARD_LIMIT_TARGET_BG[1].toDouble() } != false) { + ToastUtils.errorToast(activity,resourceHelper.gs(R.string.error_in_target_values)) + return false + } } else { - if (low?.any { it.amount < Profile.fromMgdlToUnits(HardLimits.VERY_HARD_LIMIT_TARGET_BG[0].toDouble(), GlucoseUnit.MMOL) || it.amount > Profile.fromMgdlToUnits(HardLimits.VERY_HARD_LIMIT_TARGET_BG[1].toDouble(), GlucoseUnit.MMOL) } != false) return false - if (high?.any { it.amount < Profile.fromMgdlToUnits(HardLimits.VERY_HARD_LIMIT_TARGET_BG[0].toDouble(), GlucoseUnit.MMOL) || it.amount > Profile.fromMgdlToUnits(HardLimits.VERY_HARD_LIMIT_TARGET_BG[1].toDouble(), GlucoseUnit.MMOL) } != false) return false - if (blockFromJsonArray(isf, dateUtil)?.any { it.amount < Profile.fromMgdlToUnits(HardLimits.MIN_ISF, GlucoseUnit.MMOL) || it.amount > Profile.fromMgdlToUnits(HardLimits.MAX_ISF, GlucoseUnit.MMOL) } != false) return false + if (blockFromJsonArray(isf, dateUtil)?.any { it.amount < Profile.fromMgdlToUnits(HardLimits.MIN_ISF, GlucoseUnit.MMOL) || it.amount > Profile.fromMgdlToUnits(HardLimits.MAX_ISF, GlucoseUnit.MMOL) } != false) { + ToastUtils.errorToast(activity,resourceHelper.gs(R.string.error_in_isf_values)) + return false + } + if (blockFromJsonArray(basal, dateUtil)?.any { it.amount < pumpDescription.basalMinimumRate || it.amount > 10.0 } != false) { + ToastUtils.errorToast(activity,resourceHelper.gs(R.string.error_in_basal_values)) + return false + } + if (low?.any { it.amount < Profile.fromMgdlToUnits(HardLimits.VERY_HARD_LIMIT_TARGET_BG[0].toDouble(), GlucoseUnit.MMOL) || it.amount > Profile.fromMgdlToUnits(HardLimits.VERY_HARD_LIMIT_TARGET_BG[1].toDouble(), GlucoseUnit.MMOL) } != false) { + ToastUtils.errorToast(activity,resourceHelper.gs(R.string.error_in_target_values)) + return false + } + if (high?.any { it.amount < Profile.fromMgdlToUnits(HardLimits.VERY_HARD_LIMIT_TARGET_BG[0].toDouble(), GlucoseUnit.MMOL) || it.amount > Profile.fromMgdlToUnits(HardLimits.VERY_HARD_LIMIT_TARGET_BG[1].toDouble(), GlucoseUnit.MMOL) } != false) { + ToastUtils.errorToast(activity,resourceHelper.gs(R.string.error_in_target_values)) + return false + } + } + for (i in low.indices) if (low[i].amount > high[i].amount) { + ToastUtils.errorToast(activity,resourceHelper.gs(R.string.error_in_target_values)) + return false } - for (i in low.indices) if (low[i].amount > high[i].amount) return false } return true } diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index d4d80e194a..2e4e156c6b 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -1112,5 +1112,10 @@ Timeout while waiting for finish of previous pump communication There is another bolus in queue. Try again later. Calculation in progress + Missing profile name + Error in IC vaules + Error in basal values + Error in target values + Error in ISF values