From bcc573202c90ffad9eb2f919461502f9907fb793 Mon Sep 17 00:00:00 2001 From: Milos Kozak Date: Thu, 21 Jan 2021 17:03:05 +0100 Subject: [PATCH] CarbsDialog -> jetpack --- .../androidaps/dialogs/CarbsDialog.kt | 107 ++++++++++-------- app/src/main/res/layout/dialog_carbs.xml | 34 +++--- app/src/main/res/values/strings.xml | 1 - 3 files changed, 80 insertions(+), 62 deletions(-) diff --git a/app/src/main/java/info/nightscout/androidaps/dialogs/CarbsDialog.kt b/app/src/main/java/info/nightscout/androidaps/dialogs/CarbsDialog.kt index f911c1ef66..2f252f2594 100644 --- a/app/src/main/java/info/nightscout/androidaps/dialogs/CarbsDialog.kt +++ b/app/src/main/java/info/nightscout/androidaps/dialogs/CarbsDialog.kt @@ -11,6 +11,7 @@ import info.nightscout.androidaps.Constants import info.nightscout.androidaps.MainApp import info.nightscout.androidaps.R import info.nightscout.androidaps.data.Profile +import info.nightscout.androidaps.databinding.DialogCarbsBinding import info.nightscout.androidaps.db.CareportalEvent import info.nightscout.androidaps.db.Source import info.nightscout.androidaps.db.TempTarget @@ -29,15 +30,13 @@ import info.nightscout.androidaps.utils.ToastUtils import info.nightscout.androidaps.utils.alertDialogs.OKDialog import info.nightscout.androidaps.utils.extensions.formatColor import info.nightscout.androidaps.utils.resources.ResourceHelper -import kotlinx.android.synthetic.main.dialog_carbs.* -import kotlinx.android.synthetic.main.notes.* -import kotlinx.android.synthetic.main.okcancel.* import java.text.DecimalFormat import java.util.* import javax.inject.Inject import kotlin.math.max class CarbsDialog : DialogFragmentWithDate() { + @Inject lateinit var mainApp: MainApp @Inject lateinit var resourceHelper: ResourceHelper @Inject lateinit var constraintChecker: ConstraintChecker @@ -49,6 +48,7 @@ class CarbsDialog : DialogFragmentWithDate() { @Inject lateinit var carbsGenerator: CarbsGenerator companion object { + private const val FAV1_DEFAULT = 5 private const val FAV2_DEFAULT = 10 private const val FAV3_DEFAULT = 20 @@ -65,92 +65,105 @@ class CarbsDialog : DialogFragmentWithDate() { private fun validateInputs() { val maxCarbs = constraintChecker.getMaxCarbsAllowed().value().toDouble() - val time = overview_carbs_time.value.toInt() + val time = binding.time.value.toInt() if (time > 12 * 60 || time < -12 * 60) { - overview_carbs_time.value = 0.0 + binding.time.value = 0.0 ToastUtils.showToastInUiThread(mainApp, resourceHelper.gs(R.string.constraintapllied)) } - if (overview_carbs_duration.value > 10) { - overview_carbs_duration.value = 0.0 + if (binding.duration.value > 10) { + binding.duration.value = 0.0 ToastUtils.showToastInUiThread(mainApp, resourceHelper.gs(R.string.constraintapllied)) } - if (overview_carbs_carbs.value.toInt() > maxCarbs) { - overview_carbs_carbs.value = 0.0 + if (binding.carbs.value.toInt() > maxCarbs) { + binding.carbs.value = 0.0 ToastUtils.showToastInUiThread(mainApp, resourceHelper.gs(R.string.carbsconstraintapplied)) } } + private var _binding: DialogCarbsBinding? = 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_carbs_time", overview_carbs_time.value) - savedInstanceState.putDouble("overview_carbs_duration", overview_carbs_duration.value) - savedInstanceState.putDouble("overview_carbs_carbs", overview_carbs_carbs.value) + savedInstanceState.putDouble("time", binding.time.value) + savedInstanceState.putDouble("duration", binding.duration.value) + savedInstanceState.putDouble("carbs", binding.carbs.value) } override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, - savedInstanceState: Bundle?): View? { + savedInstanceState: Bundle?): View { onCreateViewGeneral() - return inflater.inflate(R.layout.dialog_carbs, container, false) + _binding = DialogCarbsBinding.inflate(inflater, container, false) + return binding.root } override fun onViewCreated(view: View, savedInstanceState: Bundle?) { super.onViewCreated(view, savedInstanceState) val maxCarbs = constraintChecker.getMaxCarbsAllowed().value().toDouble() - overview_carbs_time.setParams(savedInstanceState?.getDouble("overview_carbs_time") - ?: 0.0, -12 * 60.0, 12 * 60.0, 5.0, DecimalFormat("0"), false, ok, textWatcher) + binding.time.setParams(savedInstanceState?.getDouble("time") + ?: 0.0, -12 * 60.0, 12 * 60.0, 5.0, DecimalFormat("0"), false, binding.okcancel.ok, textWatcher) - overview_carbs_duration.setParams(savedInstanceState?.getDouble("overview_carbs_duration") - ?: 0.0, 0.0, 10.0, 1.0, DecimalFormat("0"), false, ok, textWatcher) + binding.duration.setParams(savedInstanceState?.getDouble("duration") + ?: 0.0, 0.0, 10.0, 1.0, DecimalFormat("0"), false, binding.okcancel.ok, textWatcher) - overview_carbs_carbs.setParams(savedInstanceState?.getDouble("overview_carbs_carbs") - ?: 0.0, 0.0, maxCarbs, 1.0, DecimalFormat("0"), false, ok, textWatcher) + binding.carbs.setParams(savedInstanceState?.getDouble("carbs") + ?: 0.0, 0.0, maxCarbs, 1.0, DecimalFormat("0"), false, binding.okcancel.ok, textWatcher) - overview_carbs_plus1.text = toSignedString(sp.getInt(R.string.key_carbs_button_increment_1, FAV1_DEFAULT)) - overview_carbs_plus1.setOnClickListener { - overview_carbs_carbs.value = max(0.0, overview_carbs_carbs.value + binding.plus1.text = toSignedString(sp.getInt(R.string.key_carbs_button_increment_1, FAV1_DEFAULT)) + binding.plus1.setOnClickListener { + binding.carbs.value = max(0.0, binding.carbs.value + sp.getInt(R.string.key_carbs_button_increment_1, FAV1_DEFAULT)) validateInputs() } - overview_carbs_plus2.text = toSignedString(sp.getInt(R.string.key_carbs_button_increment_2, FAV2_DEFAULT)) - overview_carbs_plus2.setOnClickListener { - overview_carbs_carbs.value = max(0.0, overview_carbs_carbs.value + binding.plus2.text = toSignedString(sp.getInt(R.string.key_carbs_button_increment_2, FAV2_DEFAULT)) + binding.plus2.setOnClickListener { + binding.carbs.value = max(0.0, binding.carbs.value + sp.getInt(R.string.key_carbs_button_increment_2, FAV2_DEFAULT)) validateInputs() } - overview_carbs_plus3.text = toSignedString(sp.getInt(R.string.key_carbs_button_increment_3, FAV3_DEFAULT)) - overview_carbs_plus3.setOnClickListener { - overview_carbs_carbs.value = max(0.0, overview_carbs_carbs.value + binding.plus3.text = toSignedString(sp.getInt(R.string.key_carbs_button_increment_3, FAV3_DEFAULT)) + binding.plus3.setOnClickListener { + binding.carbs.value = max(0.0, binding.carbs.value + sp.getInt(R.string.key_carbs_button_increment_3, FAV3_DEFAULT)) validateInputs() } iobCobCalculatorPlugin.actualBg()?.let { bgReading -> if (bgReading.value < 72) - overview_carbs_hypo_tt.isChecked = true + binding.hypoTt.isChecked = true } - overview_carbs_hypo_tt.setOnClickListener { - overview_carbs_activity_tt.isChecked = false - overview_carbs_eating_soon_tt.isChecked = false + binding.hypoTt.setOnClickListener { + binding.activityTt.isChecked = false + binding.eatingSoonTt.isChecked = false } - overview_carbs_activity_tt.setOnClickListener { - overview_carbs_hypo_tt.isChecked = false - overview_carbs_eating_soon_tt.isChecked = false + binding.activityTt.setOnClickListener { + binding.hypoTt.isChecked = false + binding.eatingSoonTt.isChecked = false } - overview_carbs_eating_soon_tt.setOnClickListener { - overview_carbs_hypo_tt.isChecked = false - overview_carbs_activity_tt.isChecked = false + binding.eatingSoonTt.setOnClickListener { + binding.hypoTt.isChecked = false + binding.activityTt.isChecked = false } } + override fun onDestroyView() { + super.onDestroyView() + _binding = null + } + private fun toSignedString(value: Int): String { return if (value > 0) "+$value" else value.toString() } override fun submit(): Boolean { - val carbs = overview_carbs_carbs?.value?.toInt() ?: return false + if (_binding == null) return false + val carbs = binding.carbs.value?.toInt() ?: return false val carbsAfterConstraints = constraintChecker.applyCarbsConstraints(Constraint(carbs)).value() val units = profileFunction.getUnits() val activityTTDuration = defaultValueHelper.determineActivityTTDuration() @@ -162,22 +175,22 @@ class CarbsDialog : DialogFragmentWithDate() { val actions: LinkedList = LinkedList() val unitLabel = if (units == Constants.MMOL) resourceHelper.gs(R.string.mmol) else resourceHelper.gs(R.string.mgdl) - val activitySelected = overview_carbs_activity_tt.isChecked + val activitySelected = binding.activityTt.isChecked if (activitySelected) actions.add(resourceHelper.gs(R.string.temptargetshort) + ": " + (DecimalFormatter.to1Decimal(activityTT) + " " + unitLabel + " (" + resourceHelper.gs(R.string.format_mins, activityTTDuration) + ")").formatColor(resourceHelper, R.color.tempTargetConfirmation)) - val eatingSoonSelected = overview_carbs_eating_soon_tt.isChecked + val eatingSoonSelected = binding.eatingSoonTt.isChecked if (eatingSoonSelected) actions.add(resourceHelper.gs(R.string.temptargetshort) + ": " + (DecimalFormatter.to1Decimal(eatingSoonTT) + " " + unitLabel + " (" + resourceHelper.gs(R.string.format_mins, eatingSoonTTDuration) + ")").formatColor(resourceHelper, R.color.tempTargetConfirmation)) - val hypoSelected = overview_carbs_hypo_tt.isChecked + val hypoSelected = binding.hypoTt.isChecked if (hypoSelected) actions.add(resourceHelper.gs(R.string.temptargetshort) + ": " + (DecimalFormatter.to1Decimal(hypoTT) + " " + unitLabel + " (" + resourceHelper.gs(R.string.format_mins, hypoTTDuration) + ")").formatColor(resourceHelper, R.color.tempTargetConfirmation)) - val timeOffset = overview_carbs_time.value.toInt() + val timeOffset = binding.time.value.toInt() eventTime -= eventTime % 1000 val time = eventTime + timeOffset * 1000 * 60 if (timeOffset != 0) actions.add(resourceHelper.gs(R.string.time) + ": " + dateUtil.dateAndTimeString(time)) - val duration = overview_carbs_duration.value.toInt() + val duration = binding.duration.value.toInt() if (duration > 0) actions.add(resourceHelper.gs(R.string.duration) + ": " + duration + resourceHelper.gs(R.string.shorthour)) if (carbsAfterConstraints > 0) { @@ -185,7 +198,7 @@ class CarbsDialog : DialogFragmentWithDate() { if (carbsAfterConstraints != carbs) actions.add("" + resourceHelper.gs(R.string.carbsconstraintapplied) + "") } - val notes = notes.text.toString() + val notes = binding.notesLayout.notes.text.toString() if (notes.isNotEmpty()) actions.add(resourceHelper.gs(R.string.careportal_newnstreatment_notes_label) + ": " + notes) @@ -194,7 +207,7 @@ class CarbsDialog : DialogFragmentWithDate() { if (carbsAfterConstraints > 0 || activitySelected || eatingSoonSelected || hypoSelected) { activity?.let { activity -> - OKDialog.showConfirmation(activity, resourceHelper.gs(R.string.carbs), HtmlHelper.fromHtml(Joiner.on("
").join(actions)), Runnable { + OKDialog.showConfirmation(activity, resourceHelper.gs(R.string.carbs), HtmlHelper.fromHtml(Joiner.on("
").join(actions)), { when { activitySelected -> { aapsLogger.debug("USER ENTRY: TEMPTARGET ACTIVITY $activityTT duration: $activityTTDuration") diff --git a/app/src/main/res/layout/dialog_carbs.xml b/app/src/main/res/layout/dialog_carbs.xml index cd37c4795d..66c58a61bf 100644 --- a/app/src/main/res/layout/dialog_carbs.xml +++ b/app/src/main/res/layout/dialog_carbs.xml @@ -22,7 +22,7 @@ android:orientation="horizontal" android:padding="5dp"> - @@ -92,7 +92,7 @@ android:textStyle="bold" /> @@ -126,7 +126,7 @@ android:textStyle="bold" /> @@ -155,12 +155,12 @@ android:width="120dp" android:paddingLeft="10dp" android:paddingRight="10dp" - android:text="@string/overview_carbs_label" + android:text="@string/treatments_wizard_carbs_label" android:textAppearance="@style/TextAppearance.AppCompat.Small" android:textStyle="bold" /> @@ -184,21 +184,21 @@ android:padding="5dp">