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

204 lines
11 KiB
Kotlin
Raw Normal View History

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.content.Intent
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.Constants
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
import info.nightscout.androidaps.data.Profile
import info.nightscout.androidaps.db.CareportalEvent
import info.nightscout.androidaps.db.Source
import info.nightscout.androidaps.db.TempTarget
2020-01-10 23:14:58 +01:00
import info.nightscout.androidaps.interfaces.ActivePluginProvider
import info.nightscout.androidaps.interfaces.CommandQueueProvider
2019-12-20 14:52:10 +01:00
import info.nightscout.androidaps.interfaces.Constraint
2019-12-27 19:20:38 +01:00
import info.nightscout.androidaps.plugins.configBuilder.ConstraintChecker
import info.nightscout.androidaps.plugins.configBuilder.ProfileFunction
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
2020-04-08 14:03:57 +02:00
import info.nightscout.androidaps.utils.extensions.toVisibility
2019-12-27 19:20:38 +01:00
import info.nightscout.androidaps.utils.resources.ResourceHelper
2019-12-20 23:05:35 +01:00
import kotlinx.android.synthetic.main.dialog_insulin.*
2019-12-20 14:52:10 +01:00
import kotlinx.android.synthetic.main.notes.*
import kotlinx.android.synthetic.main.okcancel.*
import java.text.DecimalFormat
import java.util.*
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() {
2019-12-30 00:53:44 +01:00
@Inject lateinit var constraintChecker: ConstraintChecker
@Inject lateinit var resourceHelper: 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
2020-01-10 23:14:58 +01:00
@Inject lateinit var commandQueue: CommandQueueProvider
@Inject lateinit var activePlugin: ActivePluginProvider
2020-01-12 23:43:44 +01:00
@Inject lateinit var ctx: Context
2019-12-27 19:20:38 +01:00
2019-12-20 14:52:10 +01:00
companion object {
private const val PLUS1_DEFAULT = 0.5
private const val PLUS2_DEFAULT = 1.0
private const val PLUS3_DEFAULT = 2.0
}
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) {}
}
private fun validateInputs() {
2019-12-27 19:20:38 +01:00
val maxInsulin = constraintChecker.getMaxBolusAllowed().value()
2019-12-20 14:52:10 +01:00
if (abs(overview_insulin_time.value.toInt()) > 12 * 60) {
overview_insulin_time.value = 0.0
2020-01-10 23:14:58 +01:00
ToastUtils.showToastInUiThread(context, resourceHelper.gs(R.string.constraintapllied))
2019-12-20 14:52:10 +01:00
}
if (overview_insulin_amount.value > maxInsulin) {
overview_insulin_amount.value = 0.0
2020-01-10 23:14:58 +01:00
ToastUtils.showToastInUiThread(context, resourceHelper.gs(R.string.bolusconstraintapplied))
2019-12-20 14:52:10 +01:00
}
}
override fun onSaveInstanceState(savedInstanceState: Bundle) {
super.onSaveInstanceState(savedInstanceState)
savedInstanceState.putDouble("overview_insulin_time", overview_insulin_time.value)
savedInstanceState.putDouble("overview_insulin_amount", overview_insulin_amount.value)
}
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?): View? {
2019-12-20 23:05:35 +01:00
onCreateViewGeneral()
2019-12-20 18:55:54 +01:00
return inflater.inflate(R.layout.dialog_insulin, container, false)
2019-12-20 14:52:10 +01:00
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
2019-12-27 19:20:38 +01:00
val maxInsulin = constraintChecker.getMaxBolusAllowed().value()
2019-12-20 14:52:10 +01:00
overview_insulin_time.setParams(savedInstanceState?.getDouble("overview_insulin_time")
2019-12-20 23:05:35 +01:00
?: 0.0, -12 * 60.0, 12 * 60.0, 5.0, DecimalFormat("0"), false, ok, textWatcher)
2019-12-20 14:52:10 +01:00
overview_insulin_amount.setParams(savedInstanceState?.getDouble("overview_insulin_amount")
2020-03-17 22:56:14 +01:00
?: 0.0, 0.0, maxInsulin, activePlugin.activePump.pumpDescription.bolusStep, DecimalFormatter.pumpSupportedBolusFormat(activePlugin.activePump), false, ok, textWatcher)
2019-12-20 14:52:10 +01:00
2020-03-17 22:56:14 +01:00
overview_insulin_plus05.text = sp.getDouble(resourceHelper.gs(R.string.key_insulin_button_increment_1), PLUS1_DEFAULT).toSignedString(activePlugin.activePump)
2019-12-20 14:52:10 +01:00
overview_insulin_plus05.setOnClickListener {
overview_insulin_amount.value = max(0.0, overview_insulin_amount.value
2019-12-30 00:53:44 +01:00
+ sp.getDouble(resourceHelper.gs(R.string.key_insulin_button_increment_1), PLUS1_DEFAULT))
2019-12-20 14:52:10 +01:00
validateInputs()
}
2020-03-17 22:56:14 +01:00
overview_insulin_plus10.text = sp.getDouble(resourceHelper.gs(R.string.key_insulin_button_increment_2), PLUS2_DEFAULT).toSignedString(activePlugin.activePump)
2019-12-20 14:52:10 +01:00
overview_insulin_plus10.setOnClickListener {
overview_insulin_amount.value = max(0.0, overview_insulin_amount.value
2019-12-30 00:53:44 +01:00
+ sp.getDouble(resourceHelper.gs(R.string.key_insulin_button_increment_2), PLUS2_DEFAULT))
2019-12-20 14:52:10 +01:00
validateInputs()
}
2020-03-17 22:56:14 +01:00
overview_insulin_plus20.text = sp.getDouble(resourceHelper.gs(R.string.key_insulin_button_increment_3), PLUS3_DEFAULT).toSignedString(activePlugin.activePump)
2019-12-20 14:52:10 +01:00
overview_insulin_plus20.setOnClickListener {
2020-01-10 23:14:58 +01:00
overview_insulin_amount.value = max(0.0, overview_insulin_amount.value
2019-12-30 00:53:44 +01:00
+ sp.getDouble(resourceHelper.gs(R.string.key_insulin_button_increment_3), PLUS3_DEFAULT))
2019-12-20 14:52:10 +01:00
validateInputs()
}
overview_insulin_time_layout.visibility = View.GONE
overview_insulin_record_only.setOnCheckedChangeListener { _, isChecked: Boolean ->
2019-12-20 23:05:35 +01:00
overview_insulin_time_layout.visibility = isChecked.toVisibility()
2019-12-20 14:52:10 +01:00
}
}
2019-12-21 23:17:20 +01:00
override fun submit(): Boolean {
2020-03-16 21:40:29 +01:00
val pumpDescription = activePlugin.activePump.pumpDescription
2019-12-20 14:52:10 +01:00
val insulin = SafeParse.stringToDouble(overview_insulin_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()
val unitLabel = if (units == Constants.MMOL) resourceHelper.gs(R.string.mmol) else resourceHelper.gs(R.string.mgdl)
2019-12-20 14:52:10 +01:00
val recordOnlyChecked = overview_insulin_record_only.isChecked
val eatingSoonChecked = overview_insulin_start_eating_soon_tt.isChecked
2019-12-20 15:36:27 +01:00
if (insulinAfterConstraints > 0) {
2020-03-17 22:56:14 +01:00
actions.add(resourceHelper.gs(R.string.bolus) + ": " + "<font color='" + resourceHelper.gc(R.color.bolus) + "'>" + DecimalFormatter.toPumpSupportedBolus(insulinAfterConstraints, activePlugin.activePump) + resourceHelper.gs(R.string.insulin_unit_shortname) + "</font>")
2019-12-20 14:52:10 +01:00
if (recordOnlyChecked)
2019-12-27 19:20:38 +01:00
actions.add("<font color='" + resourceHelper.gc(R.color.warning) + "'>" + resourceHelper.gs(R.string.bolusrecordedonly) + "</font>")
2019-12-20 15:36:27 +01:00
if (abs(insulinAfterConstraints - insulin) > pumpDescription.pumpType.determineCorrectBolusStepSize(insulinAfterConstraints))
2019-12-27 19:20:38 +01:00
actions.add(resourceHelper.gs(R.string.bolusconstraintappliedwarning, resourceHelper.gc(R.color.warning), insulin, insulinAfterConstraints))
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)
2019-12-27 19:20:38 +01:00
actions.add(resourceHelper.gs(R.string.temptargetshort) + ": " + "<font color='" + resourceHelper.gc(R.color.tempTargetConfirmation) + "'>" + DecimalFormatter.to1Decimal(eatingSoonTT) + " " + unitLabel + " (" + eatingSoonTTDuration + " " + resourceHelper.gs(R.string.unit_minute_short) + ")</font>")
2019-12-20 14:52:10 +01:00
val timeOffset = overview_insulin_time.value.toInt()
val time = DateUtil.now() + T.mins(timeOffset.toLong()).msecs()
if (timeOffset != 0)
2019-12-27 19:20:38 +01:00
actions.add(resourceHelper.gs(R.string.time) + ": " + DateUtil.dateAndTimeString(time))
2019-12-20 14:52:10 +01:00
val notes = notes.text.toString()
if (notes.isNotEmpty())
2019-12-27 19:20:38 +01:00
actions.add(resourceHelper.gs(R.string.careportal_newnstreatment_notes_label) + ": " + notes)
2019-12-20 14:52:10 +01:00
if (insulinAfterConstraints > 0 || eatingSoonChecked) {
activity?.let { activity ->
2019-12-27 19:20:38 +01:00
OKDialog.showConfirmation(activity, resourceHelper.gs(R.string.bolus), HtmlHelper.fromHtml(Joiner.on("<br/>").join(actions)), Runnable {
2019-12-20 14:52:10 +01:00
if (eatingSoonChecked) {
2020-02-28 18:20:31 +01:00
aapsLogger.debug("USER ENTRY: TEMPTARGET EATING SOON $eatingSoonTT duration: $eatingSoonTTDuration")
2019-12-20 14:52:10 +01:00
val tempTarget = TempTarget()
2019-12-20 23:05:35 +01:00
.date(System.currentTimeMillis())
.duration(eatingSoonTTDuration)
2019-12-27 19:20:38 +01:00
.reason(resourceHelper.gs(R.string.eatingsoon))
2019-12-20 23:05:35 +01:00
.source(Source.USER)
2019-12-27 19:20:38 +01:00
.low(Profile.toMgdl(eatingSoonTT, profileFunction.getUnits()))
.high(Profile.toMgdl(eatingSoonTT, profileFunction.getUnits()))
2020-01-10 23:14:58 +01:00
activePlugin.activeTreatments.addToHistoryTempTarget(tempTarget)
2019-12-20 14:52:10 +01:00
}
if (insulinAfterConstraints > 0) {
val detailedBolusInfo = DetailedBolusInfo()
detailedBolusInfo.eventType = CareportalEvent.CORRECTIONBOLUS
detailedBolusInfo.insulin = insulinAfterConstraints
detailedBolusInfo.context = context
detailedBolusInfo.source = Source.USER
detailedBolusInfo.notes = notes
if (recordOnlyChecked) {
2020-02-28 18:20:31 +01:00
aapsLogger.debug("USER ENTRY: BOLUS RECORD ONLY $insulinAfterConstraints")
2019-12-20 14:52:10 +01:00
detailedBolusInfo.date = time
2020-01-10 23:14:58 +01:00
activePlugin.activeTreatments.addToHistoryTreatment(detailedBolusInfo, false)
2019-12-20 14:52:10 +01:00
} else {
2020-02-28 18:20:31 +01:00
aapsLogger.debug("USER ENTRY: BOLUS $insulinAfterConstraints")
2019-12-20 14:52:10 +01:00
detailedBolusInfo.date = DateUtil.now()
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) {
2020-01-12 23:43:44 +01:00
val i = Intent(ctx, ErrorHelperActivity::class.java)
2019-12-20 14:52:10 +01:00
i.putExtra("soundid", R.raw.boluserror)
i.putExtra("status", result.comment)
2019-12-27 19:20:38 +01:00
i.putExtra("title", resourceHelper.gs(R.string.treatmentdeliveryerror))
2019-12-20 14:52:10 +01:00
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
2020-01-12 23:43:44 +01:00
ctx.startActivity(i)
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 ->
2019-12-27 19:20:38 +01:00
OKDialog.show(activity, resourceHelper.gs(R.string.bolus), resourceHelper.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
}
}