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

68 lines
3.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
import info.nightscout.androidaps.Constants
import info.nightscout.androidaps.MainApp
import info.nightscout.androidaps.R
import info.nightscout.androidaps.data.Profile
import info.nightscout.androidaps.plugins.configBuilder.ProfileFunctions
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-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.*
class CalibrationDialog : DialogFragmentWithDate() {
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)
val units = ProfileFunctions.getSystemUnits()
val bg = Profile.fromMgdlToUnits(GlucoseStatus.getGlucoseStatusData()?.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-20 16:33:01 +01:00
overview_calibration_units.text = if (units == Constants.MMOL) MainApp.gs(R.string.mmol) else MainApp.gs(R.string.mgdl)
}
2019-12-21 23:17:20 +01:00
override fun submit() :Boolean {
2019-12-20 16:33:01 +01:00
val units = ProfileFunctions.getSystemUnits()
val unitLabel = if (units == Constants.MMOL) MainApp.gs(R.string.mmol) else MainApp.gs(R.string.mgdl)
val actions: LinkedList<String?> = LinkedList()
val bg = overview_calibration_bg.value
actions.add(MainApp.gs(R.string.treatments_wizard_bg_label) + ": " + Profile.toCurrentUnitsString(bg) + " " + unitLabel)
if (bg > 0) {
activity?.let { activity ->
2019-12-22 21:37:26 +01:00
OKDialog.showConfirmation(activity, MainApp.gs(R.string.overview_calibration), HtmlHelper.fromHtml(Joiner.on("<br/>").join(actions)), Runnable {
2019-12-20 16:33:01 +01:00
XdripCalibrations.confirmAndSendCalibration(bg, context)
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 ->
OKDialog.show(activity, MainApp.gs(R.string.overview_calibration), MainApp.gs(R.string.no_action_selected))
}
2019-12-21 23:17:20 +01:00
return true
2019-12-20 16:33:01 +01:00
}
}