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

146 lines
8 KiB
Kotlin
Raw Normal View History

2019-12-20 18:55:54 +01:00
package info.nightscout.androidaps.dialogs
2019-12-20 15:36:27 +01:00
import android.content.Intent
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
import info.nightscout.androidaps.MainApp
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
import info.nightscout.androidaps.db.CareportalEvent
import info.nightscout.androidaps.db.Source
import info.nightscout.androidaps.interfaces.Constraint
import info.nightscout.androidaps.plugins.configBuilder.ConfigBuilderPlugin
2019-12-27 19:20:38 +01:00
import info.nightscout.androidaps.plugins.configBuilder.ConstraintChecker
2019-12-20 15:36:27 +01:00
import info.nightscout.androidaps.plugins.treatments.TreatmentsPlugin
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.OKDialog
import info.nightscout.androidaps.utils.SafeParse
import info.nightscout.androidaps.utils.ToastUtils
2019-12-27 19:20:38 +01:00
import info.nightscout.androidaps.utils.resources.ResourceHelper
2019-12-20 18:55:54 +01:00
import kotlinx.android.synthetic.main.dialog_treatment.*
2019-12-20 23:05:35 +01:00
import kotlinx.android.synthetic.main.okcancel.*
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() {
2019-12-27 19:20:38 +01:00
@Inject
lateinit var constraintChecker: ConstraintChecker
@Inject
lateinit var mainApp: MainApp
@Inject
lateinit var resourceHelper: ResourceHelper
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()
2019-12-20 15:36:27 +01:00
if (SafeParse.stringToInt(overview_treatment_carbs.text) > maxCarbs) {
overview_treatment_carbs.value = 0.0
2019-12-27 19:20:38 +01:00
ToastUtils.showToastInUiThread(mainApp, resourceHelper.gs(R.string.carbsconstraintapplied))
2019-12-20 15:36:27 +01:00
}
if (SafeParse.stringToDouble(overview_treatment_insulin.text) > maxInsulin) {
overview_treatment_insulin.value = 0.0
2019-12-27 19:20:38 +01:00
ToastUtils.showToastInUiThread(mainApp, resourceHelper.gs(R.string.bolusconstraintapplied))
2019-12-20 15:36:27 +01:00
}
}
override fun onSaveInstanceState(savedInstanceState: Bundle) {
super.onSaveInstanceState(savedInstanceState)
savedInstanceState.putDouble("overview_treatment_carbs", overview_treatment_carbs.value)
savedInstanceState.putDouble("overview_treatment_insulin", overview_treatment_insulin.value)
}
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?): View? {
2019-12-20 23:05:35 +01:00
onCreateViewGeneral()
2019-12-20 18:55:54 +01:00
return inflater.inflate(R.layout.dialog_treatment, container, false)
2019-12-20 15:36:27 +01:00
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
2019-12-27 19:20:38 +01:00
val maxCarbs = constraintChecker.getMaxCarbsAllowed().value().toDouble()
val maxInsulin = constraintChecker.getMaxBolusAllowed().value()
2019-12-20 15:36:27 +01:00
val pumpDescription = ConfigBuilderPlugin.getPlugin().activePump?.pumpDescription ?: return
overview_treatment_carbs.setParams(savedInstanceState?.getDouble("overview_treatment_carbs")
2019-12-20 23:05:35 +01:00
?: 0.0, 0.0, maxCarbs, 1.0, DecimalFormat("0"), false, ok, textWatcher)
2019-12-20 15:36:27 +01:00
overview_treatment_insulin.setParams(savedInstanceState?.getDouble("overview_treatment_insulin")
2019-12-20 23:05:35 +01:00
?: 0.0, 0.0, maxInsulin, pumpDescription.bolusStep, DecimalFormatter.pumpSupportedBolusFormat(), false, ok, textWatcher)
2019-12-20 15:36:27 +01:00
}
2019-12-21 23:17:20 +01:00
override fun submit(): Boolean {
val pumpDescription = ConfigBuilderPlugin.getPlugin().activePump?.pumpDescription
?: return false
2019-12-20 15:36:27 +01:00
val insulin = SafeParse.stringToDouble(overview_treatment_insulin.text)
val carbs = SafeParse.stringToInt(overview_treatment_carbs.text)
val recordOnlyChecked = overview_treatment_record_only.isChecked
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) {
2019-12-27 19:20:38 +01:00
actions.add(resourceHelper.gs(R.string.bolus) + ": " + "<font color='" + resourceHelper.gc(R.color.bolus) + "'>" + DecimalFormatter.toPumpSupportedBolus(insulinAfterConstraints) + resourceHelper.gs(R.string.insulin_unit_shortname) + "</font>")
2019-12-20 15:36:27 +01:00
if (recordOnlyChecked)
2019-12-27 19:20:38 +01:00
actions.add("<font color='" + resourceHelper.gc(R.color.warning) + "'>" + resourceHelper.gs(R.string.bolusrecordedonly) + "</font>")
2019-12-20 15:36:27 +01:00
if (abs(insulinAfterConstraints - insulin) > pumpDescription.pumpType.determineCorrectBolusStepSize(insulinAfterConstraints))
2019-12-27 19:20:38 +01:00
actions.add(resourceHelper.gs(R.string.bolusconstraintappliedwarning, resourceHelper.gc(R.color.warning), insulin, insulinAfterConstraints))
2019-12-20 15:36:27 +01:00
}
if (carbsAfterConstraints > 0) {
2019-12-27 19:20:38 +01:00
actions.add(resourceHelper.gs(R.string.carbs) + ": " + "<font color='" + resourceHelper.gc(R.color.carbs) + "'>" + resourceHelper.gs(R.string.format_carbs, carbsAfterConstraints) + "</font>")
2019-12-20 15:36:27 +01:00
if (carbsAfterConstraints != carbs)
2019-12-27 19:20:38 +01:00
actions.add("<font color='" + resourceHelper.gc(R.color.warning) + "'>" + resourceHelper.gs(R.string.carbsconstraintapplied) + "</font>")
2019-12-20 15:36:27 +01:00
}
if (insulinAfterConstraints > 0 || carbsAfterConstraints > 0) {
activity?.let { activity ->
2019-12-27 19:20:38 +01:00
OKDialog.showConfirmation(activity, resourceHelper.gs(R.string.overview_treatment_label), HtmlHelper.fromHtml(Joiner.on("<br/>").join(actions)), Runnable {
2019-12-20 15:36:27 +01:00
val detailedBolusInfo = DetailedBolusInfo()
if (insulinAfterConstraints == 0.0) detailedBolusInfo.eventType = CareportalEvent.CARBCORRECTION
if (carbsAfterConstraints == 0) detailedBolusInfo.eventType = CareportalEvent.CORRECTIONBOLUS
detailedBolusInfo.insulin = insulinAfterConstraints
detailedBolusInfo.carbs = carbsAfterConstraints.toDouble()
detailedBolusInfo.context = context
detailedBolusInfo.source = Source.USER
if (!(recordOnlyChecked && (detailedBolusInfo.insulin > 0 || pumpDescription.storesCarbInfo))) {
ConfigBuilderPlugin.getPlugin().commandQueue.bolus(detailedBolusInfo, object : Callback() {
override fun run() {
if (!result.success) {
2019-12-27 19:20:38 +01:00
val i = Intent(mainApp, ErrorHelperActivity::class.java)
2019-12-20 15:36:27 +01:00
i.putExtra("soundid", R.raw.boluserror)
i.putExtra("status", result.comment)
2019-12-27 19:20:38 +01:00
i.putExtra("title", resourceHelper.gs(R.string.treatmentdeliveryerror))
2019-12-20 15:36:27 +01:00
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
2019-12-27 19:20:38 +01:00
mainApp.startActivity(i)
2019-12-20 15:36:27 +01:00
}
}
})
} else
TreatmentsPlugin.getPlugin().addToHistoryTreatment(detailedBolusInfo, false)
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
}
}