From 24eec08a3529fc70dd0f8ac3f15274d9282511bf Mon Sep 17 00:00:00 2001 From: Milos Kozak Date: Thu, 21 Jan 2021 16:45:54 +0100 Subject: [PATCH] CalibrationDialog -> jetpack --- .../androidaps/dialogs/CalibrationDialog.kt | 38 ++++++++++++------- .../main/res/layout/dialog_calibration.xml | 12 +++--- 2 files changed, 32 insertions(+), 18 deletions(-) diff --git a/app/src/main/java/info/nightscout/androidaps/dialogs/CalibrationDialog.kt b/app/src/main/java/info/nightscout/androidaps/dialogs/CalibrationDialog.kt index 894f017995..d2e9b11a4e 100644 --- a/app/src/main/java/info/nightscout/androidaps/dialogs/CalibrationDialog.kt +++ b/app/src/main/java/info/nightscout/androidaps/dialogs/CalibrationDialog.kt @@ -9,14 +9,13 @@ import dagger.android.HasAndroidInjector import info.nightscout.androidaps.Constants import info.nightscout.androidaps.R import info.nightscout.androidaps.data.Profile +import info.nightscout.androidaps.databinding.DialogCalibrationBinding import info.nightscout.androidaps.interfaces.ProfileFunction import info.nightscout.androidaps.plugins.iob.iobCobCalculator.GlucoseStatus import info.nightscout.androidaps.utils.HtmlHelper -import info.nightscout.androidaps.utils.alertDialogs.OKDialog import info.nightscout.androidaps.utils.XdripCalibrations +import info.nightscout.androidaps.utils.alertDialogs.OKDialog import info.nightscout.androidaps.utils.resources.ResourceHelper -import kotlinx.android.synthetic.main.dialog_calibration.* -import kotlinx.android.synthetic.main.okcancel.* import java.text.DecimalFormat import java.util.* import javax.inject.Inject @@ -28,15 +27,22 @@ class CalibrationDialog : DialogFragmentWithDate() { @Inject lateinit var profileFunction: ProfileFunction @Inject lateinit var xdripCalibrations: XdripCalibrations + private var _binding: DialogCalibrationBinding? = null + + // This property is only valid between onCreateView and + // onDestroyView. + private val binding get() = _binding!! + override fun onSaveInstanceState(savedInstanceState: Bundle) { super.onSaveInstanceState(savedInstanceState) - savedInstanceState.putDouble("overview_calibration_bg", overview_calibration_bg.value) + savedInstanceState.putDouble("bg", binding.bg.value) } override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, - savedInstanceState: Bundle?): View? { + savedInstanceState: Bundle?): View { onCreateViewGeneral() - return inflater.inflate(R.layout.dialog_calibration, container, false) + _binding = DialogCalibrationBinding.inflate(inflater, container, false) + return binding.root } override fun onViewCreated(view: View, savedInstanceState: Bundle?) { @@ -46,23 +52,29 @@ class CalibrationDialog : DialogFragmentWithDate() { val bg = Profile.fromMgdlToUnits(GlucoseStatus(injector).glucoseStatusData?.glucose ?: 0.0, units) if (units == Constants.MMOL) - overview_calibration_bg.setParams(savedInstanceState?.getDouble("overview_calibration_bg") - ?: bg, 2.0, 30.0, 0.1, DecimalFormat("0.0"), false, ok) + binding.bg.setParams(savedInstanceState?.getDouble("bg") + ?: bg, 2.0, 30.0, 0.1, DecimalFormat("0.0"), false, binding.okcancel.ok) else - overview_calibration_bg.setParams(savedInstanceState?.getDouble("overview_calibration_bg") - ?: bg, 36.0, 500.0, 1.0, DecimalFormat("0"), false, ok) - overview_calibration_units.text = if (units == Constants.MMOL) resourceHelper.gs(R.string.mmol) else resourceHelper.gs(R.string.mgdl) + binding.bg.setParams(savedInstanceState?.getDouble("bg") + ?: bg, 36.0, 500.0, 1.0, DecimalFormat("0"), false, binding.okcancel.ok) + binding.units.text = if (units == Constants.MMOL) resourceHelper.gs(R.string.mmol) else resourceHelper.gs(R.string.mgdl) + } + + override fun onDestroyView() { + super.onDestroyView() + _binding = null } override fun submit(): Boolean { + if (_binding == null) return false val units = profileFunction.getUnits() val unitLabel = if (units == Constants.MMOL) resourceHelper.gs(R.string.mmol) else resourceHelper.gs(R.string.mgdl) val actions: LinkedList = LinkedList() - val bg = overview_calibration_bg?.value ?: return false + val bg = binding.bg.value ?: return false actions.add(resourceHelper.gs(R.string.treatments_wizard_bg_label) + ": " + Profile.toCurrentUnitsString(profileFunction, bg) + " " + unitLabel) if (bg > 0) { activity?.let { activity -> - OKDialog.showConfirmation(activity, resourceHelper.gs(R.string.overview_calibration), HtmlHelper.fromHtml(Joiner.on("
").join(actions)), Runnable { + OKDialog.showConfirmation(activity, resourceHelper.gs(R.string.overview_calibration), HtmlHelper.fromHtml(Joiner.on("
").join(actions)), { aapsLogger.debug("USER ENTRY: CALIBRATION $bg") xdripCalibrations.sendIntent(bg) }) diff --git a/app/src/main/res/layout/dialog_calibration.xml b/app/src/main/res/layout/dialog_calibration.xml index d236cd6a1a..1fc30ee0f9 100644 --- a/app/src/main/res/layout/dialog_calibration.xml +++ b/app/src/main/res/layout/dialog_calibration.xml @@ -30,7 +30,7 @@ app:srcCompat="@drawable/ic_calibration" /> - +