2019-12-20 18:55:54 +01:00
|
|
|
package info.nightscout.androidaps.dialogs
|
2019-12-19 23:45:27 +01:00
|
|
|
|
2020-01-10 23:14:58 +01:00
|
|
|
import android.content.Context
|
2019-12-19 23:45:27 +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
|
2019-12-19 23:45:27 +01:00
|
|
|
import info.nightscout.androidaps.data.DetailedBolusInfo
|
2021-03-08 20:10:02 +01:00
|
|
|
import info.nightscout.androidaps.database.AppRepository
|
2021-04-03 21:29:26 +02:00
|
|
|
import info.nightscout.androidaps.database.entities.ValueWithUnit
|
2021-03-08 20:10:02 +01:00
|
|
|
import info.nightscout.androidaps.database.entities.TherapyEvent
|
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-03-29 00:31:40 +02:00
|
|
|
import info.nightscout.androidaps.database.transactions.InsertIfNewByTimestampTherapyEventTransaction
|
2021-01-21 20:21:25 +01:00
|
|
|
import info.nightscout.androidaps.databinding.DialogFillBinding
|
2021-04-14 00:45:30 +02:00
|
|
|
import info.nightscout.androidaps.interfaces.ActivePlugin
|
2020-01-10 23:14:58 +01:00
|
|
|
import info.nightscout.androidaps.interfaces.CommandQueueProvider
|
2019-12-19 23:45:27 +01:00
|
|
|
import info.nightscout.androidaps.interfaces.Constraint
|
2021-03-08 20:10:02 +01:00
|
|
|
import info.nightscout.androidaps.logging.LTag
|
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-19 23:45:27 +01:00
|
|
|
import info.nightscout.androidaps.queue.Callback
|
2019-12-27 19:20:38 +01:00
|
|
|
import info.nightscout.androidaps.utils.DecimalFormatter
|
|
|
|
import info.nightscout.androidaps.utils.HtmlHelper
|
|
|
|
import info.nightscout.androidaps.utils.SafeParse
|
2020-04-24 23:03:46 +02:00
|
|
|
import info.nightscout.androidaps.utils.alertDialogs.OKDialog
|
2021-04-05 22:41:28 +02:00
|
|
|
import info.nightscout.androidaps.extensions.formatColor
|
2019-12-27 19:20:38 +01:00
|
|
|
import info.nightscout.androidaps.utils.resources.ResourceHelper
|
2021-03-08 20:10:02 +01:00
|
|
|
import io.reactivex.disposables.CompositeDisposable
|
|
|
|
import io.reactivex.rxkotlin.plusAssign
|
2019-12-19 23:45:27 +01:00
|
|
|
import java.util.*
|
2019-12-27 19:20:38 +01:00
|
|
|
import javax.inject.Inject
|
2019-12-19 23:45:27 +01:00
|
|
|
import kotlin.math.abs
|
|
|
|
|
|
|
|
class FillDialog : DialogFragmentWithDate() {
|
2021-01-21 20:21:25 +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-10 23:14:58 +01:00
|
|
|
@Inject lateinit var ctx: Context
|
|
|
|
@Inject lateinit var commandQueue: CommandQueueProvider
|
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
|
2021-03-08 20:10:02 +01:00
|
|
|
@Inject lateinit var repository: AppRepository
|
|
|
|
|
|
|
|
private val disposable = CompositeDisposable()
|
2019-12-27 19:20:38 +01:00
|
|
|
|
2021-01-21 20:21:25 +01:00
|
|
|
private var _binding: DialogFillBinding? = null
|
|
|
|
|
|
|
|
// This property is only valid between onCreateView and
|
|
|
|
// onDestroyView.
|
|
|
|
private val binding get() = _binding!!
|
|
|
|
|
2019-12-19 23:45:27 +01:00
|
|
|
override fun onSaveInstanceState(savedInstanceState: Bundle) {
|
|
|
|
super.onSaveInstanceState(savedInstanceState)
|
2021-01-21 20:21:25 +01:00
|
|
|
savedInstanceState.putDouble("fill_insulin_amount", binding.fillInsulinamount.value)
|
2019-12-19 23:45:27 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,
|
2021-01-21 20:21:25 +01:00
|
|
|
savedInstanceState: Bundle?): View {
|
2019-12-20 23:05:35 +01:00
|
|
|
onCreateViewGeneral()
|
2021-01-21 20:21:25 +01:00
|
|
|
_binding = DialogFillBinding.inflate(inflater, container, false)
|
|
|
|
return binding.root
|
2019-12-19 23:45:27 +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()
|
2020-01-10 23:14:58 +01:00
|
|
|
val bolusStep = activePlugin.activePump.pumpDescription.bolusStep
|
2021-01-21 20:21:25 +01:00
|
|
|
binding.fillInsulinamount.setParams(savedInstanceState?.getDouble("fill_insulin_amount")
|
|
|
|
?: 0.0, 0.0, maxInsulin, bolusStep, DecimalFormatter.pumpSupportedBolusFormat(activePlugin.activePump), true, binding.okcancel.ok)
|
2019-12-27 19:20:38 +01:00
|
|
|
val amount1 = sp.getDouble("fill_button1", 0.3)
|
2019-12-19 23:45:27 +01:00
|
|
|
if (amount1 > 0) {
|
2021-01-21 20:21:25 +01:00
|
|
|
binding.fillPresetButton1.visibility = View.VISIBLE
|
|
|
|
binding.fillPresetButton1.text = DecimalFormatter.toPumpSupportedBolus(amount1, activePlugin.activePump) // + "U");
|
|
|
|
binding.fillPresetButton1.setOnClickListener { binding.fillInsulinamount.value = amount1 }
|
2019-12-19 23:45:27 +01:00
|
|
|
} else {
|
2021-01-21 20:21:25 +01:00
|
|
|
binding.fillPresetButton1.visibility = View.GONE
|
2019-12-19 23:45:27 +01:00
|
|
|
}
|
2019-12-27 19:20:38 +01:00
|
|
|
val amount2 = sp.getDouble("fill_button2", 0.0)
|
2019-12-19 23:45:27 +01:00
|
|
|
if (amount2 > 0) {
|
2021-01-21 20:21:25 +01:00
|
|
|
binding.fillPresetButton2.visibility = View.VISIBLE
|
|
|
|
binding.fillPresetButton2.text = DecimalFormatter.toPumpSupportedBolus(amount2, activePlugin.activePump) // + "U");
|
|
|
|
binding.fillPresetButton2.setOnClickListener { binding.fillInsulinamount.value = amount2 }
|
2019-12-19 23:45:27 +01:00
|
|
|
} else {
|
2021-01-21 20:21:25 +01:00
|
|
|
binding.fillPresetButton2.visibility = View.GONE
|
2019-12-19 23:45:27 +01:00
|
|
|
}
|
2019-12-27 19:20:38 +01:00
|
|
|
val amount3 = sp.getDouble("fill_button3", 0.0)
|
2019-12-19 23:45:27 +01:00
|
|
|
if (amount3 > 0) {
|
2021-01-21 20:21:25 +01:00
|
|
|
binding.fillPresetButton3.visibility = View.VISIBLE
|
|
|
|
binding.fillPresetButton3.text = DecimalFormatter.toPumpSupportedBolus(amount3, activePlugin.activePump) // + "U");
|
|
|
|
binding.fillPresetButton3.setOnClickListener { binding.fillInsulinamount.value = amount3 }
|
2019-12-19 23:45:27 +01:00
|
|
|
} else {
|
2021-01-21 20:21:25 +01:00
|
|
|
binding.fillPresetButton3.visibility = View.GONE
|
2019-12-19 23:45:27 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2021-01-21 20:21:25 +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:21:25 +01:00
|
|
|
if (_binding == null) return false
|
|
|
|
val insulin = SafeParse.stringToDouble(binding.fillInsulinamount.text ?: return false)
|
2019-12-19 23:45:27 +01:00
|
|
|
val actions: LinkedList<String?> = LinkedList()
|
|
|
|
|
2019-12-27 19:20:38 +01:00
|
|
|
val insulinAfterConstraints = constraintChecker.applyBolusConstraints(Constraint(insulin)).value()
|
2019-12-19 23:45:27 +01:00
|
|
|
if (insulinAfterConstraints > 0) {
|
2021-11-04 10:56:12 +01:00
|
|
|
actions.add(rh.gs(R.string.fillwarning))
|
2019-12-19 23:45:27 +01:00
|
|
|
actions.add("")
|
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.colorInsulinButton))
|
2019-12-19 23:45:27 +01:00
|
|
|
if (abs(insulinAfterConstraints - insulin) > 0.01)
|
2021-11-04 10:56:12 +01:00
|
|
|
actions.add(rh.gs(R.string.bolusconstraintappliedwarn, insulin, insulinAfterConstraints).formatColor(rh, R.color.warning))
|
2019-12-19 23:45:27 +01:00
|
|
|
}
|
2021-01-21 20:21:25 +01:00
|
|
|
val siteChange = binding.fillCatheterChange.isChecked
|
2019-12-19 23:45:27 +01:00
|
|
|
if (siteChange)
|
2021-11-04 10:56:12 +01:00
|
|
|
actions.add(rh.gs(R.string.record_pump_site_change).formatColor(rh, R.color.actionsConfirm))
|
2021-01-22 20:08:33 +01:00
|
|
|
val insulinChange = binding.fillCartridgeChange.isChecked
|
2019-12-19 23:45:27 +01:00
|
|
|
if (insulinChange)
|
2021-11-04 10:56:12 +01:00
|
|
|
actions.add(rh.gs(R.string.record_insulin_cartridge_change).formatColor(rh, R.color.actionsConfirm))
|
2021-03-25 02:24:26 +01:00
|
|
|
val notes: String = binding.notesLayout.notes.text.toString()
|
2019-12-19 23:45:27 +01:00
|
|
|
if (notes.isNotEmpty())
|
2021-11-04 10:56:12 +01:00
|
|
|
actions.add(rh.gs(R.string.notes_label) + ": " + notes)
|
2020-01-02 18:25:49 +01:00
|
|
|
eventTime -= eventTime % 1000
|
|
|
|
|
2019-12-19 23:45:27 +01:00
|
|
|
if (eventTimeChanged)
|
2021-11-04 10:56:12 +01:00
|
|
|
actions.add(rh.gs(R.string.time) + ": " + dateUtil.dateAndTimeString(eventTime))
|
2019-12-19 23:45:27 +01:00
|
|
|
|
2021-01-22 20:08:33 +01:00
|
|
|
if (insulinAfterConstraints > 0 || binding.fillCatheterChange.isChecked || binding.fillCartridgeChange.isChecked) {
|
2019-12-19 23:45:27 +01:00
|
|
|
activity?.let { activity ->
|
2021-11-04 10:56:12 +01:00
|
|
|
OKDialog.showConfirmation(activity, rh.gs(R.string.primefill), HtmlHelper.fromHtml(Joiner.on("<br/>").join(actions)), {
|
2019-12-19 23:45:27 +01:00
|
|
|
if (insulinAfterConstraints > 0) {
|
2021-04-01 22:59:05 +02:00
|
|
|
uel.log(Action.PRIME_BOLUS, Sources.FillDialog,
|
|
|
|
notes,
|
2021-04-03 21:29:26 +02:00
|
|
|
ValueWithUnit.Insulin(insulinAfterConstraints).takeIf { insulinAfterConstraints != 0.0 })
|
2020-01-16 20:39:25 +01:00
|
|
|
requestPrimeBolus(insulinAfterConstraints, notes)
|
2019-12-19 23:45:27 +01:00
|
|
|
}
|
2020-01-02 18:25:49 +01:00
|
|
|
if (siteChange) {
|
2021-04-04 15:05:43 +02:00
|
|
|
uel.log(Action.SITE_CHANGE, Sources.FillDialog,
|
2021-04-01 22:59:05 +02:00
|
|
|
notes,
|
2021-04-04 15:05:43 +02:00
|
|
|
ValueWithUnit.Timestamp(eventTime).takeIf { eventTimeChanged },
|
2021-04-03 21:29:26 +02:00
|
|
|
ValueWithUnit.TherapyEventType(TherapyEvent.Type.CANNULA_CHANGE))
|
2021-03-29 00:31:40 +02:00
|
|
|
disposable += repository.runTransactionForResult(InsertIfNewByTimestampTherapyEventTransaction(
|
2021-03-08 20:10:02 +01:00
|
|
|
timestamp = eventTime,
|
|
|
|
type = TherapyEvent.Type.CANNULA_CHANGE,
|
2021-03-09 22:11:30 +01:00
|
|
|
note = notes,
|
|
|
|
glucoseUnit = TherapyEvent.GlucoseUnit.MGDL
|
2021-03-28 13:45:27 +02:00
|
|
|
)).subscribe(
|
|
|
|
{ result -> result.inserted.forEach { aapsLogger.debug(LTag.DATABASE, "Inserted therapy event $it") } },
|
|
|
|
{ aapsLogger.error(LTag.DATABASE, "Error while saving therapy event", it) }
|
|
|
|
)
|
2020-01-02 18:25:49 +01:00
|
|
|
}
|
|
|
|
if (insulinChange) {
|
|
|
|
// add a second for case of both checked
|
2021-04-04 15:05:43 +02:00
|
|
|
uel.log(Action.RESERVOIR_CHANGE, Sources.FillDialog,
|
2021-04-01 22:59:05 +02:00
|
|
|
notes,
|
2021-04-04 15:05:43 +02:00
|
|
|
ValueWithUnit.Timestamp(eventTime).takeIf { eventTimeChanged },
|
2021-04-03 21:29:26 +02:00
|
|
|
ValueWithUnit.TherapyEventType(TherapyEvent.Type.INSULIN_CHANGE))
|
2021-03-29 00:31:40 +02:00
|
|
|
disposable += repository.runTransactionForResult(InsertIfNewByTimestampTherapyEventTransaction(
|
2021-03-08 20:10:02 +01:00
|
|
|
timestamp = eventTime + 1000,
|
|
|
|
type = TherapyEvent.Type.INSULIN_CHANGE,
|
2021-03-09 22:11:30 +01:00
|
|
|
note = notes,
|
|
|
|
glucoseUnit = TherapyEvent.GlucoseUnit.MGDL
|
2021-03-28 13:45:27 +02:00
|
|
|
)).subscribe(
|
|
|
|
{ result -> result.inserted.forEach { aapsLogger.debug(LTag.DATABASE, "Inserted therapy event $it") } },
|
|
|
|
{ aapsLogger.error(LTag.DATABASE, "Error while saving therapy event", it) }
|
|
|
|
)
|
2020-01-02 18:25:49 +01:00
|
|
|
}
|
2019-12-22 21:37:26 +01:00
|
|
|
}, null)
|
2019-12-19 23:45:27 +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.primefill), rh.gs(R.string.no_action_selected))
|
2019-12-22 21:37:26 +01:00
|
|
|
}
|
2019-12-19 23:45:27 +01:00
|
|
|
}
|
|
|
|
dismiss()
|
2019-12-21 23:17:20 +01:00
|
|
|
return true
|
2019-12-19 23:45:27 +01:00
|
|
|
}
|
2020-01-02 18:25:49 +01:00
|
|
|
|
2020-01-16 20:39:25 +01:00
|
|
|
private fun requestPrimeBolus(insulin: Double, notes: String) {
|
|
|
|
val detailedBolusInfo = DetailedBolusInfo()
|
|
|
|
detailedBolusInfo.insulin = insulin
|
|
|
|
detailedBolusInfo.context = context
|
2021-03-25 17:48:07 +01:00
|
|
|
detailedBolusInfo.bolusType = DetailedBolusInfo.BolusType.PRIMING
|
2020-01-16 20:39:25 +01:00
|
|
|
detailedBolusInfo.notes = notes
|
2020-01-26 21:15:57 +01:00
|
|
|
commandQueue.bolus(detailedBolusInfo, object : Callback() {
|
2020-01-16 20:39:25 +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)
|
2020-01-16 20:39:25 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
2020-01-16 15:46:53 +01:00
|
|
|
}
|