From 74ae51a9e0261c0dcaf638d7a2f837ab0cf9930e Mon Sep 17 00:00:00 2001 From: Milos Kozak Date: Wed, 3 Feb 2021 15:52:43 +0100 Subject: [PATCH] TreatmentsFragment -> jetpack --- .../plugins/treatments/TreatmentsFragment.kt | 66 ++++---- .../main/res/layout/treatments_fragment.xml | 146 +++++++++--------- 2 files changed, 108 insertions(+), 104 deletions(-) diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/treatments/TreatmentsFragment.kt b/app/src/main/java/info/nightscout/androidaps/plugins/treatments/TreatmentsFragment.kt index ed60d6ac4b..50a11f4f3e 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/treatments/TreatmentsFragment.kt +++ b/app/src/main/java/info/nightscout/androidaps/plugins/treatments/TreatmentsFragment.kt @@ -8,19 +8,21 @@ import androidx.fragment.app.Fragment import androidx.fragment.app.FragmentTransaction import dagger.android.support.DaggerFragment import info.nightscout.androidaps.R +import info.nightscout.androidaps.databinding.TreatmentsFragmentBinding import info.nightscout.androidaps.events.EventExtendedBolusChange import info.nightscout.androidaps.interfaces.ActivePluginProvider import info.nightscout.androidaps.plugins.bus.RxBusWrapper import info.nightscout.androidaps.plugins.treatments.fragments.* import info.nightscout.androidaps.utils.FabricPrivacy import info.nightscout.androidaps.utils.extensions.plusAssign +import info.nightscout.androidaps.utils.extensions.toVisibility import info.nightscout.androidaps.utils.resources.ResourceHelper import io.reactivex.android.schedulers.AndroidSchedulers import io.reactivex.disposables.CompositeDisposable -import kotlinx.android.synthetic.main.treatments_fragment.* import javax.inject.Inject class TreatmentsFragment : DaggerFragment() { + @Inject lateinit var rxBus: RxBusWrapper @Inject lateinit var resourceHelper: ResourceHelper @Inject lateinit var fabricPrivacy: FabricPrivacy @@ -29,40 +31,44 @@ class TreatmentsFragment : DaggerFragment() { private val disposable = CompositeDisposable() - override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, - savedInstanceState: Bundle?): View? { - return inflater.inflate(R.layout.treatments_fragment, container, false) - } + private var _binding: TreatmentsFragmentBinding? = 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 = + TreatmentsFragmentBinding.inflate(inflater, container, false).also { _binding = it }.root override fun onViewCreated(view: View, savedInstanceState: Bundle?) { super.onViewCreated(view, savedInstanceState) - treatments_treatments.setOnClickListener { + binding.treatments.setOnClickListener { setFragment(TreatmentsBolusFragment()) setBackgroundColorOnSelected(it) } - treatments_extendedboluses.setOnClickListener { + binding.extendedboluses.setOnClickListener { setFragment(TreatmentsExtendedBolusesFragment()) setBackgroundColorOnSelected(it) } - treatments_tempbasals.setOnClickListener { + binding.tempbasals.setOnClickListener { setFragment(TreatmentsTemporaryBasalsFragment()) setBackgroundColorOnSelected(it) } - treatments_temptargets.setOnClickListener { + binding.temptargets.setOnClickListener { setFragment(TreatmentsTempTargetFragment()) setBackgroundColorOnSelected(it) } - treatments_profileswitches.setOnClickListener { + binding.profileswitches.setOnClickListener { setFragment(TreatmentsProfileSwitchFragment()) setBackgroundColorOnSelected(it) } - treatments_careportal.setOnClickListener { + binding.careportal.setOnClickListener { setFragment(TreatmentsCareportalFragment()) setBackgroundColorOnSelected(it) } setFragment(TreatmentsBolusFragment()) - setBackgroundColorOnSelected(treatments_treatments) + setBackgroundColorOnSelected(binding.treatments) } @Synchronized @@ -71,7 +77,7 @@ class TreatmentsFragment : DaggerFragment() { disposable += rxBus .toObservable(EventExtendedBolusChange::class.java) .observeOn(AndroidSchedulers.mainThread()) - .subscribe({ updateGui() }) { fabricPrivacy.logException(it) } + .subscribe({ updateGui() }, fabricPrivacy::logException) updateGui() } @@ -81,28 +87,32 @@ class TreatmentsFragment : DaggerFragment() { disposable.clear() } + @Synchronized + override fun onDestroyView() { + super.onDestroyView() + _binding = null + } + private fun setFragment(selectedFragment: Fragment) { - val ft = childFragmentManager.beginTransaction() - ft.replace(R.id.treatments_fragment_container, selectedFragment) // f2_container is your FrameLayout container - ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN) - ft.addToBackStack(null) - ft.commit() + childFragmentManager.beginTransaction() + .replace(R.id.fragment_container, selectedFragment) // f2_container is your FrameLayout container + .setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN) + .addToBackStack(null) + .commit() } private fun setBackgroundColorOnSelected(selected: View) { - treatments_treatments.setBackgroundColor(resourceHelper.gc(R.color.defaultbackground)) - treatments_extendedboluses.setBackgroundColor(resourceHelper.gc(R.color.defaultbackground)) - treatments_tempbasals.setBackgroundColor(resourceHelper.gc(R.color.defaultbackground)) - treatments_temptargets.setBackgroundColor(resourceHelper.gc(R.color.defaultbackground)) - treatments_profileswitches.setBackgroundColor(resourceHelper.gc(R.color.defaultbackground)) - treatments_careportal.setBackgroundColor(resourceHelper.gc(R.color.defaultbackground)) + binding.treatments.setBackgroundColor(resourceHelper.gc(R.color.defaultbackground)) + binding.extendedboluses.setBackgroundColor(resourceHelper.gc(R.color.defaultbackground)) + binding.tempbasals.setBackgroundColor(resourceHelper.gc(R.color.defaultbackground)) + binding.temptargets.setBackgroundColor(resourceHelper.gc(R.color.defaultbackground)) + binding.profileswitches.setBackgroundColor(resourceHelper.gc(R.color.defaultbackground)) + binding.careportal.setBackgroundColor(resourceHelper.gc(R.color.defaultbackground)) selected.setBackgroundColor(resourceHelper.gc(R.color.tabBgColorSelected)) } private fun updateGui() { - if (activePlugin.activePump.pumpDescription.isExtendedBolusCapable || treatmentsPlugin.extendedBolusesFromHistory.size() > 0) - treatments_extendedboluses?.visibility = View.VISIBLE - else - treatments_extendedboluses?.visibility = View.GONE + if (_binding == null) return + binding.extendedboluses.visibility = (activePlugin.activePump.pumpDescription.isExtendedBolusCapable || treatmentsPlugin.extendedBolusesFromHistory.size() > 0).toVisibility() } } \ No newline at end of file diff --git a/app/src/main/res/layout/treatments_fragment.xml b/app/src/main/res/layout/treatments_fragment.xml index 316c9ea7c6..0f1954a983 100644 --- a/app/src/main/res/layout/treatments_fragment.xml +++ b/app/src/main/res/layout/treatments_fragment.xml @@ -1,92 +1,86 @@ - - + android:layout_height="wrap_content" + android:background="@color/defaultbackground" + app:alignContent="stretch" + app:alignItems="stretch" + app:flexDirection="row" + app:flexWrap="wrap" + app:justifyContent="center"> - + - + - + - + - + - + - + - + - - - - - +