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 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()
}
}

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