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

125 lines
6.7 KiB
Kotlin
Raw Normal View History

2019-12-20 18:55:54 +01:00
package info.nightscout.androidaps.dialogs
2019-12-20 11:43:21 +01:00
2020-01-12 23:43:44 +01:00
import android.content.Context
2019-12-20 11:43:21 +01:00
import android.content.Intent
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import com.google.common.base.Joiner
import info.nightscout.androidaps.R
2019-12-20 23:05:35 +01:00
import info.nightscout.androidaps.activities.ErrorHelperActivity
2020-01-10 23:14:58 +01:00
import info.nightscout.androidaps.interfaces.ActivePluginProvider
import info.nightscout.androidaps.interfaces.CommandQueueProvider
2019-12-20 11:43:21 +01:00
import info.nightscout.androidaps.interfaces.Constraint
import info.nightscout.androidaps.interfaces.PumpDescription
2019-12-27 19:20:38 +01:00
import info.nightscout.androidaps.plugins.configBuilder.ConstraintChecker
import info.nightscout.androidaps.plugins.configBuilder.ProfileFunction
2019-12-20 11:43:21 +01:00
import info.nightscout.androidaps.queue.Callback
import info.nightscout.androidaps.utils.HtmlHelper
import info.nightscout.androidaps.utils.OKDialog
import info.nightscout.androidaps.utils.SafeParse
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_tempbasal.*
2019-12-20 11:43:21 +01:00
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 11:43:21 +01:00
import kotlin.math.abs
class TempBasalDialog : DialogFragmentWithDate() {
2019-12-31 00:37:36 +01:00
@Inject lateinit var constraintChecker: ConstraintChecker
@Inject lateinit var resourceHelper: ResourceHelper
@Inject lateinit var profileFunction: ProfileFunction
2020-01-10 23:14:58 +01:00
@Inject lateinit var activePlugin: ActivePluginProvider
@Inject lateinit var commandQueue: CommandQueueProvider
2020-01-12 23:43:44 +01:00
@Inject lateinit var ctx: Context
2019-12-27 19:20:38 +01:00
2019-12-20 11:43:21 +01:00
private var isPercentPump = true
override fun onSaveInstanceState(savedInstanceState: Bundle) {
super.onSaveInstanceState(savedInstanceState)
savedInstanceState.putDouble("actions_tempbasal_duration", actions_tempbasal_duration.value)
savedInstanceState.putDouble("actions_tempbasal_basalpercentinput", actions_tempbasal_basalpercentinput.value)
savedInstanceState.putDouble("actions_tempbasal_basalabsoluteinput", actions_tempbasal_basalabsoluteinput.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_tempbasal, container, false)
2019-12-20 11:43:21 +01:00
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
2020-01-10 23:14:58 +01:00
val pumpDescription = activePlugin.activePumpPlugin?.pumpDescription ?: return
2019-12-27 19:20:38 +01:00
val profile = profileFunction.getProfile() ?: return
2019-12-20 11:43:21 +01:00
val maxTempPercent = pumpDescription.maxTempPercent.toDouble()
val tempPercentStep = pumpDescription.tempPercentStep.toDouble()
actions_tempbasal_basalpercentinput.setParams(savedInstanceState?.getDouble("actions_tempbasal_basalpercentinput")
2019-12-20 23:05:35 +01:00
?: 100.0, 0.0, maxTempPercent, tempPercentStep, DecimalFormat("0"), true, ok)
2019-12-20 11:43:21 +01:00
actions_tempbasal_basalabsoluteinput.setParams(savedInstanceState?.getDouble("actions_tempbasal_basalabsoluteinput")
2019-12-20 23:05:35 +01:00
?: profile.basal, 0.0, pumpDescription.maxTempAbsolute, pumpDescription.tempAbsoluteStep, DecimalFormat("0.00"), true, ok)
2019-12-20 11:43:21 +01:00
val tempDurationStep = pumpDescription.tempDurationStep.toDouble()
val tempMaxDuration = pumpDescription.tempMaxDuration.toDouble()
actions_tempbasal_duration.setParams(savedInstanceState?.getDouble("actions_tempbasal_duration")
2019-12-20 23:05:35 +01:00
?: tempDurationStep, tempDurationStep, tempMaxDuration, tempDurationStep, DecimalFormat("0"), false, ok)
2019-12-20 11:43:21 +01:00
isPercentPump = pumpDescription.tempBasalStyle and PumpDescription.PERCENT == PumpDescription.PERCENT
if (isPercentPump) {
actions_tempbasal_percent_layout.visibility = View.VISIBLE
actions_tempbasal_absolute_layout.visibility = View.GONE
} else {
actions_tempbasal_percent_layout.visibility = View.GONE
actions_tempbasal_absolute_layout.visibility = View.VISIBLE
}
}
2019-12-21 23:17:20 +01:00
override fun submit(): Boolean {
2019-12-20 11:43:21 +01:00
var percent = 0
var absolute = 0.0
val durationInMinutes = SafeParse.stringToInt(actions_tempbasal_duration.text)
2019-12-27 19:20:38 +01:00
val profile = profileFunction.getProfile() ?: return false
2019-12-20 11:43:21 +01:00
val actions: LinkedList<String> = LinkedList()
if (isPercentPump) {
val basalPercentInput = SafeParse.stringToInt(actions_tempbasal_basalpercentinput.text)
2019-12-27 19:20:38 +01:00
percent = constraintChecker.applyBasalPercentConstraints(Constraint(basalPercentInput), profile).value()
actions.add(resourceHelper.gs(R.string.pump_tempbasal_label) + ": $percent%")
actions.add(resourceHelper.gs(R.string.duration) + ": " + resourceHelper.gs(R.string.format_mins, durationInMinutes))
if (percent != basalPercentInput) actions.add(resourceHelper.gs(R.string.constraintapllied))
2019-12-20 11:43:21 +01:00
} else {
val basalAbsoluteInput = SafeParse.stringToDouble(actions_tempbasal_basalabsoluteinput.text)
2019-12-27 19:20:38 +01:00
absolute = constraintChecker.applyBasalConstraints(Constraint(basalAbsoluteInput), profile).value()
actions.add(resourceHelper.gs(R.string.pump_tempbasal_label) + ": " + resourceHelper.gs(R.string.pump_basebasalrate, absolute))
actions.add(resourceHelper.gs(R.string.duration) + ": " + resourceHelper.gs(R.string.format_mins, durationInMinutes))
2019-12-20 13:58:51 +01:00
if (abs(absolute - basalAbsoluteInput) > 0.01)
2019-12-27 19:20:38 +01:00
actions.add("<font color='" + resourceHelper.gc(R.color.warning) + "'>" + resourceHelper.gs(R.string.constraintapllied) + "</font>")
2019-12-20 11:43:21 +01:00
}
activity?.let { activity ->
2019-12-31 00:37:36 +01:00
OKDialog.showConfirmation(activity, resourceHelper.gs(R.string.pump_tempbasal_label), HtmlHelper.fromHtml(Joiner.on("<br/>").join(actions)), Runnable {
2019-12-20 11:43:21 +01:00
val callback: Callback = object : Callback() {
override fun run() {
if (!result.success) {
2020-01-12 23:43:44 +01:00
val i = Intent(ctx, ErrorHelperActivity::class.java)
2019-12-20 11:43:21 +01:00
i.putExtra("soundid", R.raw.boluserror)
i.putExtra("status", result.comment)
2019-12-27 19:20:38 +01:00
i.putExtra("title", resourceHelper.gs(R.string.tempbasaldeliveryerror))
2019-12-20 11:43:21 +01:00
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
2020-01-12 23:43:44 +01:00
ctx.startActivity(i)
2019-12-20 11:43:21 +01:00
}
}
}
2020-01-10 23:14:58 +01:00
if (isPercentPump)
commandQueue.tempBasalPercent(percent, durationInMinutes, true, profile, callback)
else
commandQueue.tempBasalAbsolute(absolute, durationInMinutes, true, profile, callback)
2019-12-22 21:37:26 +01:00
})
2019-12-20 11:43:21 +01:00
}
2019-12-21 23:17:20 +01:00
return true
2019-12-20 11:43:21 +01:00
}
}