TreatmentsFragment -> jetpack

This commit is contained in:
Milos Kozak 2021-02-03 15:52:43 +01:00
parent fab3c870d7
commit 74ae51a9e0
2 changed files with 108 additions and 104 deletions

View file

@ -8,19 +8,21 @@ import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentTransaction import androidx.fragment.app.FragmentTransaction
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.TreatmentsFragmentBinding
import info.nightscout.androidaps.events.EventExtendedBolusChange import info.nightscout.androidaps.events.EventExtendedBolusChange
import info.nightscout.androidaps.interfaces.ActivePluginProvider import info.nightscout.androidaps.interfaces.ActivePluginProvider
import info.nightscout.androidaps.plugins.bus.RxBusWrapper import info.nightscout.androidaps.plugins.bus.RxBusWrapper
import info.nightscout.androidaps.plugins.treatments.fragments.* import info.nightscout.androidaps.plugins.treatments.fragments.*
import info.nightscout.androidaps.utils.FabricPrivacy import info.nightscout.androidaps.utils.FabricPrivacy
import info.nightscout.androidaps.utils.extensions.plusAssign import info.nightscout.androidaps.utils.extensions.plusAssign
import info.nightscout.androidaps.utils.extensions.toVisibility
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.treatments_fragment.*
import javax.inject.Inject import javax.inject.Inject
class TreatmentsFragment : DaggerFragment() { class TreatmentsFragment : DaggerFragment() {
@Inject lateinit var rxBus: RxBusWrapper @Inject lateinit var rxBus: RxBusWrapper
@Inject lateinit var resourceHelper: ResourceHelper @Inject lateinit var resourceHelper: ResourceHelper
@Inject lateinit var fabricPrivacy: FabricPrivacy @Inject lateinit var fabricPrivacy: FabricPrivacy
@ -29,40 +31,44 @@ class TreatmentsFragment : DaggerFragment() {
private val disposable = CompositeDisposable() private val disposable = CompositeDisposable()
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, private var _binding: TreatmentsFragmentBinding? = null
savedInstanceState: Bundle?): View? {
return inflater.inflate(R.layout.treatments_fragment, container, false) // 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?) { override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState) super.onViewCreated(view, savedInstanceState)
treatments_treatments.setOnClickListener { binding.treatments.setOnClickListener {
setFragment(TreatmentsBolusFragment()) setFragment(TreatmentsBolusFragment())
setBackgroundColorOnSelected(it) setBackgroundColorOnSelected(it)
} }
treatments_extendedboluses.setOnClickListener { binding.extendedboluses.setOnClickListener {
setFragment(TreatmentsExtendedBolusesFragment()) setFragment(TreatmentsExtendedBolusesFragment())
setBackgroundColorOnSelected(it) setBackgroundColorOnSelected(it)
} }
treatments_tempbasals.setOnClickListener { binding.tempbasals.setOnClickListener {
setFragment(TreatmentsTemporaryBasalsFragment()) setFragment(TreatmentsTemporaryBasalsFragment())
setBackgroundColorOnSelected(it) setBackgroundColorOnSelected(it)
} }
treatments_temptargets.setOnClickListener { binding.temptargets.setOnClickListener {
setFragment(TreatmentsTempTargetFragment()) setFragment(TreatmentsTempTargetFragment())
setBackgroundColorOnSelected(it) setBackgroundColorOnSelected(it)
} }
treatments_profileswitches.setOnClickListener { binding.profileswitches.setOnClickListener {
setFragment(TreatmentsProfileSwitchFragment()) setFragment(TreatmentsProfileSwitchFragment())
setBackgroundColorOnSelected(it) setBackgroundColorOnSelected(it)
} }
treatments_careportal.setOnClickListener { binding.careportal.setOnClickListener {
setFragment(TreatmentsCareportalFragment()) setFragment(TreatmentsCareportalFragment())
setBackgroundColorOnSelected(it) setBackgroundColorOnSelected(it)
} }
setFragment(TreatmentsBolusFragment()) setFragment(TreatmentsBolusFragment())
setBackgroundColorOnSelected(treatments_treatments) setBackgroundColorOnSelected(binding.treatments)
} }
@Synchronized @Synchronized
@ -71,7 +77,7 @@ class TreatmentsFragment : DaggerFragment() {
disposable += rxBus disposable += rxBus
.toObservable(EventExtendedBolusChange::class.java) .toObservable(EventExtendedBolusChange::class.java)
.observeOn(AndroidSchedulers.mainThread()) .observeOn(AndroidSchedulers.mainThread())
.subscribe({ updateGui() }) { fabricPrivacy.logException(it) } .subscribe({ updateGui() }, fabricPrivacy::logException)
updateGui() updateGui()
} }
@ -81,28 +87,32 @@ class TreatmentsFragment : DaggerFragment() {
disposable.clear() disposable.clear()
} }
@Synchronized
override fun onDestroyView() {
super.onDestroyView()
_binding = null
}
private fun setFragment(selectedFragment: Fragment) { private fun setFragment(selectedFragment: Fragment) {
val ft = childFragmentManager.beginTransaction() childFragmentManager.beginTransaction()
ft.replace(R.id.treatments_fragment_container, selectedFragment) // f2_container is your FrameLayout container .replace(R.id.fragment_container, selectedFragment) // f2_container is your FrameLayout container
ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN) .setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN)
ft.addToBackStack(null) .addToBackStack(null)
ft.commit() .commit()
} }
private fun setBackgroundColorOnSelected(selected: View) { private fun setBackgroundColorOnSelected(selected: View) {
treatments_treatments.setBackgroundColor(resourceHelper.gc(R.color.defaultbackground)) binding.treatments.setBackgroundColor(resourceHelper.gc(R.color.defaultbackground))
treatments_extendedboluses.setBackgroundColor(resourceHelper.gc(R.color.defaultbackground)) binding.extendedboluses.setBackgroundColor(resourceHelper.gc(R.color.defaultbackground))
treatments_tempbasals.setBackgroundColor(resourceHelper.gc(R.color.defaultbackground)) binding.tempbasals.setBackgroundColor(resourceHelper.gc(R.color.defaultbackground))
treatments_temptargets.setBackgroundColor(resourceHelper.gc(R.color.defaultbackground)) binding.temptargets.setBackgroundColor(resourceHelper.gc(R.color.defaultbackground))
treatments_profileswitches.setBackgroundColor(resourceHelper.gc(R.color.defaultbackground)) binding.profileswitches.setBackgroundColor(resourceHelper.gc(R.color.defaultbackground))
treatments_careportal.setBackgroundColor(resourceHelper.gc(R.color.defaultbackground)) binding.careportal.setBackgroundColor(resourceHelper.gc(R.color.defaultbackground))
selected.setBackgroundColor(resourceHelper.gc(R.color.tabBgColorSelected)) selected.setBackgroundColor(resourceHelper.gc(R.color.tabBgColorSelected))
} }
private fun updateGui() { private fun updateGui() {
if (activePlugin.activePump.pumpDescription.isExtendedBolusCapable || treatmentsPlugin.extendedBolusesFromHistory.size() > 0) if (_binding == null) return
treatments_extendedboluses?.visibility = View.VISIBLE binding.extendedboluses.visibility = (activePlugin.activePump.pumpDescription.isExtendedBolusCapable || treatmentsPlugin.extendedBolusesFromHistory.size() > 0).toVisibility()
else
treatments_extendedboluses?.visibility = View.GONE
} }
} }

