From 99185f3d1f89beccbb04a87036db5f980fc71779 Mon Sep 17 00:00:00 2001 From: Milos Kozak Date: Mon, 1 Feb 2021 20:36:58 +0100 Subject: [PATCH] EditQuickWizardDialog -> jetpack --- .../overview/dialogs/EditQuickWizardDialog.kt | 160 ++++--- .../utils/extensions/RadioGroupExtension.kt | 16 + .../overview_editquickwizard_dialog.xml | 414 ++++++++++++------ app/src/main/res/values/arrays.xml | 12 - 4 files changed, 394 insertions(+), 208 deletions(-) create mode 100644 app/src/main/java/info/nightscout/androidaps/utils/extensions/RadioGroupExtension.kt diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/general/overview/dialogs/EditQuickWizardDialog.kt b/app/src/main/java/info/nightscout/androidaps/plugins/general/overview/dialogs/EditQuickWizardDialog.kt index 463c7564fa..d2bf09df4f 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/general/overview/dialogs/EditQuickWizardDialog.kt +++ b/app/src/main/java/info/nightscout/androidaps/plugins/general/overview/dialogs/EditQuickWizardDialog.kt @@ -1,29 +1,33 @@ package info.nightscout.androidaps.plugins.general.overview.dialogs +import android.app.TimePickerDialog import android.os.Bundle +import android.text.format.DateFormat import android.view.LayoutInflater import android.view.View import android.view.ViewGroup import android.view.Window import android.view.WindowManager -import android.widget.AdapterView -import android.widget.ArrayAdapter import dagger.android.support.DaggerDialogFragment -import info.nightscout.androidaps.R -import info.nightscout.androidaps.utils.wizard.QuickWizard -import info.nightscout.androidaps.utils.wizard.QuickWizardEntry +import info.nightscout.androidaps.databinding.OverviewEditquickwizardDialogBinding import info.nightscout.androidaps.logging.AAPSLogger import info.nightscout.androidaps.plugins.bus.RxBusWrapper import info.nightscout.androidaps.plugins.general.overview.events.EventQuickWizardChange import info.nightscout.androidaps.utils.DateUtil import info.nightscout.androidaps.utils.SafeParse +import info.nightscout.androidaps.utils.T +import info.nightscout.androidaps.utils.extensions.selectedItemPosition +import info.nightscout.androidaps.utils.extensions.setEnableForChildren +import info.nightscout.androidaps.utils.extensions.setSelection +import info.nightscout.androidaps.utils.wizard.QuickWizard +import info.nightscout.androidaps.utils.wizard.QuickWizardEntry import kotlinx.android.synthetic.main.okcancel.* import kotlinx.android.synthetic.main.overview_editquickwizard_dialog.* import org.json.JSONException -import java.util.* import javax.inject.Inject -class EditQuickWizardDialog : DaggerDialogFragment() { +class EditQuickWizardDialog : DaggerDialogFragment(), View.OnClickListener { + @Inject lateinit var rxBus: RxBusWrapper @Inject lateinit var aapsLogger: AAPSLogger @Inject lateinit var quickWizard: QuickWizard @@ -31,37 +35,43 @@ class EditQuickWizardDialog : DaggerDialogFragment() { var position = -1 + var fromSeconds: Int = 0 + var toSeconds: Int = 0 + + private var _binding: OverviewEditquickwizardDialogBinding? = null + + // This property is only valid between onCreateView and + // onDestroyView. + private val binding get() = _binding!! + override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, - savedInstanceState: Bundle?): View? { + savedInstanceState: Bundle?): View { dialog?.window?.requestFeature(Window.FEATURE_NO_TITLE) dialog?.window?.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN) isCancelable = true dialog?.setCanceledOnTouchOutside(false) - return inflater.inflate(R.layout.overview_editquickwizard_dialog, container, false) + _binding = OverviewEditquickwizardDialogBinding.inflate(inflater, container, false) + return binding.root } override fun onViewCreated(view: View, savedInstanceState: Bundle?) { (arguments ?: savedInstanceState)?.let { bundle -> position = bundle.getInt("position", -1) } - val entry = if (position ==-1) quickWizard.newEmptyItem() else quickWizard[position] + val entry = if (position == -1) quickWizard.newEmptyItem() else quickWizard[position] ok.setOnClickListener { - if (overview_editquickwizard_from_spinner.selectedItem == null) return@setOnClickListener - if (overview_editquickwizard_to_spinner.selectedItem == null) return@setOnClickListener try { - entry.storage.put("buttonText", overview_editquickwizard_button_edit.text.toString()) - entry.storage.put("carbs", SafeParse.stringToInt(overview_editquickwizard_carbs_edit.text.toString())) - val validFromInt = DateUtil.toSeconds(overview_editquickwizard_from_spinner.selectedItem.toString()) - entry.storage.put("validFrom", validFromInt) - val validToInt = DateUtil.toSeconds(overview_editquickwizard_to_spinner.selectedItem.toString()) - entry.storage.put("validTo", validToInt) - entry.storage.put("useBG", overview_editquickwizard_usebg_spinner.selectedItemPosition) - entry.storage.put("useCOB", overview_editquickwizard_usecob_spinner.selectedItemPosition) - entry.storage.put("useBolusIOB", overview_editquickwizard_usebolusiob_spinner.selectedItemPosition) - entry.storage.put("useBasalIOB", overview_editquickwizard_usebasaliob_spinner.selectedItemPosition) - entry.storage.put("useTrend", overview_editquickwizard_usetrend_spinner.selectedItemPosition) - entry.storage.put("useSuperBolus", overview_editquickwizard_usesuperbolus_spinner.selectedItemPosition) - entry.storage.put("useTempTarget", overview_editquickwizard_usetemptarget_spinner.selectedItemPosition) + entry.storage.put("buttonText", binding.buttonEdit.text.toString()) + entry.storage.put("carbs", SafeParse.stringToInt(binding.carbsEdit.text.toString())) + entry.storage.put("validFrom", fromSeconds) + entry.storage.put("validTo", toSeconds) + entry.storage.put("useBG", binding.useBg.selectedItemPosition) + entry.storage.put("useCOB", binding.useCob.selectedItemPosition) + entry.storage.put("useBolusIOB", binding.useBolusIob.selectedItemPosition) + entry.storage.put("useBasalIOB", binding.useBasalIob.selectedItemPosition) + entry.storage.put("useTrend", binding.useTrend.selectedItemPosition) + entry.storage.put("useSuperBolus", binding.useSuperBolus.selectedItemPosition) + entry.storage.put("useTempTarget", binding.useTempTarget.selectedItemPosition) } catch (e: JSONException) { aapsLogger.error("Unhandled exception", e) } @@ -72,41 +82,58 @@ class EditQuickWizardDialog : DaggerDialogFragment() { } cancel.setOnClickListener { dismiss() } - var posFrom = 0 - var posTo = 95 - val timeList = ArrayList() - var pos = 0 - var t = 0 - while (t < 24 * 60 * 60) { - timeList.add(dateUtil.timeString(DateUtil.toDate(t))) - if (entry.validFrom() == t) posFrom = pos - if (entry.validTo() == t) posTo = pos - pos++ - t += 15 * 60 + // create an OnTimeSetListener + val fromTimeSetListener = TimePickerDialog.OnTimeSetListener { _, hour, minute -> + fromSeconds = (T.hours(hour.toLong()).secs() + T.mins(minute.toLong()).secs()).toInt() + from.text = dateUtil.timeString(DateUtil.toDate(fromSeconds)) } - timeList.add(dateUtil.timeString(DateUtil.toDate(24 * 60 * 60 - 60))) - val adapter = context?.let { context -> ArrayAdapter(context, R.layout.spinner_centered, timeList) } - overview_editquickwizard_from_spinner.adapter = adapter - overview_editquickwizard_to_spinner.adapter = adapter - - overview_editquickwizard_button_edit.setText(entry.buttonText()) - overview_editquickwizard_carbs_edit.setText(entry.carbs().toString()) - overview_editquickwizard_from_spinner.setSelection(posFrom) - overview_editquickwizard_to_spinner.setSelection(posTo) - - overview_editquickwizard_usebg_spinner.setSelection(entry.useBG()) - overview_editquickwizard_usecob_spinner.setSelection(entry.useCOB()) - overview_editquickwizard_usebolusiob_spinner.setSelection(entry.useBolusIOB()) - overview_editquickwizard_usebasaliob_spinner.setSelection(entry.useBasalIOB()) - overview_editquickwizard_usetrend_spinner.setSelection(entry.useTrend()) - overview_editquickwizard_usesuperbolus_spinner.setSelection(entry.useSuperBolus()) - overview_editquickwizard_usetemptarget_spinner.setSelection(entry.useTempTarget()) - - overview_editquickwizard_usecob_spinner.onItemSelectedListener = object : AdapterView.OnItemSelectedListener { - override fun onItemSelected(parent: AdapterView<*>, view: View?, position: Int, id: Long) = processCob() - override fun onNothingSelected(parent: AdapterView<*>) {} + binding.from.setOnClickListener { + context?.let { + TimePickerDialog(it, fromTimeSetListener, + T.secs(fromSeconds.toLong()).hours().toInt(), + T.secs((fromSeconds % 3600).toLong()).mins().toInt(), + DateFormat.is24HourFormat(context) + ).show() + } } + fromSeconds = entry.validFrom() + from.text = dateUtil.timeString(DateUtil.toDate(fromSeconds)) + + val toTimeSetListener = TimePickerDialog.OnTimeSetListener { _, hour, minute -> + toSeconds = (T.hours(hour.toLong()).secs() + T.mins(minute.toLong()).secs()).toInt() + binding.from.text = dateUtil.timeString(DateUtil.toDate(toSeconds)) + } + + binding.to.setOnClickListener { + context?.let { + TimePickerDialog(it, toTimeSetListener, + T.secs(fromSeconds.toLong()).hours().toInt(), + T.secs((fromSeconds % 3600).toLong()).mins().toInt(), + DateFormat.is24HourFormat(context) + ).show() + } + } + toSeconds = entry.validFrom() + binding.to.text = dateUtil.timeString(DateUtil.toDate(toSeconds)) + + binding.buttonEdit.setText(entry.buttonText()) + binding.carbsEdit.setText(entry.carbs().toString()) + + binding.useBg.setSelection(entry.useBG()) + binding.useCob.setSelection(entry.useCOB()) + binding.useBolusIob.setSelection(entry.useBolusIOB()) + binding.useBasalIob.setSelection(entry.useBasalIOB()) + binding.useTrend.setSelection(entry.useTrend()) + binding.useSuperBolus.setSelection(entry.useSuperBolus()) + binding.useTempTarget.setSelection(entry.useTempTarget()) + + use_cob_yes.setOnClickListener(this) + use_cob_no.setOnClickListener(this) + processCob() + } + + override fun onClick(v: View?) { processCob() } @@ -120,15 +147,20 @@ class EditQuickWizardDialog : DaggerDialogFragment() { outState.putInt("position", position) } + override fun onDestroyView() { + super.onDestroyView() + _binding = null + } + private fun processCob() { - if (overview_editquickwizard_usecob_spinner.selectedItemPosition == QuickWizardEntry.YES) { - overview_editquickwizard_usebolusiob_spinner.isEnabled = false - overview_editquickwizard_usebasaliob_spinner.isEnabled = false - overview_editquickwizard_usebolusiob_spinner.setSelection(QuickWizardEntry.YES) - overview_editquickwizard_usebasaliob_spinner.setSelection(QuickWizardEntry.YES) + if (binding.useCob.selectedItemPosition == QuickWizardEntry.YES) { + binding.useBolusIob.setEnableForChildren(false) + binding.useBasalIob.setEnableForChildren(false) + binding.useBolusIob.setSelection(QuickWizardEntry.YES) + binding.useBasalIob.setSelection(QuickWizardEntry.YES) } else { - overview_editquickwizard_usebolusiob_spinner.isEnabled = true - overview_editquickwizard_usebasaliob_spinner.isEnabled = true + binding.useBolusIob.setEnableForChildren(true) + binding.useBasalIob.setEnableForChildren(true) } } } diff --git a/app/src/main/java/info/nightscout/androidaps/utils/extensions/RadioGroupExtension.kt b/app/src/main/java/info/nightscout/androidaps/utils/extensions/RadioGroupExtension.kt new file mode 100644 index 0000000000..0bf51f656f --- /dev/null +++ b/app/src/main/java/info/nightscout/androidaps/utils/extensions/RadioGroupExtension.kt @@ -0,0 +1,16 @@ +package info.nightscout.androidaps.utils.extensions + +import android.widget.RadioGroup +import androidx.appcompat.widget.AppCompatRadioButton +import androidx.core.view.forEach + +val RadioGroup.selectedItemPosition: Int + get() = this.indexOfChild(this.findViewById(this.checkedRadioButtonId)) + +fun RadioGroup.setSelection(index: Int) { + (this.getChildAt(index) as AppCompatRadioButton).isChecked = true +} + +fun RadioGroup.setEnableForChildren(state : Boolean) { + forEach { child -> child.isEnabled = state} +} \ No newline at end of file diff --git a/app/src/main/res/layout/overview_editquickwizard_dialog.xml b/app/src/main/res/layout/overview_editquickwizard_dialog.xml index f7517bcfbe..5dd381371a 100644 --- a/app/src/main/res/layout/overview_editquickwizard_dialog.xml +++ b/app/src/main/res/layout/overview_editquickwizard_dialog.xml @@ -1,159 +1,309 @@ - - + android:layout_height="wrap_content" + android:orientation="vertical"> + + + + + + + + + + + + + + + + + android:layout_height="wrap_content" + android:orientation="horizontal"> - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/app/src/main/res/values/arrays.xml b/app/src/main/res/values/arrays.xml index 3328d5a3fe..0b02d9a048 100644 --- a/app/src/main/res/values/arrays.xml +++ b/app/src/main/res/values/arrays.xml @@ -87,18 +87,6 @@ @string/key_pregnant - - @string/yes - @string/no - - - - @string/yes - @string/no - @string/positiveonly - @string/negativeonly - - @string/use_passive_location @string/use_network_location