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
|
|
|
|
import info.nightscout.androidaps.database.entities.TherapyEvent
|
|
|
|
import info.nightscout.androidaps.database.transactions.InsertTherapyEventIfNewTransaction
|
2021-02-28 15:54:37 +01:00
|
|
|
import info.nightscout.androidaps.database.entities.UserEntry.*
|
2021-01-21 20:21:25 +01:00
|
|
|
import info.nightscout.androidaps.databinding.DialogFillBinding
|
2019-12-19 23:45:27 +01:00
|
|
|
import info.nightscout.androidaps.db.Source
|
2020-01-10 23:14:58 +01:00
|
|
|
import info.nightscout.androidaps.interfaces.ActivePluginProvider
|
|
|
|
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.plugins.general.nsclient.NSUpload
|
|
|
|
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
|
2020-08-19 21:24:01 +02:00
|
|
|
import info.nightscout.androidaps.utils.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
|
|
|
|
@Inject lateinit var resourceHelper: ResourceHelper
|
2020-01-10 23:14:58 +01:00
|
|
|
@Inject lateinit var ctx: Context
|
2020-05-09 10:52:20 +02:00
|
|
|
@Inject lateinit var nsUpload: NSUpload
|
2020-01-10 23:14:58 +01:00
|
|
|
@Inject lateinit var commandQueue: CommandQueueProvider
|
|
|
|
@Inject lateinit var activePlugin: ActivePluginProvider
|
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) {
|
2019-12-27 19:20:38 +01:00
|
|
|
actions.add(resourceHelper.gs(R.string.fillwarning))
|
2019-12-19 23:45:27 +01:00
|
|
|
actions.add("")
|
2020-08-19 21:24:01 +02:00
|
|
|
actions.add(resourceHelper.gs(R.string.bolus) + ": " + DecimalFormatter.toPumpSupportedBolus(insulinAfterConstraints, activePlugin.activePump, resourceHelper).formatColor(resourceHelper, R.color.colorInsulinButton))
|
2019-12-19 23:45:27 +01:00
|
|
|
if (abs(insulinAfterConstraints - insulin) > 0.01)
|
2020-08-19 21:24:01 +02:00
|
|
|
actions.add(resourceHelper.gs(R.string.bolusconstraintappliedwarn, insulin, insulinAfterConstraints).formatColor(resourceHelper, 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)
|
2020-08-19 21:24:01 +02:00
|
|
|
actions.add(resourceHelper.gs(R.string.record_pump_site_change).formatColor(resourceHelper, 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)
|
2020-08-19 21:24:01 +02:00
|
|
|
actions.add(resourceHelper.gs(R.string.record_insulin_cartridge_change).formatColor(resourceHelper, R.color.actionsConfirm))
|
2021-01-21 20:21:25 +01:00
|
|
|
val notes = binding.notesLayout.notes.text.toString()
|
2019-12-19 23:45:27 +01:00
|
|
|
if (notes.isNotEmpty())
|
2021-02-22 18:04:30 +01:00
|
|
|
actions.add(resourceHelper.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)
|
2020-05-07 23:40:59 +02:00
|
|
|
actions.add(resourceHelper.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-01-21 20:21:25 +01:00
|
|
|
OKDialog.showConfirmation(activity, resourceHelper.gs(R.string.primefill), HtmlHelper.fromHtml(Joiner.on("<br/>").join(actions)), {
|
2019-12-19 23:45:27 +01:00
|
|
|
if (insulinAfterConstraints > 0) {
|
2021-03-13 12:15:21 +01:00
|
|
|
uel.log(Action.PRIME_BOLUS, notes, ValueWithUnit(insulinAfterConstraints, Units.U, 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-03-02 22:43:18 +01:00
|
|
|
uel.log(Action.SITE_CHANGE, notes)
|
2021-03-08 20:10:02 +01:00
|
|
|
disposable += repository.runTransactionForResult(InsertTherapyEventIfNewTransaction(
|
|
|
|
timestamp = eventTime,
|
|
|
|
type = TherapyEvent.Type.CANNULA_CHANGE,
|
2021-03-09 22:11:30 +01:00
|
|
|
note = notes,
|
|
|
|
glucoseUnit = TherapyEvent.GlucoseUnit.MGDL
|
2021-03-08 20:10:02 +01:00
|
|
|
)).subscribe({ result ->
|
|
|
|
result.inserted.forEach { nsUpload.uploadEvent(it) }
|
|
|
|
}, {
|
|
|
|
aapsLogger.error(LTag.BGSOURCE, "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-03-02 22:43:18 +01:00
|
|
|
uel.log(Action.INSULIN_CHANGE, notes)
|
2021-03-08 20:10:02 +01:00
|
|
|
disposable += repository.runTransactionForResult(InsertTherapyEventIfNewTransaction(
|
|
|
|
timestamp = eventTime + 1000,
|
|
|
|
type = TherapyEvent.Type.INSULIN_CHANGE,
|
2021-03-09 22:11:30 +01:00
|
|
|
note = notes,
|
|
|
|
glucoseUnit = TherapyEvent.GlucoseUnit.MGDL
|
2021-03-08 20:10:02 +01:00
|
|
|
)).subscribe({ result ->
|
|
|
|
result.inserted.forEach { nsUpload.uploadEvent(it) }
|
|
|
|
}, {
|
|
|
|
aapsLogger.error(LTag.BGSOURCE, "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 ->
|
2019-12-27 19:20:38 +01:00
|
|
|
OKDialog.show(activity, resourceHelper.gs(R.string.primefill), resourceHelper.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
|
|
|
|
detailedBolusInfo.source = Source.USER
|
|
|
|
detailedBolusInfo.isValid = false // do not count it in IOB (for pump history)
|
|
|
|
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-02-14 15:09:06 +01:00
|
|
|
ErrorHelperActivity.runAlarm(ctx, result.comment, resourceHelper.gs(R.string.treatmentdeliveryerror), info.nightscout.androidaps.dana.R.raw.boluserror)
|
2020-01-16 20:39:25 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
2020-01-16 15:46:53 +01:00
|
|
|
}
|