AndroidAPS/app/src/main/java/info/nightscout/androidaps/dialogs/TempBasalDialog.kt

141 lines
6.9 KiB
Kotlin
Raw Normal View History

2019-12-20 18:55:54 +01:00
package info.nightscout.androidaps.dialogs
2019-12-20 11:43:21 +01:00
2020-01-12 23:43:44 +01:00
import android.content.Context
2019-12-20 11:43:21 +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
import info.nightscout.androidaps.database.entities.UserEntry.Action
import info.nightscout.androidaps.database.entities.UserEntry.Sources
2021-01-21 20:35:14 +01:00
import info.nightscout.androidaps.databinding.DialogTempbasalBinding
2021-04-01 23:46:21 +02:00
import info.nightscout.androidaps.interfaces.*
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 11:43:21 +01:00
import info.nightscout.androidaps.queue.Callback
import info.nightscout.androidaps.utils.HtmlHelper
import info.nightscout.androidaps.utils.SafeParse
2021-01-21 20:35:14 +01:00
import info.nightscout.androidaps.utils.alertDialogs.OKDialog
import info.nightscout.androidaps.extensions.formatColor
2019-12-27 19:20:38 +01:00
import info.nightscout.androidaps.utils.resources.ResourceHelper
2019-12-20 11:43:21 +01:00
import java.text.DecimalFormat
import java.util.*
2019-12-27 19:20:38 +01:00
import javax.inject.Inject
2019-12-20 11:43:21 +01:00
import kotlin.math.abs
class TempBasalDialog : DialogFragmentWithDate() {
2021-01-21 20:35:14 +01:00
2019-12-31 00:37:36 +01:00
@Inject lateinit var constraintChecker: ConstraintChecker
2021-11-04 10:56:12 +01:00
@Inject lateinit var rh: ResourceHelper
2019-12-31 00:37:36 +01:00
@Inject lateinit var profileFunction: ProfileFunction
2021-04-14 00:45:30 +02:00
@Inject lateinit var activePlugin: ActivePlugin
2021-11-07 17:19:06 +01:00
@Inject lateinit var commandQueue: CommandQueue
2020-01-12 23:43:44 +01:00
@Inject lateinit var ctx: Context
2021-02-09 17:57:28 +01:00
@Inject lateinit var uel: UserEntryLogger
2019-12-27 19:20:38 +01:00
2019-12-20 11:43:21 +01:00
private var isPercentPump = true
2021-01-21 20:35:14 +01:00
private var _binding: DialogTempbasalBinding? = null
// This property is only valid between onCreateView and
// onDestroyView.
private val binding get() = _binding!!
2019-12-20 11:43:21 +01:00
override fun onSaveInstanceState(savedInstanceState: Bundle) {
super.onSaveInstanceState(savedInstanceState)
2021-01-21 20:35:14 +01:00
savedInstanceState.putDouble("duration", binding.duration.value)
savedInstanceState.putDouble("basalpercentinput", binding.basalpercentinput.value)
savedInstanceState.putDouble("basalabsoluteinput", binding.basalabsoluteinput.value)
2019-12-20 11:43:21 +01:00
}
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,
2021-01-21 20:35:14 +01:00
savedInstanceState: Bundle?): View {
2019-12-20 23:05:35 +01:00
onCreateViewGeneral()
2021-01-21 20:35:14 +01:00
_binding = DialogTempbasalBinding.inflate(inflater, container, false)
return binding.root
2019-12-20 11:43:21 +01:00
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
2020-03-16 21:40:29 +01:00
val pumpDescription = activePlugin.activePump.pumpDescription
2019-12-27 19:20:38 +01:00
val profile = profileFunction.getProfile() ?: return
2019-12-20 11:43:21 +01:00
val maxTempPercent = pumpDescription.maxTempPercent.toDouble()
val tempPercentStep = pumpDescription.tempPercentStep.toDouble()
2021-01-21 20:35:14 +01:00
binding.basalpercentinput.setParams(savedInstanceState?.getDouble("basalpercentinput")
?: 100.0, 0.0, maxTempPercent, tempPercentStep, DecimalFormat("0"), true, binding.okcancel.ok)
2019-12-20 11:43:21 +01:00
2021-01-21 20:35:14 +01:00
binding.basalabsoluteinput.setParams(savedInstanceState?.getDouble("basalabsoluteinput")
2021-04-19 18:44:17 +02:00
?: profile.getBasal(), 0.0, pumpDescription.maxTempAbsolute, pumpDescription.tempAbsoluteStep, DecimalFormat("0.00"), true, binding.okcancel.ok)
2019-12-20 11:43:21 +01:00
val tempDurationStep = pumpDescription.tempDurationStep.toDouble()
val tempMaxDuration = pumpDescription.tempMaxDuration.toDouble()
2021-01-21 20:35:14 +01:00
binding.duration.setParams(savedInstanceState?.getDouble("duration")
?: tempDurationStep, tempDurationStep, tempMaxDuration, tempDurationStep, DecimalFormat("0"), false, binding.okcancel.ok)
2019-12-20 11:43:21 +01:00
isPercentPump = pumpDescription.tempBasalStyle and PumpDescription.PERCENT == PumpDescription.PERCENT
if (isPercentPump) {
2021-01-21 20:35:14 +01:00
binding.percentLayout.visibility = View.VISIBLE
binding.absoluteLayout.visibility = View.GONE
2019-12-20 11:43:21 +01:00
} else {
2021-01-21 20:35:14 +01:00
binding.percentLayout.visibility = View.GONE
binding.absoluteLayout.visibility = View.VISIBLE
2019-12-20 11:43:21 +01:00
}
}
2021-01-21 20:35:14 +01:00
override fun onDestroyView() {
super.onDestroyView()
_binding = null
}
2019-12-21 23:17:20 +01:00
override fun submit(): Boolean {
2021-01-21 20:35:14 +01:00
if (_binding == null) return false
2019-12-20 11:43:21 +01:00
var percent = 0
var absolute = 0.0
2021-01-21 20:35:14 +01:00
val durationInMinutes = binding.duration.value?.toInt() ?: return false
2019-12-27 19:20:38 +01:00
val profile = profileFunction.getProfile() ?: return false
2019-12-20 11:43:21 +01:00
val actions: LinkedList<String> = LinkedList()
if (isPercentPump) {
2021-01-21 20:35:14 +01:00
val basalPercentInput = SafeParse.stringToInt(binding.basalpercentinput.text)
2019-12-27 19:20:38 +01:00
percent = constraintChecker.applyBasalPercentConstraints(Constraint(basalPercentInput), profile).value()
2021-11-04 10:56:12 +01:00
actions.add(rh.gs(R.string.tempbasal_label) + ": $percent%")
actions.add(rh.gs(R.string.duration) + ": " + rh.gs(R.string.format_mins, durationInMinutes))
if (percent != basalPercentInput) actions.add(rh.gs(R.string.constraintapllied))
2019-12-20 11:43:21 +01:00
} else {
2021-01-21 20:35:14 +01:00
val basalAbsoluteInput = SafeParse.stringToDouble(binding.basalabsoluteinput.text)
2019-12-27 19:20:38 +01:00
absolute = constraintChecker.applyBasalConstraints(Constraint(basalAbsoluteInput), profile).value()
2021-11-04 10:56:12 +01:00
actions.add(rh.gs(R.string.tempbasal_label) + ": " + rh.gs(R.string.pump_basebasalrate, absolute))
actions.add(rh.gs(R.string.duration) + ": " + rh.gs(R.string.format_mins, durationInMinutes))
2019-12-20 13:58:51 +01:00
if (abs(absolute - basalAbsoluteInput) > 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 11:43:21 +01:00
}
activity?.let { activity ->
2021-11-04 10:56:12 +01:00
OKDialog.showConfirmation(activity, rh.gs(R.string.tempbasal_label), HtmlHelper.fromHtml(Joiner.on("<br/>").join(actions)), {
2019-12-20 11:43:21 +01:00
val callback: Callback = object : Callback() {
override fun run() {
if (!result.success) {
2021-11-04 10:56:12 +01:00
ErrorHelperActivity.runAlarm(ctx, result.comment, rh.gs(R.string.tempbasaldeliveryerror), R.raw.boluserror)
2019-12-20 11:43:21 +01:00
}
}
}
if (isPercentPump) {
uel.log(Action.TEMP_BASAL, Sources.TempBasalDialog,
2021-04-03 21:29:26 +02:00
ValueWithUnit.Percent(percent),
ValueWithUnit.Minute(durationInMinutes))
2021-04-01 23:46:21 +02:00
commandQueue.tempBasalPercent(percent, durationInMinutes, true, profile, PumpSync.TemporaryBasalType.NORMAL, callback)
2019-12-20 11:43:21 +01:00
} else {
uel.log(Action.TEMP_BASAL, Sources.TempBasalDialog,
2021-04-03 21:29:26 +02:00
ValueWithUnit.Insulin(absolute),
ValueWithUnit.Minute(durationInMinutes))
2021-04-01 23:46:21 +02:00
commandQueue.tempBasalAbsolute(absolute, durationInMinutes, true, profile, PumpSync.TemporaryBasalType.NORMAL, callback)
2019-12-20 11:43:21 +01:00
}
2019-12-22 21:37:26 +01:00
})
2019-12-20 11:43:21 +01:00
}
2019-12-21 23:17:20 +01:00
return true
2019-12-20 11:43:21 +01:00
}
}