diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/treatments/fragments/TreatmentsBolusFragment.kt b/app/src/main/java/info/nightscout/androidaps/plugins/treatments/fragments/TreatmentsBolusFragment.kt index c4639114d4..5aa694b269 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/treatments/fragments/TreatmentsBolusFragment.kt +++ b/app/src/main/java/info/nightscout/androidaps/plugins/treatments/fragments/TreatmentsBolusFragment.kt @@ -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) : RecyclerView.Adapter() { + 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 } } \ No newline at end of file diff --git a/app/src/main/res/layout/treatments_bolus_fragment.xml b/app/src/main/res/layout/treatments_bolus_fragment.xml index 6ed831e84e..feb54e2752 100644 --- a/app/src/main/res/layout/treatments_bolus_fragment.xml +++ b/app/src/main/res/layout/treatments_bolus_fragment.xml @@ -1,85 +1,79 @@ - + android:layout_height="wrap_content" + android:orientation="horizontal" + android:paddingTop="10dp" + android:paddingBottom="10dp"> - + android:paddingStart="10dp" + android:text="@string/treatments_iobtotal_label_string" + android:textAppearance="?android:attr/textAppearanceSmall" /> - - - - - - - - - - - + android:paddingStart="10dp" + android:textAppearance="?android:attr/textAppearanceSmall" + android:textStyle="bold" /> -