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
|
2021-03-01 12:42:42 +01:00
|
|
|
import info.nightscout.androidaps.database.AppRepository
|
|
|
|
import info.nightscout.androidaps.database.ValueWrapper
|
|
|
|
import info.nightscout.androidaps.database.entities.TemporaryTarget
|
2021-02-28 15:54:37 +01:00
|
|
|
import info.nightscout.androidaps.database.entities.UserEntry.*
|
2021-03-01 12:42:42 +01:00
|
|
|
import info.nightscout.androidaps.database.transactions.CancelCurrentTemporaryTargetIfAnyTransaction
|
|
|
|
import info.nightscout.androidaps.database.transactions.InsertTemporaryTargetAndCancelCurrentTransaction
|
2021-01-21 20:41:29 +01:00
|
|
|
import info.nightscout.androidaps.databinding.DialogTemptargetBinding
|
2020-05-07 09:54:36 +02:00
|
|
|
import info.nightscout.androidaps.interfaces.ProfileFunction
|
2021-03-01 12:42:42 +01:00
|
|
|
import info.nightscout.androidaps.logging.LTag
|
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
|
2021-03-01 12:42:42 +01:00
|
|
|
import info.nightscout.androidaps.plugins.general.nsclient.NSUpload
|
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
|
2021-03-01 12:42:42 +01:00
|
|
|
import io.reactivex.disposables.CompositeDisposable
|
|
|
|
import io.reactivex.rxkotlin.plusAssign
|
2019-12-18 23:22:03 +01:00
|
|
|
import java.text.DecimalFormat
|
|
|
|
import java.util.*
|
2021-03-01 12:42:42 +01:00
|
|
|
import java.util.concurrent.TimeUnit
|
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
|
2021-02-09 17:57:28 +01:00
|
|
|
@Inject lateinit var uel: UserEntryLogger
|
2021-03-01 12:42:42 +01:00
|
|
|
@Inject lateinit var repository: AppRepository
|
|
|
|
@Inject lateinit var nsUpload: NSUpload
|
2019-12-27 19:20:38 +01:00
|
|
|
|
2021-02-09 17:57:28 +01:00
|
|
|
private lateinit var reasonList: List<String>
|
2020-12-21 21:03:30 +01:00
|
|
|
|
2021-03-01 12:42:42 +01:00
|
|
|
private val disposable = CompositeDisposable()
|
|
|
|
|
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)
|
2021-03-01 12:42:42 +01:00
|
|
|
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(
|
2021-03-01 12:42:42 +01:00
|
|
|
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(
|
2021-03-01 12:42:42 +01:00
|
|
|
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
|
|
|
|
2020-12-23 19:28:50 +01:00
|
|
|
// temp target
|
|
|
|
context?.let { context ->
|
2021-03-01 12:42:42 +01:00
|
|
|
if (repository.getTemporaryTargetActiveAt(dateUtil._now()).blockingGet() is ValueWrapper.Existing)
|
2021-01-21 20:41:29 +01:00
|
|
|
binding.targetCancel.visibility = View.VISIBLE
|
2020-12-23 19:28:50 +01:00
|
|
|
else
|
2021-01-21 20:41:29 +01:00
|
|
|
binding.targetCancel.visibility = View.GONE
|
2020-12-23 19:28:50 +01:00
|
|
|
|
|
|
|
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
|
2020-12-23 19:28:50 +01:00
|
|
|
|
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) }
|
2020-12-22 11:15:31 +01:00
|
|
|
|
2021-01-21 20:41:29 +01:00
|
|
|
binding.eatingSoon.setOnLongClickListener {
|
2020-12-23 19:28:50 +01:00
|
|
|
longClick(it)
|
|
|
|
return@setOnLongClickListener true
|
|
|
|
}
|
2021-01-21 20:41:29 +01:00
|
|
|
binding.activity.setOnLongClickListener {
|
2020-12-23 19:28:50 +01:00
|
|
|
longClick(it)
|
|
|
|
return@setOnLongClickListener true
|
|
|
|
}
|
2021-01-21 20:41:29 +01:00
|
|
|
binding.hypo.setOnLongClickListener {
|
2020-12-23 19:28:50 +01:00
|
|
|
longClick(it)
|
|
|
|
return@setOnLongClickListener true
|
|
|
|
}
|
2020-12-22 11:15:31 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-21 20:41:29 +01:00
|
|
|
private fun shortClick(v: View) {
|
2020-12-22 11:15:31 +01:00
|
|
|
v.performLongClick()
|
|
|
|
if (submit()) dismiss()
|
|
|
|
}
|
|
|
|
|
2021-01-21 20:41:29 +01:00
|
|
|
private fun longClick(v: View) {
|
2020-12-22 11:15:31 +01:00
|
|
|
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)))
|
2020-12-22 11:15:31 +01:00
|
|
|
}
|
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)))
|
2020-12-22 11:15:31 +01:00
|
|
|
}
|
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)))
|
2020-12-22 11:15:31 +01:00
|
|
|
}
|
2021-03-03 22:49:31 +01:00
|
|
|
|
2021-02-23 10:16:11 +01:00
|
|
|
R.id.cancel -> {
|
|
|
|
binding.duration.value = 0.0
|
|
|
|
}
|
2020-12-20 19:54:54 +01:00
|
|
|
}
|
2019-12-18 23:22:03 +01:00
|
|
|
}
|
|
|
|
|
2021-01-21 20:41:29 +01:00
|
|
|
override fun onDestroyView() {
|
|
|
|
super.onDestroyView()
|
2021-03-01 12:42:42 +01:00
|
|
|
disposable.clear()
|
2021-01-21 20:41:29 +01:00
|
|
|
_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()
|
2021-02-22 18:43:37 +01:00
|
|
|
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))
|
2021-02-22 18:43:37 +01:00
|
|
|
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)), {
|
2021-02-28 15:54:37 +01:00
|
|
|
val units = profileFunction.getUnits()
|
2021-03-13 12:15:21 +01:00
|
|
|
when(reason) {
|
2021-03-17 22:45:08 +01:00
|
|
|
resourceHelper.gs(R.string.eatingsoon) -> uel.log(Action.TT, ValueWithUnit(eventTime, Units.Timestamp, eventTimeChanged), ValueWithUnit(TemporaryTarget.Reason.EATING_SOON.text, Units.TT_Reason), ValueWithUnit(target, units), ValueWithUnit(duration, Units.M))
|
|
|
|
resourceHelper.gs(R.string.activity) -> uel.log(Action.TT, ValueWithUnit(eventTime, Units.Timestamp, eventTimeChanged), ValueWithUnit(TemporaryTarget.Reason.ACTIVITY.text, Units.TT_Reason), ValueWithUnit(target, units), ValueWithUnit(duration, Units.M))
|
|
|
|
resourceHelper.gs(R.string.hypo) -> uel.log(Action.TT, ValueWithUnit(eventTime, Units.Timestamp, eventTimeChanged), ValueWithUnit(TemporaryTarget.Reason.HYPOGLYCEMIA.text, Units.TT_Reason), ValueWithUnit(target, units), ValueWithUnit(duration, Units.M))
|
2021-03-13 12:15:21 +01:00
|
|
|
resourceHelper.gs(R.string.manual) -> uel.log(Action.TT, ValueWithUnit(eventTime, Units.Timestamp, eventTimeChanged), ValueWithUnit(target, units), ValueWithUnit(duration, Units.M))
|
|
|
|
resourceHelper.gs(R.string.stoptemptarget) -> uel.log(Action.CANCEL_TT, ValueWithUnit(eventTime, Units.Timestamp, eventTimeChanged))
|
|
|
|
}
|
2020-02-25 20:46:04 +01:00
|
|
|
if (target == 0.0 || duration == 0) {
|
2021-03-01 12:42:42 +01:00
|
|
|
disposable += repository.runTransactionForResult(CancelCurrentTemporaryTargetIfAnyTransaction(eventTime))
|
|
|
|
.subscribe({ result ->
|
|
|
|
result.updated.forEach { nsUpload.updateTempTarget(it) }
|
|
|
|
}, {
|
|
|
|
aapsLogger.error(LTag.BGSOURCE, "Error while saving temporary target", it)
|
|
|
|
})
|
2019-12-18 23:22:03 +01:00
|
|
|
} else {
|
2021-03-01 12:42:42 +01:00
|
|
|
disposable += repository.runTransactionForResult(InsertTemporaryTargetAndCancelCurrentTransaction(
|
|
|
|
timestamp = eventTime,
|
|
|
|
duration = TimeUnit.MINUTES.toMillis(duration.toLong()),
|
|
|
|
reason = when (reason) {
|
2021-03-13 17:31:58 +01:00
|
|
|
resourceHelper.gs(R.string.eatingsoon) -> TemporaryTarget.Reason.EATING_SOON
|
|
|
|
resourceHelper.gs(R.string.activity) -> TemporaryTarget.Reason.ACTIVITY
|
|
|
|
resourceHelper.gs(R.string.hypo) -> TemporaryTarget.Reason.HYPOGLYCEMIA
|
|
|
|
else -> TemporaryTarget.Reason.CUSTOM
|
2021-03-01 12:42:42 +01:00
|
|
|
},
|
|
|
|
lowTarget = Profile.toMgdl(target, profileFunction.getUnits()),
|
|
|
|
highTarget = Profile.toMgdl(target, profileFunction.getUnits())
|
|
|
|
)).subscribe({ result ->
|
|
|
|
result.inserted.forEach { nsUpload.uploadTempTarget(it) }
|
|
|
|
result.updated.forEach { nsUpload.updateTempTarget(it) }
|
|
|
|
}, {
|
|
|
|
aapsLogger.error(LTag.BGSOURCE, "Error while saving temporary target", it)
|
|
|
|
})
|
2019-12-18 23:22:03 +01:00
|
|
|
}
|
2021-03-17 22:45:08 +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
|
|
|
}
|
|
|
|
}
|