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

77 lines
3.5 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
2019-12-27 19:20:38 +01:00
import info.nightscout.androidaps.plugins.configBuilder.ProfileFunction
2019-12-20 16:33:01 +01:00
import info.nightscout.androidaps.plugins.iob.iobCobCalculator.GlucoseStatus
import info.nightscout.androidaps.utils.HtmlHelper
import info.nightscout.androidaps.utils.OKDialog
import info.nightscout.androidaps.utils.XdripCalibrations
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_calibration.*
2019-12-20 23:05:35 +01:00
import kotlinx.android.synthetic.main.okcancel.*
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
2019-12-27 19:20:38 +01:00
2019-12-20 16:33:01 +01:00
override fun onSaveInstanceState(savedInstanceState: Bundle) {
super.onSaveInstanceState(savedInstanceState)
savedInstanceState.putDouble("overview_calibration_bg", overview_calibration_bg.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_calibration, container, false)
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()
2020-03-10 18:58:27 +01:00
val bg = Profile.fromMgdlToUnits(GlucoseStatus(injector).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)
overview_calibration_bg.setParams(savedInstanceState?.getDouble("overview_calibration_bg")
2019-12-20 23:05:35 +01:00
?: bg, 2.0, 30.0, 0.1, DecimalFormat("0.0"), false, ok)
2019-12-20 16:33:01 +01:00
else
overview_calibration_bg.setParams(savedInstanceState?.getDouble("overview_calibration_bg")
2019-12-20 23:05:35 +01:00
?: bg, 36.0, 500.0, 1.0, DecimalFormat("0"), false, ok)
2019-12-27 19:20:38 +01:00
overview_calibration_units.text = if (units == Constants.MMOL) resourceHelper.gs(R.string.mmol) else resourceHelper.gs(R.string.mgdl)
2019-12-20 16:33:01 +01:00
}
2019-12-27 19:20:38 +01:00
override fun submit(): Boolean {
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()
val bg = overview_calibration_bg.value
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 ->
2019-12-27 19:20:38 +01:00
OKDialog.showConfirmation(activity, resourceHelper.gs(R.string.overview_calibration), HtmlHelper.fromHtml(Joiner.on("<br/>").join(actions)), Runnable {
2020-02-05 15:58:29 +01:00
aapsLogger.debug("USER ENTRY: CALIBRATION $bg")
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
}
}