173 lines
6.4 KiB
Kotlin
173 lines
6.4 KiB
Kotlin
package info.nightscout.androidaps.dialogs
|
|
|
|
import android.app.Activity
|
|
import android.os.Bundle
|
|
import android.os.SystemClock
|
|
import android.view.LayoutInflater
|
|
import android.view.View
|
|
import android.view.ViewGroup
|
|
import android.view.Window
|
|
import android.view.WindowManager
|
|
import dagger.android.support.DaggerDialogFragment
|
|
import info.nightscout.androidaps.R
|
|
import info.nightscout.androidaps.activities.BolusProgressHelperActivity
|
|
import info.nightscout.androidaps.events.EventPumpStatusChanged
|
|
import info.nightscout.androidaps.interfaces.CommandQueueProvider
|
|
import info.nightscout.androidaps.logging.AAPSLogger
|
|
import info.nightscout.androidaps.logging.LTag
|
|
import info.nightscout.androidaps.plugins.bus.RxBusWrapper
|
|
import info.nightscout.androidaps.plugins.general.overview.events.EventDismissBolusProgressIfRunning
|
|
import info.nightscout.androidaps.plugins.general.overview.events.EventOverviewBolusProgress
|
|
import info.nightscout.androidaps.utils.FabricPrivacy
|
|
import info.nightscout.androidaps.utils.resources.ResourceHelper
|
|
import io.reactivex.android.schedulers.AndroidSchedulers
|
|
import io.reactivex.disposables.CompositeDisposable
|
|
import kotlinx.android.synthetic.main.dialog_bolusprogress.*
|
|
import javax.inject.Inject
|
|
|
|
class BolusProgressDialog : DaggerDialogFragment() {
|
|
@Inject lateinit var aapsLogger: AAPSLogger
|
|
@Inject lateinit var rxBus: RxBusWrapper
|
|
@Inject lateinit var resourceHelper: ResourceHelper
|
|
@Inject lateinit var commandQueue: CommandQueueProvider
|
|
@Inject lateinit var fabricPrivacy: FabricPrivacy
|
|
|
|
private val disposable = CompositeDisposable()
|
|
|
|
companion object {
|
|
@JvmField
|
|
var bolusEnded = false
|
|
|
|
@JvmField
|
|
var stopPressed = false
|
|
}
|
|
|
|
private var running = true
|
|
private var amount = 0.0
|
|
private var state: String? = null
|
|
private var helpActivity: BolusProgressHelperActivity? = null
|
|
|
|
fun setInsulin(amount: Double): BolusProgressDialog {
|
|
this.amount = amount
|
|
bolusEnded = false
|
|
return this
|
|
}
|
|
|
|
fun setHelperActivity(activity: BolusProgressHelperActivity): BolusProgressDialog {
|
|
helpActivity = activity
|
|
return this
|
|
}
|
|
|
|
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
|
|
dialog?.setCanceledOnTouchOutside(false)
|
|
return inflater.inflate(R.layout.dialog_bolusprogress, container, false)
|
|
}
|
|
|
|
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
|
|
savedInstanceState?.let {
|
|
amount = it.getDouble("amount")
|
|
}
|
|
overview_bolusprogress_title.text = resourceHelper.gs(R.string.overview_bolusprogress_goingtodeliver, amount)
|
|
overview_bolusprogress_stop.setOnClickListener {
|
|
aapsLogger.debug(LTag.UI, "Stop bolus delivery button pressed")
|
|
stopPressed = true
|
|
overview_bolusprogress_stoppressed.visibility = View.VISIBLE
|
|
overview_bolusprogress_stop.visibility = View.INVISIBLE
|
|
commandQueue.cancelAllBoluses()
|
|
}
|
|
val defaultState = resourceHelper.gs(R.string.waitingforpump)
|
|
overview_bolusprogress_progressbar.max = 100
|
|
state = savedInstanceState?.getString("state", defaultState) ?: defaultState
|
|
overview_bolusprogress_status.text = state
|
|
stopPressed = false
|
|
}
|
|
|
|
override fun onStart() {
|
|
super.onStart()
|
|
dialog?.window?.setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT)
|
|
}
|
|
|
|
override fun onResume() {
|
|
super.onResume()
|
|
aapsLogger.debug(LTag.UI, "onResume")
|
|
if (!commandQueue.bolusInQueue())
|
|
bolusEnded = true
|
|
|
|
if (bolusEnded) dismiss()
|
|
else running = true
|
|
|
|
disposable.add(rxBus
|
|
.toObservable(EventPumpStatusChanged::class.java)
|
|
.observeOn(AndroidSchedulers.mainThread())
|
|
.subscribe({ overview_bolusprogress_status.text = it.getStatus(resourceHelper) }) { fabricPrivacy.logException(it) }
|
|
)
|
|
disposable.add(rxBus
|
|
.toObservable(EventDismissBolusProgressIfRunning::class.java)
|
|
.observeOn(AndroidSchedulers.mainThread())
|
|
.subscribe({ if (running) dismiss() }) { fabricPrivacy.logException(it) }
|
|
)
|
|
disposable.add(rxBus
|
|
.toObservable(EventOverviewBolusProgress::class.java)
|
|
.observeOn(AndroidSchedulers.mainThread())
|
|
.subscribe({
|
|
aapsLogger.debug(LTag.UI, "Status: ${it.status} Percent: ${it.percent}")
|
|
overview_bolusprogress_status.text = it.status
|
|
overview_bolusprogress_progressbar.progress = it.percent
|
|
if (it.percent == 100) {
|
|
overview_bolusprogress_stop.visibility = View.INVISIBLE
|
|
scheduleDismiss()
|
|
}
|
|
state = it.status
|
|
}) { fabricPrivacy.logException(it) }
|
|
)
|
|
}
|
|
|
|
override fun dismiss() {
|
|
aapsLogger.debug(LTag.UI, "dismiss")
|
|
try {
|
|
super.dismiss()
|
|
} catch (e: IllegalStateException) {
|
|
// dialog not running yet. onResume will try again. Set bolusEnded to make extra
|
|
// sure onResume will catch this
|
|
bolusEnded = true
|
|
aapsLogger.error("Unhandled exception", e)
|
|
}
|
|
helpActivity?.finish()
|
|
}
|
|
|
|
override fun onPause() {
|
|
super.onPause()
|
|
aapsLogger.debug(LTag.UI, "onPause")
|
|
running = false
|
|
disposable.clear()
|
|
}
|
|
|
|
override fun onSaveInstanceState(outState: Bundle) {
|
|
super.onSaveInstanceState(outState)
|
|
outState.putString("state", state)
|
|
outState.putDouble("amount", amount)
|
|
}
|
|
|
|
private fun scheduleDismiss() {
|
|
aapsLogger.debug(LTag.UI, "scheduleDismiss")
|
|
Thread(Runnable {
|
|
SystemClock.sleep(5000)
|
|
bolusEnded = true
|
|
val activity: Activity? = activity
|
|
activity?.runOnUiThread {
|
|
if (running) {
|
|
aapsLogger.debug(LTag.UI, "executing")
|
|
try {
|
|
dismiss()
|
|
} catch (e: Exception) {
|
|
aapsLogger.error("Unhandled exception", e)
|
|
}
|
|
}
|
|
}
|
|
}).start()
|
|
}
|
|
}
|