2019-12-20 18:55:54 +01:00
|
|
|
package info.nightscout.androidaps.dialogs
|
2019-12-20 12:29:45 +01:00
|
|
|
|
2021-01-01 11:37:24 +01:00
|
|
|
import android.content.Context
|
2019-12-20 12:29:45 +01:00
|
|
|
import android.os.Bundle
|
|
|
|
import android.view.LayoutInflater
|
|
|
|
import android.view.View
|
|
|
|
import android.view.ViewGroup
|
|
|
|
import com.google.common.base.Joiner
|
|
|
|
import info.nightscout.androidaps.R
|
2019-12-20 23:05:35 +01:00
|
|
|
import info.nightscout.androidaps.activities.ErrorHelperActivity
|
2021-04-03 21:29:26 +02:00
|
|
|
import info.nightscout.androidaps.database.entities.ValueWithUnit
|
2021-03-31 00:18:55 +02:00
|
|
|
import info.nightscout.androidaps.database.entities.UserEntry.Action
|
|
|
|
import info.nightscout.androidaps.database.entities.UserEntry.Sources
|
2021-01-21 20:16:13 +01:00
|
|
|
import info.nightscout.androidaps.databinding.DialogExtendedbolusBinding
|
2021-04-14 00:45:30 +02:00
|
|
|
import info.nightscout.androidaps.interfaces.ActivePlugin
|
2021-11-07 17:19:06 +01:00
|
|
|
import info.nightscout.androidaps.interfaces.CommandQueue
|
2019-12-20 12:29:45 +01:00
|
|
|
import info.nightscout.androidaps.interfaces.Constraint
|
2021-02-09 17:57:28 +01:00
|
|
|
import info.nightscout.androidaps.logging.UserEntryLogger
|
2019-12-27 19:20:38 +01:00
|
|
|
import info.nightscout.androidaps.plugins.configBuilder.ConstraintChecker
|
2019-12-20 12:29:45 +01:00
|
|
|
import info.nightscout.androidaps.queue.Callback
|
|
|
|
import info.nightscout.androidaps.utils.HtmlHelper
|
2021-12-10 08:58:23 +01:00
|
|
|
import info.nightscout.shared.SafeParse
|
2021-01-01 11:37:24 +01:00
|
|
|
import info.nightscout.androidaps.utils.alertDialogs.OKDialog
|
2021-04-05 22:41:28 +02:00
|
|
|
import info.nightscout.androidaps.extensions.formatColor
|
2022-03-07 11:49:37 +01:00
|
|
|
import info.nightscout.androidaps.utils.ToastUtils
|
|
|
|
import info.nightscout.androidaps.utils.protection.ProtectionCheck
|
2022-03-23 13:39:38 +01:00
|
|
|
import info.nightscout.androidaps.utils.protection.ProtectionCheck.Protection.BOLUS
|
2019-12-27 19:20:38 +01:00
|
|
|
import info.nightscout.androidaps.utils.resources.ResourceHelper
|
2022-03-07 11:49:37 +01:00
|
|
|
import info.nightscout.shared.logging.LTag
|
2019-12-20 12:29:45 +01:00
|
|
|
import java.text.DecimalFormat
|
|
|
|
import java.util.*
|
2019-12-27 19:20:38 +01:00
|
|
|
import javax.inject.Inject
|
2019-12-20 12:29:45 +01:00
|
|
|
import kotlin.math.abs
|
|
|
|
|
|
|
|
class ExtendedBolusDialog : DialogFragmentWithDate() {
|
2021-01-01 11:37:24 +01:00
|
|
|
|
|
|
|
@Inject lateinit var ctx: Context
|
2021-11-04 10:56:12 +01:00
|
|
|
@Inject lateinit var rh: ResourceHelper
|
2019-12-30 00:53:44 +01:00
|
|
|
@Inject lateinit var constraintChecker: ConstraintChecker
|
2021-11-07 17:19:06 +01:00
|
|
|
@Inject lateinit var commandQueue: CommandQueue
|
2021-04-14 00:45:30 +02:00
|
|
|
@Inject lateinit var activePlugin: ActivePlugin
|
2021-02-09 17:57:28 +01:00
|
|
|
@Inject lateinit var uel: UserEntryLogger
|
2022-03-07 11:49:37 +01:00
|
|
|
@Inject lateinit var protectionCheck: ProtectionCheck
|
2019-12-27 19:20:38 +01:00
|
|
|
|
2022-03-23 13:39:38 +01:00
|
|
|
private var queryingProtection = false
|
2021-01-21 20:16:13 +01:00
|
|
|
private var _binding: DialogExtendedbolusBinding? = null
|
|
|
|
|
2022-03-23 13:39:38 +01:00
|
|
|
// This property is only valid between onCreateView and onDestroyView.
|
2021-01-21 20:16:13 +01:00
|
|
|
private val binding get() = _binding!!
|
|
|
|
|
2019-12-20 12:29:45 +01:00
|
|
|
override fun onSaveInstanceState(savedInstanceState: Bundle) {
|
|
|
|
super.onSaveInstanceState(savedInstanceState)
|
2021-01-21 20:16:13 +01:00
|
|
|
savedInstanceState.putDouble("insulin", binding.insulin.value)
|
|
|
|
savedInstanceState.putDouble("duration", binding.duration.value)
|
2019-12-20 12:29:45 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,
|
2021-01-21 20:16:13 +01:00
|
|
|
savedInstanceState: Bundle?): View {
|
2019-12-20 23:05:35 +01:00
|
|
|
onCreateViewGeneral()
|
2021-01-21 20:16:13 +01:00
|
|
|
_binding = DialogExtendedbolusBinding.inflate(inflater, container, false)
|
|
|
|
return binding.root
|
2019-12-20 12:29:45 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
|
|
|
|
super.onViewCreated(view, savedInstanceState)
|
|
|
|
|
2020-01-10 23:14:58 +01:00
|
|
|
val pumpDescription = activePlugin.activePump.pumpDescription
|
2019-12-20 12:29:45 +01:00
|
|
|
|
2019-12-27 19:20:38 +01:00
|
|
|
val maxInsulin = constraintChecker.getMaxExtendedBolusAllowed().value()
|
2019-12-20 12:29:45 +01:00
|
|
|
val extendedStep = pumpDescription.extendedBolusStep
|
2021-01-21 20:16:13 +01:00
|
|
|
binding.insulin.setParams(savedInstanceState?.getDouble("insulin")
|
|
|
|
?: extendedStep, extendedStep, maxInsulin, extendedStep, DecimalFormat("0.00"), false, binding.okcancel.ok)
|
2019-12-20 12:29:45 +01:00
|
|
|
|
|
|
|
val extendedDurationStep = pumpDescription.extendedBolusDurationStep
|
|
|
|
val extendedMaxDuration = pumpDescription.extendedBolusMaxDuration
|
2021-01-21 20:16:13 +01:00
|
|
|
binding.duration.setParams(savedInstanceState?.getDouble("duration")
|
|
|
|
?: extendedDurationStep, extendedDurationStep, extendedMaxDuration, extendedDurationStep, DecimalFormat("0"), false, binding.okcancel.ok)
|
2022-02-12 08:26:07 +01:00
|
|
|
binding.insulin.editText?.id?.let { binding.insulinLabel.labelFor = it }
|
|
|
|
binding.duration.editText?.id?.let { binding.durationLabel.labelFor = it }
|
2021-01-21 20:16:13 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
override fun onDestroyView() {
|
|
|
|
super.onDestroyView()
|
|
|
|
_binding = null
|
2019-12-20 12:29:45 +01:00
|
|
|
}
|
|
|
|
|
2019-12-21 23:17:20 +01:00
|
|
|
override fun submit(): Boolean {
|
2021-01-21 20:16:13 +01:00
|
|
|
if (_binding == null) return false
|
2021-11-23 19:53:45 +01:00
|
|
|
val insulin = SafeParse.stringToDouble(binding.insulin.text)
|
2021-01-21 20:16:13 +01:00
|
|
|
val durationInMinutes = binding.duration.value.toInt()
|
2019-12-20 12:29:45 +01:00
|
|
|
val actions: LinkedList<String> = LinkedList()
|
2019-12-27 19:20:38 +01:00
|
|
|
val insulinAfterConstraint = constraintChecker.applyExtendedBolusConstraints(Constraint(insulin)).value()
|
2021-11-04 10:56:12 +01:00
|
|
|
actions.add(rh.gs(R.string.formatinsulinunits, insulinAfterConstraint))
|
|
|
|
actions.add(rh.gs(R.string.duration) + ": " + rh.gs(R.string.format_mins, durationInMinutes))
|
2019-12-20 13:58:51 +01:00
|
|
|
if (abs(insulinAfterConstraint - insulin) > 0.01)
|
2021-11-04 10:56:12 +01:00
|
|
|
actions.add(rh.gs(R.string.constraintapllied).formatColor(rh, R.color.warning))
|
2019-12-20 12:29:45 +01:00
|
|
|
|
|
|
|
activity?.let { activity ->
|
2021-11-04 10:56:12 +01:00
|
|
|
OKDialog.showConfirmation(activity, rh.gs(R.string.extended_bolus), HtmlHelper.fromHtml(Joiner.on("<br/>").join(actions)), {
|
2021-04-01 22:59:05 +02:00
|
|
|
uel.log(Action.EXTENDED_BOLUS, Sources.ExtendedBolusDialog,
|
2021-04-03 21:29:26 +02:00
|
|
|
ValueWithUnit.Insulin(insulinAfterConstraint),
|
|
|
|
ValueWithUnit.Minute(durationInMinutes))
|
2020-01-10 23:14:58 +01:00
|
|
|
commandQueue.extendedBolus(insulinAfterConstraint, durationInMinutes, object : Callback() {
|
2019-12-20 12:29:45 +01:00
|
|
|
override fun run() {
|
|
|
|
if (!result.success) {
|
2021-11-04 10:56:12 +01:00
|
|
|
ErrorHelperActivity.runAlarm(ctx, result.comment, rh.gs(R.string.treatmentdeliveryerror), R.raw.boluserror)
|
2019-12-20 12:29:45 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
2019-12-22 21:37:26 +01:00
|
|
|
}, null)
|
2019-12-20 12:29:45 +01:00
|
|
|
}
|
2019-12-21 23:17:20 +01:00
|
|
|
return true
|
2019-12-20 12:29:45 +01:00
|
|
|
}
|
2022-03-07 11:49:37 +01:00
|
|
|
|
|
|
|
override fun onResume() {
|
|
|
|
super.onResume()
|
2022-03-23 13:39:38 +01:00
|
|
|
if(!queryingProtection) {
|
|
|
|
queryingProtection = true
|
|
|
|
activity?.let { activity ->
|
|
|
|
val cancelFail = {
|
|
|
|
queryingProtection = false
|
|
|
|
aapsLogger.debug(LTag.APS, "Dialog canceled on resume protection: ${this.javaClass.name}")
|
|
|
|
ToastUtils.showToastInUiThread(ctx, R.string.dialog_canceled)
|
|
|
|
dismiss()
|
|
|
|
}
|
|
|
|
protectionCheck.queryProtection(activity, BOLUS, { queryingProtection = false }, cancelFail, cancelFail)
|
2022-03-07 11:49:37 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|