2019-12-20 18:55:54 +01:00
|
|
|
package info.nightscout.androidaps.dialogs
|
2019-12-20 15:36:27 +01:00
|
|
|
|
2020-01-12 23:43:44 +01:00
|
|
|
import android.content.Context
|
2019-12-20 15:36:27 +01:00
|
|
|
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
|
2020-07-24 13:00:11 +02:00
|
|
|
import info.nightscout.androidaps.Config
|
2019-12-20 15:36:27 +01:00
|
|
|
import info.nightscout.androidaps.R
|
2019-12-20 23:05:35 +01:00
|
|
|
import info.nightscout.androidaps.activities.ErrorHelperActivity
|
2019-12-20 15:36:27 +01:00
|
|
|
import info.nightscout.androidaps.data.DetailedBolusInfo
|
2021-03-25 17:48:07 +01:00
|
|
|
import info.nightscout.androidaps.database.AppRepository
|
2021-03-22 16:59:09 +01:00
|
|
|
import info.nightscout.androidaps.database.entities.UserEntry.Action
|
|
|
|
import info.nightscout.androidaps.database.entities.UserEntry.Units
|
|
|
|
import info.nightscout.androidaps.database.entities.UserEntry.ValueWithUnit
|
2021-01-21 20:46:21 +01:00
|
|
|
import info.nightscout.androidaps.databinding.DialogTreatmentBinding
|
2020-01-10 23:14:58 +01:00
|
|
|
import info.nightscout.androidaps.interfaces.ActivePluginProvider
|
|
|
|
import info.nightscout.androidaps.interfaces.CommandQueueProvider
|
2019-12-20 15:36:27 +01:00
|
|
|
import info.nightscout.androidaps.interfaces.Constraint
|
2021-03-25 17:48:07 +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
|
2021-03-25 17:48:07 +01:00
|
|
|
import info.nightscout.androidaps.plugins.general.nsclient.NSUpload
|
2019-12-20 15:36:27 +01:00
|
|
|
import info.nightscout.androidaps.queue.Callback
|
2019-12-20 23:05:35 +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.ToastUtils
|
2021-01-21 20:46:21 +01: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-25 17:48:07 +01:00
|
|
|
import io.reactivex.disposables.CompositeDisposable
|
|
|
|
import io.reactivex.rxkotlin.plusAssign
|
2019-12-20 15:36:27 +01:00
|
|
|
import java.text.DecimalFormat
|
|
|
|
import java.util.*
|
2019-12-27 19:20:38 +01:00
|
|
|
import javax.inject.Inject
|
2019-12-20 15:36:27 +01:00
|
|
|
import kotlin.math.abs
|
|
|
|
|
|
|
|
class TreatmentDialog : DialogFragmentWithDate() {
|
2021-01-21 20:46:21 +01:00
|
|
|
|
2019-12-31 00:37:36 +01:00
|
|
|
@Inject lateinit var constraintChecker: ConstraintChecker
|
|
|
|
@Inject lateinit var resourceHelper: ResourceHelper
|
2020-01-10 23:14:58 +01:00
|
|
|
@Inject lateinit var activePlugin: ActivePluginProvider
|
|
|
|
@Inject lateinit var commandQueue: CommandQueueProvider
|
2020-01-12 23:43:44 +01:00
|
|
|
@Inject lateinit var ctx: Context
|
2020-07-24 13:00:11 +02:00
|
|
|
@Inject lateinit var config: Config
|
2021-02-09 17:57:28 +01:00
|
|
|
@Inject lateinit var uel: UserEntryLogger
|
2021-03-25 17:48:07 +01:00
|
|
|
@Inject lateinit var nsUpload: NSUpload
|
|
|
|
@Inject lateinit var repository: AppRepository
|
|
|
|
|
|
|
|
private val disposable = CompositeDisposable()
|
2019-12-20 15:36:27 +01:00
|
|
|
|
|
|
|
private val textWatcher: TextWatcher = object : TextWatcher {
|
|
|
|
override fun afterTextChanged(s: Editable) {}
|
|
|
|
override fun beforeTextChanged(s: CharSequence, start: Int, count: Int, after: Int) {}
|
|
|
|
override fun onTextChanged(s: CharSequence, start: Int, before: Int, count: Int) {
|
|
|
|
validateInputs()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private fun validateInputs() {
|
2019-12-27 19:20:38 +01:00
|
|
|
val maxCarbs = constraintChecker.getMaxCarbsAllowed().value().toDouble()
|
|
|
|
val maxInsulin = constraintChecker.getMaxBolusAllowed().value()
|
2021-01-21 20:46:21 +01:00
|
|
|
if (SafeParse.stringToInt(binding.carbs.text) > maxCarbs) {
|
|
|
|
binding.carbs.value = 0.0
|
2020-01-10 23:14:58 +01:00
|
|
|
ToastUtils.showToastInUiThread(context, resourceHelper.gs(R.string.carbsconstraintapplied))
|
2019-12-20 15:36:27 +01:00
|
|
|
}
|
2021-01-21 20:46:21 +01:00
|
|
|
if (SafeParse.stringToDouble(binding.insulin.text) > maxInsulin) {
|
|
|
|
binding.insulin.value = 0.0
|
2020-01-10 23:14:58 +01:00
|
|
|
ToastUtils.showToastInUiThread(context, resourceHelper.gs(R.string.bolusconstraintapplied))
|
2019-12-20 15:36:27 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-21 20:46:21 +01:00
|
|
|
private var _binding: DialogTreatmentBinding? = null
|
|
|
|
|
|
|
|
// This property is only valid between onCreateView and
|
|
|
|
// onDestroyView.
|
|
|
|
private val binding get() = _binding!!
|
|
|
|
|
2019-12-20 15:36:27 +01:00
|
|
|
override fun onSaveInstanceState(savedInstanceState: Bundle) {
|
|
|
|
super.onSaveInstanceState(savedInstanceState)
|
2021-01-21 20:46:21 +01:00
|
|
|
savedInstanceState.putDouble("carbs", binding.carbs.value)
|
|
|
|
savedInstanceState.putDouble("insulin", binding.insulin.value)
|
2019-12-20 15:36:27 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,
|
2021-01-21 20:46:21 +01:00
|
|
|
savedInstanceState: Bundle?): View {
|
2019-12-20 23:05:35 +01:00
|
|
|
onCreateViewGeneral()
|
2021-01-21 20:46:21 +01:00
|
|
|
_binding = DialogTreatmentBinding.inflate(inflater, container, false)
|
|
|
|
return binding.root
|
2019-12-20 15:36:27 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
|
|
|
|
super.onViewCreated(view, savedInstanceState)
|
|
|
|
|
2020-07-24 13:00:11 +02:00
|
|
|
if (config.NSCLIENT) {
|
2021-01-21 20:46:21 +01:00
|
|
|
binding.recordOnly.isChecked = true
|
|
|
|
binding.recordOnly.isEnabled = false
|
2020-07-24 13:00:11 +02:00
|
|
|
}
|
2019-12-27 19:20:38 +01:00
|
|
|
val maxCarbs = constraintChecker.getMaxCarbsAllowed().value().toDouble()
|
|
|
|
val maxInsulin = constraintChecker.getMaxBolusAllowed().value()
|
2020-03-16 21:40:29 +01:00
|
|
|
val pumpDescription = activePlugin.activePump.pumpDescription
|
2021-01-21 20:46:21 +01:00
|
|
|
binding.carbs.setParams(savedInstanceState?.getDouble("carbs")
|
|
|
|
?: 0.0, 0.0, maxCarbs, 1.0, DecimalFormat("0"), false, binding.okcancel.ok, textWatcher)
|
|
|
|
binding.insulin.setParams(savedInstanceState?.getDouble("insulin")
|
|
|
|
?: 0.0, 0.0, maxInsulin, pumpDescription.bolusStep, DecimalFormatter.pumpSupportedBolusFormat(activePlugin.activePump), false, binding.okcancel.ok, textWatcher)
|
|
|
|
}
|
|
|
|
|
|
|
|
override fun onDestroyView() {
|
|
|
|
super.onDestroyView()
|
|
|
|
_binding = null
|
2019-12-20 15:36:27 +01:00
|
|
|
}
|
|
|
|
|
2019-12-21 23:17:20 +01:00
|
|
|
override fun submit(): Boolean {
|
2021-01-21 20:46:21 +01:00
|
|
|
if (_binding == null) return false
|
2020-03-16 21:40:29 +01:00
|
|
|
val pumpDescription = activePlugin.activePump.pumpDescription
|
2021-01-21 20:46:21 +01:00
|
|
|
val insulin = SafeParse.stringToDouble(binding.insulin.text ?: return false)
|
|
|
|
val carbs = SafeParse.stringToInt(binding.carbs.text)
|
|
|
|
val recordOnlyChecked = binding.recordOnly.isChecked
|
2019-12-20 15:36:27 +01:00
|
|
|
val actions: LinkedList<String?> = LinkedList()
|
2019-12-27 19:20:38 +01:00
|
|
|
val insulinAfterConstraints = constraintChecker.applyBolusConstraints(Constraint(insulin)).value()
|
|
|
|
val carbsAfterConstraints = constraintChecker.applyCarbsConstraints(Constraint(carbs)).value()
|
2019-12-20 15:36:27 +01:00
|
|
|
|
|
|
|
if (insulinAfterConstraints > 0) {
|
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.bolus))
|
2019-12-20 15:36:27 +01:00
|
|
|
if (recordOnlyChecked)
|
2020-08-19 21:24:01 +02:00
|
|
|
actions.add(resourceHelper.gs(R.string.bolusrecordedonly).formatColor(resourceHelper, R.color.warning))
|
2019-12-20 15:36:27 +01:00
|
|
|
if (abs(insulinAfterConstraints - insulin) > pumpDescription.pumpType.determineCorrectBolusStepSize(insulinAfterConstraints))
|
2020-08-19 21:24:01 +02:00
|
|
|
actions.add(resourceHelper.gs(R.string.bolusconstraintappliedwarn, insulin, insulinAfterConstraints).formatColor(resourceHelper, R.color.warning))
|
2019-12-20 15:36:27 +01:00
|
|
|
}
|
|
|
|
if (carbsAfterConstraints > 0) {
|
2020-08-19 21:24:01 +02:00
|
|
|
actions.add(resourceHelper.gs(R.string.carbs) + ": " + resourceHelper.gs(R.string.format_carbs, carbsAfterConstraints).formatColor(resourceHelper, R.color.carbs))
|
2019-12-20 15:36:27 +01:00
|
|
|
if (carbsAfterConstraints != carbs)
|
2020-08-19 21:24:01 +02:00
|
|
|
actions.add(resourceHelper.gs(R.string.carbsconstraintapplied).formatColor(resourceHelper, R.color.warning))
|
2019-12-20 15:36:27 +01:00
|
|
|
}
|
|
|
|
if (insulinAfterConstraints > 0 || carbsAfterConstraints > 0) {
|
|
|
|
activity?.let { activity ->
|
2021-01-21 20:46:21 +01:00
|
|
|
OKDialog.showConfirmation(activity, resourceHelper.gs(R.string.overview_treatment_label), HtmlHelper.fromHtml(Joiner.on("<br/>").join(actions)), {
|
2019-12-20 15:36:27 +01:00
|
|
|
val detailedBolusInfo = DetailedBolusInfo()
|
2021-03-25 17:48:07 +01:00
|
|
|
if (insulinAfterConstraints == 0.0) detailedBolusInfo.eventType = DetailedBolusInfo.EventType.CARBS_CORRECTION
|
|
|
|
if (carbsAfterConstraints == 0) detailedBolusInfo.eventType = DetailedBolusInfo.EventType.CORRECTION_BOLUS
|
2019-12-20 15:36:27 +01:00
|
|
|
detailedBolusInfo.insulin = insulinAfterConstraints
|
|
|
|
detailedBolusInfo.carbs = carbsAfterConstraints.toDouble()
|
|
|
|
detailedBolusInfo.context = context
|
2021-03-25 23:52:08 +01:00
|
|
|
uel.log(Action.TREATMENT,
|
|
|
|
ValueWithUnit(detailedBolusInfo.timestamp, Units.Timestamp),
|
|
|
|
ValueWithUnit(insulin, Units.U, insulin != 0.0),
|
|
|
|
ValueWithUnit(carbs, Units.G, carbs != 0)
|
|
|
|
)
|
|
|
|
if (recordOnlyChecked) {
|
|
|
|
disposable += repository.runTransactionForResult(detailedBolusInfo.insertBolusTransaction())
|
2021-03-28 13:45:27 +02:00
|
|
|
.subscribe(
|
|
|
|
{ result -> result.inserted.forEach { aapsLogger.debug(LTag.DATABASE, "Inserted bolus $it") } },
|
|
|
|
{ aapsLogger.error(LTag.DATABASE, "Error while saving bolus", it) }
|
|
|
|
)
|
2021-03-25 23:52:08 +01:00
|
|
|
disposable += repository.runTransactionForResult(detailedBolusInfo.insertCarbsTransaction())
|
2021-03-28 13:45:27 +02:00
|
|
|
.subscribe(
|
|
|
|
{ result -> result.inserted.forEach { aapsLogger.debug(LTag.DATABASE, "Inserted carbs $it") } },
|
|
|
|
{ aapsLogger.error(LTag.DATABASE, "Error while saving carbs", it) }
|
|
|
|
)
|
2021-03-25 23:52:08 +01:00
|
|
|
} else {
|
2020-01-10 23:14:58 +01:00
|
|
|
commandQueue.bolus(detailedBolusInfo, object : Callback() {
|
2019-12-20 15:36:27 +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)
|
2021-03-25 23:52:08 +01:00
|
|
|
}
|
2019-12-20 15:36:27 +01:00
|
|
|
}
|
|
|
|
})
|
2021-03-25 17:48:07 +01:00
|
|
|
}
|
2019-12-22 21:37:26 +01:00
|
|
|
})
|
2019-12-20 15:36: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.overview_treatment_label), 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 15:36:27 +01:00
|
|
|
}
|
|
|
|
}
|