diff --git a/shared/src/main/java/info/nightscout/shared/SafeParse.kt b/shared/src/main/java/info/nightscout/shared/SafeParse.kt index badcc231d6..f8d9aefa0b 100644 --- a/shared/src/main/java/info/nightscout/shared/SafeParse.kt +++ b/shared/src/main/java/info/nightscout/shared/SafeParse.kt @@ -3,12 +3,12 @@ package info.nightscout.shared object SafeParse { // private static Logger log = StacktraceLoggerWrapper.getLogger(SafeParse.class); - fun stringToDouble(inputString: String?): Double { - var input = inputString ?: return 0.0 - var result = 0.0 + fun stringToDouble(inputString: String?, defaultValue: Double = 0.0): Double { + var input = inputString ?: return defaultValue + var result = defaultValue input = input.replace(",", ".") input = input.replace("−", "-") - if (input == "") return 0.0 + if (input == "") return defaultValue try { result = input.toDouble() } catch (e: Exception) { diff --git a/wear/src/main/java/info/nightscout/androidaps/interaction/actions/BolusActivity.kt b/wear/src/main/java/info/nightscout/androidaps/interaction/actions/BolusActivity.kt index 012d5ec516..f67a34f690 100644 --- a/wear/src/main/java/info/nightscout/androidaps/interaction/actions/BolusActivity.kt +++ b/wear/src/main/java/info/nightscout/androidaps/interaction/actions/BolusActivity.kt @@ -42,7 +42,7 @@ class BolusActivity : ViewSelectorActivity() { override fun instantiateItem(container: ViewGroup, row: Int, col: Int): View = when (col) { 0 -> { val viewAdapter = EditPlusMinusViewAdapter.getViewAdapter(sp, applicationContext, container, true) - val initValue = if (editInsulin != null) SafeParse.stringToDouble(editInsulin?.editText?.text.toString()) else 0.0 + val initValue = SafeParse.stringToDouble(editInsulin?.editText?.text.toString(), 0.0) val maxBolus = sp.getDouble(getString(R.string.key_treatments_safety_max_bolus), 3.0) val title = getString(R.string.action_insulin) editInsulin = PlusMinusEditText(viewAdapter, initValue, 0.0, maxBolus, stepValues, DecimalFormat("#0.0"), false, title) diff --git a/wear/src/main/java/info/nightscout/androidaps/interaction/actions/CarbActivity.kt b/wear/src/main/java/info/nightscout/androidaps/interaction/actions/CarbActivity.kt index b941049002..6521ffd413 100644 --- a/wear/src/main/java/info/nightscout/androidaps/interaction/actions/CarbActivity.kt +++ b/wear/src/main/java/info/nightscout/androidaps/interaction/actions/CarbActivity.kt @@ -42,12 +42,9 @@ class CarbActivity : ViewSelectorActivity() { 0 -> { val viewAdapter = EditPlusMinusViewAdapter.getViewAdapter(sp, applicationContext, container, true) val view = viewAdapter.root - var def = 0.0 - if (editCarbs != null) { - def = SafeParse.stringToDouble(editCarbs?.editText?.text.toString()) - } - val maxCarbs = sp.getInt(getString(R.string.key_treatments_safety_max_carbs), 48) - editCarbs = PlusMinusEditText(viewAdapter, def, 0.0, maxCarbs.toDouble(), stepValues, DecimalFormat("0"), true, getString(R.string.action_carbs)) + var initValue = SafeParse.stringToDouble(editCarbs?.editText?.text.toString(), 0.0) + val maxCarbs = sp.getInt(getString(R.string.key_treatments_safety_max_carbs), 48).toDouble() + editCarbs = PlusMinusEditText(viewAdapter, initValue, 0.0, maxCarbs, stepValues, DecimalFormat("0"), true, getString(R.string.action_carbs)) container.addView(view) view.requestFocus() view diff --git a/wear/src/main/java/info/nightscout/androidaps/interaction/actions/ECarbActivity.kt b/wear/src/main/java/info/nightscout/androidaps/interaction/actions/ECarbActivity.kt index 919d92b1dd..bc2662066d 100644 --- a/wear/src/main/java/info/nightscout/androidaps/interaction/actions/ECarbActivity.kt +++ b/wear/src/main/java/info/nightscout/androidaps/interaction/actions/ECarbActivity.kt @@ -45,12 +45,9 @@ class ECarbActivity : ViewSelectorActivity() { 0 -> { val viewAdapter = EditPlusMinusViewAdapter.getViewAdapter(sp, applicationContext, container, true) val view = viewAdapter.root - var def = 0.0 - if (editCarbs != null) { - def = stringToDouble(editCarbs?.editText?.text.toString()) - } - val maxCarbs = sp.getInt(getString(R.string.key_treatments_safety_max_carbs), 48) - editCarbs = PlusMinusEditText(viewAdapter, def, 0.0, maxCarbs.toDouble(), stepValues, DecimalFormat("0"), true, getString(R.string.action_carbs)) + var initValue = stringToDouble(editCarbs?.editText?.text.toString(), 0.0) + val maxCarbs = sp.getInt(getString(R.string.key_treatments_safety_max_carbs), 48).toDouble() + editCarbs = PlusMinusEditText(viewAdapter, initValue, 0.0, maxCarbs, stepValues, DecimalFormat("0"), true, getString(R.string.action_carbs)) container.addView(view) view.requestFocus() view @@ -59,11 +56,8 @@ class ECarbActivity : ViewSelectorActivity() { 1 -> { val viewAdapter = EditPlusMinusViewAdapter.getViewAdapter(sp, applicationContext, container, false) val view = viewAdapter.root - var def = 0.0 - if (editStartTime != null) { - def = stringToDouble(editStartTime?.editText?.text.toString()) - } - editStartTime = PlusMinusEditText(viewAdapter, 15.0, def, -60.0, 300.0, DecimalFormat("0"), false, getString(R.string.action_start_min)) + var initValue = stringToDouble(editStartTime?.editText?.text.toString(), 0.0) + editStartTime = PlusMinusEditText(viewAdapter, initValue, -60.0, 300.0, 15.0, DecimalFormat("0"), false, getString(R.string.action_start_min)) container.addView(view) view } @@ -71,11 +65,8 @@ class ECarbActivity : ViewSelectorActivity() { 2 -> { val viewAdapter = EditPlusMinusViewAdapter.getViewAdapter(sp, applicationContext, container, false) val view = viewAdapter.root - var def = 0.0 - if (editDuration != null) { - def = stringToDouble(editDuration?.editText?.text.toString()) - } - editDuration = PlusMinusEditText(viewAdapter, 1.0, def, 0.0, 8.0, DecimalFormat("0"), false, getString(R.string.action_duration_h)) + var initValue = stringToDouble(editDuration?.editText?.text.toString(), 0.0) + editDuration = PlusMinusEditText(viewAdapter, initValue, 0.0, 8.0, 1.0, DecimalFormat("0"), false, getString(R.string.action_duration_h)) container.addView(view) view } diff --git a/wear/src/main/java/info/nightscout/androidaps/interaction/actions/FillActivity.kt b/wear/src/main/java/info/nightscout/androidaps/interaction/actions/FillActivity.kt index 9fdfa97d26..d2c3abc2a3 100644 --- a/wear/src/main/java/info/nightscout/androidaps/interaction/actions/FillActivity.kt +++ b/wear/src/main/java/info/nightscout/androidaps/interaction/actions/FillActivity.kt @@ -38,11 +38,8 @@ class FillActivity : ViewSelectorActivity() { 0 -> { val viewAdapter = EditPlusMinusViewAdapter.getViewAdapter(sp, applicationContext, container, false) val view = viewAdapter.root - var def = 0.0 - if (editInsulin != null) { - def = stringToDouble(editInsulin?.editText?.text.toString()) - } - editInsulin = PlusMinusEditText(viewAdapter, def, 0.0, 30.0, 0.1, DecimalFormat("#0.0"), false, getString(R.string.action_insulin)) + var initValue = stringToDouble(editInsulin?.editText?.text.toString(), 0.0) + editInsulin = PlusMinusEditText(viewAdapter, initValue, 0.0, 30.0, 0.1, DecimalFormat("#0.0"), false, getString(R.string.action_insulin)) container.addView(view) view.requestFocus() view diff --git a/wear/src/main/java/info/nightscout/androidaps/interaction/actions/ProfileSwitchActivity.kt b/wear/src/main/java/info/nightscout/androidaps/interaction/actions/ProfileSwitchActivity.kt index 8aa2a26437..75de43d82d 100644 --- a/wear/src/main/java/info/nightscout/androidaps/interaction/actions/ProfileSwitchActivity.kt +++ b/wear/src/main/java/info/nightscout/androidaps/interaction/actions/ProfileSwitchActivity.kt @@ -48,11 +48,8 @@ class ProfileSwitchActivity : ViewSelectorActivity() { 0 -> { val viewAdapter = EditPlusMinusViewAdapter.getViewAdapter(sp, applicationContext, container, false) val view = viewAdapter.root - var def = timeshift.toDouble() - if (editTimeshift != null) { - def = SafeParse.stringToDouble(editTimeshift?.editText?.text.toString()) - } - editTimeshift = PlusMinusEditText(viewAdapter, def, 0.0, 23.0, 1.0, DecimalFormat("0"), true, getString(R.string.action_timeshift), true) + var initValue = SafeParse.stringToDouble(editTimeshift?.editText?.text.toString(), timeshift.toDouble()) + editTimeshift = PlusMinusEditText(viewAdapter, initValue, 0.0, 23.0, 1.0, DecimalFormat("0"), true, getString(R.string.action_timeshift), true) container.addView(view) view.requestFocus() view @@ -61,12 +58,8 @@ class ProfileSwitchActivity : ViewSelectorActivity() { 1 -> { val viewAdapter = EditPlusMinusViewAdapter.getViewAdapter(sp, applicationContext, container, false) val view = viewAdapter.root - var def = percentage.toDouble() - if (editPercentage != null) { - def = SafeParse.stringToDouble(editPercentage?.editText?.text.toString()) - } - editPercentage = PlusMinusEditText(viewAdapter, def, 30.0, 250.0, 1.0, DecimalFormat("0"), false, getString(R.string.action_percentage)) - + var initValue = SafeParse.stringToDouble(editPercentage?.editText?.text.toString(), percentage.toDouble()) + editPercentage = PlusMinusEditText(viewAdapter, initValue, 30.0, 250.0, 1.0, DecimalFormat("0"), false, getString(R.string.action_percentage)) container.addView(view) view } diff --git a/wear/src/main/java/info/nightscout/androidaps/interaction/actions/TempTargetActivity.kt b/wear/src/main/java/info/nightscout/androidaps/interaction/actions/TempTargetActivity.kt index 77f1c256dc..dd18434b38 100644 --- a/wear/src/main/java/info/nightscout/androidaps/interaction/actions/TempTargetActivity.kt +++ b/wear/src/main/java/info/nightscout/androidaps/interaction/actions/TempTargetActivity.kt @@ -50,12 +50,8 @@ class TempTargetActivity : ViewSelectorActivity() { col == 0 -> { val viewAdapter = EditPlusMinusViewAdapter.getViewAdapter(sp, applicationContext, container, false) val view = viewAdapter.root - time = if (time == null) { - PlusMinusEditText(viewAdapter, 60.0, 0.0, 24 * 60.0, 5.0, DecimalFormat("0"), false, getString(R.string.action_duration)) - } else { - val def = SafeParse.stringToDouble(time?.editText?.text.toString()) - PlusMinusEditText(viewAdapter, def, 0.0, 24 * 60.0, 5.0, DecimalFormat("0"), false, getString(R.string.action_duration)) - } + val initValue = SafeParse.stringToDouble(time?.editText?.text.toString(), 60.0) + time = PlusMinusEditText(viewAdapter, initValue, 0.0, 24 * 60.0, 5.0, DecimalFormat("0"), false, getString(R.string.action_duration)) container.addView(view) view.requestFocus() view @@ -66,13 +62,11 @@ class TempTargetActivity : ViewSelectorActivity() { val view = viewAdapter.root val title = if (isSingleTarget) getString(R.string.action_target) else getString(R.string.action_low) if (isMGDL) { - var def = 100.0 - if (lowRange != null) def = SafeParse.stringToDouble(lowRange?.editText?.text.toString()) - lowRange = PlusMinusEditText(viewAdapter, def, 72.0, 180.0, 1.0, DecimalFormat("0"), false, title) + var initValue = SafeParse.stringToDouble(lowRange?.editText?.text.toString(), 100.0) + lowRange = PlusMinusEditText(viewAdapter, initValue, 72.0, 180.0, 1.0, DecimalFormat("0"), false, title) } else { - var def = 5.5 - if (lowRange != null) def = SafeParse.stringToDouble(lowRange?.editText?.text.toString()) - lowRange = PlusMinusEditText(viewAdapter, def, 4.0, 10.0, 0.1, DecimalFormat("#0.0"), false, title) + var initValue = SafeParse.stringToDouble(lowRange?.editText?.text.toString(), 5.5) + lowRange = PlusMinusEditText(viewAdapter, initValue, 4.0, 10.0, 0.1, DecimalFormat("#0.0"), false, title) } container.addView(view) view @@ -82,13 +76,11 @@ class TempTargetActivity : ViewSelectorActivity() { val viewAdapter = EditPlusMinusViewAdapter.getViewAdapter(sp, applicationContext, container, false) val view = viewAdapter.root if (isMGDL) { - var def = 100.0 - if (highRange != null) def = SafeParse.stringToDouble(highRange?.editText?.text.toString()) - highRange = PlusMinusEditText(viewAdapter, def, 72.0, 180.0, 1.0, DecimalFormat("0"), false, getString(R.string.action_high)) + var initValue = SafeParse.stringToDouble(highRange?.editText?.text.toString(), 100.0) + highRange = PlusMinusEditText(viewAdapter, initValue, 72.0, 180.0, 1.0, DecimalFormat("0"), false, getString(R.string.action_high)) } else { - var def = 5.5 - if (highRange != null) def = SafeParse.stringToDouble(highRange?.editText?.text.toString()) - highRange = PlusMinusEditText(viewAdapter, def, 4.0, 10.0, 0.1, DecimalFormat("#0.0"), false, getString(R.string.action_high)) + var initValue = SafeParse.stringToDouble(highRange?.editText?.text.toString(), 5.5) + highRange = PlusMinusEditText(viewAdapter, initValue, 4.0, 10.0, 0.1, DecimalFormat("#0.0"), false, getString(R.string.action_high)) } container.addView(view) view diff --git a/wear/src/main/java/info/nightscout/androidaps/interaction/actions/TreatmentActivity.kt b/wear/src/main/java/info/nightscout/androidaps/interaction/actions/TreatmentActivity.kt index 1cb7c36879..82e631de58 100644 --- a/wear/src/main/java/info/nightscout/androidaps/interaction/actions/TreatmentActivity.kt +++ b/wear/src/main/java/info/nightscout/androidaps/interaction/actions/TreatmentActivity.kt @@ -48,10 +48,9 @@ class TreatmentActivity : ViewSelectorActivity() { 0 -> { val viewAdapter = EditPlusMinusViewAdapter.getViewAdapter(sp, applicationContext, container, true) val view = viewAdapter.root - var def = 0.0 - if (editInsulin != null) def = stringToDouble(editInsulin?.editText?.text.toString()) + var initValue = stringToDouble(editInsulin?.editText?.text.toString(), 0.0) val maxBolus = sp.getDouble(getString(R.string.key_treatments_safety_max_bolus), 3.0) - editInsulin = PlusMinusEditText(viewAdapter, def, 0.0, maxBolus, stepValuesInsulin, DecimalFormat("#0.0"), false, getString(R.string.action_insulin)) + editInsulin = PlusMinusEditText(viewAdapter, initValue, 0.0, maxBolus, stepValuesInsulin, DecimalFormat("#0.0"), false, getString(R.string.action_insulin)) container.addView(view) view.requestFocus() view @@ -60,11 +59,9 @@ class TreatmentActivity : ViewSelectorActivity() { 1 -> { val viewAdapter = EditPlusMinusViewAdapter.getViewAdapter(sp, applicationContext, container, true) val view = viewAdapter.root - var def = 0.0 - val maxCarbs = sp.getInt(getString(R.string.key_treatments_safety_max_carbs), 48) - if (editCarbs != null) def = stringToDouble(editCarbs?.editText?.text.toString()) - - editCarbs = PlusMinusEditText(viewAdapter, def, 0.0, maxCarbs.toDouble(), stepValuesCarbs, DecimalFormat("0"), false, getString(R.string.action_carbs)) + val maxCarbs = sp.getInt(getString(R.string.key_treatments_safety_max_carbs), 48).toDouble() + var initValue = stringToDouble(editCarbs?.editText?.text.toString(), 0.0) + editCarbs = PlusMinusEditText(viewAdapter, initValue, 0.0, maxCarbs, stepValuesCarbs, DecimalFormat("0"), false, getString(R.string.action_carbs)) container.addView(view) view } diff --git a/wear/src/main/java/info/nightscout/androidaps/interaction/actions/WizardActivity.kt b/wear/src/main/java/info/nightscout/androidaps/interaction/actions/WizardActivity.kt index 9a9bd01967..58448f765f 100644 --- a/wear/src/main/java/info/nightscout/androidaps/interaction/actions/WizardActivity.kt +++ b/wear/src/main/java/info/nightscout/androidaps/interaction/actions/WizardActivity.kt @@ -44,13 +44,9 @@ class WizardActivity : ViewSelectorActivity() { col == 0 -> { val viewAdapter = EditPlusMinusViewAdapter.getViewAdapter(sp, applicationContext, container, true) val view = viewAdapter.root - val maxCarbs = sp.getInt(getString(R.string.key_treatments_safety_max_carbs), 48) - editCarbs = if (editCarbs == null) { - PlusMinusEditText(viewAdapter, 0.0, 0.0, maxCarbs.toDouble(), stepValues, DecimalFormat("0"), false, getString(R.string.action_carbs)) - } else { - val def = SafeParse.stringToDouble(editCarbs?.editText?.text.toString()) - PlusMinusEditText(viewAdapter, def, 0.0, maxCarbs.toDouble(), 1.0, DecimalFormat("0"), false, getString(R.string.action_carbs)) - } + val maxCarbs = sp.getInt(getString(R.string.key_treatments_safety_max_carbs), 48).toDouble() + val initValue = SafeParse.stringToDouble(editCarbs?.editText?.text.toString(), 0.0) + editCarbs = PlusMinusEditText(viewAdapter, initValue, 0.0, maxCarbs, stepValues, DecimalFormat("0"), false, getString(R.string.action_carbs)) container.addView(view) view.requestFocus() view @@ -59,13 +55,9 @@ class WizardActivity : ViewSelectorActivity() { col == 1 && hasPercentage -> { val viewAdapter = EditPlusMinusViewAdapter.getViewAdapter(sp, applicationContext, container, false) val view = viewAdapter.root - val percentage = sp.getInt(getString(R.string.key_bolus_wizard_percentage), 100) - editPercentage = if (editPercentage == null) { - PlusMinusEditText(viewAdapter, percentage.toDouble(), 50.0, 150.0, 1.0, DecimalFormat("0"), false, getString(R.string.action_percentage)) - } else { - val def = SafeParse.stringToDouble(editPercentage?.editText?.text.toString()) - PlusMinusEditText(viewAdapter, def, 50.0, 150.0, 1.0, DecimalFormat("0"), false, getString(R.string.action_percentage)) - } + val percentage = sp.getInt(getString(R.string.key_bolus_wizard_percentage), 100).toDouble() + val initValue = SafeParse.stringToDouble(editPercentage?.editText?.text.toString(), percentage) + editPercentage = PlusMinusEditText(viewAdapter, initValue, 50.0, 150.0, 1.0, DecimalFormat("0"), false, getString(R.string.action_percentage)) container.addView(view) view }