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

200 lines
10 KiB
Kotlin
Raw Normal View History

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
import info.nightscout.androidaps.database.entities.UserEntry.Sources
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
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
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
@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) {
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))
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-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-02-22 18:04:30 +01:00
actions.add(resourceHelper.gs(R.string.notes_label) + ": " + notes)
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
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) {
uel.log(Action.PRIME_BOLUS, Sources.FillDialog,
notes,
2021-04-03 21:29:26 +02:00
ValueWithUnit.Insulin(insulinAfterConstraints).takeIf { insulinAfterConstraints != 0.0 })
requestPrimeBolus(insulinAfterConstraints, notes)
2019-12-19 23:45:27 +01:00
}
if (siteChange) {
uel.log(Action.SITE_CHANGE, Sources.FillDialog,
notes,
ValueWithUnit.Timestamp(eventTime).takeIf { eventTimeChanged },
2021-04-03 21:29:26 +02:00
ValueWithUnit.TherapyEventType(TherapyEvent.Type.CANNULA_CHANGE))
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
)).subscribe(
{ result -> result.inserted.forEach { aapsLogger.debug(LTag.DATABASE, "Inserted therapy event $it") } },
{ aapsLogger.error(LTag.DATABASE, "Error while saving therapy event", it) }
)
}
if (insulinChange) {
// add a second for case of both checked
uel.log(Action.RESERVOIR_CHANGE, Sources.FillDialog,
notes,
ValueWithUnit.Timestamp(eventTime).takeIf { eventTimeChanged },
2021-04-03 21:29:26 +02:00
ValueWithUnit.TherapyEventType(TherapyEvent.Type.INSULIN_CHANGE))
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
)).subscribe(
{ result -> result.inserted.forEach { aapsLogger.debug(LTag.DATABASE, "Inserted therapy event $it") } },
{ aapsLogger.error(LTag.DATABASE, "Error while saving therapy event", it) }
)
}
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
}
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
detailedBolusInfo.notes = notes
2020-01-26 21:15:57 +01:00
commandQueue.bolus(detailedBolusInfo, object : Callback() {
override fun run() {
if (!result.success) {
2021-06-23 21:01:42 +02:00
ErrorHelperActivity.runAlarm(ctx, result.comment, resourceHelper.gs(R.string.treatmentdeliveryerror), R.raw.boluserror)
}
}
})
}
}