View file

@ -1,15 +1,11 @@
<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" xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="match_parent" android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".plugins.treatments.TreatmentsFragment"> tools:context=".plugins.treatments.TreatmentsFragment">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<com.google.android.flexbox.FlexboxLayout xmlns:app="http://schemas.android.com/apk/res-auto" <com.google.android.flexbox.FlexboxLayout xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
@ -21,7 +17,7 @@
app:justifyContent="center"> app:justifyContent="center">
<TextView <TextView
android:id="@+id/treatments_treatments" android:id="@+id/treatments"
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="30dp" android:layout_height="30dp"
android:layout_weight="1" android:layout_weight="1"
@ -31,7 +27,7 @@
android:text="@string/bolus" /> android:text="@string/bolus" />
<TextView <TextView
android:id="@+id/treatments_extendedboluses" android:id="@+id/extendedboluses"
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="30dp" android:layout_height="30dp"
android:layout_weight="1" android:layout_weight="1"
@ -41,7 +37,7 @@
android:text="@string/extended_bolus" /> android:text="@string/extended_bolus" />
<TextView <TextView
android:id="@+id/treatments_tempbasals" android:id="@+id/tempbasals"
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="30dp" android:layout_height="30dp"
android:layout_weight="1" android:layout_weight="1"
@ -51,7 +47,7 @@
android:text="@string/tempbasal_label" /> android:text="@string/tempbasal_label" />
<TextView <TextView
android:id="@+id/treatments_temptargets" android:id="@+id/temptargets"
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="30dp" android:layout_height="30dp"
android:layout_weight="1" android:layout_weight="1"
@ -61,7 +57,7 @@
android:text="@string/careportal_temporarytarget" /> android:text="@string/careportal_temporarytarget" />
<TextView <TextView
android:id="@+id/treatments_profileswitches" android:id="@+id/profileswitches"
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="30dp" android:layout_height="30dp"
android:layout_weight="1" android:layout_weight="1"
@ -71,7 +67,7 @@
android:text="@string/careportal_profileswitch" /> android:text="@string/careportal_profileswitch" />
<TextView <TextView
android:id="@+id/treatments_careportal" android:id="@+id/careportal"
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="30dp" android:layout_height="30dp"
android:layout_weight="1" android:layout_weight="1"
@ -83,10 +79,8 @@
</com.google.android.flexbox.FlexboxLayout> </com.google.android.flexbox.FlexboxLayout>
<FrameLayout <FrameLayout
android:id="@+id/treatments_fragment_container" android:id="@+id/fragment_container"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" /> android:layout_height="wrap_content" />
</LinearLayout> </LinearLayout>
</FrameLayout>