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

175 lines
8.4 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
import info.nightscout.androidaps.interfaces.ActivePluginProvider
2019-12-27 19:20:38 +01:00
import info.nightscout.androidaps.plugins.configBuilder.ConstraintChecker
2020-05-07 09:54:36 +02:00
import info.nightscout.androidaps.interfaces.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.DefaultValueHelper
import info.nightscout.androidaps.utils.HtmlHelper
2020-04-08 14:03:57 +02:00
import info.nightscout.androidaps.utils.alertDialogs.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
@Inject lateinit var activePlugin: ActivePluginProvider
2019-12-27 19:20:38 +01:00
lateinit var reasonList: List<String>
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")
2021-01-19 10:59:46 +01:00
?: 8.0,
2019-12-20 23:05:35 +01:00
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")
2021-01-19 10:59:46 +01:00
?: 144.0,
2019-12-20 23:05:35 +01:00
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-20 23:05:35 +01:00
// temp target
context?.let { context ->
if (activePlugin.activeTreatments.tempTargetFromHistory != null)
overview_temptarget_cancel?.visibility = View.VISIBLE
else
overview_temptarget_cancel?.visibility = View.GONE
reasonList = Lists.newArrayList(
resourceHelper.gs(R.string.manual),
resourceHelper.gs(R.string.eatingsoon),
resourceHelper.gs(R.string.activity),
resourceHelper.gs(R.string.hypo)
)
val adapterReason = ArrayAdapter(context, R.layout.spinner_centered, reasonList)
overview_temptarget_reason.adapter = adapterReason
overview_temptarget_cancel?.setOnClickListener { shortClick(it) }
overview_temptarget_eating_soon?.setOnClickListener { shortClick(it) }
overview_temptarget_activity?.setOnClickListener { shortClick(it) }
overview_temptarget_hypo?.setOnClickListener { shortClick(it) }
overview_temptarget_eating_soon?.setOnLongClickListener {
longClick(it)
return@setOnLongClickListener true
}
overview_temptarget_activity?.setOnLongClickListener {
longClick(it)
return@setOnLongClickListener true
}
overview_temptarget_hypo?.setOnLongClickListener {
longClick(it)
return@setOnLongClickListener true
}
}
}
private fun shortClick(v:View){
v.performLongClick()
if (submit()) dismiss()
}
private fun longClick(v:View) {
when (v.id) {
R.id.overview_temptarget_eating_soon -> {
overview_temptarget_temptarget.value = defaultValueHelper.determineEatingSoonTT()
overview_temptarget_duration.value = defaultValueHelper.determineEatingSoonTTDuration().toDouble()
overview_temptarget_reason.setSelection(reasonList.indexOf( resourceHelper.gs(R.string.eatingsoon)))
}
R.id.overview_temptarget_activity -> {
overview_temptarget_temptarget.value = defaultValueHelper.determineActivityTT()
overview_temptarget_duration.value = defaultValueHelper.determineActivityTTDuration().toDouble()
overview_temptarget_reason.setSelection(reasonList.indexOf(resourceHelper.gs(R.string.activity)))
}
R.id.overview_temptarget_hypo -> {
overview_temptarget_temptarget.value = defaultValueHelper.determineHypoTT()
overview_temptarget_duration.value = defaultValueHelper.determineHypoTTDuration().toDouble()
overview_temptarget_reason.setSelection(reasonList.indexOf(resourceHelper.gs(R.string.hypo)))
}
}
2019-12-18 23:22:03 +01:00
}
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() ?: return false
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)
2020-05-11 16:58:59 +02:00
actions.add(resourceHelper.gs(R.string.target_label) + ": " + Profile.toCurrentUnitsString(profileFunction, 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)
2020-05-07 23:40:59 +02: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
}
}