WizardDialog spinner replacement

This commit is contained in:
Milos Kozak 2022-03-19 13:43:45 +01:00
parent 6d27f5fad0
commit a3ffd23e29
2 changed files with 63 additions and 62 deletions

View file

@ -1,12 +1,16 @@
package info.nightscout.androidaps.dialogs package info.nightscout.androidaps.dialogs
import android.annotation.SuppressLint
import android.content.Context import android.content.Context
import android.os.Bundle import android.os.Bundle
import android.text.Editable import android.text.Editable
import android.text.TextWatcher import android.text.TextWatcher
import android.view.* 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.AdapterView
import android.widget.AdapterView.OnItemSelectedListener
import android.widget.ArrayAdapter import android.widget.ArrayAdapter
import android.widget.CompoundButton import android.widget.CompoundButton
import androidx.fragment.app.FragmentManager import androidx.fragment.app.FragmentManager
@ -20,23 +24,23 @@ import info.nightscout.androidaps.database.ValueWrapper
import info.nightscout.androidaps.databinding.DialogWizardBinding import info.nightscout.androidaps.databinding.DialogWizardBinding
import info.nightscout.androidaps.events.EventAutosensCalculationFinished import info.nightscout.androidaps.events.EventAutosensCalculationFinished
import info.nightscout.androidaps.extensions.formatColor import info.nightscout.androidaps.extensions.formatColor
import info.nightscout.shared.logging.AAPSLogger
import info.nightscout.shared.logging.LTag
import info.nightscout.androidaps.plugins.bus.RxBus
import info.nightscout.androidaps.plugins.configBuilder.ConstraintChecker
import info.nightscout.shared.SafeParse
import info.nightscout.androidaps.extensions.toVisibility import info.nightscout.androidaps.extensions.toVisibility
import info.nightscout.androidaps.extensions.valueToUnits import info.nightscout.androidaps.extensions.valueToUnits
import info.nightscout.androidaps.interfaces.* import info.nightscout.androidaps.interfaces.*
import info.nightscout.androidaps.plugins.bus.RxBus
import info.nightscout.androidaps.plugins.configBuilder.ConstraintChecker
import info.nightscout.androidaps.utils.* import info.nightscout.androidaps.utils.*
import info.nightscout.androidaps.utils.protection.ProtectionCheck import info.nightscout.androidaps.utils.protection.ProtectionCheck
import info.nightscout.androidaps.utils.resources.ResourceHelper import info.nightscout.androidaps.utils.resources.ResourceHelper
import info.nightscout.androidaps.utils.rx.AapsSchedulers import info.nightscout.androidaps.utils.rx.AapsSchedulers
import info.nightscout.shared.sharedPreferences.SP
import info.nightscout.androidaps.utils.wizard.BolusWizard import info.nightscout.androidaps.utils.wizard.BolusWizard
import info.nightscout.shared.SafeParse
import info.nightscout.shared.logging.AAPSLogger
import info.nightscout.shared.logging.LTag
import info.nightscout.shared.sharedPreferences.SP
import io.reactivex.rxjava3.disposables.CompositeDisposable import io.reactivex.rxjava3.disposables.CompositeDisposable
import io.reactivex.rxjava3.kotlin.plusAssign
import java.text.DecimalFormat import java.text.DecimalFormat
import java.util.*
import javax.inject.Inject import javax.inject.Inject
import kotlin.math.abs import kotlin.math.abs
@ -108,8 +112,7 @@ class WizardDialog : DaggerDialogFragment() {
savedInstanceState.putDouble("carb_time_input", binding.carbTimeInput.value) savedInstanceState.putDouble("carb_time_input", binding.carbTimeInput.value)
} }
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View {
savedInstanceState: Bundle?): View {
this.arguments?.let { bundle -> this.arguments?.let { bundle ->
carbsPassedIntoWizard = bundle.getInt("carbs_input").toDouble() carbsPassedIntoWizard = bundle.getInt("carbs_input").toDouble()
notesPassedIntoWizard = bundle.getString("notes_input").toString() notesPassedIntoWizard = bundle.getString("notes_input").toString()
@ -139,14 +142,18 @@ class WizardDialog : DaggerDialogFragment() {
if (profileFunction.getUnits() == GlucoseUnit.MGDL) { if (profileFunction.getUnits() == GlucoseUnit.MGDL) {
binding.bgInput.setParams( binding.bgInput.setParams(
savedInstanceState?.getDouble("bg_input") savedInstanceState?.getDouble("bg_input")
?: 0.0, 0.0, 500.0, 1.0, DecimalFormat("0"), false, binding.okcancel.ok, timeTextWatcher) ?: 0.0, 0.0, 500.0, 1.0, DecimalFormat("0"), false, binding.okcancel.ok, timeTextWatcher
)
} else { } else {
binding.bgInput.setParams( binding.bgInput.setParams(
savedInstanceState?.getDouble("bg_input") savedInstanceState?.getDouble("bg_input")
?: 0.0, 0.0, 30.0, 0.1, DecimalFormat("0.0"), false, binding.okcancel.ok, textWatcher) ?: 0.0, 0.0, 30.0, 0.1, DecimalFormat("0.0"), false, binding.okcancel.ok, textWatcher
)
} }
binding.carbsInput.setParams(savedInstanceState?.getDouble("carbs_input") binding.carbsInput.setParams(
?: 0.0, 0.0, maxCarbs.toDouble(), 1.0, DecimalFormat("0"), false, binding.okcancel.ok, textWatcher) savedInstanceState?.getDouble("carbs_input")
?: 0.0, 0.0, maxCarbs.toDouble(), 1.0, DecimalFormat("0"), false, binding.okcancel.ok, textWatcher
)
if (correctionPercent) { if (correctionPercent) {
calculatedPercentage = sp.getInt(R.string.key_boluswizard_percentage, 100).toDouble() calculatedPercentage = sp.getInt(R.string.key_boluswizard_percentage, 100).toDouble()
@ -156,11 +163,14 @@ class WizardDialog : DaggerDialogFragment() {
} else { } else {
binding.correctionInput.setParams( binding.correctionInput.setParams(
savedInstanceState?.getDouble("correction_input") savedInstanceState?.getDouble("correction_input")
?: 0.0, -maxCorrection, maxCorrection, bolusStep, DecimalFormatter.pumpSupportedBolusFormat(activePlugin.activePump), false, binding.okcancel.ok, textWatcher) ?: 0.0, -maxCorrection, maxCorrection, bolusStep, DecimalFormatter.pumpSupportedBolusFormat(activePlugin.activePump), false, binding.okcancel.ok, textWatcher
)
binding.correctionUnit.text = rh.gs(R.string.insulin_unit_shortname) binding.correctionUnit.text = rh.gs(R.string.insulin_unit_shortname)
} }
binding.carbTimeInput.setParams(savedInstanceState?.getDouble("carb_time_input") binding.carbTimeInput.setParams(
?: 0.0, -60.0, 60.0, 5.0, DecimalFormat("0"), false, binding.okcancel.ok, timeTextWatcher) savedInstanceState?.getDouble("carb_time_input")
?: 0.0, -60.0, 60.0, 5.0, DecimalFormat("0"), false, binding.okcancel.ok, timeTextWatcher
)
initDialog() initDialog()
calculatedPercentage = sp.getInt(R.string.key_boluswizard_percentage, 100).toDouble() calculatedPercentage = sp.getInt(R.string.key_boluswizard_percentage, 100).toDouble()
binding.percentUsed.text = rh.gs(R.string.format_percent, sp.getInt(R.string.key_boluswizard_percentage, 100)) binding.percentUsed.text = rh.gs(R.string.format_percent, sp.getInt(R.string.key_boluswizard_percentage, 100))
@ -211,7 +221,7 @@ class WizardDialog : DaggerDialogFragment() {
processEnabledIcons() processEnabledIcons()
binding.correctionPercent.setOnCheckedChangeListener {_, isChecked -> binding.correctionPercent.setOnCheckedChangeListener { _, isChecked ->
run { run {
sp.putBoolean(rh.gs(R.string.key_wizard_correction_percent), isChecked) sp.putBoolean(rh.gs(R.string.key_wizard_correction_percent), isChecked)
binding.correctionUnit.text = if (isChecked) "%" else rh.gs(R.string.insulin_unit_shortname) binding.correctionUnit.text = if (isChecked) "%" else rh.gs(R.string.insulin_unit_shortname)
@ -230,27 +240,13 @@ class WizardDialog : DaggerDialogFragment() {
binding.correctionInput.value = if (correctionPercent) calculatedPercentage else Round.roundTo(calculatedCorrection, bolusStep) binding.correctionInput.value = if (correctionPercent) calculatedPercentage else Round.roundTo(calculatedCorrection, bolusStep)
} }
} }
// profile spinner // profile
binding.profile.onItemSelectedListener = object : OnItemSelectedListener { binding.profileList.onItemClickListener = AdapterView.OnItemClickListener { _, _, _, _ -> calculateInsulin() }
override fun onNothingSelected(parent: AdapterView<*>?) {
ToastUtils.showToastInUiThread(ctx, rh.gs(R.string.noprofileset))
binding.okcancel.ok.visibility = View.GONE
}
override fun onItemSelected(parent: AdapterView<*>?, view: View?, position: Int, id: Long) {
calculateInsulin()
binding.okcancel.ok.visibility = View.VISIBLE
}
}
// bus // bus
disposable.add(rxBus disposable += rxBus
.toObservable(EventAutosensCalculationFinished::class.java) .toObservable(EventAutosensCalculationFinished::class.java)
.observeOn(aapsSchedulers.main) .observeOn(aapsSchedulers.main)
.subscribe({ .subscribe({ calculateInsulin() }, fabricPrivacy::logException)
activity?.runOnUiThread { calculateInsulin() }
}, fabricPrivacy::logException)
)
setA11yLabels() setA11yLabels()
} }
@ -320,7 +316,7 @@ class WizardDialog : DaggerDialogFragment() {
private fun loadCheckedStates() { private fun loadCheckedStates() {
binding.bgTrendCheckbox.isChecked = sp.getBoolean(R.string.key_wizard_include_trend_bg, false) binding.bgTrendCheckbox.isChecked = sp.getBoolean(R.string.key_wizard_include_trend_bg, false)
binding.cobCheckbox.isChecked = sp.getBoolean(R.string.key_wizard_include_cob, false) binding.cobCheckbox.isChecked = sp.getBoolean(R.string.key_wizard_include_cob, false)
correctionPercent = sp.getBoolean(R.string.key_wizard_correction_percent,false) correctionPercent = sp.getBoolean(R.string.key_wizard_correction_percent, false)
binding.correctionPercent.isChecked = correctionPercent binding.correctionPercent.isChecked = correctionPercent
} }
@ -329,10 +325,10 @@ class WizardDialog : DaggerDialogFragment() {
else DecimalFormatter.to1Decimal(value * Constants.MGDL_TO_MMOLL) else DecimalFormatter.to1Decimal(value * Constants.MGDL_TO_MMOLL)
private fun initDialog() { private fun initDialog() {
if(carbsPassedIntoWizard != 0.0) { if (carbsPassedIntoWizard != 0.0) {
binding.carbsInput.value = carbsPassedIntoWizard binding.carbsInput.value = carbsPassedIntoWizard
} }
if(notesPassedIntoWizard.isNotBlank()) { if (notesPassedIntoWizard.isNotBlank()) {
binding.notes.setText(notesPassedIntoWizard) binding.notes.setText(notesPassedIntoWizard)
} }
val profile = profileFunction.getProfile() val profile = profileFunction.getProfile()
@ -347,9 +343,9 @@ class WizardDialog : DaggerDialogFragment() {
val profileList: ArrayList<CharSequence> = profileStore.getProfileList() val profileList: ArrayList<CharSequence> = profileStore.getProfileList()
profileList.add(0, rh.gs(R.string.active)) profileList.add(0, rh.gs(R.string.active))
context?.let { context -> context?.let { context ->
val adapter = ArrayAdapter(context, R.layout.spinner_centered, profileList) binding.profileList.setAdapter(ArrayAdapter(context, R.layout.spinner_centered, profileList))
binding.profile.adapter = adapter binding.profileList.setText(profileList[0], false)
} ?: return }
val units = profileFunction.getUnits() val units = profileFunction.getUnits()
binding.bgUnits.text = units.asText binding.bgUnits.text = units.asText
@ -372,11 +368,10 @@ class WizardDialog : DaggerDialogFragment() {
binding.percentUsed.visibility = (sp.getInt(R.string.key_boluswizard_percentage, 100) != 100 || correctionPercent).toVisibility() binding.percentUsed.visibility = (sp.getInt(R.string.key_boluswizard_percentage, 100) != 100 || correctionPercent).toVisibility()
} }
@SuppressLint("SetTextI18n")
private fun calculateInsulin() { private fun calculateInsulin() {
val profileStore = activePlugin.activeProfileSource.profile val profileStore = activePlugin.activeProfileSource.profile ?: return // not initialized yet
if (binding.profile.selectedItem == null || profileStore == null) var profileName = binding.profileList.text.toString()
return // not initialized yet
var profileName = binding.profile.selectedItem.toString()
val specificProfile: Profile? val specificProfile: Profile?
if (profileName == rh.gs(R.string.active)) { if (profileName == rh.gs(R.string.active)) {
specificProfile = profileFunction.getProfile() specificProfile = profileFunction.getProfile()
@ -398,7 +393,7 @@ class WizardDialog : DaggerDialogFragment() {
} else } else
0.0 0.0
val percentageCorrection = if (usePercentage) { val percentageCorrection = if (usePercentage) {
if (Round.roundTo(calculatedPercentage,1.0) == SafeParse.stringToDouble(binding.correctionInput.text)) if (Round.roundTo(calculatedPercentage, 1.0) == SafeParse.stringToDouble(binding.correctionInput.text))
calculatedPercentage calculatedPercentage
else else
SafeParse.stringToDouble(binding.correctionInput.text) SafeParse.stringToDouble(binding.correctionInput.text)
@ -424,7 +419,8 @@ class WizardDialog : DaggerDialogFragment() {
val carbTime = SafeParse.stringToInt(binding.carbTimeInput.text) val carbTime = SafeParse.stringToInt(binding.carbTimeInput.text)
wizard = BolusWizard(injector).doCalc(specificProfile, profileName, tempTarget, carbsAfterConstraint, cob, bg, correction, sp.getInt(R.string.key_boluswizard_percentage, 100), wizard = BolusWizard(injector).doCalc(
specificProfile, profileName, tempTarget, carbsAfterConstraint, cob, bg, correction, sp.getInt(R.string.key_boluswizard_percentage, 100),
binding.bgCheckbox.isChecked, binding.bgCheckbox.isChecked,
binding.cobCheckbox.isChecked, binding.cobCheckbox.isChecked,
binding.iobCheckbox.isChecked, binding.iobCheckbox.isChecked,

View file

@ -403,21 +403,26 @@
android:layout_gravity="center_horizontal" android:layout_gravity="center_horizontal"
android:orientation="horizontal"> android:orientation="horizontal">
<TextView <com.google.android.material.textfield.TextInputLayout
android:layout_width="wrap_content" style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox.ExposedDropdownMenu"
android:layout_height="wrap_content"
android:labelFor="@id/profile"
android:padding="10dp"
android:text="@string/profile_label"
android:textAppearance="@style/TextAppearance.AppCompat.Small"
android:textStyle="bold" />
<Spinner
android:id="@+id/profile"
android:layout_width="0dp" android:layout_width="0dp"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_gravity="center_vertical|center_horizontal" android:layout_marginStart="5dp"
android:layout_weight="0.5" /> android:layout_marginEnd="5dp"
android:hint="@string/profile"
android:paddingStart="7dp"
android:layout_weight="0.5"
app:boxStrokeColor="@color/list_delimiter">
<androidx.appcompat.widget.AppCompatAutoCompleteTextView
android:id="@+id/profileList"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:clickable="true"
android:enabled="false"
tools:ignore="KeyboardInaccessibleWidget" />
</com.google.android.material.textfield.TextInputLayout>
<CheckBox <CheckBox
android:id="@+id/sb_checkbox" android:id="@+id/sb_checkbox"