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

253 lines
14 KiB
Kotlin
Raw Normal View History

2019-12-20 18:55:54 +01:00
package info.nightscout.androidaps.dialogs
2019-12-20 13:58:51 +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
import info.nightscout.androidaps.Constants
import info.nightscout.androidaps.MainApp
import info.nightscout.androidaps.R
import info.nightscout.androidaps.data.Profile
import info.nightscout.androidaps.db.CareportalEvent
import info.nightscout.androidaps.db.Source
import info.nightscout.androidaps.db.TempTarget
import info.nightscout.androidaps.interfaces.Constraint
2020-05-07 09:54:36 +02:00
import info.nightscout.androidaps.interfaces.ProfileFunction
2020-08-19 21:24:01 +02:00
import info.nightscout.androidaps.plugins.configBuilder.ConstraintChecker
2019-12-20 13:58:51 +01:00
import info.nightscout.androidaps.plugins.general.nsclient.NSUpload
2020-01-10 23:14:58 +01:00
import info.nightscout.androidaps.plugins.iob.iobCobCalculator.IobCobCalculatorPlugin
2019-12-20 13:58:51 +01:00
import info.nightscout.androidaps.plugins.treatments.CarbsGenerator
import info.nightscout.androidaps.plugins.treatments.TreatmentsPlugin
2020-08-19 21:24:01 +02:00
import info.nightscout.androidaps.utils.DateUtil
import info.nightscout.androidaps.utils.DecimalFormatter
import info.nightscout.androidaps.utils.DefaultValueHelper
import info.nightscout.androidaps.utils.HtmlHelper
import info.nightscout.androidaps.utils.ToastUtils
2020-04-08 14:03:57 +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
2019-12-20 23:05:35 +01:00
import kotlinx.android.synthetic.main.dialog_carbs.*
2019-12-20 13:58:51 +01:00
import kotlinx.android.synthetic.main.notes.*
import kotlinx.android.synthetic.main.okcancel.*
import java.text.DecimalFormat
import java.util.*
2019-12-27 19:20:38 +01:00
import javax.inject.Inject
2019-12-20 13:58:51 +01:00
import kotlin.math.max
class CarbsDialog : DialogFragmentWithDate() {
2019-12-30 00:53:44 +01:00
@Inject lateinit var mainApp: MainApp
@Inject lateinit var resourceHelper: ResourceHelper
@Inject lateinit var constraintChecker: ConstraintChecker
2020-01-01 23:23:16 +01:00
@Inject lateinit var defaultValueHelper: DefaultValueHelper
2019-12-30 00:53:44 +01:00
@Inject lateinit var treatmentsPlugin: TreatmentsPlugin
2019-12-31 00:37:36 +01:00
@Inject lateinit var profileFunction: ProfileFunction
2020-02-28 18:20:31 +01:00
@Inject lateinit var iobCobCalculatorPlugin: IobCobCalculatorPlugin
2020-05-09 10:52:20 +02:00
@Inject lateinit var nsUpload: NSUpload
2020-03-16 21:40:29 +01:00
@Inject lateinit var carbsGenerator: CarbsGenerator
2019-12-27 19:20:38 +01:00
2019-12-20 13:58:51 +01:00
companion object {
private const val FAV1_DEFAULT = 5
private const val FAV2_DEFAULT = 10
private const val FAV3_DEFAULT = 20
}
private val textWatcher: TextWatcher = object : TextWatcher {
override fun afterTextChanged(s: Editable) {
validateInputs()
}
override fun beforeTextChanged(s: CharSequence, start: Int, count: Int, after: Int) {}
override fun onTextChanged(s: CharSequence, start: Int, before: Int, count: Int) {}
}
private fun validateInputs() {
2019-12-27 19:20:38 +01:00
val maxCarbs = constraintChecker.getMaxCarbsAllowed().value().toDouble()
2019-12-20 13:58:51 +01:00
val time = overview_carbs_time.value.toInt()
if (time > 12 * 60 || time < -12 * 60) {
overview_carbs_time.value = 0.0
2019-12-27 19:20:38 +01:00
ToastUtils.showToastInUiThread(mainApp, resourceHelper.gs(R.string.constraintapllied))
2019-12-20 13:58:51 +01:00
}
2019-12-20 14:52:10 +01:00
if (overview_carbs_duration.value > 10) {
2019-12-20 13:58:51 +01:00
overview_carbs_duration.value = 0.0
2019-12-27 19:20:38 +01:00
ToastUtils.showToastInUiThread(mainApp, resourceHelper.gs(R.string.constraintapllied))
2019-12-20 13:58:51 +01:00
}
2019-12-20 14:52:10 +01:00
if (overview_carbs_carbs.value.toInt() > maxCarbs) {
2019-12-20 13:58:51 +01:00
overview_carbs_carbs.value = 0.0
2019-12-27 19:20:38 +01:00
ToastUtils.showToastInUiThread(mainApp, resourceHelper.gs(R.string.carbsconstraintapplied))
2019-12-20 13:58:51 +01:00
}
}
override fun onSaveInstanceState(savedInstanceState: Bundle) {
super.onSaveInstanceState(savedInstanceState)
savedInstanceState.putDouble("overview_carbs_time", overview_carbs_time.value)
savedInstanceState.putDouble("overview_carbs_duration", overview_carbs_duration.value)
savedInstanceState.putDouble("overview_carbs_carbs", overview_carbs_carbs.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_carbs, container, false)
2019-12-20 13:58:51 +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()
2019-12-20 13:58:51 +01:00
overview_carbs_time.setParams(savedInstanceState?.getDouble("overview_carbs_time")
2019-12-20 23:05:35 +01:00
?: 0.0, -12 * 60.0, 12 * 60.0, 5.0, DecimalFormat("0"), false, ok, textWatcher)
2019-12-20 13:58:51 +01:00
overview_carbs_duration.setParams(savedInstanceState?.getDouble("overview_carbs_duration")
2019-12-20 23:05:35 +01:00
?: 0.0, 0.0, 10.0, 1.0, DecimalFormat("0"), false, ok, textWatcher)
2019-12-20 13:58:51 +01:00
overview_carbs_carbs.setParams(savedInstanceState?.getDouble("overview_carbs_carbs")
2019-12-20 23:05:35 +01:00
?: 0.0, 0.0, maxCarbs, 1.0, DecimalFormat("0"), false, ok, textWatcher)
2019-12-20 13:58:51 +01:00
2019-12-30 00:53:44 +01:00
overview_carbs_plus1.text = toSignedString(sp.getInt(R.string.key_carbs_button_increment_1, FAV1_DEFAULT))
2019-12-20 13:58:51 +01:00
overview_carbs_plus1.setOnClickListener {
overview_carbs_carbs.value = max(0.0, overview_carbs_carbs.value
2019-12-30 00:53:44 +01:00
+ sp.getInt(R.string.key_carbs_button_increment_1, FAV1_DEFAULT))
2019-12-20 13:58:51 +01:00
validateInputs()
}
2019-12-30 00:53:44 +01:00
overview_carbs_plus2.text = toSignedString(sp.getInt(R.string.key_carbs_button_increment_2, FAV2_DEFAULT))
2019-12-20 13:58:51 +01:00
overview_carbs_plus2.setOnClickListener {
overview_carbs_carbs.value = max(0.0, overview_carbs_carbs.value
2019-12-30 00:53:44 +01:00
+ sp.getInt(R.string.key_carbs_button_increment_2, FAV2_DEFAULT))
2019-12-20 13:58:51 +01:00
validateInputs()
}
2019-12-30 00:53:44 +01:00
overview_carbs_plus3.text = toSignedString(sp.getInt(R.string.key_carbs_button_increment_3, FAV3_DEFAULT))
2019-12-20 13:58:51 +01:00
overview_carbs_plus3.setOnClickListener {
overview_carbs_carbs.value = max(0.0, overview_carbs_carbs.value
2019-12-30 00:53:44 +01:00
+ sp.getInt(R.string.key_carbs_button_increment_3, FAV3_DEFAULT))
2019-12-20 13:58:51 +01:00
validateInputs()
}
2020-01-10 23:14:58 +01:00
iobCobCalculatorPlugin.actualBg()?.let { bgReading ->
2019-12-20 13:58:51 +01:00
if (bgReading.value < 72)
2019-12-30 00:53:44 +01:00
overview_carbs_hypo_tt.isChecked = true
2019-12-20 13:58:51 +01:00
}
overview_carbs_hypo_tt.setOnClickListener {
overview_carbs_activity_tt.isChecked = false
overview_carbs_eating_soon_tt.isChecked = false
}
overview_carbs_activity_tt.setOnClickListener {
overview_carbs_hypo_tt.isChecked = false
overview_carbs_eating_soon_tt.isChecked = false
}
overview_carbs_eating_soon_tt.setOnClickListener {
overview_carbs_hypo_tt.isChecked = false
overview_carbs_activity_tt.isChecked = false
}
}
private fun toSignedString(value: Int): String {
return if (value > 0) "+$value" else value.toString()
}
2019-12-21 23:17:20 +01:00
override fun submit(): Boolean {
2019-12-20 13:58:51 +01:00
val carbs = overview_carbs_carbs.value.toInt()
2019-12-27 19:20:38 +01:00
val carbsAfterConstraints = constraintChecker.applyCarbsConstraints(Constraint(carbs)).value()
2019-12-31 00:37:36 +01:00
val units = profileFunction.getUnits()
2020-01-01 23:23:16 +01:00
val activityTTDuration = defaultValueHelper.determineActivityTTDuration()
val activityTT = defaultValueHelper.determineActivityTT()
val eatingSoonTTDuration = defaultValueHelper.determineEatingSoonTTDuration()
val eatingSoonTT = defaultValueHelper.determineEatingSoonTT()
val hypoTTDuration = defaultValueHelper.determineHypoTTDuration()
val hypoTT = defaultValueHelper.determineHypoTT()
2019-12-20 13:58:51 +01:00
val actions: LinkedList<String?> = LinkedList()
2019-12-27 19:20:38 +01:00
val unitLabel = if (units == Constants.MMOL) resourceHelper.gs(R.string.mmol) else resourceHelper.gs(R.string.mgdl)
2019-12-20 13:58:51 +01:00
val activitySelected = overview_carbs_activity_tt.isChecked
if (activitySelected)
2020-08-19 21:24:01 +02:00
actions.add(resourceHelper.gs(R.string.temptargetshort) + ": " + (DecimalFormatter.to1Decimal(activityTT) + " " + unitLabel + " (" + resourceHelper.gs(R.string.format_mins, activityTTDuration) + ")").formatColor(resourceHelper, R.color.tempTargetConfirmation))
2019-12-20 13:58:51 +01:00
val eatingSoonSelected = overview_carbs_eating_soon_tt.isChecked
if (eatingSoonSelected)
2020-08-19 21:24:01 +02:00
actions.add(resourceHelper.gs(R.string.temptargetshort) + ": " + (DecimalFormatter.to1Decimal(eatingSoonTT) + " " + unitLabel + " (" + resourceHelper.gs(R.string.format_mins, eatingSoonTTDuration) + ")").formatColor(resourceHelper, R.color.tempTargetConfirmation))
2019-12-20 13:58:51 +01:00
val hypoSelected = overview_carbs_hypo_tt.isChecked
if (hypoSelected)
2020-08-19 21:24:01 +02:00
actions.add(resourceHelper.gs(R.string.temptargetshort) + ": " + (DecimalFormatter.to1Decimal(hypoTT) + " " + unitLabel + " (" + resourceHelper.gs(R.string.format_mins, hypoTTDuration) + ")").formatColor(resourceHelper, R.color.tempTargetConfirmation))
2019-12-20 13:58:51 +01:00
val timeOffset = overview_carbs_time.value.toInt()
eventTime -= eventTime % 1000
2020-01-02 00:40:03 +01:00
val time = eventTime + timeOffset * 1000 * 60
2019-12-20 13:58:51 +01:00
if (timeOffset != 0)
2020-05-07 23:40:59 +02:00
actions.add(resourceHelper.gs(R.string.time) + ": " + dateUtil.dateAndTimeString(time))
2019-12-20 13:58:51 +01:00
val duration = overview_carbs_duration.value.toInt()
if (duration > 0)
2019-12-27 19:20:38 +01:00
actions.add(resourceHelper.gs(R.string.duration) + ": " + duration + resourceHelper.gs(R.string.shorthour))
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
}
2019-12-20 13:58:51 +01:00
val notes = notes.text.toString()
if (notes.isNotEmpty())
2019-12-27 19:20:38 +01:00
actions.add(resourceHelper.gs(R.string.careportal_newnstreatment_notes_label) + ": " + notes)
2019-12-20 13:58:51 +01:00
2020-01-02 00:40:03 +01:00
if (eventTimeChanged)
2020-05-07 23:40:59 +02:00
actions.add(resourceHelper.gs(R.string.time) + ": " + dateUtil.dateAndTimeString(eventTime))
2020-01-02 00:40:03 +01:00
2019-12-20 13:58:51 +01:00
if (carbsAfterConstraints > 0 || activitySelected || eatingSoonSelected || hypoSelected) {
activity?.let { activity ->
2019-12-27 19:20:38 +01:00
OKDialog.showConfirmation(activity, resourceHelper.gs(R.string.carbs), HtmlHelper.fromHtml(Joiner.on("<br/>").join(actions)), Runnable {
2019-12-30 00:53:44 +01:00
when {
activitySelected -> {
2020-02-28 18:20:31 +01:00
aapsLogger.debug("USER ENTRY: TEMPTARGET ACTIVITY $activityTT duration: $activityTTDuration")
2019-12-30 00:53:44 +01:00
val tempTarget = TempTarget()
.date(System.currentTimeMillis())
.duration(activityTTDuration)
.reason(resourceHelper.gs(R.string.activity))
.source(Source.USER)
2019-12-31 00:37:36 +01:00
.low(Profile.toMgdl(activityTT, profileFunction.getUnits()))
.high(Profile.toMgdl(activityTT, profileFunction.getUnits()))
2019-12-30 00:53:44 +01:00
treatmentsPlugin.addToHistoryTempTarget(tempTarget)
}
eatingSoonSelected -> {
2020-02-28 18:20:31 +01:00
aapsLogger.debug("USER ENTRY: TEMPTARGET EATING SOON $eatingSoonTT duration: $eatingSoonTTDuration")
2019-12-30 00:53:44 +01:00
val tempTarget = TempTarget()
.date(System.currentTimeMillis())
.duration(eatingSoonTTDuration)
.reason(resourceHelper.gs(R.string.eatingsoon))
.source(Source.USER)
2019-12-31 00:37:36 +01:00
.low(Profile.toMgdl(eatingSoonTT, profileFunction.getUnits()))
.high(Profile.toMgdl(eatingSoonTT, profileFunction.getUnits()))
2019-12-30 00:53:44 +01:00
treatmentsPlugin.addToHistoryTempTarget(tempTarget)
}
hypoSelected -> {
2020-02-28 18:20:31 +01:00
aapsLogger.debug("USER ENTRY: TEMPTARGET HYPO $hypoTT duration: $hypoTTDuration")
2019-12-30 00:53:44 +01:00
val tempTarget = TempTarget()
.date(System.currentTimeMillis())
.duration(hypoTTDuration)
.reason(resourceHelper.gs(R.string.hypo))
.source(Source.USER)
2019-12-31 00:37:36 +01:00
.low(Profile.toMgdl(hypoTT, profileFunction.getUnits()))
.high(Profile.toMgdl(hypoTT, profileFunction.getUnits()))
2019-12-30 00:53:44 +01:00
treatmentsPlugin.addToHistoryTempTarget(tempTarget)
}
2019-12-20 13:58:51 +01:00
}
if (carbsAfterConstraints > 0) {
if (duration == 0) {
2020-02-28 18:20:31 +01:00
aapsLogger.debug("USER ENTRY: CARBS $carbsAfterConstraints time: $time")
2020-03-16 21:40:29 +01:00
carbsGenerator.createCarb(carbsAfterConstraints, time, CareportalEvent.CARBCORRECTION, notes)
2019-12-20 13:58:51 +01:00
} else {
2020-02-28 18:20:31 +01:00
aapsLogger.debug("USER ENTRY: CARBS $carbsAfterConstraints time: $time duration: $duration")
2020-03-16 21:40:29 +01:00
carbsGenerator.generateCarbs(carbsAfterConstraints, time, duration, notes)
2020-05-09 10:52:20 +02:00
nsUpload.uploadEvent(CareportalEvent.NOTE, DateUtil.now() - 2000, resourceHelper.gs(R.string.generated_ecarbs_note, carbsAfterConstraints, duration, timeOffset))
2019-12-20 13:58:51 +01:00
}
}
2019-12-22 21:37:26 +01:00
}, null)
2019-12-20 13:58:51 +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.carbs), 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 13:58:51 +01:00
}
}