try to fix closed BolusProggressDialog and storing carbs if bolus was canceled

This commit is contained in:
Milos Kozak 2021-11-07 18:15:15 +01:00
parent 0db34981d4
commit dcf3c9cc57
6 changed files with 66 additions and 42 deletions

View file

@ -221,11 +221,14 @@ class CommandQueueImplementation @Inject constructor(
// Check if pump store carbs // Check if pump store carbs
// If not, it's not necessary add command to the queue and initiate connection // If not, it's not necessary add command to the queue and initiate connection
// Assuming carbs in the future and carbs with duration are NOT stores anyway // Assuming carbs in the future and carbs with duration are NOT stores anyway
var carbsRunnable = Runnable { }
if ((detailedBolusInfo.carbs > 0) && if ((detailedBolusInfo.carbs > 0) &&
(!activePlugin.activePump.pumpDescription.storesCarbInfo || (!activePlugin.activePump.pumpDescription.storesCarbInfo ||
detailedBolusInfo.carbsDuration != 0L || detailedBolusInfo.carbsDuration != 0L ||
(detailedBolusInfo.carbsTimestamp ?: detailedBolusInfo.timestamp) > dateUtil.now()) (detailedBolusInfo.carbsTimestamp ?: detailedBolusInfo.timestamp) > dateUtil.now())
) { ) {
carbsRunnable = Runnable {
disposable += repository.runTransactionForResult(detailedBolusInfo.insertCarbsTransaction()) disposable += repository.runTransactionForResult(detailedBolusInfo.insertCarbsTransaction())
.subscribeBy( .subscribeBy(
onSuccess = { result -> onSuccess = { result ->
@ -238,10 +241,14 @@ class CommandQueueImplementation @Inject constructor(
callback?.result(PumpEnactResult(injector).enacted(false).success(false))?.run() callback?.result(PumpEnactResult(injector).enacted(false).success(false))?.run()
} }
) )
}
// Do not process carbs anymore // Do not process carbs anymore
detailedBolusInfo.carbs = 0.0 detailedBolusInfo.carbs = 0.0
// if no insulin just exit // if no insulin just exit
if (detailedBolusInfo.insulin == 0.0) return true if (detailedBolusInfo.insulin == 0.0) {
carbsRunnable.run() // store carbs
return true
}
} }
var type = if (detailedBolusInfo.bolusType == DetailedBolusInfo.BolusType.SMB) CommandType.SMB_BOLUS else CommandType.BOLUS var type = if (detailedBolusInfo.bolusType == DetailedBolusInfo.BolusType.SMB) CommandType.SMB_BOLUS else CommandType.BOLUS
@ -277,10 +284,10 @@ class CommandQueueImplementation @Inject constructor(
if (detailedBolusInfo.bolusType == DetailedBolusInfo.BolusType.SMB) { if (detailedBolusInfo.bolusType == DetailedBolusInfo.BolusType.SMB) {
add(CommandSMBBolus(injector, detailedBolusInfo, callback)) add(CommandSMBBolus(injector, detailedBolusInfo, callback))
} else { } else {
add(CommandBolus(injector, detailedBolusInfo, callback, type)) add(CommandBolus(injector, detailedBolusInfo, callback, type, carbsRunnable))
if (type == CommandType.BOLUS) { // Bring up bolus progress dialog (start here, so the dialog is shown when the bolus is requested, if (type == CommandType.BOLUS) { // Bring up bolus progress dialog (start here, so the dialog is shown when the bolus is requested,
// not when the Bolus command is starting. The command closes the dialog upon completion). // not when the Bolus command is starting. The command closes the dialog upon completion).
showBolusProgressDialog(detailedBolusInfo.insulin, detailedBolusInfo.context) showBolusProgressDialog(detailedBolusInfo)
// Notify Wear about upcoming bolus // Notify Wear about upcoming bolus
rxBus.send(EventBolusRequested(detailedBolusInfo.insulin)) rxBus.send(EventBolusRequested(detailedBolusInfo.insulin))
} }
@ -307,7 +314,7 @@ class CommandQueueImplementation @Inject constructor(
@Synchronized @Synchronized
override fun cancelAllBoluses() { override fun cancelAllBoluses() {
if (!isRunning(CommandType.BOLUS)) { if (!isRunning(CommandType.BOLUS)) {
rxBus.send(EventDismissBolusProgressIfRunning(PumpEnactResult(injector).success(true).enacted(false))) rxBus.send(EventDismissBolusProgressIfRunning(PumpEnactResult(injector).success(true).enacted(false), null))
} }
removeAll(CommandType.BOLUS) removeAll(CommandType.BOLUS)
removeAll(CommandType.SMB_BOLUS) removeAll(CommandType.SMB_BOLUS)
@ -573,14 +580,16 @@ class CommandQueueImplementation @Inject constructor(
return result return result
} }
private fun showBolusProgressDialog(insulin: Double, ctx: Context?) { private fun showBolusProgressDialog(detailedBolusInfo: DetailedBolusInfo) {
if (ctx != null) { if (detailedBolusInfo.context != null) {
val bolusProgressDialog = BolusProgressDialog() val bolusProgressDialog = BolusProgressDialog()
bolusProgressDialog.setInsulin(insulin) bolusProgressDialog.setInsulin(detailedBolusInfo.insulin)
bolusProgressDialog.show((ctx as AppCompatActivity).supportFragmentManager, "BolusProgress") bolusProgressDialog.setTimestamp(detailedBolusInfo.timestamp)
bolusProgressDialog.show((detailedBolusInfo.context as AppCompatActivity).supportFragmentManager, "BolusProgress")
} else { } else {
val i = Intent() val i = Intent()
i.putExtra("insulin", insulin) i.putExtra("insulin", detailedBolusInfo.insulin)
i.putExtra("timestamp", detailedBolusInfo.timestamp)
i.setClass(context, BolusProgressHelperActivity::class.java) i.setClass(context, BolusProgressHelperActivity::class.java)
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK) i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
context.startActivity(i) context.startActivity(i)

View file

@ -46,7 +46,7 @@ class QueueThread internal constructor(
val secondsElapsed = (System.currentTimeMillis() - connectionStartTime) / 1000 val secondsElapsed = (System.currentTimeMillis() - connectionStartTime) / 1000
val pump = activePlugin.activePump val pump = activePlugin.activePump
if (!pump.isConnected() && secondsElapsed > Constants.PUMP_MAX_CONNECTION_TIME_IN_SECONDS) { if (!pump.isConnected() && secondsElapsed > Constants.PUMP_MAX_CONNECTION_TIME_IN_SECONDS) {
rxBus.send(EventDismissBolusProgressIfRunning(null)) rxBus.send(EventDismissBolusProgressIfRunning(null, null))
rxBus.send(EventPumpStatusChanged(rh.gs(R.string.connectiontimedout))) rxBus.send(EventPumpStatusChanged(rh.gs(R.string.connectiontimedout)))
aapsLogger.debug(LTag.PUMPQUEUE, "timed out") aapsLogger.debug(LTag.PUMPQUEUE, "timed out")
pump.stopConnecting() pump.stopConnecting()

View file

@ -15,7 +15,8 @@ class CommandBolus(
injector: HasAndroidInjector, injector: HasAndroidInjector,
private val detailedBolusInfo: DetailedBolusInfo, private val detailedBolusInfo: DetailedBolusInfo,
callback: Callback?, callback: Callback?,
type: CommandType type: CommandType,
private val carbsRunnable: Runnable
) : Command(injector, type, callback) { ) : Command(injector, type, callback) {
@Inject lateinit var rxBus: RxBus @Inject lateinit var rxBus: RxBus
@ -23,8 +24,9 @@ class CommandBolus(
override fun execute() { override fun execute() {
val r = activePlugin.activePump.deliverTreatment(detailedBolusInfo) val r = activePlugin.activePump.deliverTreatment(detailedBolusInfo)
if (r.success) carbsRunnable.run()
BolusProgressDialog.bolusEnded = true BolusProgressDialog.bolusEnded = true
rxBus.send(EventDismissBolusProgressIfRunning(r)) rxBus.send(EventDismissBolusProgressIfRunning(r, detailedBolusInfo.timestamp))
aapsLogger.debug(LTag.PUMPQUEUE, "Result success: ${r.success} enacted: ${r.enacted}") aapsLogger.debug(LTag.PUMPQUEUE, "Result success: ${r.success} enacted: ${r.enacted}")
callback?.result(r)?.run() callback?.result(r)?.run()
} }

View file

@ -9,6 +9,7 @@ class BolusProgressHelperActivity : DialogAppCompatActivity() {
BolusProgressDialog() BolusProgressDialog()
.setHelperActivity(this) .setHelperActivity(this)
.setInsulin(intent.getDoubleExtra("insulin", 0.0)) .setInsulin(intent.getDoubleExtra("insulin", 0.0))
.setTimestamp(intent.getLongExtra("timestamp", 0L))
.show(supportFragmentManager, "BolusProgress") .show(supportFragmentManager, "BolusProgress")
} }
} }

View file

@ -25,6 +25,7 @@ import info.nightscout.androidaps.utils.FabricPrivacy
import info.nightscout.androidaps.utils.resources.ResourceHelper import info.nightscout.androidaps.utils.resources.ResourceHelper
import info.nightscout.androidaps.utils.rx.AapsSchedulers import info.nightscout.androidaps.utils.rx.AapsSchedulers
import io.reactivex.disposables.CompositeDisposable import io.reactivex.disposables.CompositeDisposable
import io.reactivex.rxkotlin.plusAssign
import javax.inject.Inject import javax.inject.Inject
class BolusProgressDialog : DaggerDialogFragment() { class BolusProgressDialog : DaggerDialogFragment() {
@ -40,15 +41,22 @@ class BolusProgressDialog : DaggerDialogFragment() {
private val disposable = CompositeDisposable() private val disposable = CompositeDisposable()
companion object { companion object {
var bolusEnded = false var bolusEnded = false
var stopPressed = false var stopPressed = false
} }
private var running = true private var running = true
private var amount = 0.0 private var amount = 0.0
var timestamp: Long = 0L
private var state: String? = null private var state: String? = null
private var helpActivity: BolusProgressHelperActivity? = null private var helpActivity: BolusProgressHelperActivity? = null
fun setTimestamp(timestamp: Long): BolusProgressDialog {
this.timestamp = timestamp
return this
}
fun setInsulin(amount: Double): BolusProgressDialog { fun setInsulin(amount: Double): BolusProgressDialog {
this.amount = amount this.amount = amount
bolusEnded = false bolusEnded = false
@ -66,8 +74,10 @@ class BolusProgressDialog : DaggerDialogFragment() {
// onDestroyView. // onDestroyView.
private val binding get() = _binding!! private val binding get() = _binding!!
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, override fun onCreateView(
savedInstanceState: Bundle?): View { inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View {
dialog?.window?.requestFeature(Window.FEATURE_NO_TITLE) dialog?.window?.requestFeature(Window.FEATURE_NO_TITLE)
dialog?.window?.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN) dialog?.window?.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN)
isCancelable = false isCancelable = false
@ -80,6 +90,7 @@ class BolusProgressDialog : DaggerDialogFragment() {
override fun onViewCreated(view: View, savedInstanceState: Bundle?) { override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
savedInstanceState?.let { savedInstanceState?.let {
amount = it.getDouble("amount") amount = it.getDouble("amount")
timestamp = it.getLong("timestamp")
} }
binding.title.text = rh.gs(R.string.goingtodeliver, amount) binding.title.text = rh.gs(R.string.goingtodeliver, amount)
binding.stop.setOnClickListener { binding.stop.setOnClickListener {
@ -111,17 +122,18 @@ class BolusProgressDialog : DaggerDialogFragment() {
if (bolusEnded) dismiss() if (bolusEnded) dismiss()
else running = true else running = true
disposable.add(rxBus disposable += rxBus
.toObservable(EventPumpStatusChanged::class.java) .toObservable(EventPumpStatusChanged::class.java)
.observeOn(aapsSchedulers.main) .observeOn(aapsSchedulers.main)
.subscribe({ binding.status.text = it.getStatus(rh) }, fabricPrivacy::logException) .subscribe({ binding.status.text = it.getStatus(rh) }, fabricPrivacy::logException)
) disposable += rxBus
disposable.add(rxBus
.toObservable(EventDismissBolusProgressIfRunning::class.java) .toObservable(EventDismissBolusProgressIfRunning::class.java)
.observeOn(aapsSchedulers.main) .observeOn(aapsSchedulers.main)
.subscribe({ if (running) dismiss() }, fabricPrivacy::logException) .subscribe({
) if (it.bolusTimestamp == null || it.bolusTimestamp == timestamp)
disposable.add(rxBus if (running) dismiss()
}, fabricPrivacy::logException)
disposable += rxBus
.toObservable(EventOverviewBolusProgress::class.java) .toObservable(EventOverviewBolusProgress::class.java)
.observeOn(aapsSchedulers.main) .observeOn(aapsSchedulers.main)
.subscribe({ .subscribe({
@ -134,7 +146,6 @@ class BolusProgressDialog : DaggerDialogFragment() {
} }
state = it.status state = it.status
}, fabricPrivacy::logException) }, fabricPrivacy::logException)
)
} }
override fun dismiss() { override fun dismiss() {
@ -161,6 +172,7 @@ class BolusProgressDialog : DaggerDialogFragment() {
super.onSaveInstanceState(outState) super.onSaveInstanceState(outState)
outState.putString("state", state) outState.putString("state", state)
outState.putDouble("amount", amount) outState.putDouble("amount", amount)
outState.putLong("timestamp", timestamp)
} }
override fun onDestroyView() { override fun onDestroyView() {

View file

@ -3,4 +3,4 @@ package info.nightscout.androidaps.plugins.general.overview.events
import info.nightscout.androidaps.data.PumpEnactResult import info.nightscout.androidaps.data.PumpEnactResult
import info.nightscout.androidaps.events.Event import info.nightscout.androidaps.events.Event
class EventDismissBolusProgressIfRunning(val result: PumpEnactResult?) : Event() class EventDismissBolusProgressIfRunning(val result: PumpEnactResult?, val bolusTimestamp: Long?) : Event()