2019-12-20 18:55:54 +01:00
|
|
|
package info.nightscout.androidaps.dialogs
|
2019-08-23 16:31:30 +02:00
|
|
|
|
|
|
|
import android.os.Bundle
|
|
|
|
import android.text.Editable
|
|
|
|
import android.text.TextWatcher
|
2019-12-20 23:05:35 +01:00
|
|
|
import android.view.LayoutInflater
|
|
|
|
import android.view.View
|
|
|
|
import android.view.ViewGroup
|
|
|
|
import android.view.Window
|
|
|
|
import android.view.WindowManager
|
2019-08-23 16:31:30 +02:00
|
|
|
import android.widget.AdapterView
|
2019-10-10 23:02:35 +02:00
|
|
|
import android.widget.AdapterView.OnItemSelectedListener
|
2019-08-23 16:31:30 +02:00
|
|
|
import android.widget.ArrayAdapter
|
|
|
|
import android.widget.CompoundButton
|
2020-08-21 13:41:48 +02:00
|
|
|
import androidx.fragment.app.FragmentManager
|
2021-02-06 00:30:27 +01:00
|
|
|
import dagger.android.HasAndroidInjector
|
2019-12-27 19:20:38 +01:00
|
|
|
import dagger.android.support.DaggerDialogFragment
|
2019-08-23 16:31:30 +02:00
|
|
|
import info.nightscout.androidaps.Constants
|
|
|
|
import info.nightscout.androidaps.MainApp
|
|
|
|
import info.nightscout.androidaps.R
|
|
|
|
import info.nightscout.androidaps.data.Profile
|
2020-12-28 16:42:00 +01:00
|
|
|
import info.nightscout.androidaps.databinding.DialogWizardBinding
|
2020-03-16 21:40:29 +01:00
|
|
|
import info.nightscout.androidaps.interfaces.ActivePluginProvider
|
2019-08-23 16:31:30 +02:00
|
|
|
import info.nightscout.androidaps.interfaces.Constraint
|
2020-08-21 13:41:48 +02:00
|
|
|
import info.nightscout.androidaps.interfaces.ProfileFunction
|
2019-12-30 13:40:42 +01:00
|
|
|
import info.nightscout.androidaps.logging.AAPSLogger
|
|
|
|
import info.nightscout.androidaps.logging.LTag
|
2019-12-30 00:53:44 +01:00
|
|
|
import info.nightscout.androidaps.plugins.bus.RxBusWrapper
|
2019-12-27 19:20:38 +01:00
|
|
|
import info.nightscout.androidaps.plugins.configBuilder.ConstraintChecker
|
2019-08-23 16:31:30 +02:00
|
|
|
import info.nightscout.androidaps.plugins.iob.iobCobCalculator.IobCobCalculatorPlugin
|
2021-02-22 17:22:30 +01:00
|
|
|
import info.nightscout.androidaps.events.EventAutosensCalculationFinished
|
2019-08-23 16:31:30 +02:00
|
|
|
import info.nightscout.androidaps.plugins.treatments.TreatmentsPlugin
|
2019-12-31 13:25:28 +01:00
|
|
|
import info.nightscout.androidaps.utils.DecimalFormatter
|
|
|
|
import info.nightscout.androidaps.utils.FabricPrivacy
|
|
|
|
import info.nightscout.androidaps.utils.SafeParse
|
|
|
|
import info.nightscout.androidaps.utils.ToastUtils
|
2020-08-21 13:41:48 +02:00
|
|
|
import info.nightscout.androidaps.utils.extensions.toVisibility
|
2019-12-27 19:20:38 +01:00
|
|
|
import info.nightscout.androidaps.utils.resources.ResourceHelper
|
2021-02-04 20:54:09 +01:00
|
|
|
import info.nightscout.androidaps.utils.rx.AapsSchedulers
|
2019-12-27 19:20:38 +01:00
|
|
|
import info.nightscout.androidaps.utils.sharedPreferences.SP
|
2021-02-06 00:30:27 +01:00
|
|
|
import info.nightscout.androidaps.utils.valueToUnits
|
2019-12-31 11:57:58 +01:00
|
|
|
import info.nightscout.androidaps.utils.wizard.BolusWizard
|
2019-08-23 16:31:30 +02:00
|
|
|
import io.reactivex.disposables.CompositeDisposable
|
|
|
|
import java.text.DecimalFormat
|
|
|
|
import java.util.*
|
2019-12-27 19:20:38 +01:00
|
|
|
import javax.inject.Inject
|
2019-10-24 11:34:22 +02:00
|
|
|
import kotlin.math.abs
|
2019-08-23 16:31:30 +02:00
|
|
|
|
2019-12-27 19:20:38 +01:00
|
|
|
class WizardDialog : DaggerDialogFragment() {
|
2019-08-23 16:31:30 +02:00
|
|
|
|
2021-02-06 00:30:27 +01:00
|
|
|
@Inject lateinit var injector: HasAndroidInjector
|
2019-12-30 13:40:42 +01:00
|
|
|
@Inject lateinit var aapsLogger: AAPSLogger
|
2021-02-04 20:54:09 +01:00
|
|
|
@Inject lateinit var aapsSchedulers: AapsSchedulers
|
2019-12-30 00:53:44 +01:00
|
|
|
@Inject lateinit var constraintChecker: ConstraintChecker
|
|
|
|
@Inject lateinit var mainApp: MainApp
|
2019-12-30 13:40:42 +01:00
|
|
|
@Inject lateinit var sp: SP
|
|
|
|
@Inject lateinit var rxBus: RxBusWrapper
|
2020-01-03 14:30:39 +01:00
|
|
|
@Inject lateinit var fabricPrivacy: FabricPrivacy
|
2019-12-30 00:53:44 +01:00
|
|
|
@Inject lateinit var resourceHelper: ResourceHelper
|
|
|
|
@Inject lateinit var profileFunction: ProfileFunction
|
|
|
|
@Inject lateinit var treatmentsPlugin: TreatmentsPlugin
|
2020-03-16 21:40:29 +01:00
|
|
|
@Inject lateinit var activePlugin: ActivePluginProvider
|
2019-12-30 13:40:42 +01:00
|
|
|
@Inject lateinit var iobCobCalculatorPlugin: IobCobCalculatorPlugin
|
2019-12-27 19:20:38 +01:00
|
|
|
|
2019-08-23 16:31:30 +02:00
|
|
|
private var wizard: BolusWizard? = null
|
|
|
|
|
|
|
|
//one shot guards
|
|
|
|
private var okClicked: Boolean = false
|
|
|
|
|
|
|
|
private val textWatcher = object : TextWatcher {
|
|
|
|
override fun afterTextChanged(s: Editable) {}
|
|
|
|
override fun beforeTextChanged(s: CharSequence, start: Int, count: Int, after: Int) {}
|
|
|
|
override fun onTextChanged(s: CharSequence, start: Int, before: Int, count: Int) {
|
|
|
|
calculateInsulin()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-28 16:42:00 +01:00
|
|
|
private val timeTextWatcher = object : TextWatcher {
|
|
|
|
override fun afterTextChanged(s: Editable) {}
|
|
|
|
override fun beforeTextChanged(s: CharSequence, start: Int, count: Int, after: Int) {}
|
|
|
|
override fun onTextChanged(s: CharSequence, start: Int, before: Int, count: Int) {
|
|
|
|
calculateInsulin()
|
|
|
|
binding.alarm.isChecked = binding.carbTimeInput.value > 0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-23 16:31:30 +02:00
|
|
|
private var disposable: CompositeDisposable = CompositeDisposable()
|
|
|
|
|
2020-12-28 16:42:00 +01:00
|
|
|
private var _binding: DialogWizardBinding? = null
|
|
|
|
|
|
|
|
// This property is only valid between onCreateView and
|
|
|
|
// onDestroyView.
|
|
|
|
private val binding get() = _binding!!
|
|
|
|
|
2019-10-10 23:02:35 +02:00
|
|
|
override fun onStart() {
|
|
|
|
super.onStart()
|
|
|
|
dialog?.window?.setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT)
|
|
|
|
}
|
|
|
|
|
2019-08-23 16:31:30 +02:00
|
|
|
override fun onSaveInstanceState(savedInstanceState: Bundle) {
|
|
|
|
super.onSaveInstanceState(savedInstanceState)
|
2020-12-28 16:42:00 +01:00
|
|
|
savedInstanceState.putDouble("bg_input", binding.bgInput.value)
|
|
|
|
savedInstanceState.putDouble("carbs_input", binding.carbsInput.value)
|
|
|
|
savedInstanceState.putDouble("correction_input", binding.correctionInput.value)
|
|
|
|
savedInstanceState.putDouble("carb_time_input", binding.carbTimeInput.value)
|
2019-08-23 16:31:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,
|
2020-12-28 16:42:00 +01:00
|
|
|
savedInstanceState: Bundle?): View {
|
2019-09-15 18:56:54 +02:00
|
|
|
dialog?.window?.requestFeature(Window.FEATURE_NO_TITLE)
|
|
|
|
dialog?.window?.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN)
|
2019-08-23 16:31:30 +02:00
|
|
|
isCancelable = true
|
2019-09-15 18:56:54 +02:00
|
|
|
dialog?.setCanceledOnTouchOutside(false)
|
2019-08-23 16:31:30 +02:00
|
|
|
|
2020-12-28 16:42:00 +01:00
|
|
|
_binding = DialogWizardBinding.inflate(inflater, container, false)
|
|
|
|
return binding.root
|
2019-08-23 16:31:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
|
|
|
|
loadCheckedStates()
|
|
|
|
processCobCheckBox()
|
2020-12-28 16:42:00 +01:00
|
|
|
binding.sbcheckbox.visibility = sp.getBoolean(R.string.key_usesuperbolus, false).toVisibility()
|
|
|
|
binding.notesLayout.visibility = sp.getBoolean(R.string.key_show_notes_entry_dialogs, false).toVisibility()
|
2019-08-23 16:31:30 +02:00
|
|
|
|
2019-12-27 19:20:38 +01:00
|
|
|
val maxCarbs = constraintChecker.getMaxCarbsAllowed().value()
|
|
|
|
val maxCorrection = constraintChecker.getMaxBolusAllowed().value()
|
2019-08-23 16:31:30 +02:00
|
|
|
|
2020-08-19 10:58:45 +02:00
|
|
|
if (profileFunction.getUnits() == Constants.MGDL)
|
2020-12-28 16:42:00 +01:00
|
|
|
binding.bgInput.setParams(savedInstanceState?.getDouble("bg_input")
|
|
|
|
?: 0.0, 0.0, 500.0, 1.0, DecimalFormat("0"), false, binding.ok, timeTextWatcher)
|
2020-08-19 10:58:45 +02:00
|
|
|
else
|
2020-12-28 16:42:00 +01:00
|
|
|
binding.bgInput.setParams(savedInstanceState?.getDouble("bg_input")
|
|
|
|
?: 0.0, 0.0, 30.0, 0.1, DecimalFormat("0.0"), false, binding.ok, textWatcher)
|
|
|
|
binding.carbsInput.setParams(savedInstanceState?.getDouble("carbs_input")
|
|
|
|
?: 0.0, 0.0, maxCarbs.toDouble(), 1.0, DecimalFormat("0"), false, binding.ok, textWatcher)
|
2020-03-16 21:40:29 +01:00
|
|
|
val bolusStep = activePlugin.activePump.pumpDescription.bolusStep
|
2020-12-28 16:42:00 +01:00
|
|
|
binding.correctionInput.setParams(savedInstanceState?.getDouble("correction_input")
|
|
|
|
?: 0.0, -maxCorrection, maxCorrection, bolusStep, DecimalFormatter.pumpSupportedBolusFormat(activePlugin.activePump), false, binding.ok, textWatcher)
|
|
|
|
binding.carbTimeInput.setParams(savedInstanceState?.getDouble("carb_time_input")
|
|
|
|
?: 0.0, -60.0, 60.0, 5.0, DecimalFormat("0"), false, binding.ok, timeTextWatcher)
|
2019-08-23 16:31:30 +02:00
|
|
|
initDialog()
|
|
|
|
|
2020-12-28 16:42:00 +01:00
|
|
|
binding.percentUsed.text = resourceHelper.gs(R.string.format_percent, sp.getInt(R.string.key_boluswizard_percentage, 100))
|
2019-08-23 16:31:30 +02:00
|
|
|
// ok button
|
2020-12-28 16:42:00 +01:00
|
|
|
binding.ok.setOnClickListener {
|
2019-08-23 16:31:30 +02:00
|
|
|
if (okClicked) {
|
2019-12-30 13:40:42 +01:00
|
|
|
aapsLogger.debug(LTag.UI, "guarding: ok already clicked")
|
2019-08-23 16:31:30 +02:00
|
|
|
} else {
|
|
|
|
okClicked = true
|
2019-11-07 12:46:55 +01:00
|
|
|
calculateInsulin()
|
2019-12-20 18:55:54 +01:00
|
|
|
context?.let { context ->
|
2019-08-23 16:31:30 +02:00
|
|
|
wizard?.confirmAndExecute(context)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
dismiss()
|
|
|
|
}
|
|
|
|
// cancel button
|
2020-12-28 16:42:00 +01:00
|
|
|
binding.cancel.setOnClickListener { dismiss() }
|
2019-08-23 16:31:30 +02:00
|
|
|
// checkboxes
|
2020-12-28 16:42:00 +01:00
|
|
|
binding.bgcheckbox.setOnCheckedChangeListener(::onCheckedChanged)
|
|
|
|
binding.ttcheckbox.setOnCheckedChangeListener(::onCheckedChanged)
|
|
|
|
binding.cobcheckbox.setOnCheckedChangeListener(::onCheckedChanged)
|
|
|
|
binding.basaliobcheckbox.setOnCheckedChangeListener(::onCheckedChanged)
|
|
|
|
binding.bolusiobcheckbox.setOnCheckedChangeListener(::onCheckedChanged)
|
|
|
|
binding.bgtrendcheckbox.setOnCheckedChangeListener(::onCheckedChanged)
|
|
|
|
binding.sbcheckbox.setOnCheckedChangeListener(::onCheckedChanged)
|
2019-10-10 23:02:35 +02:00
|
|
|
|
2020-12-25 12:06:54 +01:00
|
|
|
val showCalc = sp.getBoolean(R.string.key_wizard_calculation_visible, false)
|
2020-12-28 16:42:00 +01:00
|
|
|
binding.delimiter.visibility = showCalc.toVisibility()
|
|
|
|
binding.resulttable.visibility = showCalc.toVisibility()
|
|
|
|
binding.calculationcheckbox.isChecked = showCalc
|
|
|
|
binding.calculationcheckbox.setOnCheckedChangeListener { _, isChecked ->
|
2019-10-10 23:02:35 +02:00
|
|
|
run {
|
2019-12-27 19:20:38 +01:00
|
|
|
sp.putBoolean(resourceHelper.gs(R.string.key_wizard_calculation_visible), isChecked)
|
2020-12-28 16:42:00 +01:00
|
|
|
binding.delimiter.visibility = isChecked.toVisibility()
|
|
|
|
binding.resulttable.visibility = isChecked.toVisibility()
|
2019-10-10 23:02:35 +02:00
|
|
|
}
|
|
|
|
}
|
2019-08-23 16:31:30 +02:00
|
|
|
// profile spinner
|
2020-12-28 16:42:00 +01:00
|
|
|
binding.profile.onItemSelectedListener = object : OnItemSelectedListener {
|
2019-08-23 16:31:30 +02:00
|
|
|
override fun onNothingSelected(parent: AdapterView<*>?) {
|
2019-12-27 19:20:38 +01:00
|
|
|
ToastUtils.showToastInUiThread(mainApp, resourceHelper.gs(R.string.noprofileselected))
|
2020-12-28 16:42:00 +01:00
|
|
|
binding.ok.visibility = View.GONE
|
2019-08-23 16:31:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
override fun onItemSelected(parent: AdapterView<*>?, view: View?, position: Int, id: Long) {
|
|
|
|
calculateInsulin()
|
2020-12-28 16:42:00 +01:00
|
|
|
binding.ok.visibility = View.VISIBLE
|
2019-08-23 16:31:30 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
// bus
|
2019-12-30 00:53:44 +01:00
|
|
|
disposable.add(rxBus
|
2019-12-20 23:05:35 +01:00
|
|
|
.toObservable(EventAutosensCalculationFinished::class.java)
|
2021-02-04 20:54:09 +01:00
|
|
|
.observeOn(aapsSchedulers.main)
|
2019-12-20 23:05:35 +01:00
|
|
|
.subscribe({
|
|
|
|
activity?.runOnUiThread { calculateInsulin() }
|
2021-02-04 20:54:09 +01:00
|
|
|
}, fabricPrivacy::logException)
|
2019-08-23 16:31:30 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
override fun onDestroyView() {
|
|
|
|
super.onDestroyView()
|
|
|
|
disposable.clear()
|
2020-12-28 16:42:00 +01:00
|
|
|
_binding = null
|
2019-08-23 16:31:30 +02:00
|
|
|
}
|
|
|
|
|
2019-12-21 14:30:14 +01:00
|
|
|
private fun onCheckedChanged(buttonView: CompoundButton, @Suppress("UNUSED_PARAMETER") state: Boolean) {
|
2019-08-23 16:31:30 +02:00
|
|
|
saveCheckedStates()
|
2020-12-28 16:42:00 +01:00
|
|
|
binding.ttcheckbox.isEnabled = binding.bgcheckbox.isChecked && treatmentsPlugin.tempTargetFromHistory != null
|
|
|
|
if (buttonView.id == binding.cobcheckbox.id)
|
2019-08-23 16:31:30 +02:00
|
|
|
processCobCheckBox()
|
|
|
|
calculateInsulin()
|
|
|
|
}
|
|
|
|
|
|
|
|
private fun processCobCheckBox() {
|
2020-12-28 16:42:00 +01:00
|
|
|
if (binding.cobcheckbox.isChecked) {
|
|
|
|
binding.bolusiobcheckbox.isEnabled = false
|
|
|
|
binding.basaliobcheckbox.isEnabled = false
|
|
|
|
binding.bolusiobcheckbox.isChecked = true
|
|
|
|
binding.basaliobcheckbox.isChecked = true
|
2019-08-23 16:31:30 +02:00
|
|
|
} else {
|
2020-12-28 16:42:00 +01:00
|
|
|
binding.bolusiobcheckbox.isEnabled = true
|
|
|
|
binding.basaliobcheckbox.isEnabled = true
|
2019-08-23 16:31:30 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private fun saveCheckedStates() {
|
2020-12-28 16:42:00 +01:00
|
|
|
sp.putBoolean(R.string.key_wizard_include_cob, binding.cobcheckbox.isChecked)
|
|
|
|
sp.putBoolean(R.string.key_wizard_include_trend_bg, binding.bgtrendcheckbox.isChecked)
|
2019-08-23 16:31:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
private fun loadCheckedStates() {
|
2020-12-28 16:42:00 +01:00
|
|
|
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)
|
2019-08-23 16:31:30 +02:00
|
|
|
}
|
|
|
|
|
2021-02-08 10:05:21 +01:00
|
|
|
private fun valueToUnitsToString(value: Double, units: String): String =
|
|
|
|
if (units == Constants.MGDL) DecimalFormatter.to0Decimal(value)
|
|
|
|
else DecimalFormatter.to1Decimal(value * Constants.MGDL_TO_MMOLL)
|
|
|
|
|
2019-08-23 16:31:30 +02:00
|
|
|
private fun initDialog() {
|
2019-12-27 19:20:38 +01:00
|
|
|
val profile = profileFunction.getProfile()
|
2020-03-16 21:40:29 +01:00
|
|
|
val profileStore = activePlugin.activeProfileInterface.profile
|
2019-08-23 16:31:30 +02:00
|
|
|
|
|
|
|
if (profile == null || profileStore == null) {
|
2019-12-27 19:20:38 +01:00
|
|
|
ToastUtils.showToastInUiThread(mainApp, resourceHelper.gs(R.string.noprofile))
|
2019-08-23 16:31:30 +02:00
|
|
|
dismiss()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-02-08 10:05:21 +01:00
|
|
|
val profileList: ArrayList<CharSequence> = profileStore.getProfileList()
|
2019-12-27 19:20:38 +01:00
|
|
|
profileList.add(0, resourceHelper.gs(R.string.active))
|
2019-08-23 16:31:30 +02:00
|
|
|
context?.let { context ->
|
|
|
|
val adapter = ArrayAdapter(context, R.layout.spinner_centered, profileList)
|
2020-12-28 16:42:00 +01:00
|
|
|
binding.profile.adapter = adapter
|
2019-08-23 16:31:30 +02:00
|
|
|
} ?: return
|
|
|
|
|
2019-12-27 19:20:38 +01:00
|
|
|
val units = profileFunction.getUnits()
|
2020-12-28 16:42:00 +01:00
|
|
|
binding.bgunits.text = units
|
2019-08-23 16:31:30 +02:00
|
|
|
if (units == Constants.MGDL)
|
2020-12-28 16:42:00 +01:00
|
|
|
binding.bgInput.setStep(1.0)
|
2019-08-23 16:31:30 +02:00
|
|
|
else
|
2020-12-28 16:42:00 +01:00
|
|
|
binding.bgInput.setStep(0.1)
|
2019-08-23 16:31:30 +02:00
|
|
|
|
|
|
|
// Set BG if not old
|
2020-01-10 23:14:58 +01:00
|
|
|
val lastBg = iobCobCalculatorPlugin.actualBg()
|
2019-08-23 16:31:30 +02:00
|
|
|
|
|
|
|
if (lastBg != null) {
|
2020-12-28 16:42:00 +01:00
|
|
|
binding.bgInput.value = lastBg.valueToUnits(units)
|
2019-08-23 16:31:30 +02:00
|
|
|
} else {
|
2020-12-28 16:42:00 +01:00
|
|
|
binding.bgInput.value = 0.0
|
2019-08-23 16:31:30 +02:00
|
|
|
}
|
2020-12-28 16:42:00 +01:00
|
|
|
binding.ttcheckbox.isEnabled = treatmentsPlugin.tempTargetFromHistory != null
|
2019-08-23 16:31:30 +02:00
|
|
|
|
|
|
|
// IOB calculation
|
2019-12-30 00:53:44 +01:00
|
|
|
treatmentsPlugin.updateTotalIOBTreatments()
|
|
|
|
val bolusIob = treatmentsPlugin.lastCalculationTreatments.round()
|
|
|
|
treatmentsPlugin.updateTotalIOBTempBasals()
|
|
|
|
val basalIob = treatmentsPlugin.lastCalculationTempBasals.round()
|
2019-08-23 16:31:30 +02:00
|
|
|
|
2020-12-28 16:42:00 +01:00
|
|
|
binding.bolusiobinsulin.text = resourceHelper.gs(R.string.formatinsulinunits, -bolusIob.iob)
|
|
|
|
binding.basaliobinsulin.text = resourceHelper.gs(R.string.formatinsulinunits, -basalIob.basaliob)
|
2019-08-23 16:31:30 +02:00
|
|
|
|
|
|
|
calculateInsulin()
|
|
|
|
|
2020-12-28 16:42:00 +01:00
|
|
|
binding.percentUsed.visibility = (sp.getInt(R.string.key_boluswizard_percentage, 100) != 100).toVisibility()
|
2019-08-23 16:31:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
private fun calculateInsulin() {
|
2020-03-16 21:40:29 +01:00
|
|
|
val profileStore = activePlugin.activeProfileInterface.profile
|
2020-12-28 16:42:00 +01:00
|
|
|
if (binding.profile.selectedItem == null || profileStore == null)
|
2019-08-23 16:31:30 +02:00
|
|
|
return // not initialized yet
|
2020-12-28 16:42:00 +01:00
|
|
|
var profileName = binding.profile.selectedItem.toString()
|
2019-08-23 16:31:30 +02:00
|
|
|
val specificProfile: Profile?
|
2019-12-27 19:20:38 +01:00
|
|
|
if (profileName == resourceHelper.gs(R.string.active)) {
|
|
|
|
specificProfile = profileFunction.getProfile()
|
2019-12-30 13:40:42 +01:00
|
|
|
profileName = profileFunction.getProfileName()
|
2019-08-23 16:31:30 +02:00
|
|
|
} else
|
|
|
|
specificProfile = profileStore.getSpecificProfile(profileName)
|
|
|
|
|
|
|
|
if (specificProfile == null) return
|
|
|
|
|
|
|
|
// Entered values
|
2020-12-28 16:42:00 +01:00
|
|
|
var bg = SafeParse.stringToDouble(binding.bgInput.text)
|
|
|
|
val carbs = SafeParse.stringToInt(binding.carbsInput.text)
|
|
|
|
val correction = SafeParse.stringToDouble(binding.correctionInput.text)
|
2019-12-27 19:20:38 +01:00
|
|
|
val carbsAfterConstraint = constraintChecker.applyCarbsConstraints(Constraint(carbs)).value()
|
2019-10-24 11:34:22 +02:00
|
|
|
if (abs(carbs - carbsAfterConstraint) > 0.01) {
|
2020-12-28 16:42:00 +01:00
|
|
|
binding.carbsInput.value = 0.0
|
2019-12-27 19:20:38 +01:00
|
|
|
ToastUtils.showToastInUiThread(mainApp, resourceHelper.gs(R.string.carbsconstraintapplied))
|
2019-08-23 16:31:30 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-12-28 16:42:00 +01:00
|
|
|
bg = if (binding.bgcheckbox.isChecked) bg else 0.0
|
|
|
|
val tempTarget = if (binding.ttcheckbox.isChecked) treatmentsPlugin.tempTargetFromHistory else null
|
2019-08-23 16:31:30 +02:00
|
|
|
|
|
|
|
// COB
|
2019-10-24 11:34:22 +02:00
|
|
|
var cob = 0.0
|
2020-12-28 16:42:00 +01:00
|
|
|
if (binding.cobcheckbox.isChecked) {
|
2019-12-30 13:40:42 +01:00
|
|
|
val cobInfo = iobCobCalculatorPlugin.getCobInfo(false, "Wizard COB")
|
2019-10-24 11:34:22 +02:00
|
|
|
cobInfo.displayCob?.let { cob = it }
|
2019-08-23 16:31:30 +02:00
|
|
|
}
|
|
|
|
|
2020-12-28 16:42:00 +01:00
|
|
|
val carbTime = SafeParse.stringToInt(binding.carbTimeInput.text)
|
2019-08-23 16:31:30 +02:00
|
|
|
|
2019-12-31 11:57:58 +01:00
|
|
|
wizard = BolusWizard(mainApp).doCalc(specificProfile, profileName, tempTarget, carbsAfterConstraint, cob, bg, correction,
|
2019-12-27 19:20:38 +01:00
|
|
|
sp.getInt(R.string.key_boluswizard_percentage, 100).toDouble(),
|
2020-12-28 16:42:00 +01:00
|
|
|
binding.bgcheckbox.isChecked,
|
|
|
|
binding.cobcheckbox.isChecked,
|
|
|
|
binding.bolusiobcheckbox.isChecked,
|
|
|
|
binding.basaliobcheckbox.isChecked,
|
|
|
|
binding.sbcheckbox.isChecked,
|
|
|
|
binding.ttcheckbox.isChecked,
|
|
|
|
binding.bgtrendcheckbox.isChecked,
|
|
|
|
binding.alarm.isChecked,
|
|
|
|
binding.notes.text.toString(), carbTime)
|
2019-08-23 16:31:30 +02:00
|
|
|
|
|
|
|
wizard?.let { wizard ->
|
2021-02-08 10:05:21 +01:00
|
|
|
binding.bg.text = String.format(resourceHelper.gs(R.string.format_bg_isf), valueToUnitsToString(Profile.toMgdl(bg, profileFunction.getUnits()), profileFunction.getUnits()), wizard.sens)
|
2020-12-28 16:42:00 +01:00
|
|
|
binding.bginsulin.text = resourceHelper.gs(R.string.formatinsulinunits, wizard.insulinFromBG)
|
2019-08-23 16:31:30 +02:00
|
|
|
|
2020-12-28 16:42:00 +01:00
|
|
|
binding.carbs.text = String.format(resourceHelper.gs(R.string.format_carbs_ic), carbs.toDouble(), wizard.ic)
|
|
|
|
binding.carbsinsulin.text = resourceHelper.gs(R.string.formatinsulinunits, wizard.insulinFromCarbs)
|
2019-08-23 16:31:30 +02:00
|
|
|
|
2020-12-28 16:42:00 +01:00
|
|
|
binding.bolusiobinsulin.text = resourceHelper.gs(R.string.formatinsulinunits, wizard.insulinFromBolusIOB)
|
|
|
|
binding.basaliobinsulin.text = resourceHelper.gs(R.string.formatinsulinunits, wizard.insulinFromBasalIOB)
|
2019-08-23 16:31:30 +02:00
|
|
|
|
2020-12-28 16:42:00 +01:00
|
|
|
binding.correctioninsulin.text = resourceHelper.gs(R.string.formatinsulinunits, wizard.insulinFromCorrection)
|
2019-08-23 16:31:30 +02:00
|
|
|
|
|
|
|
// Superbolus
|
2020-12-28 16:42:00 +01:00
|
|
|
binding.sb.text = if (binding.sbcheckbox.isChecked) resourceHelper.gs(R.string.twohours) else ""
|
|
|
|
binding.sbinsulin.text = resourceHelper.gs(R.string.formatinsulinunits, wizard.insulinFromSuperBolus)
|
2019-08-23 16:31:30 +02:00
|
|
|
|
|
|
|
// Trend
|
2020-12-28 16:42:00 +01:00
|
|
|
if (binding.bgtrendcheckbox.isChecked && wizard.glucoseStatus != null) {
|
|
|
|
binding.bgtrend.text = ((if (wizard.trend > 0) "+" else "")
|
2019-12-27 19:20:38 +01:00
|
|
|
+ Profile.toUnitsString(wizard.trend * 3, wizard.trend * 3 / Constants.MMOLL_TO_MGDL, profileFunction.getUnits())
|
|
|
|
+ " " + profileFunction.getUnits())
|
2019-08-23 16:31:30 +02:00
|
|
|
} else {
|
2020-12-28 16:42:00 +01:00
|
|
|
binding.bgtrend.text = ""
|
2019-08-23 16:31:30 +02:00
|
|
|
}
|
2020-12-28 16:42:00 +01:00
|
|
|
binding.bgtrendinsulin.text = resourceHelper.gs(R.string.formatinsulinunits, wizard.insulinFromTrend)
|
2019-08-23 16:31:30 +02:00
|
|
|
|
|
|
|
// COB
|
2020-12-28 16:42:00 +01:00
|
|
|
if (binding.cobcheckbox.isChecked) {
|
|
|
|
binding.cob.text = String.format(resourceHelper.gs(R.string.format_cob_ic), cob, wizard.ic)
|
|
|
|
binding.cobinsulin.text = resourceHelper.gs(R.string.formatinsulinunits, wizard.insulinFromCOB)
|
2019-08-23 16:31:30 +02:00
|
|
|
} else {
|
2020-12-28 16:42:00 +01:00
|
|
|
binding.cob.text = ""
|
|
|
|
binding.cobinsulin.text = ""
|
2019-08-23 16:31:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (wizard.calculatedTotalInsulin > 0.0 || carbsAfterConstraint > 0.0) {
|
2019-12-27 19:20:38 +01:00
|
|
|
val insulinText = if (wizard.calculatedTotalInsulin > 0.0) resourceHelper.gs(R.string.formatinsulinunits, wizard.calculatedTotalInsulin) else ""
|
|
|
|
val carbsText = if (carbsAfterConstraint > 0.0) resourceHelper.gs(R.string.format_carbs, carbsAfterConstraint) else ""
|
2020-12-28 16:42:00 +01:00
|
|
|
binding.total.text = resourceHelper.gs(R.string.result_insulin_carbs, insulinText, carbsText)
|
|
|
|
binding.ok.visibility = View.VISIBLE
|
2019-08-23 16:31:30 +02:00
|
|
|
} else {
|
2020-12-28 16:42:00 +01:00
|
|
|
binding.total.text = resourceHelper.gs(R.string.missing_carbs, wizard.carbsEquivalent.toInt())
|
|
|
|
binding.ok.visibility = View.INVISIBLE
|
2019-08-23 16:31:30 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2020-08-21 13:41:48 +02:00
|
|
|
|
|
|
|
override fun show(manager: FragmentManager, tag: String?) {
|
|
|
|
try {
|
|
|
|
manager.beginTransaction().let {
|
|
|
|
it.add(this, tag)
|
|
|
|
it.commitAllowingStateLoss()
|
|
|
|
}
|
|
|
|
} catch (e: IllegalStateException) {
|
|
|
|
aapsLogger.debug(e.localizedMessage)
|
|
|
|
}
|
|
|
|
}
|
2019-08-23 16:31:30 +02:00
|
|
|
}
|