prevent NPE

This commit is contained in:
Milos Kozak 2020-01-26 17:18:13 +01:00
parent 81e4d1604b
commit b793931186

View file

@ -1,6 +1,5 @@
package info.nightscout.androidaps.plugins.aps.loop package info.nightscout.androidaps.plugins.aps.loop
import android.os.Bundle import android.os.Bundle
import android.view.LayoutInflater import android.view.LayoutInflater
import android.view.View import android.view.View
@ -12,7 +11,11 @@ import info.nightscout.androidaps.interfaces.Constraint
import info.nightscout.androidaps.plugins.aps.loop.events.EventLoopSetLastRunGui import info.nightscout.androidaps.plugins.aps.loop.events.EventLoopSetLastRunGui
import info.nightscout.androidaps.plugins.aps.loop.events.EventLoopUpdateGui import info.nightscout.androidaps.plugins.aps.loop.events.EventLoopUpdateGui
import info.nightscout.androidaps.plugins.bus.RxBus import info.nightscout.androidaps.plugins.bus.RxBus
import info.nightscout.androidaps.utils.* import info.nightscout.androidaps.utils.DateUtil
import info.nightscout.androidaps.utils.FabricPrivacy
import info.nightscout.androidaps.utils.HtmlHelper
import info.nightscout.androidaps.utils.SP
import info.nightscout.androidaps.utils.plusAssign
import io.reactivex.android.schedulers.AndroidSchedulers import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.disposables.CompositeDisposable import io.reactivex.disposables.CompositeDisposable
import kotlinx.android.synthetic.main.loop_fragment.* import kotlinx.android.synthetic.main.loop_fragment.*
@ -39,23 +42,21 @@ class LoopFragment : Fragment() {
override fun onResume() { override fun onResume() {
super.onResume() super.onResume()
disposable += RxBus disposable += RxBus
.toObservable(EventLoopUpdateGui::class.java) .toObservable(EventLoopUpdateGui::class.java)
.observeOn(AndroidSchedulers.mainThread()) .observeOn(AndroidSchedulers.mainThread())
.subscribe({ .subscribe({
updateGUI() updateGUI()
}, { }, {
FabricPrivacy.logException(it) FabricPrivacy.logException(it)
}) })
disposable += RxBus disposable += RxBus
.toObservable(EventLoopSetLastRunGui::class.java) .toObservable(EventLoopSetLastRunGui::class.java)
.observeOn(AndroidSchedulers.mainThread()) .observeOn(AndroidSchedulers.mainThread())
.subscribe({ .subscribe({
clearGUI() clearGUI()
loop_lastrun.text = it.text loop_lastrun?.text = it.text
}, { }, { FabricPrivacy.logException(it) })
FabricPrivacy.logException(it)
})
updateGUI() updateGUI()
SP.putBoolean(R.string.key_objectiveuseloop, true) SP.putBoolean(R.string.key_objectiveuseloop, true)
@ -71,39 +72,38 @@ class LoopFragment : Fragment() {
fun updateGUI() { fun updateGUI() {
if (loop_request == null) return if (loop_request == null) return
LoopPlugin.lastRun?.let { LoopPlugin.lastRun?.let {
loop_request.text = it.request?.toSpanned() ?: "" loop_request?.text = it.request?.toSpanned() ?: ""
loop_constraintsprocessed.text = it.constraintsProcessed?.toSpanned() ?: "" loop_constraintsprocessed?.text = it.constraintsProcessed?.toSpanned() ?: ""
loop_source.text = it.source ?: "" loop_source?.text = it.source ?: ""
loop_lastrun.text = it.lastAPSRun?.let { lastRun -> DateUtil.dateAndTimeString(lastRun.time) } loop_lastrun?.text = it.lastAPSRun?.let { lastRun -> DateUtil.dateAndTimeString(lastRun.time) }
?: "" ?: ""
loop_lastenact.text = it.lastAPSRun?.let { lastEnact -> DateUtil.dateAndTimeString(lastEnact.time) } loop_lastenact?.text = it.lastAPSRun?.let { lastEnact -> DateUtil.dateAndTimeString(lastEnact.time) }
?: "" ?: ""
loop_tbrsetbypump.text = it.tbrSetByPump?.let { tbrSetByPump -> HtmlHelper.fromHtml(tbrSetByPump.toHtml()) } loop_tbrsetbypump?.text = it.tbrSetByPump?.let { tbrSetByPump -> HtmlHelper.fromHtml(tbrSetByPump.toHtml()) }
?: "" ?: ""
loop_smbsetbypump.text = it.smbSetByPump?.let { smbSetByPump -> HtmlHelper.fromHtml(smbSetByPump.toHtml()) } loop_smbsetbypump?.text = it.smbSetByPump?.let { smbSetByPump -> HtmlHelper.fromHtml(smbSetByPump.toHtml()) }
?: "" ?: ""
val constraints = val constraints =
it.constraintsProcessed?.let { constraintsProcessed -> it.constraintsProcessed?.let { constraintsProcessed ->
val allConstraints = Constraint(0.0) val allConstraints = Constraint(0.0)
constraintsProcessed.rateConstraint?.let { rateConstraint -> allConstraints.copyReasons(rateConstraint) } constraintsProcessed.rateConstraint?.let { rateConstraint -> allConstraints.copyReasons(rateConstraint) }
constraintsProcessed.smbConstraint?.let { smbConstraint -> allConstraints.copyReasons(smbConstraint) } constraintsProcessed.smbConstraint?.let { smbConstraint -> allConstraints.copyReasons(smbConstraint) }
allConstraints.mostLimitedReasons allConstraints.mostLimitedReasons
} ?: "" } ?: ""
loop_constraints.text = constraints loop_constraints?.text = constraints
} }
} }
@Synchronized @Synchronized
private fun clearGUI() { private fun clearGUI() {
if (loop_request == null) return loop_request?.text = ""
loop_request.text = "" loop_constraints?.text = ""
loop_constraints.text = "" loop_constraintsprocessed?.text = ""
loop_constraintsprocessed.text = "" loop_source?.text = ""
loop_source.text = "" loop_lastrun?.text = ""
loop_lastrun.text = "" loop_lastenact?.text = ""
loop_lastenact.text = "" loop_tbrsetbypump?.text = ""
loop_tbrsetbypump.text = "" loop_smbsetbypump?.text = ""
loop_smbsetbypump.text = ""
} }
} }