diff --git a/app/src/main/java/info/nightscout/androidaps/queue/CommandQueueImplementation.kt b/app/src/main/java/info/nightscout/androidaps/queue/CommandQueueImplementation.kt index c1ae6915e7..98101b7cf0 100644 --- a/app/src/main/java/info/nightscout/androidaps/queue/CommandQueueImplementation.kt +++ b/app/src/main/java/info/nightscout/androidaps/queue/CommandQueueImplementation.kt @@ -221,27 +221,34 @@ class CommandQueueImplementation @Inject constructor( // Check if pump store carbs // 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 + + var carbsRunnable = Runnable { } if ((detailedBolusInfo.carbs > 0) && (!activePlugin.activePump.pumpDescription.storesCarbInfo || detailedBolusInfo.carbsDuration != 0L || (detailedBolusInfo.carbsTimestamp ?: detailedBolusInfo.timestamp) > dateUtil.now()) ) { - disposable += repository.runTransactionForResult(detailedBolusInfo.insertCarbsTransaction()) - .subscribeBy( - onSuccess = { result -> - result.inserted.forEach { aapsLogger.debug(LTag.DATABASE, "Inserted carbs $it") } - callback?.result(PumpEnactResult(injector).enacted(false).success(true))?.run() + carbsRunnable = Runnable { + disposable += repository.runTransactionForResult(detailedBolusInfo.insertCarbsTransaction()) + .subscribeBy( + onSuccess = { result -> + result.inserted.forEach { aapsLogger.debug(LTag.DATABASE, "Inserted carbs $it") } + callback?.result(PumpEnactResult(injector).enacted(false).success(true))?.run() - }, - onError = { - aapsLogger.error(LTag.DATABASE, "Error while saving carbs", it) - callback?.result(PumpEnactResult(injector).enacted(false).success(false))?.run() - } - ) + }, + onError = { + aapsLogger.error(LTag.DATABASE, "Error while saving carbs", it) + callback?.result(PumpEnactResult(injector).enacted(false).success(false))?.run() + } + ) + } // Do not process carbs anymore detailedBolusInfo.carbs = 0.0 // 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 @@ -277,10 +284,10 @@ class CommandQueueImplementation @Inject constructor( if (detailedBolusInfo.bolusType == DetailedBolusInfo.BolusType.SMB) { add(CommandSMBBolus(injector, detailedBolusInfo, callback)) } 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, // 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 rxBus.send(EventBolusRequested(detailedBolusInfo.insulin)) } @@ -307,7 +314,7 @@ class CommandQueueImplementation @Inject constructor( @Synchronized override fun cancelAllBoluses() { 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.SMB_BOLUS) @@ -573,14 +580,16 @@ class CommandQueueImplementation @Inject constructor( return result } - private fun showBolusProgressDialog(insulin: Double, ctx: Context?) { - if (ctx != null) { + private fun showBolusProgressDialog(detailedBolusInfo: DetailedBolusInfo) { + if (detailedBolusInfo.context != null) { val bolusProgressDialog = BolusProgressDialog() - bolusProgressDialog.setInsulin(insulin) - bolusProgressDialog.show((ctx as AppCompatActivity).supportFragmentManager, "BolusProgress") + bolusProgressDialog.setInsulin(detailedBolusInfo.insulin) + bolusProgressDialog.setTimestamp(detailedBolusInfo.timestamp) + bolusProgressDialog.show((detailedBolusInfo.context as AppCompatActivity).supportFragmentManager, "BolusProgress") } else { val i = Intent() - i.putExtra("insulin", insulin) + i.putExtra("insulin", detailedBolusInfo.insulin) + i.putExtra("timestamp", detailedBolusInfo.timestamp) i.setClass(context, BolusProgressHelperActivity::class.java) i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK) context.startActivity(i) diff --git a/app/src/main/java/info/nightscout/androidaps/queue/QueueThread.kt b/app/src/main/java/info/nightscout/androidaps/queue/QueueThread.kt index 2828f51e7e..1bdbdc7d64 100644 --- a/app/src/main/java/info/nightscout/androidaps/queue/QueueThread.kt +++ b/app/src/main/java/info/nightscout/androidaps/queue/QueueThread.kt @@ -46,7 +46,7 @@ class QueueThread internal constructor( val secondsElapsed = (System.currentTimeMillis() - connectionStartTime) / 1000 val pump = activePlugin.activePump 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))) aapsLogger.debug(LTag.PUMPQUEUE, "timed out") pump.stopConnecting() diff --git a/app/src/main/java/info/nightscout/androidaps/queue/commands/CommandBolus.kt b/app/src/main/java/info/nightscout/androidaps/queue/commands/CommandBolus.kt index 7f2fe5dc0a..0de6850a6d 100644 --- a/app/src/main/java/info/nightscout/androidaps/queue/commands/CommandBolus.kt +++ b/app/src/main/java/info/nightscout/androidaps/queue/commands/CommandBolus.kt @@ -15,7 +15,8 @@ class CommandBolus( injector: HasAndroidInjector, private val detailedBolusInfo: DetailedBolusInfo, callback: Callback?, - type: CommandType + type: CommandType, + private val carbsRunnable: Runnable ) : Command(injector, type, callback) { @Inject lateinit var rxBus: RxBus @@ -23,8 +24,9 @@ class CommandBolus( override fun execute() { val r = activePlugin.activePump.deliverTreatment(detailedBolusInfo) + if (r.success) carbsRunnable.run() BolusProgressDialog.bolusEnded = true - rxBus.send(EventDismissBolusProgressIfRunning(r)) + rxBus.send(EventDismissBolusProgressIfRunning(r, detailedBolusInfo.timestamp)) aapsLogger.debug(LTag.PUMPQUEUE, "Result success: ${r.success} enacted: ${r.enacted}") callback?.result(r)?.run() } diff --git a/core/src/main/java/info/nightscout/androidaps/activities/BolusProgressHelperActivity.kt b/core/src/main/java/info/nightscout/androidaps/activities/BolusProgressHelperActivity.kt index fe55cdcdf2..063ebe0d7d 100644 --- a/core/src/main/java/info/nightscout/androidaps/activities/BolusProgressHelperActivity.kt +++ b/core/src/main/java/info/nightscout/androidaps/activities/BolusProgressHelperActivity.kt @@ -9,6 +9,7 @@ class BolusProgressHelperActivity : DialogAppCompatActivity() { BolusProgressDialog() .setHelperActivity(this) .setInsulin(intent.getDoubleExtra("insulin", 0.0)) + .setTimestamp(intent.getLongExtra("timestamp", 0L)) .show(supportFragmentManager, "BolusProgress") } } \ No newline at end of file diff --git a/core/src/main/java/info/nightscout/androidaps/dialogs/BolusProgressDialog.kt b/core/src/main/java/info/nightscout/androidaps/dialogs/BolusProgressDialog.kt index 0068707bd1..c73ad818eb 100644 --- a/core/src/main/java/info/nightscout/androidaps/dialogs/BolusProgressDialog.kt +++ b/core/src/main/java/info/nightscout/androidaps/dialogs/BolusProgressDialog.kt @@ -25,6 +25,7 @@ import info.nightscout.androidaps.utils.FabricPrivacy import info.nightscout.androidaps.utils.resources.ResourceHelper import info.nightscout.androidaps.utils.rx.AapsSchedulers import io.reactivex.disposables.CompositeDisposable +import io.reactivex.rxkotlin.plusAssign import javax.inject.Inject class BolusProgressDialog : DaggerDialogFragment() { @@ -40,15 +41,22 @@ class BolusProgressDialog : DaggerDialogFragment() { private val disposable = CompositeDisposable() companion object { + var bolusEnded = false var stopPressed = false } private var running = true private var amount = 0.0 + var timestamp: Long = 0L private var state: String? = null private var helpActivity: BolusProgressHelperActivity? = null + fun setTimestamp(timestamp: Long): BolusProgressDialog { + this.timestamp = timestamp + return this + } + fun setInsulin(amount: Double): BolusProgressDialog { this.amount = amount bolusEnded = false @@ -66,8 +74,10 @@ class BolusProgressDialog : DaggerDialogFragment() { // onDestroyView. private val binding get() = _binding!! - override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, - savedInstanceState: Bundle?): View { + override fun onCreateView( + inflater: LayoutInflater, container: ViewGroup?, + savedInstanceState: Bundle? + ): View { dialog?.window?.requestFeature(Window.FEATURE_NO_TITLE) dialog?.window?.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN) isCancelable = false @@ -80,6 +90,7 @@ class BolusProgressDialog : DaggerDialogFragment() { override fun onViewCreated(view: View, savedInstanceState: Bundle?) { savedInstanceState?.let { amount = it.getDouble("amount") + timestamp = it.getLong("timestamp") } binding.title.text = rh.gs(R.string.goingtodeliver, amount) binding.stop.setOnClickListener { @@ -111,30 +122,30 @@ class BolusProgressDialog : DaggerDialogFragment() { if (bolusEnded) dismiss() else running = true - disposable.add(rxBus + disposable += rxBus .toObservable(EventPumpStatusChanged::class.java) .observeOn(aapsSchedulers.main) .subscribe({ binding.status.text = it.getStatus(rh) }, fabricPrivacy::logException) - ) - disposable.add(rxBus + disposable += rxBus .toObservable(EventDismissBolusProgressIfRunning::class.java) .observeOn(aapsSchedulers.main) - .subscribe({ if (running) dismiss() }, fabricPrivacy::logException) - ) - disposable.add(rxBus + .subscribe({ + if (it.bolusTimestamp == null || it.bolusTimestamp == timestamp) + if (running) dismiss() + }, fabricPrivacy::logException) + disposable += rxBus .toObservable(EventOverviewBolusProgress::class.java) .observeOn(aapsSchedulers.main) .subscribe({ - aapsLogger.debug(LTag.UI, "Status: ${it.status} Percent: ${it.percent}") - binding.status.text = it.status - binding.progressbar.progress = it.percent - if (it.percent == 100) { - binding.stop.visibility = View.INVISIBLE - scheduleDismiss() - } - state = it.status - }, fabricPrivacy::logException) - ) + aapsLogger.debug(LTag.UI, "Status: ${it.status} Percent: ${it.percent}") + binding.status.text = it.status + binding.progressbar.progress = it.percent + if (it.percent == 100) { + binding.stop.visibility = View.INVISIBLE + scheduleDismiss() + } + state = it.status + }, fabricPrivacy::logException) } override fun dismiss() { @@ -161,6 +172,7 @@ class BolusProgressDialog : DaggerDialogFragment() { super.onSaveInstanceState(outState) outState.putString("state", state) outState.putDouble("amount", amount) + outState.putLong("timestamp", timestamp) } override fun onDestroyView() { diff --git a/core/src/main/java/info/nightscout/androidaps/plugins/general/overview/events/EventDismissBolusProgressIfRunning.kt b/core/src/main/java/info/nightscout/androidaps/plugins/general/overview/events/EventDismissBolusProgressIfRunning.kt index 53ab699d9c..dfde13b932 100644 --- a/core/src/main/java/info/nightscout/androidaps/plugins/general/overview/events/EventDismissBolusProgressIfRunning.kt +++ b/core/src/main/java/info/nightscout/androidaps/plugins/general/overview/events/EventDismissBolusProgressIfRunning.kt @@ -3,4 +3,4 @@ package info.nightscout.androidaps.plugins.general.overview.events import info.nightscout.androidaps.data.PumpEnactResult import info.nightscout.androidaps.events.Event -class EventDismissBolusProgressIfRunning(val result: PumpEnactResult?) : Event() \ No newline at end of file +class EventDismissBolusProgressIfRunning(val result: PumpEnactResult?, val bolusTimestamp: Long?) : Event() \ No newline at end of file