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,92 +1,86 @@
<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 <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="match_parent" android:layout_height="wrap_content"
android:orientation="vertical"> android:background="@color/defaultbackground"
app:alignContent="stretch"
app:alignItems="stretch"
app:flexDirection="row"
app:flexWrap="wrap"
app:justifyContent="center">
<com.google.android.flexbox.FlexboxLayout xmlns:app="http://schemas.android.com/apk/res-auto" <TextView
android:layout_width="match_parent" android:id="@+id/treatments"
android:layout_height="wrap_content" android:layout_width="wrap_content"
android:background="@color/defaultbackground" android:layout_height="30dp"
app:alignContent="stretch" android:layout_weight="1"
app:alignItems="stretch" android:gravity="center_vertical|center_horizontal"
app:flexDirection="row" android:paddingLeft="5dp"
app:flexWrap="wrap" android:paddingRight="5dp"
app:justifyContent="center"> android:text="@string/bolus" />
<TextView <TextView
android:id="@+id/treatments_treatments" 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"
android:gravity="center_vertical|center_horizontal" android:gravity="center_vertical|center_horizontal"
android:paddingLeft="5dp" android:paddingLeft="5dp"
android:paddingRight="5dp" android:paddingRight="5dp"
android:text="@string/bolus" /> android:text="@string/extended_bolus" />
<TextView <TextView
android:id="@+id/treatments_extendedboluses" 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"
android:gravity="center_vertical|center_horizontal" android:gravity="center_vertical|center_horizontal"
android:paddingLeft="5dp" android:paddingLeft="5dp"
android:paddingRight="5dp" android:paddingRight="5dp"
android:text="@string/extended_bolus" /> android:text="@string/tempbasal_label" />
<TextView <TextView
android:id="@+id/treatments_tempbasals" 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"
android:gravity="center_vertical|center_horizontal" android:gravity="center_vertical|center_horizontal"
android:paddingLeft="5dp" android:paddingLeft="5dp"
android:paddingRight="5dp" android:paddingRight="5dp"
android:text="@string/tempbasal_label" /> android:text="@string/careportal_temporarytarget" />
<TextView <TextView
android:id="@+id/treatments_temptargets" 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"
android:gravity="center_vertical|center_horizontal" android:gravity="center_vertical|center_horizontal"
android:paddingLeft="5dp" android:paddingLeft="5dp"
android:paddingRight="5dp" android:paddingRight="5dp"
android:text="@string/careportal_temporarytarget" /> android:text="@string/careportal_profileswitch" />
<TextView <TextView
android:id="@+id/treatments_profileswitches" 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"
android:gravity="center_vertical|center_horizontal" android:gravity="center_vertical|center_horizontal"
android:paddingLeft="5dp" android:paddingLeft="5dp"
android:paddingRight="5dp" android:paddingRight="5dp"
android:text="@string/careportal_profileswitch" /> android:text="@string/careportal" />
<TextView </com.google.android.flexbox.FlexboxLayout>
android:id="@+id/treatments_careportal"
android:layout_width="wrap_content"
android:layout_height="30dp"
android:layout_weight="1"
android:gravity="center_vertical|center_horizontal"
android:paddingLeft="5dp"
android:paddingRight="5dp"
android:text="@string/careportal" />
</com.google.android.flexbox.FlexboxLayout> <FrameLayout
android:id="@+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<FrameLayout </LinearLayout>
android:id="@+id/treatments_fragment_container"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
</FrameLayout>