diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/general/autotune/AutotuneFragment.kt b/app/src/main/java/info/nightscout/androidaps/plugins/general/autotune/AutotuneFragment.kt index 7714a6bbc2..b968c7fc88 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/general/autotune/AutotuneFragment.kt +++ b/app/src/main/java/info/nightscout/androidaps/plugins/general/autotune/AutotuneFragment.kt @@ -381,57 +381,80 @@ class AutotuneFragment : DaggerFragment() { } private fun showResults() { - Thread { + context?.let { context -> runOnUiThread { - binding.autotuneResults.removeAllViews() - if (autotunePlugin.result.isNotBlank()) { - var toMgDl = 1.0 - if (profileFunction.getUnits() == GlucoseUnit.MMOL) toMgDl = Constants.MMOLL_TO_MGDL - val isfFormat = if (profileFunction.getUnits() == GlucoseUnit.MMOL) "%.2f" else "%.1f" - binding.autotuneResults.addView( - TableLayout(context).also { layout -> - layout.addView( - TextView(context).apply { - text = autotunePlugin.result - setTypeface(typeface, Typeface.BOLD) - gravity = Gravity.CENTER_HORIZONTAL - setTextAppearance(android.R.style.TextAppearance_Material_Medium) - }) - autotunePlugin.tunedProfile?.let { tuned -> - layout.addView(toTableRowHeader()) - val tuneInsulin = sp.getBoolean(R.string.key_autotune_tune_insulin_curve, false) - if (tuneInsulin) { - layout.addView(toTableRowValue(rh.gs(R.string.insulin_peak), autotunePlugin.pumpProfile.localInsulin.peak.toDouble(), tuned.localInsulin.peak.toDouble(), "%.0f")) - layout.addView(toTableRowValue(rh.gs(R.string.dia), Round.roundTo(autotunePlugin.pumpProfile.localInsulin.dia, 0.1), Round.roundTo(tuned.localInsulin.dia, 0.1),"%.1f")) - } - layout.addView(toTableRowValue(rh.gs(R.string.isf_short), Round.roundTo(autotunePlugin.pumpProfile.isf / toMgDl, 0.001), Round.roundTo(tuned.isf / toMgDl, 0.001), isfFormat)) - layout.addView(toTableRowValue(rh.gs(R.string.ic_short), Round.roundTo(autotunePlugin.pumpProfile.ic, 0.001), Round.roundTo(tuned.ic, 0.001), "%.2f")) + _binding?.let { + binding.autotuneResults.removeAllViews() + if (autotunePlugin.result.isNotBlank()) { + var toMgDl = 1.0 + if (profileFunction.getUnits() == GlucoseUnit.MMOL) toMgDl = Constants.MMOLL_TO_MGDL + val isfFormat = if (profileFunction.getUnits() == GlucoseUnit.MMOL) "%.2f" else "%.1f" + binding.autotuneResults.addView( + TableLayout(context).also { layout -> layout.addView( TextView(context).apply { - text = rh.gs(R.string.basal) + text = autotunePlugin.result setTypeface(typeface, Typeface.BOLD) gravity = Gravity.CENTER_HORIZONTAL setTextAppearance(android.R.style.TextAppearance_Material_Medium) + }) + autotunePlugin.tunedProfile?.let { tuned -> + layout.addView(toTableRowHeader()) + val tuneInsulin = sp.getBoolean(R.string.key_autotune_tune_insulin_curve, false) + if (tuneInsulin) { + layout.addView( + toTableRowValue( + rh.gs(R.string.insulin_peak), + autotunePlugin.pumpProfile.localInsulin.peak.toDouble(), + tuned.localInsulin.peak.toDouble(), + "%.0f" + ) + ) + layout.addView( + toTableRowValue( + rh.gs(R.string.dia), + Round.roundTo(autotunePlugin.pumpProfile.localInsulin.dia, 0.1), + Round.roundTo(tuned.localInsulin.dia, 0.1), + "%.1f" + ) + ) } - ) - layout.addView(toTableRowHeader(true)) - var totalPump = 0.0 - var totalTuned = 0.0 - for (h in 0 until tuned.basal.size) { - val df = DecimalFormat("00") - val time = df.format(h.toLong()) + ":00" - totalPump += autotunePlugin.pumpProfile.basal[h] - totalTuned += tuned.basal[h] - layout.addView(toTableRowValue(time, autotunePlugin.pumpProfile.basal[h], tuned.basal[h], "%.3f", tuned.basalUntuned[h].toString())) + layout.addView( + toTableRowValue( + rh.gs(R.string.isf_short), + Round.roundTo(autotunePlugin.pumpProfile.isf / toMgDl, 0.001), + Round.roundTo(tuned.isf / toMgDl, 0.001), + isfFormat + ) + ) + layout.addView(toTableRowValue(rh.gs(R.string.ic_short), Round.roundTo(autotunePlugin.pumpProfile.ic, 0.001), Round.roundTo(tuned.ic, 0.001), "%.2f")) + layout.addView( + TextView(context).apply { + text = rh.gs(R.string.basal) + setTypeface(typeface, Typeface.BOLD) + gravity = Gravity.CENTER_HORIZONTAL + setTextAppearance(android.R.style.TextAppearance_Material_Medium) + } + ) + layout.addView(toTableRowHeader(true)) + var totalPump = 0.0 + var totalTuned = 0.0 + for (h in 0 until tuned.basal.size) { + val df = DecimalFormat("00") + val time = df.format(h.toLong()) + ":00" + totalPump += autotunePlugin.pumpProfile.basal[h] + totalTuned += tuned.basal[h] + layout.addView(toTableRowValue(time, autotunePlugin.pumpProfile.basal[h], tuned.basal[h], "%.3f", tuned.basalUntuned[h].toString())) + } + layout.addView(toTableRowValue("∑", totalPump, totalTuned, "%.3f", " ")) } - layout.addView(toTableRowValue("∑", totalPump, totalTuned, "%.3f", " ")) } - } - ) + ) + } + binding.autotuneResultsCard.visibility = if (autotunePlugin.calculationRunning && autotunePlugin.result.isEmpty()) View.GONE else View.VISIBLE } - binding.autotuneResultsCard.visibility = if (autotunePlugin.calculationRunning && autotunePlugin.result.isEmpty()) View.GONE else View.VISIBLE } - }.start() + } } private fun toTableRowHeader(basal:Boolean = false): TableRow =