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

175 lines
8.4 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.content.Intent
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-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.CareportalEvent
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-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
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
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
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-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())
2019-12-27 19:20:38 +01:00
actions.add(resourceHelper.gs(R.string.careportal_newnstreatment_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) {
2021-02-09 17:57:28 +01:00
uel.log("PRIME BOLUS", d1 = insulinAfterConstraints)
requestPrimeBolus(insulinAfterConstraints, notes)
2019-12-19 23:45:27 +01:00
}
if (siteChange) {
2021-02-09 17:57:28 +01:00
uel.log("SITE CHANGE")
2020-05-09 10:52:20 +02:00
nsUpload.generateCareportalEvent(CareportalEvent.SITECHANGE, eventTime, notes)
}
if (insulinChange) {
// add a second for case of both checked
2021-02-09 17:57:28 +01:00
uel.log("INSULIN CHANGE")
2020-05-09 10:52:20 +02:00
nsUpload.generateCareportalEvent(CareportalEvent.INSULINCHANGE, eventTime + 1000, notes)
}
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
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() {
override fun run() {
if (!result.success) {
2020-01-26 21:15:57 +01:00
val i = Intent(ctx, ErrorHelperActivity::class.java)
i.putExtra("soundid", R.raw.boluserror)
i.putExtra("status", result.comment)
2020-01-26 21:15:57 +01:00
i.putExtra("title", resourceHelper.gs(R.string.treatmentdeliveryerror))
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
2020-01-26 21:15:57 +01:00
ctx.startActivity(i)
}
}
})
}
}