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

93 lines
4 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.Constants
import info.nightscout.androidaps.R
import info.nightscout.androidaps.data.Profile
2021-02-25 12:28:36 +01:00
import info.nightscout.androidaps.database.entities.UserEntry.*
2021-01-21 16:45:54 +01:00
import info.nightscout.androidaps.databinding.DialogCalibrationBinding
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
import info.nightscout.androidaps.utils.XdripCalibrations
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
2020-02-05 15:58:29 +01:00
@Inject lateinit var resourceHelper: ResourceHelper
@Inject lateinit var profileFunction: ProfileFunction
2020-03-21 19:13:24 +01:00
@Inject lateinit var xdripCalibrations: XdripCalibrations
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)
2019-12-20 16:33:01 +01:00
if (units == Constants.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)
binding.units.text = if (units == Constants.MMOL) resourceHelper.gs(R.string.mmol) else resourceHelper.gs(R.string.mgdl)
}
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()
val unitLabel = if (units == Constants.MMOL) resourceHelper.gs(R.string.mmol) else resourceHelper.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
2020-03-10 18:58:27 +01:00
actions.add(resourceHelper.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-01-21 16:45:54 +01:00
OKDialog.showConfirmation(activity, resourceHelper.gs(R.string.overview_calibration), HtmlHelper.fromHtml(Joiner.on("<br/>").join(actions)), {
uel.log(Action.CALIBRATION, ValueWithUnit(bg, units))
2020-03-21 19:13:24 +01:00
xdripCalibrations.sendIntent(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 ->
2019-12-27 19:20:38 +01:00
OKDialog.show(activity, resourceHelper.gs(R.string.overview_calibration), resourceHelper.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
}
}