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

95 lines
4.1 KiB
Kotlin
Raw Normal View History

2019-12-20 18:55:54 +01:00
package info.nightscout.androidaps.dialogs
2019-12-20 16:33:01 +01:00
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import com.google.common.base.Joiner
2020-03-10 18:58:27 +01:00
import dagger.android.HasAndroidInjector
2019-12-20 16:33:01 +01:00
import info.nightscout.androidaps.R
import info.nightscout.androidaps.database.entities.UserEntry.Action
import info.nightscout.androidaps.database.entities.UserEntry.Sources
2021-11-18 15:47:22 +01:00
import info.nightscout.androidaps.database.entities.ValueWithUnit
2021-01-21 16:45:54 +01:00
import info.nightscout.androidaps.databinding.DialogCalibrationBinding
2021-04-19 18:44:17 +02:00
import info.nightscout.androidaps.interfaces.GlucoseUnit
2021-11-18 15:47:22 +01:00
import info.nightscout.androidaps.interfaces.Profile
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
import info.nightscout.androidaps.plugins.iob.iobCobCalculator.GlucoseStatusProvider
2019-12-20 16:33:01 +01:00
import info.nightscout.androidaps.utils.HtmlHelper
2021-11-18 15:47:22 +01:00
import info.nightscout.androidaps.utils.XDripBroadcast
2021-01-21 16:45:54 +01: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 16:33:01 +01:00
import java.text.DecimalFormat
import java.util.*
2019-12-27 19:20:38 +01:00
import javax.inject.Inject
2019-12-20 16:33:01 +01:00
class CalibrationDialog : DialogFragmentWithDate() {
2020-03-10 18:58:27 +01:00
@Inject lateinit var injector: HasAndroidInjector
2021-11-04 10:56:12 +01:00
@Inject lateinit var rh: ResourceHelper
2020-02-05 15:58:29 +01:00
@Inject lateinit var profileFunction: ProfileFunction
2021-11-18 15:47:22 +01:00
@Inject lateinit var xDripBroadcast: XDripBroadcast
2021-02-09 17:57:28 +01:00
@Inject lateinit var uel: UserEntryLogger
@Inject lateinit var glucoseStatusProvider: GlucoseStatusProvider
2019-12-27 19:20:38 +01:00
2021-01-21 16:45:54 +01:00
private var _binding: DialogCalibrationBinding? = null
// This property is only valid between onCreateView and
// onDestroyView.
private val binding get() = _binding!!
2019-12-20 16:33:01 +01:00
override fun onSaveInstanceState(savedInstanceState: Bundle) {
super.onSaveInstanceState(savedInstanceState)
2021-01-21 16:45:54 +01:00
savedInstanceState.putDouble("bg", binding.bg.value)
2019-12-20 16:33:01 +01:00
}
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,
2021-01-21 16:45:54 +01:00
savedInstanceState: Bundle?): View {
2019-12-20 23:05:35 +01:00
onCreateViewGeneral()
2021-01-21 16:45:54 +01:00
_binding = DialogCalibrationBinding.inflate(inflater, container, false)
return binding.root
2019-12-20 16:33:01 +01:00
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
2019-12-27 19:20:38 +01:00
val units = profileFunction.getUnits()
val bg = Profile.fromMgdlToUnits(glucoseStatusProvider.glucoseStatusData?.glucose
2019-12-20 23:05:35 +01:00
?: 0.0, units)
2021-04-19 18:44:17 +02:00
if (units == GlucoseUnit.MMOL)
2021-01-21 16:45:54 +01:00
binding.bg.setParams(savedInstanceState?.getDouble("bg")
?: bg, 2.0, 30.0, 0.1, DecimalFormat("0.0"), false, binding.okcancel.ok)
2019-12-20 16:33:01 +01:00
else
2021-01-21 16:45:54 +01:00
binding.bg.setParams(savedInstanceState?.getDouble("bg")
?: bg, 36.0, 500.0, 1.0, DecimalFormat("0"), false, binding.okcancel.ok)
2021-11-04 10:56:12 +01:00
binding.units.text = if (units == GlucoseUnit.MMOL) rh.gs(R.string.mmol) else rh.gs(R.string.mgdl)
2021-01-21 16:45:54 +01:00
}
override fun onDestroyView() {
super.onDestroyView()
_binding = null
2019-12-20 16:33:01 +01:00
}
2019-12-27 19:20:38 +01:00
override fun submit(): Boolean {
2021-01-21 16:45:54 +01:00
if (_binding == null) return false
2019-12-27 19:20:38 +01:00
val units = profileFunction.getUnits()
2021-11-04 10:56:12 +01:00
val unitLabel = if (units == GlucoseUnit.MMOL) rh.gs(R.string.mmol) else rh.gs(R.string.mgdl)
2019-12-20 16:33:01 +01:00
val actions: LinkedList<String?> = LinkedList()
2021-01-21 16:45:54 +01:00
val bg = binding.bg.value ?: return false
2021-11-04 10:56:12 +01:00
actions.add(rh.gs(R.string.treatments_wizard_bg_label) + ": " + Profile.toCurrentUnitsString(profileFunction, bg) + " " + unitLabel)
2019-12-20 16:33:01 +01:00
if (bg > 0) {
activity?.let { activity ->
2021-11-04 10:56:12 +01:00
OKDialog.showConfirmation(activity, rh.gs(R.string.overview_calibration), HtmlHelper.fromHtml(Joiner.on("<br/>").join(actions)), {
2021-04-19 18:44:17 +02:00
uel.log(Action.CALIBRATION, Sources.CalibrationDialog, ValueWithUnit.fromGlucoseUnit(bg, units.asText))
2021-11-18 15:47:22 +01:00
xDripBroadcast.sendCalibration(bg)
2019-12-22 21:37:26 +01:00
})
2019-12-20 16:33:01 +01:00
}
} else
2019-12-22 21:37:26 +01:00
activity?.let { activity ->
2021-11-04 10:56:12 +01:00
OKDialog.show(activity, rh.gs(R.string.overview_calibration), rh.gs(R.string.no_action_selected))
2019-12-22 21:37:26 +01:00
}
2019-12-21 23:17:20 +01:00
return true
2019-12-20 16:33:01 +01:00
}
}