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

201 lines
9.3 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.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.database.entities.UserEntry.*
2021-01-21 20:41:29 +01:00
import info.nightscout.androidaps.databinding.DialogTemptargetBinding
2019-12-18 23:22:03 +01:00
import info.nightscout.androidaps.db.Source
import info.nightscout.androidaps.db.TempTarget
import info.nightscout.androidaps.interfaces.ActivePluginProvider
2020-05-07 09:54:36 +02:00
import info.nightscout.androidaps.interfaces.ProfileFunction
2021-02-09 17:57:28 +01:00
import info.nightscout.androidaps.logging.UserEntryLogger
2021-01-21 20:41:29 +01:00
import info.nightscout.androidaps.plugins.configBuilder.ConstraintChecker
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-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() {
2021-01-21 20:41:29 +01:00
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
2021-02-09 17:57:28 +01:00
@Inject lateinit var uel: UserEntryLogger
2019-12-27 19:20:38 +01:00
2021-02-09 17:57:28 +01:00
private lateinit var reasonList: List<String>
2021-01-21 20:41:29 +01:00
private var _binding: DialogTemptargetBinding? = null
// This property is only valid between onCreateView and
// onDestroyView.
private val binding get() = _binding!!
2019-12-18 23:22:03 +01:00
override fun onSaveInstanceState(savedInstanceState: Bundle) {
super.onSaveInstanceState(savedInstanceState)
2021-01-21 20:41:29 +01:00
savedInstanceState.putDouble("duration", binding.duration.value)
savedInstanceState.putDouble("temptarget", binding.temptarget.value)
2019-12-18 23:22:03 +01:00
}
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,
2021-01-21 20:41:29 +01:00
savedInstanceState: Bundle?): View {
2019-12-20 23:05:35 +01:00
onCreateViewGeneral()
2021-01-21 20:41:29 +01:00
_binding = DialogTemptargetBinding.inflate(inflater, container, false)
return binding.root
2019-12-18 23:22:03 +01:00
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
2021-01-21 20:41:29 +01:00
binding.duration.setParams(savedInstanceState?.getDouble("duration")
?: 0.0, 0.0, Constants.MAX_PROFILE_SWITCH_DURATION, 10.0, DecimalFormat("0"), false, binding.okcancel.ok)
2019-12-18 23:22:03 +01:00
2019-12-27 19:20:38 +01:00
if (profileFunction.getUnits() == Constants.MMOL)
2021-01-21 20:41:29 +01:00
binding.temptarget.setParams(
savedInstanceState?.getDouble("temptarget")
2021-01-19 10:59:46 +01:00
?: 8.0,
2021-01-21 20:41:29 +01:00
Constants.MIN_TT_MMOL, Constants.MAX_TT_MMOL, 0.1, DecimalFormat("0.0"), false, binding.okcancel.ok)
2019-12-18 23:22:03 +01:00
else
2021-01-21 20:41:29 +01:00
binding.temptarget.setParams(
savedInstanceState?.getDouble("temptarget")
2021-01-19 10:59:46 +01:00
?: 144.0,
2021-01-21 20:41:29 +01:00
Constants.MIN_TT_MGDL, Constants.MAX_TT_MGDL, 1.0, DecimalFormat("0"), false, binding.okcancel.ok)
2019-12-18 23:22:03 +01:00
2019-12-27 19:20:38 +01:00
val units = profileFunction.getUnits()
2021-01-21 20:41:29 +01:00
binding.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)
2021-01-21 20:41:29 +01:00
binding.targetCancel.visibility = View.VISIBLE
else
2021-01-21 20:41:29 +01:00
binding.targetCancel.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)
2021-01-21 20:41:29 +01:00
binding.reason.adapter = adapterReason
2021-01-21 20:41:29 +01:00
binding.targetCancel.setOnClickListener { shortClick(it) }
binding.eatingSoon.setOnClickListener { shortClick(it) }
binding.activity.setOnClickListener { shortClick(it) }
binding.hypo.setOnClickListener { shortClick(it) }
2021-01-21 20:41:29 +01:00
binding.eatingSoon.setOnLongClickListener {
longClick(it)
return@setOnLongClickListener true
}
2021-01-21 20:41:29 +01:00
binding.activity.setOnLongClickListener {
longClick(it)
return@setOnLongClickListener true
}
2021-01-21 20:41:29 +01:00
binding.hypo.setOnLongClickListener {
longClick(it)
return@setOnLongClickListener true
}
}
}
2021-01-21 20:41:29 +01:00
private fun shortClick(v: View) {
v.performLongClick()
if (submit()) dismiss()
}
2021-01-21 20:41:29 +01:00
private fun longClick(v: View) {
when (v.id) {
2021-01-21 20:41:29 +01:00
R.id.eating_soon -> {
binding.temptarget.value = defaultValueHelper.determineEatingSoonTT()
binding.duration.value = defaultValueHelper.determineEatingSoonTTDuration().toDouble()
binding.reason.setSelection(reasonList.indexOf(resourceHelper.gs(R.string.eatingsoon)))
}
2021-01-21 20:41:29 +01:00
R.id.activity -> {
binding.temptarget.value = defaultValueHelper.determineActivityTT()
binding.duration.value = defaultValueHelper.determineActivityTTDuration().toDouble()
binding.reason.setSelection(reasonList.indexOf(resourceHelper.gs(R.string.activity)))
}
2021-01-21 20:41:29 +01:00
R.id.hypo -> {
binding.temptarget.value = defaultValueHelper.determineHypoTT()
binding.duration.value = defaultValueHelper.determineHypoTTDuration().toDouble()
binding.reason.setSelection(reasonList.indexOf(resourceHelper.gs(R.string.hypo)))
}
R.id.cancel -> {
binding.duration.value = 0.0
}
}
2019-12-18 23:22:03 +01:00
}
2021-01-21 20:41:29 +01:00
override fun onDestroyView() {
super.onDestroyView()
_binding = null
}
2019-12-21 23:17:20 +01:00
override fun submit(): Boolean {
2021-01-21 20:41:29 +01:00
if (_binding == null) return false
2019-12-18 23:22:03 +01:00
val actions: LinkedList<String> = LinkedList()
var reason = binding.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
2021-01-21 20:41:29 +01:00
val target = binding.temptarget.value
val duration = binding.duration.value.toInt()
2020-02-25 20:46:04 +01:00
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))
reason = 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 ->
2021-01-21 20:41:29 +01:00
OKDialog.showConfirmation(activity, resourceHelper.gs(R.string.careportal_temporarytarget), HtmlHelper.fromHtml(Joiner.on("<br/>").join(actions)), {
val units = profileFunction.getUnits()
when(reason) {
resourceHelper.gs(R.string.eatingsoon) -> uel.log(Action.TT_EATING_SOON, ValueWithUnit(target, units), ValueWithUnit(duration, Units.M))
resourceHelper.gs(R.string.activity) -> uel.log(Action.TT_ACTIVITY, ValueWithUnit(target, units), ValueWithUnit(duration, Units.M))
resourceHelper.gs(R.string.hypo) -> uel.log(Action.TT_HYPO, ValueWithUnit(target, units), ValueWithUnit(duration, Units.M))
resourceHelper.gs(R.string.manual) -> uel.log(Action.TT, ValueWithUnit(target, units), ValueWithUnit(duration, Units.M))
resourceHelper.gs(R.string.stoptemptarget) -> uel.log(Action.CANCEL_TT)
}
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
}
}