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

234 lines
12 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.DatabaseHelper
import info.nightscout.androidaps.db.Source
import info.nightscout.androidaps.db.TempTarget
import info.nightscout.androidaps.interfaces.Constraint
2019-12-27 19:20:38 +01:00
import info.nightscout.androidaps.plugins.configBuilder.ConstraintChecker
2019-12-31 00:37:36 +01:00
import info.nightscout.androidaps.plugins.configBuilder.ProfileFunction
2019-12-20 13:58:51 +01:00
import info.nightscout.androidaps.plugins.general.nsclient.NSUpload
import info.nightscout.androidaps.plugins.treatments.CarbsGenerator
import info.nightscout.androidaps.plugins.treatments.TreatmentsPlugin
import info.nightscout.androidaps.utils.*
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
@Inject lateinit var treatmentsPlugin: TreatmentsPlugin
2019-12-31 00:37:36 +01:00
@Inject lateinit var profileFunction: ProfileFunction
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()
}
DatabaseHelper.actualBg()?.let { bgReading ->
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()
2019-12-20 13:58:51 +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()
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)
2019-12-27 19:20:38 +01:00
actions.add(resourceHelper.gs(R.string.temptargetshort) + ": " + "<font color='" + resourceHelper.gc(R.color.tempTargetConfirmation) + "'>" + DecimalFormatter.to1Decimal(activityTT) + " " + unitLabel + " (" + activityTTDuration + " " + resourceHelper.gs(R.string.unit_minute_short) + ")</font>")
2019-12-20 13:58:51 +01:00
val eatingSoonSelected = overview_carbs_eating_soon_tt.isChecked
if (eatingSoonSelected)
2019-12-27 19:20:38 +01:00
actions.add(resourceHelper.gs(R.string.temptargetshort) + ": " + "<font color='" + resourceHelper.gc(R.color.tempTargetConfirmation) + "'>" + DecimalFormatter.to1Decimal(eatingSoonTT) + " " + unitLabel + " (" + eatingSoonTTDuration + " " + resourceHelper.gs(R.string.unit_minute_short) + ")</font>")
2019-12-20 13:58:51 +01:00
val hypoSelected = overview_carbs_hypo_tt.isChecked
if (hypoSelected)
2019-12-27 19:20:38 +01:00
actions.add(resourceHelper.gs(R.string.temptargetshort) + ": " + "<font color='" + resourceHelper.gc(R.color.tempTargetConfirmation) + "'>" + DecimalFormatter.to1Decimal(hypoTT) + " " + unitLabel + " (" + hypoTTDuration + " " + resourceHelper.gs(R.string.unit_minute_short) + ")</font>")
2019-12-20 13:58:51 +01:00
val timeOffset = overview_carbs_time.value.toInt()
val time = DateUtil.now() + timeOffset * 1000 * 60
if (timeOffset != 0)
2019-12-27 19:20:38 +01: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
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 -> {
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 -> {
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 -> {
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) {
CarbsGenerator.createCarb(carbsAfterConstraints, time, CareportalEvent.CARBCORRECTION, notes)
} else {
CarbsGenerator.generateCarbs(carbsAfterConstraints, time, duration, notes)
2019-12-27 19:20:38 +01: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
}
}