TreatmentsBolusFragment -> jetpack

This commit is contained in:
Milos Kozak 2021-02-03 16:04:09 +01:00
parent 74ae51a9e0
commit 8a15fe12ba
3 changed files with 132 additions and 133 deletions

View file

@ -5,23 +5,23 @@ import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.cardview.widget.CardView
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import dagger.android.support.DaggerFragment
import info.nightscout.androidaps.R
import info.nightscout.androidaps.databinding.TreatmentsBolusFragmentBinding
import info.nightscout.androidaps.databinding.TreatmentsBolusItemBinding
import info.nightscout.androidaps.db.Source
import info.nightscout.androidaps.db.Treatment
import info.nightscout.androidaps.dialogs.WizardInfoDialog
import info.nightscout.androidaps.events.EventTreatmentChange
import info.nightscout.androidaps.plugins.bus.RxBusWrapper
import info.nightscout.androidaps.interfaces.ProfileFunction
import info.nightscout.androidaps.plugins.bus.RxBusWrapper
import info.nightscout.androidaps.plugins.general.nsclient.NSUpload
import info.nightscout.androidaps.plugins.general.nsclient.UploadQueue
import info.nightscout.androidaps.plugins.general.nsclient.events.EventNSClientRestart
import info.nightscout.androidaps.plugins.iob.iobCobCalculator.events.EventAutosensCalculationFinished
import info.nightscout.androidaps.db.Treatment
import info.nightscout.androidaps.plugins.treatments.TreatmentsPlugin
import info.nightscout.androidaps.dialogs.WizardInfoDialog
import info.nightscout.androidaps.plugins.treatments.fragments.TreatmentsBolusFragment.RecyclerViewAdapter.TreatmentsViewHolder
import info.nightscout.androidaps.utils.DateUtil
import info.nightscout.androidaps.utils.FabricPrivacy
@ -31,10 +31,10 @@ import info.nightscout.androidaps.utils.resources.ResourceHelper
import info.nightscout.androidaps.utils.sharedPreferences.SP
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.disposables.CompositeDisposable
import kotlinx.android.synthetic.main.treatments_bolus_fragment.*
import javax.inject.Inject
class TreatmentsBolusFragment : DaggerFragment() {
private val disposable = CompositeDisposable()
@Inject lateinit var rxBus: RxBusWrapper
@ -48,25 +48,29 @@ class TreatmentsBolusFragment : DaggerFragment() {
@Inject lateinit var dateUtil: DateUtil
@Inject lateinit var buildHelper: BuildHelper
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?): View? {
return inflater.inflate(R.layout.treatments_bolus_fragment, container, false)
}
private var _binding: TreatmentsBolusFragmentBinding? = null
// This property is only valid between onCreateView and
// onDestroyView.
private val binding get() = _binding!!
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View =
TreatmentsBolusFragmentBinding.inflate(inflater, container, false).also { _binding = it }.root
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
treatments_recyclerview.setHasFixedSize(true)
treatments_recyclerview.layoutManager = LinearLayoutManager(view.context)
treatments_recyclerview.adapter = RecyclerViewAdapter(treatmentsPlugin.treatmentsFromHistory)
treatments_reshreshfromnightscout.setOnClickListener {
binding.recyclerview.setHasFixedSize(true)
binding.recyclerview.layoutManager = LinearLayoutManager(view.context)
binding.recyclerview.adapter = RecyclerViewAdapter(treatmentsPlugin.treatmentsFromHistory)
binding.reshreshFromNightscout.setOnClickListener {
activity?.let { activity ->
OKDialog.showConfirmation(activity, resourceHelper.gs(R.string.refresheventsfromnightscout) + "?", Runnable {
OKDialog.showConfirmation(activity, resourceHelper.gs(R.string.refresheventsfromnightscout) + "?") {
treatmentsPlugin.service.resetTreatments()
rxBus.send(EventNSClientRestart())
})
}
}
treatments_delete_future_treatments.setOnClickListener {
}
binding.deleteFutureTreatments.setOnClickListener {
activity?.let { activity ->
OKDialog.showConfirmation(activity, resourceHelper.gs(R.string.overview_treatment_label), resourceHelper.gs(R.string.deletefuturetreatments) + "?", Runnable {
val futureTreatments = treatmentsPlugin.service.getTreatmentDataFromTime(DateUtil.now() + 1000, true)
@ -82,10 +86,11 @@ class TreatmentsBolusFragment : DaggerFragment() {
}
}
val nsUploadOnly = sp.getBoolean(R.string.key_ns_upload_only, true) || !buildHelper.isEngineeringMode()
if (nsUploadOnly) treatments_reshreshfromnightscout.visibility = View.GONE
if (nsUploadOnly) binding.reshreshFromNightscout.visibility = View.GONE
}
@Synchronized override fun onResume() {
@Synchronized
override fun onResume() {
super.onResume()
disposable.add(rxBus
.toObservable(EventTreatmentChange::class.java)
@ -100,12 +105,20 @@ class TreatmentsBolusFragment : DaggerFragment() {
updateGui()
}
@Synchronized override fun onPause() {
@Synchronized
override fun onPause() {
super.onPause()
disposable.clear()
}
@Synchronized
override fun onDestroyView() {
super.onDestroyView()
_binding = null
}
inner class RecyclerViewAdapter internal constructor(var treatments: List<Treatment>) : RecyclerView.Adapter<TreatmentsViewHolder>() {
override fun onCreateViewHolder(viewGroup: ViewGroup, viewType: Int): TreatmentsViewHolder {
val v = LayoutInflater.from(viewGroup.context).inflate(R.layout.treatments_bolus_item, viewGroup, false)
return TreatmentsViewHolder(v)
@ -114,41 +127,32 @@ class TreatmentsBolusFragment : DaggerFragment() {
override fun onBindViewHolder(holder: TreatmentsViewHolder, position: Int) {
val profile = profileFunction.getProfile() ?: return
val t = treatments[position]
holder.date.text = dateUtil.dateAndTimeString(t.date)
holder.insulin.text = resourceHelper.gs(R.string.formatinsulinunits, t.insulin)
holder.carbs.text = resourceHelper.gs(R.string.format_carbs, t.carbs.toInt())
holder.binding.date.text = dateUtil.dateAndTimeString(t.date)
holder.binding.insulin.text = resourceHelper.gs(R.string.formatinsulinunits, t.insulin)
holder.binding.carbs.text = resourceHelper.gs(R.string.format_carbs, t.carbs.toInt())
val iob = t.iobCalc(System.currentTimeMillis(), profile.dia)
holder.iob.text = resourceHelper.gs(R.string.formatinsulinunits, iob.iobContrib)
holder.mealOrCorrection.text = if (t.isSMB) "SMB" else if (t.mealBolus) resourceHelper.gs(R.string.mealbolus) else resourceHelper.gs(R.string.correctionbous)
holder.ph.visibility = if (t.source == Source.PUMP) View.VISIBLE else View.GONE
holder.ns.visibility = if (NSUpload.isIdValid(t._id)) View.VISIBLE else View.GONE
holder.invalid.visibility = if (t.isValid) View.GONE else View.VISIBLE
if (iob.iobContrib != 0.0) holder.iob.setTextColor(resourceHelper.gc(R.color.colorActive)) else holder.iob.setTextColor(holder.carbs.currentTextColor)
if (t.date > DateUtil.now()) holder.date.setTextColor(resourceHelper.gc(R.color.colorScheduled)) else holder.date.setTextColor(holder.carbs.currentTextColor)
holder.remove.tag = t
holder.calculation.tag = t
holder.calculation.visibility = if (t.getBoluscalc() == null) View.INVISIBLE else View.VISIBLE
holder.binding.iob.text = resourceHelper.gs(R.string.formatinsulinunits, iob.iobContrib)
holder.binding.mealOrCorrection.text = if (t.isSMB) "SMB" else if (t.mealBolus) resourceHelper.gs(R.string.mealbolus) else resourceHelper.gs(R.string.correctionbous)
holder.binding.pump.visibility = if (t.source == Source.PUMP) View.VISIBLE else View.GONE
holder.binding.ns.visibility = if (NSUpload.isIdValid(t._id)) View.VISIBLE else View.GONE
holder.binding.invalid.visibility = if (t.isValid) View.GONE else View.VISIBLE
if (iob.iobContrib != 0.0) holder.binding.iob.setTextColor(resourceHelper.gc(R.color.colorActive)) else holder.binding.iob.setTextColor(holder.binding.carbs.currentTextColor)
if (t.date > DateUtil.now()) holder.binding.date.setTextColor(resourceHelper.gc(R.color.colorScheduled)) else holder.binding.date.setTextColor(holder.binding.carbs.currentTextColor)
holder.binding.remove.tag = t
holder.binding.calculation.tag = t
holder.binding.calculation.visibility = if (t.getBoluscalc() == null) View.INVISIBLE else View.VISIBLE
}
override fun getItemCount(): Int {
return treatments.size
}
inner class TreatmentsViewHolder internal constructor(itemView: View) : RecyclerView.ViewHolder(itemView) {
var cv: CardView = itemView.findViewById(R.id.treatments_cardview)
var date: TextView = itemView.findViewById(R.id.treatments_date)
var insulin: TextView = itemView.findViewById(R.id.treatments_insulin)
var carbs: TextView = itemView.findViewById(R.id.treatments_carbs)
var iob: TextView = itemView.findViewById(R.id.treatments_iob)
var mealOrCorrection: TextView = itemView.findViewById(R.id.treatments_mealorcorrection)
var remove: TextView = itemView.findViewById(R.id.treatments_remove)
var calculation: TextView = itemView.findViewById(R.id.treatments_calculation)
var ph: TextView = itemView.findViewById(R.id.pump_sign)
var ns: TextView = itemView.findViewById(R.id.ns_sign)
var invalid: TextView = itemView.findViewById(R.id.invalid_sign)
inner class TreatmentsViewHolder internal constructor(view: View) : RecyclerView.ViewHolder(view) {
val binding = TreatmentsBolusItemBinding.bind(view)
init {
calculation.setOnClickListener {
binding.calculation.setOnClickListener {
val treatment = it.tag as Treatment
if (treatment.getBoluscalc() != null) {
val wizardDialog = WizardInfoDialog()
@ -156,8 +160,8 @@ class TreatmentsBolusFragment : DaggerFragment() {
wizardDialog.show(childFragmentManager, "WizardInfoDialog")
}
}
calculation.paintFlags = calculation.paintFlags or Paint.UNDERLINE_TEXT_FLAG
remove.setOnClickListener {
binding.calculation.paintFlags = binding.calculation.paintFlags or Paint.UNDERLINE_TEXT_FLAG
binding.remove.setOnClickListener {
val treatment = it.tag as Treatment? ?: return@setOnClickListener
activity?.let { activity ->
val text = resourceHelper.gs(R.string.configbuilder_insulin) + ": " +
@ -179,20 +183,21 @@ class TreatmentsBolusFragment : DaggerFragment() {
})
}
}
remove.paintFlags = remove.paintFlags or Paint.UNDERLINE_TEXT_FLAG
binding.remove.paintFlags = binding.remove.paintFlags or Paint.UNDERLINE_TEXT_FLAG
}
}
}
private fun updateGui() {
treatments_recyclerview?.swapAdapter(RecyclerViewAdapter(treatmentsPlugin.treatmentsFromHistory), false)
if (_binding == null) return
binding.recyclerview.swapAdapter(RecyclerViewAdapter(treatmentsPlugin.treatmentsFromHistory), false)
if (treatmentsPlugin.lastCalculationTreatments != null) {
treatments_iobtotal?.text = resourceHelper.gs(R.string.formatinsulinunits, treatmentsPlugin.lastCalculationTreatments.iob)
treatments_iobactivitytotal?.text = resourceHelper.gs(R.string.formatinsulinunits, treatmentsPlugin.lastCalculationTreatments.activity)
binding.iobTotal.text = resourceHelper.gs(R.string.formatinsulinunits, treatmentsPlugin.lastCalculationTreatments.iob)
binding.iobActivityTotal.text = resourceHelper.gs(R.string.formatinsulinunits, treatmentsPlugin.lastCalculationTreatments.activity)
}
if (treatmentsPlugin.service.getTreatmentDataFromTime(DateUtil.now() + 1000, true).isNotEmpty())
treatments_delete_future_treatments?.visibility = View.VISIBLE
binding.deleteFutureTreatments.visibility = View.VISIBLE
else
treatments_delete_future_treatments?.visibility = View.GONE
binding.deleteFutureTreatments.visibility = View.GONE
}
}

View file

@ -1,48 +1,44 @@
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".plugins.treatments.fragments.TreatmentsBolusFragment">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:paddingBottom="10dp"
android:paddingTop="10dp">
android:paddingTop="10dp"
android:paddingBottom="10dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="10dp"
android:paddingStart="10dp"
android:text="@string/treatments_iobtotal_label_string"
android:textAppearance="?android:attr/textAppearanceSmall" />
<TextView
android:id="@+id/treatments_iobtotal"
android:id="@+id/iob_total"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="10dp"
android:paddingStart="10dp"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textStyle="bold" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="10dp"
android:paddingStart="10dp"
android:text="@string/treatments_iobactivitytotal_label_string"
android:textAppearance="?android:attr/textAppearanceSmall" />
<TextView
android:id="@+id/treatments_iobactivitytotal"
android:id="@+id/iob_activity_total"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="10dp"
android:paddingStart="10dp"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textStyle="bold" />
@ -54,7 +50,7 @@
android:orientation="horizontal">
<Button
android:id="@+id/treatments_reshreshfromnightscout"
android:id="@+id/reshresh_from_nightscout"
style="?android:attr/buttonStyle"
android:layout_width="match_parent"
android:layout_height="match_parent"
@ -63,7 +59,7 @@
android:text="@string/nav_refreshtreatments" />
<Button
android:id="@+id/treatments_delete_future_treatments"
android:id="@+id/delete_future_treatments"
style="?android:attr/buttonStyle"
android:layout_width="match_parent"
android:layout_height="match_parent"
@ -74,12 +70,10 @@
</LinearLayout>
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/treatments_recyclerview"
android:id="@+id/recyclerview"
android:layout_width="match_parent"
android:layout_height="match_parent">
</androidx.recyclerview.widget.RecyclerView>
</LinearLayout>
</FrameLayout>

View file

@ -27,13 +27,13 @@
android:text="{fa-clock-o}" />
<TextView
android:id="@+id/treatments_date"
android:id="@+id/date"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="5dp" />
<TextView
android:id="@+id/treatments_mealorcorrection"
android:id="@+id/meal_or_correction"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="10dp"
@ -58,7 +58,7 @@
android:text="@string/treatments_insulin_label_string" />
<TextView
android:id="@+id/treatments_insulin"
android:id="@+id/insulin"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
@ -74,7 +74,7 @@
android:text="@string/treatments_carbs_label_string" />
<TextView
android:id="@+id/treatments_carbs"
android:id="@+id/carbs"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
@ -88,7 +88,7 @@
android:text="" />
<TextView
android:id="@+id/pump_sign"
android:id="@+id/pump"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="10dp"
@ -96,7 +96,7 @@
android:textColor="@color/colorSetTempButton" />
<TextView
android:id="@+id/ns_sign"
android:id="@+id/ns"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="10dp"
@ -104,7 +104,7 @@
android:textColor="@color/colorSetTempButton" />
<TextView
android:id="@+id/invalid_sign"
android:id="@+id/invalid"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="10dp"
@ -127,7 +127,7 @@
android:text="@string/treatments_iob_label_string" />
<TextView
android:id="@+id/treatments_iob"
android:id="@+id/iob"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
@ -142,7 +142,7 @@
android:textStyle="bold" />
<TextView
android:id="@+id/treatments_calculation"
android:id="@+id/calculation"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="10dp"
@ -153,7 +153,7 @@
android:textColor="@color/colorCalculatorButton" />
<TextView
android:id="@+id/treatments_remove"
android:id="@+id/remove"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="10dp"