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

160 lines
7.9 KiB
Kotlin
Raw Normal View History

2019-12-20 18:55:54 +01:00
package info.nightscout.androidaps.dialogs
2019-12-18 23:22:03 +01:00
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.AdapterView
import android.widget.ArrayAdapter
import com.google.common.base.Joiner
import com.google.common.collect.Lists
import info.nightscout.androidaps.Constants
import info.nightscout.androidaps.R
import info.nightscout.androidaps.data.Profile
import info.nightscout.androidaps.db.Source
import info.nightscout.androidaps.db.TempTarget
2019-12-27 19:20:38 +01:00
import info.nightscout.androidaps.plugins.configBuilder.ConstraintChecker
import info.nightscout.androidaps.plugins.configBuilder.ProfileFunction
2019-12-18 23:22:03 +01:00
import info.nightscout.androidaps.plugins.treatments.TreatmentsPlugin
2019-12-20 23:05:35 +01:00
import info.nightscout.androidaps.utils.DateUtil
import info.nightscout.androidaps.utils.DefaultValueHelper
import info.nightscout.androidaps.utils.HtmlHelper
import info.nightscout.androidaps.utils.OKDialog
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_temptarget.*
2019-12-20 23:05:35 +01:00
import kotlinx.android.synthetic.main.okcancel.*
2019-12-18 23:22:03 +01:00
import java.text.DecimalFormat
import java.util.*
2019-12-27 19:20:38 +01:00
import javax.inject.Inject
2019-12-18 23:22:03 +01:00
class TempTargetDialog : DialogFragmentWithDate() {
2019-12-30 00:53:44 +01:00
@Inject lateinit var constraintChecker: ConstraintChecker
@Inject lateinit var resourceHelper: ResourceHelper
@Inject lateinit var profileFunction: ProfileFunction
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-27 19:20:38 +01:00
2019-12-18 23:22:03 +01:00
override fun onSaveInstanceState(savedInstanceState: Bundle) {
super.onSaveInstanceState(savedInstanceState)
savedInstanceState.putDouble("overview_temptarget_duration", overview_temptarget_duration.value)
savedInstanceState.putDouble("overview_temptarget_temptarget", overview_temptarget_temptarget.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_temptarget, container, false)
2019-12-18 23:22:03 +01:00
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
overview_temptarget_duration.setParams(savedInstanceState?.getDouble("overview_temptarget_duration")
2019-12-20 23:05:35 +01:00
?: 0.0, 0.0, Constants.MAX_PROFILE_SWITCH_DURATION, 10.0, DecimalFormat("0"), false, ok)
2019-12-18 23:22:03 +01:00
2019-12-27 19:20:38 +01:00
if (profileFunction.getUnits() == Constants.MMOL)
2019-12-18 23:22:03 +01:00
overview_temptarget_temptarget.setParams(
2019-12-20 23:05:35 +01:00
savedInstanceState?.getDouble("overview_temptarget_temptarget")
?: Constants.MIN_TT_MMOL,
Constants.MIN_TT_MMOL, Constants.MAX_TT_MMOL, 0.1, DecimalFormat("0.0"), false, ok)
2019-12-18 23:22:03 +01:00
else
overview_temptarget_temptarget.setParams(
2019-12-20 23:05:35 +01:00
savedInstanceState?.getDouble("overview_temptarget_temptarget")
?: Constants.MIN_TT_MGDL,
Constants.MIN_TT_MGDL, Constants.MAX_TT_MGDL, 1.0, DecimalFormat("0"), false, ok)
2019-12-18 23:22:03 +01:00
2019-12-27 19:20:38 +01:00
val units = profileFunction.getUnits()
overview_temptarget_units.text = if (units == Constants.MMOL) resourceHelper.gs(R.string.mmol) else resourceHelper.gs(R.string.mgdl)
2019-12-18 23:22:03 +01:00
// temp target
context?.let { context ->
val reasonList: List<String> = Lists.newArrayList(
2019-12-27 19:20:38 +01:00
resourceHelper.gs(R.string.manual),
resourceHelper.gs(R.string.cancel),
resourceHelper.gs(R.string.eatingsoon),
resourceHelper.gs(R.string.activity),
resourceHelper.gs(R.string.hypo)
2019-12-18 23:22:03 +01:00
)
val adapterReason = ArrayAdapter(context, R.layout.spinner_centered, reasonList)
overview_temptarget_reason.adapter = adapterReason
overview_temptarget_reason.onItemSelectedListener = object : AdapterView.OnItemSelectedListener {
override fun onItemSelected(parent: AdapterView<*>?, view: View, position: Int, id: Long) {
val defaultDuration: Double
val defaultTarget: Double
when (reasonList[position]) {
2019-12-27 19:20:38 +01:00
resourceHelper.gs(R.string.eatingsoon) -> {
2020-01-01 23:23:16 +01:00
defaultDuration = defaultValueHelper.determineEatingSoonTTDuration().toDouble()
defaultTarget = defaultValueHelper.determineEatingSoonTT()
2019-12-18 23:22:03 +01:00
}
2019-12-20 23:05:35 +01:00
2019-12-27 19:20:38 +01:00
resourceHelper.gs(R.string.activity) -> {
2020-01-01 23:23:16 +01:00
defaultDuration = defaultValueHelper.determineActivityTTDuration().toDouble()
defaultTarget = defaultValueHelper.determineActivityTT()
2019-12-18 23:22:03 +01:00
}
2019-12-20 23:05:35 +01:00
2019-12-27 19:20:38 +01:00
resourceHelper.gs(R.string.hypo) -> {
2020-01-01 23:23:16 +01:00
defaultDuration = defaultValueHelper.determineHypoTTDuration().toDouble()
defaultTarget = defaultValueHelper.determineHypoTT()
2019-12-18 23:22:03 +01:00
}
2019-12-20 23:05:35 +01:00
2019-12-27 19:20:38 +01:00
resourceHelper.gs(R.string.cancel) -> {
2019-12-18 23:22:03 +01:00
defaultDuration = 0.0
defaultTarget = 0.0
}
2019-12-20 23:05:35 +01:00
2019-12-27 19:20:38 +01:00
else -> {
2019-12-18 23:22:03 +01:00
defaultDuration = overview_temptarget_duration.value
defaultTarget = overview_temptarget_temptarget.value
}
}
overview_temptarget_temptarget.value = defaultTarget
overview_temptarget_duration.value = defaultDuration
}
override fun onNothingSelected(parent: AdapterView<*>?) {}
}
}
}
2019-12-21 23:17:20 +01:00
override fun submit(): Boolean {
2019-12-18 23:22:03 +01:00
val actions: LinkedList<String> = LinkedList()
val reason = overview_temptarget_reason.selectedItem.toString()
2019-12-27 19:20:38 +01:00
val unitResId = if (profileFunction.getUnits() == Constants.MGDL) R.string.mgdl else R.string.mmol
2019-12-18 23:22:03 +01:00
val target = overview_temptarget_temptarget.value
2020-02-25 20:46:04 +01:00
val duration = overview_temptarget_duration.value.toInt()
if (target != 0.0 && duration != 0) {
2019-12-27 19:20:38 +01:00
actions.add(resourceHelper.gs(R.string.reason) + ": " + reason)
actions.add(resourceHelper.gs(R.string.nsprofileview_target_label) + ": " + Profile.toCurrentUnitsString(target) + " " + resourceHelper.gs(unitResId))
2020-02-28 18:20:31 +01:00
actions.add(resourceHelper.gs(R.string.duration) + ": " + resourceHelper.gs(R.string.format_mins, duration))
2019-12-18 23:22:03 +01:00
} else {
2019-12-27 19:20:38 +01:00
actions.add(resourceHelper.gs(R.string.stoptemptarget))
2019-12-18 23:22:03 +01:00
}
if (eventTimeChanged)
2019-12-27 19:20:38 +01:00
actions.add(resourceHelper.gs(R.string.time) + ": " + DateUtil.dateAndTimeString(eventTime))
2019-12-18 23:22:03 +01:00
activity?.let { activity ->
2019-12-27 19:20:38 +01:00
OKDialog.showConfirmation(activity, resourceHelper.gs(R.string.careportal_temporarytarget), HtmlHelper.fromHtml(Joiner.on("<br/>").join(actions)), Runnable {
2020-02-28 18:20:31 +01:00
aapsLogger.debug("USER ENTRY: TEMP TARGET $target duration: $duration")
2020-02-25 20:46:04 +01:00
if (target == 0.0 || duration == 0) {
2019-12-18 23:22:03 +01:00
val tempTarget = TempTarget()
2019-12-20 23:05:35 +01:00
.date(eventTime)
.duration(0)
.low(0.0).high(0.0)
.source(Source.USER)
2019-12-30 00:53:44 +01:00
treatmentsPlugin.addToHistoryTempTarget(tempTarget)
2019-12-18 23:22:03 +01:00
} else {
val tempTarget = TempTarget()
2019-12-20 23:05:35 +01:00
.date(eventTime)
2020-02-28 18:20:31 +01:00
.duration(duration)
2019-12-20 23:05:35 +01:00
.reason(reason)
.source(Source.USER)
2019-12-27 19:20:38 +01:00
.low(Profile.toMgdl(target, profileFunction.getUnits()))
.high(Profile.toMgdl(target, profileFunction.getUnits()))
2019-12-30 00:53:44 +01:00
treatmentsPlugin.addToHistoryTempTarget(tempTarget)
2019-12-18 23:22:03 +01:00
}
2020-02-28 18:20:31 +01:00
if (duration == 10) sp.putBoolean(R.string.key_objectiveusetemptarget, true)
2019-12-22 21:37:26 +01:00
})
2019-12-18 23:22:03 +01:00
}
2019-12-21 23:17:20 +01:00
return true
2019-12-18 23:22:03 +01:00
}
}