2019-12-20 18:55:54 +01:00
|
|
|
package info.nightscout.androidaps.dialogs
|
2019-12-20 14:52:10 +01:00
|
|
|
|
2020-01-12 23:43:44 +01:00
|
|
|
import android.content.Context
|
2019-12-20 14:52:10 +01:00
|
|
|
import android.os.Bundle
|
|
|
|
import android.text.Editable
|
|
|
|
import android.text.TextWatcher
|
|
|
|
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
|
2019-12-20 14:52:10 +01:00
|
|
|
import info.nightscout.androidaps.data.DetailedBolusInfo
|
2021-02-09 17:57:28 +01:00
|
|
|
import info.nightscout.androidaps.database.AppRepository
|
2021-03-01 12:42:42 +01:00
|
|
|
import info.nightscout.androidaps.database.entities.TemporaryTarget
|
2021-03-22 16:59:09 +01:00
|
|
|
import info.nightscout.androidaps.database.entities.UserEntry.Action
|
2021-03-31 00:18:55 +02:00
|
|
|
import info.nightscout.androidaps.database.entities.UserEntry.Sources
|
2021-12-10 08:58:23 +01:00
|
|
|
import info.nightscout.androidaps.database.entities.ValueWithUnit
|
2021-05-28 16:06:44 +02:00
|
|
|
import info.nightscout.androidaps.database.transactions.InsertAndCancelCurrentTemporaryTargetTransaction
|
2021-01-21 20:31:20 +01:00
|
|
|
import info.nightscout.androidaps.databinding.DialogInsulinBinding
|
2021-12-10 08:58:23 +01:00
|
|
|
import info.nightscout.androidaps.extensions.formatColor
|
|
|
|
import info.nightscout.androidaps.extensions.toVisibility
|
|
|
|
import info.nightscout.androidaps.interfaces.*
|
2021-12-10 15:19:19 +01:00
|
|
|
import info.nightscout.shared.logging.LTag
|
2021-02-09 17:57:28 +01:00
|
|
|
import info.nightscout.androidaps.logging.UserEntryLogger
|
2021-01-21 20:31:20 +01:00
|
|
|
import info.nightscout.androidaps.plugins.configBuilder.ConstraintChecker
|
2019-12-20 14:52:10 +01:00
|
|
|
import info.nightscout.androidaps.queue.Callback
|
|
|
|
import info.nightscout.androidaps.utils.*
|
2020-04-08 14:03:57 +02:00
|
|
|
import info.nightscout.androidaps.utils.alertDialogs.OKDialog
|
2019-12-31 11:57:58 +01:00
|
|
|
import info.nightscout.androidaps.utils.extensions.toSignedString
|
2019-12-27 19:20:38 +01:00
|
|
|
import info.nightscout.androidaps.utils.resources.ResourceHelper
|
2021-12-10 08:58:23 +01:00
|
|
|
import info.nightscout.shared.SafeParse
|
2022-02-10 19:11:58 +01:00
|
|
|
import io.reactivex.rxjava3.disposables.CompositeDisposable
|
|
|
|
import io.reactivex.rxjava3.kotlin.plusAssign
|
2019-12-20 14:52:10 +01:00
|
|
|
import java.text.DecimalFormat
|
|
|
|
import java.util.*
|
2021-03-01 12:42:42 +01:00
|
|
|
import java.util.concurrent.TimeUnit
|
2019-12-27 19:20:38 +01:00
|
|
|
import javax.inject.Inject
|
2019-12-20 14:52:10 +01:00
|
|
|
import kotlin.math.abs
|
|
|
|
import kotlin.math.max
|
|
|
|
|
|
|
|
class InsulinDialog : DialogFragmentWithDate() {
|
2021-01-21 20:31:20 +01:00
|
|
|
|
2019-12-30 00:53:44 +01:00
|
|
|
@Inject lateinit var constraintChecker: ConstraintChecker
|
2021-11-04 10:56:12 +01:00
|
|
|
@Inject lateinit var rh: ResourceHelper
|
2020-01-01 23:23:16 +01:00
|
|
|
@Inject lateinit var defaultValueHelper: DefaultValueHelper
|
2019-12-30 00:53:44 +01:00
|
|
|
@Inject lateinit var profileFunction: ProfileFunction
|
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
|
2020-01-12 23:43:44 +01:00
|
|
|
@Inject lateinit var ctx: Context
|
2021-02-09 17:57:28 +01:00
|
|
|
@Inject lateinit var repository: AppRepository
|
2020-07-24 13:00:11 +02:00
|
|
|
@Inject lateinit var config: Config
|
2021-10-26 22:39:52 +02:00
|
|
|
@Inject lateinit var bolusTimer: BolusTimer
|
2021-02-09 17:57:28 +01:00
|
|
|
@Inject lateinit var uel: UserEntryLogger
|
2019-12-27 19:20:38 +01:00
|
|
|
|
2019-12-20 14:52:10 +01:00
|
|
|
companion object {
|
2021-01-21 20:31:20 +01:00
|
|
|
|
2019-12-20 14:52:10 +01:00
|
|
|
private const val PLUS1_DEFAULT = 0.5
|
|
|
|
private const val PLUS2_DEFAULT = 1.0
|
|
|
|
private const val PLUS3_DEFAULT = 2.0
|
|
|
|
}
|
|
|
|
|
2021-03-01 12:42:42 +01:00
|
|
|
private val disposable = CompositeDisposable()
|
|
|
|
|
2019-12-20 14:52:10 +01:00
|
|
|
private val textWatcher: TextWatcher = object : TextWatcher {
|
|
|
|
override fun afterTextChanged(s: Editable) {
|
|
|
|
validateInputs()
|
|
|
|
}
|
|
|
|
|
|
|
|
override fun beforeTextChanged(s: CharSequence, start: Int, count: Int, after: Int) {}
|
|
|
|
override fun onTextChanged(s: CharSequence, start: Int, before: Int, count: Int) {}
|
|
|
|
}
|
|
|
|
|
2021-01-21 20:31:20 +01:00
|
|
|
private var _binding: DialogInsulinBinding? = null
|
|
|
|
|
|
|
|
// This property is only valid between onCreateView and
|
|
|
|
// onDestroyView.
|
|
|
|
private val binding get() = _binding!!
|
|
|
|
|
2019-12-20 14:52:10 +01:00
|
|
|
private fun validateInputs() {
|
2019-12-27 19:20:38 +01:00
|
|
|
val maxInsulin = constraintChecker.getMaxBolusAllowed().value()
|
2021-01-21 20:31:20 +01:00
|
|
|
if (abs(binding.time.value.toInt()) > 12 * 60) {
|
|
|
|
binding.time.value = 0.0
|
2021-11-04 10:56:12 +01:00
|
|
|
ToastUtils.showToastInUiThread(context, rh.gs(R.string.constraintapllied))
|
2019-12-20 14:52:10 +01:00
|
|
|
}
|
2021-01-21 20:31:20 +01:00
|
|
|
if (binding.amount.value > maxInsulin) {
|
|
|
|
binding.amount.value = 0.0
|
2021-11-04 10:56:12 +01:00
|
|
|
ToastUtils.showToastInUiThread(context, rh.gs(R.string.bolusconstraintapplied))
|
2019-12-20 14:52:10 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
override fun onSaveInstanceState(savedInstanceState: Bundle) {
|
|
|
|
super.onSaveInstanceState(savedInstanceState)
|
2021-01-21 20:31:20 +01:00
|
|
|
savedInstanceState.putDouble("time", binding.time.value)
|
|
|
|
savedInstanceState.putDouble("amount", binding.amount.value)
|
2019-12-20 14:52:10 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,
|
2021-01-21 20:31:20 +01:00
|
|
|
savedInstanceState: Bundle?): View {
|
2019-12-20 23:05:35 +01:00
|
|
|
onCreateViewGeneral()
|
2021-01-21 20:31:20 +01:00
|
|
|
_binding = DialogInsulinBinding.inflate(inflater, container, false)
|
|
|
|
return binding.root
|
2019-12-20 14:52:10 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
|
|
|
|
super.onViewCreated(view, savedInstanceState)
|
|
|
|
|
2020-07-24 13:00:11 +02:00
|
|
|
if (config.NSCLIENT) {
|
2021-01-21 20:31:20 +01:00
|
|
|
binding.recordOnly.isChecked = true
|
|
|
|
binding.recordOnly.isEnabled = false
|
2020-07-24 13:00:11 +02:00
|
|
|
}
|
2019-12-27 19:20:38 +01:00
|
|
|
val maxInsulin = constraintChecker.getMaxBolusAllowed().value()
|
|
|
|
|
2021-01-21 20:31:20 +01:00
|
|
|
binding.time.setParams(savedInstanceState?.getDouble("time")
|
|
|
|
?: 0.0, -12 * 60.0, 12 * 60.0, 5.0, DecimalFormat("0"), false, binding.okcancel.ok, textWatcher)
|
|
|
|
binding.amount.setParams(savedInstanceState?.getDouble("amount")
|
|
|
|
?: 0.0, 0.0, maxInsulin, activePlugin.activePump.pumpDescription.bolusStep, DecimalFormatter.pumpSupportedBolusFormat(activePlugin.activePump), false, binding.okcancel.ok, textWatcher)
|
2019-12-20 14:52:10 +01:00
|
|
|
|
2022-02-12 08:26:07 +01:00
|
|
|
val plus05Text = sp.getDouble(rh.gs(R.string.key_insulin_button_increment_1), PLUS1_DEFAULT).toSignedString(activePlugin.activePump)
|
|
|
|
binding.plus05.text = plus05Text
|
|
|
|
binding.plus05.contentDescription = rh.gs(R.string.overview_insulin_label) + " " + plus05Text
|
2021-01-21 20:31:20 +01:00
|
|
|
binding.plus05.setOnClickListener {
|
|
|
|
binding.amount.value = max(0.0, binding.amount.value
|
2021-11-04 10:56:12 +01:00
|
|
|
+ sp.getDouble(rh.gs(R.string.key_insulin_button_increment_1), PLUS1_DEFAULT))
|
2019-12-20 14:52:10 +01:00
|
|
|
validateInputs()
|
2022-02-12 08:26:07 +01:00
|
|
|
binding.amount.announceValue()
|
2019-12-20 14:52:10 +01:00
|
|
|
}
|
2022-02-12 08:26:07 +01:00
|
|
|
val plus10Text = sp.getDouble(rh.gs(R.string.key_insulin_button_increment_2), PLUS2_DEFAULT).toSignedString(activePlugin.activePump)
|
|
|
|
binding.plus10.text = plus10Text
|
|
|
|
binding.plus10.contentDescription = rh.gs(R.string.overview_insulin_label) + " " + plus10Text
|
2021-01-21 20:31:20 +01:00
|
|
|
binding.plus10.setOnClickListener {
|
|
|
|
binding.amount.value = max(0.0, binding.amount.value
|
2021-11-04 10:56:12 +01:00
|
|
|
+ sp.getDouble(rh.gs(R.string.key_insulin_button_increment_2), PLUS2_DEFAULT))
|
2019-12-20 14:52:10 +01:00
|
|
|
validateInputs()
|
2022-02-12 08:26:07 +01:00
|
|
|
binding.amount.announceValue()
|
2019-12-20 14:52:10 +01:00
|
|
|
}
|
2022-02-12 08:26:07 +01:00
|
|
|
val plus20Text = sp.getDouble(rh.gs(R.string.key_insulin_button_increment_3), PLUS3_DEFAULT).toSignedString(activePlugin.activePump)
|
|
|
|
binding.plus20.text = plus20Text
|
|
|
|
binding.plus20.contentDescription = rh.gs(R.string.overview_insulin_label) + " " + plus20Text
|
2021-01-21 20:31:20 +01:00
|
|
|
binding.plus20.setOnClickListener {
|
|
|
|
binding.amount.value = max(0.0, binding.amount.value
|
2021-11-04 10:56:12 +01:00
|
|
|
+ sp.getDouble(rh.gs(R.string.key_insulin_button_increment_3), PLUS3_DEFAULT))
|
2019-12-20 14:52:10 +01:00
|
|
|
validateInputs()
|
2022-02-12 08:26:07 +01:00
|
|
|
binding.amount.announceValue()
|
2019-12-20 14:52:10 +01:00
|
|
|
}
|
|
|
|
|
2021-01-21 20:31:20 +01:00
|
|
|
binding.timeLayout.visibility = View.GONE
|
|
|
|
binding.recordOnly.setOnCheckedChangeListener { _, isChecked: Boolean ->
|
|
|
|
binding.timeLayout.visibility = isChecked.toVisibility()
|
2019-12-20 14:52:10 +01:00
|
|
|
}
|
2022-02-12 08:26:07 +01:00
|
|
|
binding.amount.editText?.id?.let { binding.insulinLabel.labelFor = it }
|
|
|
|
binding.time.editText?.id?.let { binding.timeLabel.labelFor = it }
|
2019-12-20 14:52:10 +01:00
|
|
|
}
|
|
|
|
|
2021-01-21 20:31:20 +01:00
|
|
|
override fun onDestroyView() {
|
|
|
|
super.onDestroyView()
|
2021-03-01 12:42:42 +01:00
|
|
|
disposable.clear()
|
2021-01-21 20:31:20 +01:00
|
|
|
_binding = null
|
|
|
|
}
|
|
|
|
|
2019-12-21 23:17:20 +01:00
|
|
|
override fun submit(): Boolean {
|
2021-01-21 20:31:20 +01:00
|
|
|
if (_binding == null) return false
|
2020-03-16 21:40:29 +01:00
|
|
|
val pumpDescription = activePlugin.activePump.pumpDescription
|
2021-11-23 19:53:45 +01:00
|
|
|
val insulin = SafeParse.stringToDouble(binding.amount.text)
|
2019-12-27 19:20:38 +01:00
|
|
|
val insulinAfterConstraints = constraintChecker.applyBolusConstraints(Constraint(insulin)).value()
|
2019-12-20 14:52:10 +01:00
|
|
|
val actions: LinkedList<String?> = LinkedList()
|
2019-12-27 19:20:38 +01:00
|
|
|
val units = profileFunction.getUnits()
|
2021-11-04 10:56:12 +01:00
|
|
|
val unitLabel = if (units == GlucoseUnit.MMOL) rh.gs(R.string.mmol) else rh.gs(R.string.mgdl)
|
2021-01-21 20:31:20 +01:00
|
|
|
val recordOnlyChecked = binding.recordOnly.isChecked
|
|
|
|
val eatingSoonChecked = binding.startEatingSoonTt.isChecked
|
2019-12-20 14:52:10 +01:00
|
|
|
|
2019-12-20 15:36:27 +01:00
|
|
|
if (insulinAfterConstraints > 0) {
|
2021-11-04 10:56:12 +01:00
|
|
|
actions.add(rh.gs(R.string.bolus) + ": " + DecimalFormatter.toPumpSupportedBolus(insulinAfterConstraints, activePlugin.activePump, rh).formatColor(rh, R.color.bolus))
|
2019-12-20 14:52:10 +01:00
|
|
|
if (recordOnlyChecked)
|
2021-11-04 10:56:12 +01:00
|
|
|
actions.add(rh.gs(R.string.bolusrecordedonly).formatColor(rh, R.color.warning))
|
2019-12-20 15:36:27 +01:00
|
|
|
if (abs(insulinAfterConstraints - insulin) > pumpDescription.pumpType.determineCorrectBolusStepSize(insulinAfterConstraints))
|
2021-11-04 10:56:12 +01:00
|
|
|
actions.add(rh.gs(R.string.bolusconstraintappliedwarn, insulin, insulinAfterConstraints).formatColor(rh, R.color.warning))
|
2019-12-20 14:52:10 +01:00
|
|
|
}
|
2020-01-01 23:23:16 +01:00
|
|
|
val eatingSoonTTDuration = defaultValueHelper.determineEatingSoonTTDuration()
|
|
|
|
val eatingSoonTT = defaultValueHelper.determineEatingSoonTT()
|
2019-12-20 14:52:10 +01:00
|
|
|
if (eatingSoonChecked)
|
2021-11-04 10:56:12 +01:00
|
|
|
actions.add(rh.gs(R.string.temptargetshort) + ": " + (DecimalFormatter.to1Decimal(eatingSoonTT) + " " + unitLabel + " (" + rh.gs(R.string.format_mins, eatingSoonTTDuration) + ")").formatColor(rh, R.color.tempTargetConfirmation))
|
2019-12-20 14:52:10 +01:00
|
|
|
|
2021-01-21 20:31:20 +01:00
|
|
|
val timeOffset = binding.time.value.toInt()
|
2021-04-11 17:58:50 +02:00
|
|
|
val time = dateUtil.now() + T.mins(timeOffset.toLong()).msecs()
|
2019-12-20 14:52:10 +01:00
|
|
|
if (timeOffset != 0)
|
2021-11-04 10:56:12 +01:00
|
|
|
actions.add(rh.gs(R.string.time) + ": " + dateUtil.dateAndTimeString(time))
|
2019-12-20 14:52:10 +01:00
|
|
|
|
2021-01-21 20:31:20 +01:00
|
|
|
val notes = binding.notesLayout.notes.text.toString()
|
2019-12-20 14:52:10 +01:00
|
|
|
if (notes.isNotEmpty())
|
2021-11-04 10:56:12 +01:00
|
|
|
actions.add(rh.gs(R.string.notes_label) + ": " + notes)
|
2019-12-20 14:52:10 +01:00
|
|
|
|
|
|
|
if (insulinAfterConstraints > 0 || eatingSoonChecked) {
|
|
|
|
activity?.let { activity ->
|
2021-11-04 10:56:12 +01:00
|
|
|
OKDialog.showConfirmation(activity, rh.gs(R.string.bolus), HtmlHelper.fromHtml(Joiner.on("<br/>").join(actions)), {
|
2019-12-20 14:52:10 +01:00
|
|
|
if (eatingSoonChecked) {
|
2021-04-01 22:59:05 +02:00
|
|
|
uel.log(Action.TT, Sources.InsulinDialog,
|
|
|
|
notes,
|
2021-04-03 21:29:26 +02:00
|
|
|
ValueWithUnit.TherapyEventTTReason(TemporaryTarget.Reason.EATING_SOON),
|
2021-04-19 18:44:17 +02:00
|
|
|
ValueWithUnit.fromGlucoseUnit(eatingSoonTT, units.asText),
|
2021-04-03 21:29:26 +02:00
|
|
|
ValueWithUnit.Minute(eatingSoonTTDuration))
|
2021-05-28 16:06:44 +02:00
|
|
|
disposable += repository.runTransactionForResult(InsertAndCancelCurrentTemporaryTargetTransaction(
|
2021-03-01 12:42:42 +01:00
|
|
|
timestamp = System.currentTimeMillis(),
|
|
|
|
duration = TimeUnit.MINUTES.toMillis(eatingSoonTTDuration.toLong()),
|
|
|
|
reason = TemporaryTarget.Reason.EATING_SOON,
|
|
|
|
lowTarget = Profile.toMgdl(eatingSoonTT, profileFunction.getUnits()),
|
|
|
|
highTarget = Profile.toMgdl(eatingSoonTT, profileFunction.getUnits())
|
|
|
|
)).subscribe({ result ->
|
2021-03-26 16:23:47 +01:00
|
|
|
result.inserted.forEach { aapsLogger.debug(LTag.DATABASE, "Inserted temp target $it") }
|
|
|
|
result.updated.forEach { aapsLogger.debug(LTag.DATABASE, "Updated temp target $it") }
|
2021-03-01 12:42:42 +01:00
|
|
|
}, {
|
2021-03-26 16:23:47 +01:00
|
|
|
aapsLogger.error(LTag.DATABASE, "Error while saving temporary target", it)
|
2021-03-01 12:42:42 +01:00
|
|
|
})
|
2019-12-20 14:52:10 +01:00
|
|
|
}
|
|
|
|
if (insulinAfterConstraints > 0) {
|
|
|
|
val detailedBolusInfo = DetailedBolusInfo()
|
2021-03-25 17:48:07 +01:00
|
|
|
detailedBolusInfo.eventType = DetailedBolusInfo.EventType.CORRECTION_BOLUS
|
2019-12-20 14:52:10 +01:00
|
|
|
detailedBolusInfo.insulin = insulinAfterConstraints
|
|
|
|
detailedBolusInfo.context = context
|
|
|
|
detailedBolusInfo.notes = notes
|
2021-03-25 23:52:08 +01:00
|
|
|
detailedBolusInfo.timestamp = time
|
2019-12-20 14:52:10 +01:00
|
|
|
if (recordOnlyChecked) {
|
2021-04-01 22:59:05 +02:00
|
|
|
uel.log(Action.BOLUS, Sources.InsulinDialog,
|
2021-11-04 10:56:12 +01:00
|
|
|
rh.gs(R.string.record) + if (notes.isNotEmpty()) ": " + notes else "",
|
|
|
|
ValueWithUnit.SimpleString(rh.gsNotLocalised(R.string.record)),
|
2021-04-03 21:29:26 +02:00
|
|
|
ValueWithUnit.Insulin(insulinAfterConstraints),
|
|
|
|
ValueWithUnit.Minute(timeOffset).takeIf { timeOffset!= 0 })
|
2021-03-25 23:52:08 +01:00
|
|
|
disposable += repository.runTransactionForResult(detailedBolusInfo.insertBolusTransaction())
|
2021-03-28 13:45:27 +02:00
|
|
|
.subscribe(
|
|
|
|
{ result -> result.inserted.forEach { aapsLogger.debug(LTag.DATABASE, "Inserted bolus $it") } },
|
|
|
|
{ aapsLogger.error(LTag.DATABASE, "Error while saving bolus", it) }
|
|
|
|
)
|
2021-10-26 22:39:52 +02:00
|
|
|
if (timeOffset == 0)
|
|
|
|
bolusTimer.removeBolusReminder()
|
2019-12-20 14:52:10 +01:00
|
|
|
} else {
|
2021-04-11 16:11:37 +02:00
|
|
|
uel.log(Action.BOLUS, Sources.InsulinDialog,
|
|
|
|
notes,
|
|
|
|
ValueWithUnit.Insulin(insulinAfterConstraints))
|
2020-01-10 23:14:58 +01:00
|
|
|
commandQueue.bolus(detailedBolusInfo, object : Callback() {
|
2019-12-20 14:52:10 +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)
|
2021-10-26 22:39:52 +02:00
|
|
|
} else {
|
|
|
|
bolusTimer.removeBolusReminder()
|
2021-04-11 16:11:37 +02:00
|
|
|
}
|
2019-12-20 14:52:10 +01:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2019-12-22 21:37:26 +01:00
|
|
|
})
|
2019-12-20 14:52:10 +01:00
|
|
|
}
|
|
|
|
} else
|
2019-12-22 21:37:26 +01:00
|
|
|
activity?.let { activity ->
|
2021-11-04 10:56:12 +01:00
|
|
|
OKDialog.show(activity, rh.gs(R.string.bolus), rh.gs(R.string.no_action_selected))
|
2019-12-22 21:37:26 +01:00
|
|
|
}
|
2019-12-21 23:17:20 +01:00
|
|
|
return true
|
2019-12-20 14:52:10 +01:00
|
|
|
}
|
|
|
|
}
|