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

166 lines
8.2 KiB
Kotlin
Raw Normal View History

2019-12-20 18:55:54 +01:00
package info.nightscout.androidaps.dialogs
2019-12-19 23:45:27 +01:00
2020-01-10 23:14:58 +01:00
import android.content.Context
2019-12-19 23:45:27 +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
2020-04-24 22:39:56 +02:00
import dagger.android.HasAndroidInjector
2019-12-19 23:45:27 +01:00
import info.nightscout.androidaps.MainApp
import info.nightscout.androidaps.R
2019-12-20 23:05:35 +01:00
import info.nightscout.androidaps.activities.ErrorHelperActivity
2019-12-19 23:45:27 +01:00
import info.nightscout.androidaps.data.DetailedBolusInfo
import info.nightscout.androidaps.db.CareportalEvent
import info.nightscout.androidaps.db.Source
2020-01-10 23:14:58 +01:00
import info.nightscout.androidaps.interfaces.ActivePluginProvider
import info.nightscout.androidaps.interfaces.CommandQueueProvider
2019-12-19 23:45:27 +01:00
import info.nightscout.androidaps.interfaces.Constraint
2019-12-27 19:20:38 +01:00
import info.nightscout.androidaps.plugins.configBuilder.ConstraintChecker
2019-12-19 23:45:27 +01:00
import info.nightscout.androidaps.plugins.general.nsclient.NSUpload
import info.nightscout.androidaps.queue.Callback
2019-12-27 19:20:38 +01:00
import info.nightscout.androidaps.utils.DateUtil
import info.nightscout.androidaps.utils.DecimalFormatter
import info.nightscout.androidaps.utils.HtmlHelper
import info.nightscout.androidaps.utils.SafeParse
import info.nightscout.androidaps.utils.alertDialogs.OKDialog
2019-12-27 19:20:38 +01:00
import info.nightscout.androidaps.utils.resources.ResourceHelper
2020-04-19 12:57:08 +02:00
import info.nightscout.androidaps.utils.sharedPreferences.SP
2019-12-20 18:55:54 +01:00
import kotlinx.android.synthetic.main.dialog_fill.*
2019-12-19 23:45:27 +01:00
import kotlinx.android.synthetic.main.notes.*
import kotlinx.android.synthetic.main.okcancel.*
import org.json.JSONException
import org.json.JSONObject
2019-12-19 23:45:27 +01:00
import java.util.*
2019-12-27 19:20:38 +01:00
import javax.inject.Inject
2019-12-19 23:45:27 +01:00
import kotlin.math.abs
class FillDialog : DialogFragmentWithDate() {
2019-12-30 00:53:44 +01:00
@Inject lateinit var constraintChecker: ConstraintChecker
@Inject lateinit var resourceHelper: ResourceHelper
2020-01-10 23:14:58 +01:00
@Inject lateinit var ctx: Context
2020-05-09 10:52:20 +02:00
@Inject lateinit var nsUpload: NSUpload
2020-01-10 23:14:58 +01:00
@Inject lateinit var commandQueue: CommandQueueProvider
@Inject lateinit var activePlugin: ActivePluginProvider
2019-12-27 19:20:38 +01:00
2019-12-19 23:45:27 +01:00
override fun onSaveInstanceState(savedInstanceState: Bundle) {
super.onSaveInstanceState(savedInstanceState)
2019-12-30 00:53:44 +01:00
savedInstanceState.putDouble("fill_insulin_amount", fill_insulinamount.value)
2019-12-19 23:45:27 +01:00
}
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_fill, container, false)
2019-12-19 23:45:27 +01:00
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
2019-12-27 19:20:38 +01:00
val maxInsulin = constraintChecker.getMaxBolusAllowed().value()
2020-01-10 23:14:58 +01:00
val bolusStep = activePlugin.activePump.pumpDescription.bolusStep
2019-12-30 00:53:44 +01:00
fill_insulinamount.setParams(savedInstanceState?.getDouble("fill_insulin_amount")
2020-03-17 22:56:14 +01:00
?: 0.0, 0.0, maxInsulin, bolusStep, DecimalFormatter.pumpSupportedBolusFormat(activePlugin.activePump), true, ok)
2019-12-27 19:20:38 +01:00
val amount1 = sp.getDouble("fill_button1", 0.3)
2019-12-19 23:45:27 +01:00
if (amount1 > 0) {
fill_preset_button1.visibility = View.VISIBLE
2020-03-17 22:56:14 +01:00
fill_preset_button1.text = DecimalFormatter.toPumpSupportedBolus(amount1, activePlugin.activePump) // + "U");
2019-12-19 23:45:27 +01:00
fill_preset_button1.setOnClickListener { fill_insulinamount.value = amount1 }
} else {
fill_preset_button1.visibility = View.GONE
}
2019-12-27 19:20:38 +01:00
val amount2 = sp.getDouble("fill_button2", 0.0)
2019-12-19 23:45:27 +01:00
if (amount2 > 0) {
fill_preset_button2.visibility = View.VISIBLE
2020-03-17 22:56:14 +01:00
fill_preset_button2.text = DecimalFormatter.toPumpSupportedBolus(amount2, activePlugin.activePump) // + "U");
2019-12-19 23:45:27 +01:00
fill_preset_button2.setOnClickListener { fill_insulinamount.value = amount2 }
} else {
fill_preset_button2.visibility = View.GONE
}
2019-12-27 19:20:38 +01:00
val amount3 = sp.getDouble("fill_button3", 0.0)
2019-12-19 23:45:27 +01:00
if (amount3 > 0) {
fill_preset_button3.visibility = View.VISIBLE
2020-03-17 22:56:14 +01:00
fill_preset_button3.text = DecimalFormatter.toPumpSupportedBolus(amount3, activePlugin.activePump) // + "U");
2019-12-19 23:45:27 +01:00
fill_preset_button3.setOnClickListener { fill_insulinamount.value = amount3 }
} else {
fill_preset_button3.visibility = View.GONE
}
}
2019-12-21 23:17:20 +01:00
override fun submit(): Boolean {
2019-12-19 23:45:27 +01:00
val insulin = SafeParse.stringToDouble(fill_insulinamount.text)
val actions: LinkedList<String?> = LinkedList()
2019-12-27 19:20:38 +01:00
val insulinAfterConstraints = constraintChecker.applyBolusConstraints(Constraint(insulin)).value()
2019-12-19 23:45:27 +01:00
if (insulinAfterConstraints > 0) {
2019-12-27 19:20:38 +01:00
actions.add(resourceHelper.gs(R.string.fillwarning))
2019-12-19 23:45:27 +01:00
actions.add("")
2020-03-17 22:56:14 +01:00
actions.add(resourceHelper.gs(R.string.bolus) + ": " + "<font color='" + resourceHelper.gc(R.color.colorInsulinButton) + "'>" + DecimalFormatter.toPumpSupportedBolus(insulinAfterConstraints, activePlugin.activePump) + resourceHelper.gs(R.string.insulin_unit_shortname) + "</font>")
2019-12-19 23:45:27 +01:00
if (abs(insulinAfterConstraints - insulin) > 0.01)
2019-12-27 19:20:38 +01:00
actions.add(resourceHelper.gs(R.string.bolusconstraintappliedwarning, resourceHelper.gc(R.color.warning), insulin, insulinAfterConstraints))
2019-12-19 23:45:27 +01:00
}
val siteChange = fill_catheter_change.isChecked
if (siteChange)
2019-12-27 19:20:38 +01:00
actions.add("" + "<font color='" + resourceHelper.gc(R.color.actionsConfirm) + "'>" + resourceHelper.gs(R.string.record_pump_site_change) + "</font>")
2019-12-19 23:45:27 +01:00
val insulinChange = fill_cartridge_change.isChecked
if (insulinChange)
2019-12-27 19:20:38 +01:00
actions.add("" + "<font color='" + resourceHelper.gc(R.color.actionsConfirm) + "'>" + resourceHelper.gs(R.string.record_insulin_cartridge_change) + "</font>")
2019-12-19 23:45:27 +01:00
val notes = notes.text.toString()
if (notes.isNotEmpty())
2019-12-27 19:20:38 +01:00
actions.add(resourceHelper.gs(R.string.careportal_newnstreatment_notes_label) + ": " + notes)
eventTime -= eventTime % 1000
2019-12-19 23:45:27 +01:00
if (eventTimeChanged)
2020-05-07 23:40:59 +02:00
actions.add(resourceHelper.gs(R.string.time) + ": " + dateUtil.dateAndTimeString(eventTime))
2019-12-19 23:45:27 +01:00
if (insulinAfterConstraints > 0 || fill_catheter_change.isChecked || fill_cartridge_change.isChecked) {
activity?.let { activity ->
2019-12-27 19:20:38 +01:00
OKDialog.showConfirmation(activity, resourceHelper.gs(R.string.primefill), HtmlHelper.fromHtml(Joiner.on("<br/>").join(actions)), Runnable {
2019-12-19 23:45:27 +01:00
if (insulinAfterConstraints > 0) {
2020-02-28 18:20:31 +01:00
aapsLogger.debug("USER ENTRY: PRIME BOLUS $insulinAfterConstraints")
requestPrimeBolus(insulinAfterConstraints, notes)
2019-12-19 23:45:27 +01:00
}
if (siteChange) {
2020-02-28 18:20:31 +01:00
aapsLogger.debug("USER ENTRY: SITE CHANGE")
2020-05-09 10:52:20 +02:00
nsUpload.generateCareportalEvent(CareportalEvent.SITECHANGE, eventTime, notes)
}
if (insulinChange) {
// add a second for case of both checked
2020-02-28 18:20:31 +01:00
aapsLogger.debug("USER ENTRY: INSULIN CHANGE")
2020-05-09 10:52:20 +02:00
nsUpload.generateCareportalEvent(CareportalEvent.INSULINCHANGE, eventTime + 1000, notes)
}
2019-12-22 21:37:26 +01:00
}, null)
2019-12-19 23:45:27 +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.primefill), resourceHelper.gs(R.string.no_action_selected))
2019-12-22 21:37:26 +01:00
}
2019-12-19 23:45:27 +01:00
}
dismiss()
2019-12-21 23:17:20 +01:00
return true
2019-12-19 23:45:27 +01:00
}
private fun requestPrimeBolus(insulin: Double, notes: String) {
val detailedBolusInfo = DetailedBolusInfo()
detailedBolusInfo.insulin = insulin
detailedBolusInfo.context = context
detailedBolusInfo.source = Source.USER
detailedBolusInfo.isValid = false // do not count it in IOB (for pump history)
detailedBolusInfo.notes = notes
2020-01-26 21:15:57 +01:00
commandQueue.bolus(detailedBolusInfo, object : Callback() {
override fun run() {
if (!result.success) {
2020-01-26 21:15:57 +01:00
val i = Intent(ctx, ErrorHelperActivity::class.java)
i.putExtra("soundid", R.raw.boluserror)
i.putExtra("status", result.comment)
2020-01-26 21:15:57 +01:00
i.putExtra("title", resourceHelper.gs(R.string.treatmentdeliveryerror))
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
2020-01-26 21:15:57 +01:00
ctx.startActivity(i)
}
}
})
}
}