OpenAPSSMBFragment -> jetpack

This commit is contained in:
Milos Kozak 2021-01-24 14:55:38 +01:00
parent 95e97edc74
commit c4eb6eae91

View file

@ -8,6 +8,7 @@ import android.view.View
import android.view.ViewGroup import android.view.ViewGroup
import dagger.android.support.DaggerFragment import dagger.android.support.DaggerFragment
import info.nightscout.androidaps.R import info.nightscout.androidaps.R
import info.nightscout.androidaps.databinding.OpenapsamaFragmentBinding
import info.nightscout.androidaps.logging.AAPSLogger import info.nightscout.androidaps.logging.AAPSLogger
import info.nightscout.androidaps.logging.LTag import info.nightscout.androidaps.logging.LTag
import info.nightscout.androidaps.plugins.aps.events.EventOpenAPSUpdateGui import info.nightscout.androidaps.plugins.aps.events.EventOpenAPSUpdateGui
@ -20,7 +21,6 @@ import info.nightscout.androidaps.utils.extensions.plusAssign
import info.nightscout.androidaps.utils.resources.ResourceHelper import info.nightscout.androidaps.utils.resources.ResourceHelper
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.openapsama_fragment.*
import org.json.JSONArray import org.json.JSONArray
import org.json.JSONException import org.json.JSONException
import javax.inject.Inject import javax.inject.Inject
@ -35,15 +35,22 @@ class OpenAPSSMBFragment : DaggerFragment() {
@Inject lateinit var openAPSSMBPlugin: OpenAPSSMBPlugin @Inject lateinit var openAPSSMBPlugin: OpenAPSSMBPlugin
@Inject lateinit var dateUtil: DateUtil @Inject lateinit var dateUtil: DateUtil
private var _binding: OpenapsamaFragmentBinding? = null
// This property is only valid between onCreateView and
// onDestroyView.
private val binding get() = _binding!!
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?): View? { savedInstanceState: Bundle?): View {
return inflater.inflate(R.layout.openapsama_fragment, container, false) _binding = OpenapsamaFragmentBinding.inflate(inflater, container, false)
return binding.root
} }
override fun onViewCreated(view: View, savedInstanceState: Bundle?) { override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState) super.onViewCreated(view, savedInstanceState)
openapsma_run.setOnClickListener { binding.run.setOnClickListener {
openAPSSMBPlugin.invoke("OpenAPSSMB button", false) openAPSSMBPlugin.invoke("OpenAPSSMB button", false)
} }
} }
@ -73,52 +80,58 @@ class OpenAPSSMBFragment : DaggerFragment() {
disposable.clear() disposable.clear()
} }
@Synchronized
override fun onDestroyView() {
super.onDestroyView()
_binding = null
}
@Synchronized @Synchronized
fun updateGUI() { fun updateGUI() {
if (openapsma_result == null) return if (_binding == null) return
openAPSSMBPlugin.lastAPSResult?.let { lastAPSResult -> openAPSSMBPlugin.lastAPSResult?.let { lastAPSResult ->
openapsma_result.text = JSONFormatter.format(lastAPSResult.json) binding.result.text = JSONFormatter.format(lastAPSResult.json)
openapsma_request.text = lastAPSResult.toSpanned() binding.request.text = lastAPSResult.toSpanned()
} }
openAPSSMBPlugin.lastDetermineBasalAdapterSMBJS?.let { determineBasalAdapterSMBJS -> openAPSSMBPlugin.lastDetermineBasalAdapterSMBJS?.let { determineBasalAdapterSMBJS ->
openapsma_glucosestatus.text = JSONFormatter.format(determineBasalAdapterSMBJS.glucoseStatusParam) binding.glucosestatus.text = JSONFormatter.format(determineBasalAdapterSMBJS.glucoseStatusParam)
openapsma_currenttemp.text = JSONFormatter.format(determineBasalAdapterSMBJS.currentTempParam) binding.currenttemp.text = JSONFormatter.format(determineBasalAdapterSMBJS.currentTempParam)
try { try {
val iobArray = JSONArray(determineBasalAdapterSMBJS.iobDataParam) val iobArray = JSONArray(determineBasalAdapterSMBJS.iobDataParam)
openapsma_iobdata.text = TextUtils.concat(resourceHelper.gs(R.string.array_of_elements, iobArray.length()) + "\n", JSONFormatter.format(iobArray.getString(0))) binding.iobdata.text = TextUtils.concat(resourceHelper.gs(R.string.array_of_elements, iobArray.length()) + "\n", JSONFormatter.format(iobArray.getString(0)))
} catch (e: JSONException) { } catch (e: JSONException) {
aapsLogger.error(LTag.APS, "Unhandled exception", e) aapsLogger.error(LTag.APS, "Unhandled exception", e)
@SuppressLint("SetTextI18n") @SuppressLint("SetTextI18n")
openapsma_iobdata.text = "JSONException see log for details" binding.iobdata.text = "JSONException see log for details"
} }
openapsma_profile.text = JSONFormatter.format(determineBasalAdapterSMBJS.profileParam) binding.profile.text = JSONFormatter.format(determineBasalAdapterSMBJS.profileParam)
openapsma_mealdata.text = JSONFormatter.format(determineBasalAdapterSMBJS.mealDataParam) binding.mealdata.text = JSONFormatter.format(determineBasalAdapterSMBJS.mealDataParam)
openapsma_scriptdebugdata.text = determineBasalAdapterSMBJS.scriptDebug binding.scriptdebugdata.text = determineBasalAdapterSMBJS.scriptDebug
openAPSSMBPlugin.lastAPSResult?.inputConstraints?.let { openAPSSMBPlugin.lastAPSResult?.inputConstraints?.let {
openapsma_constraints.text = it.getReasons(aapsLogger) binding.constraints.text = it.getReasons(aapsLogger)
} }
} }
if (openAPSSMBPlugin.lastAPSRun != 0L) { if (openAPSSMBPlugin.lastAPSRun != 0L) {
openapsma_lastrun.text = dateUtil.dateAndTimeString(openAPSSMBPlugin.lastAPSRun) binding.lastrun.text = dateUtil.dateAndTimeString(openAPSSMBPlugin.lastAPSRun)
} }
openAPSSMBPlugin.lastAutosensResult?.let { openAPSSMBPlugin.lastAutosensResult?.let {
openapsma_autosensdata.text = JSONFormatter.format(it.json()) binding.autosensdata.text = JSONFormatter.format(it.json())
} }
} }
@Synchronized @Synchronized
private fun updateResultGUI(text: String) { private fun updateResultGUI(text: String) {
if (openapsma_result == null) return if (_binding == null) return
openapsma_result.text = text binding.result.text = text
openapsma_glucosestatus.text = "" binding.glucosestatus.text = ""
openapsma_currenttemp.text = "" binding.currenttemp.text = ""
openapsma_iobdata.text = "" binding.iobdata.text = ""
openapsma_profile.text = "" binding.profile.text = ""
openapsma_mealdata.text = "" binding.mealdata.text = ""
openapsma_autosensdata.text = "" binding.autosensdata.text = ""
openapsma_scriptdebugdata.text = "" binding.scriptdebugdata.text = ""
openapsma_request.text = "" binding.request.text = ""
openapsma_lastrun.text = "" binding.lastrun.text = ""
} }
} }