From 1fbc6d24cbc2a4b2b5e30989d43bedab9f8df89b Mon Sep 17 00:00:00 2001 From: Andries Smit Date: Wed, 6 Apr 2022 14:20:32 +0200 Subject: [PATCH] chore: convert treamtents buttons to tabs --- .../activities/TreatmentsActivity.kt | 76 ++++++----------- .../main/res/layout/treatments_fragment.xml | 84 ++++--------------- 2 files changed, 44 insertions(+), 116 deletions(-) diff --git a/app/src/main/java/info/nightscout/androidaps/activities/TreatmentsActivity.kt b/app/src/main/java/info/nightscout/androidaps/activities/TreatmentsActivity.kt index 53659297d7..3b88ff8770 100644 --- a/app/src/main/java/info/nightscout/androidaps/activities/TreatmentsActivity.kt +++ b/app/src/main/java/info/nightscout/androidaps/activities/TreatmentsActivity.kt @@ -2,12 +2,14 @@ package info.nightscout.androidaps.activities import android.os.Bundle import android.view.MenuItem -import android.view.View import androidx.fragment.app.Fragment import androidx.fragment.app.FragmentTransaction +import com.google.android.material.tabs.TabLayout +import com.google.android.material.tabs.TabLayout.OnTabSelectedListener import info.nightscout.androidaps.R import info.nightscout.androidaps.activities.fragments.* import info.nightscout.androidaps.databinding.TreatmentsFragmentBinding +import info.nightscout.androidaps.extensions.toVisibility import info.nightscout.androidaps.interfaces.ActivePlugin import info.nightscout.androidaps.utils.buildHelper.BuildHelper import javax.inject.Inject @@ -23,49 +25,34 @@ class TreatmentsActivity : NoSplashAppCompatActivity() { super.onCreate(savedInstanceState) binding = TreatmentsFragmentBinding.inflate(layoutInflater) setContentView(binding.root) - //binding.tempBasals.visibility = buildHelper.isEngineeringMode().toVisibility() - //binding.extendedBoluses.visibility = (buildHelper.isEngineeringMode() && !activePlugin.activePump.isFakingTempsByExtendedBoluses).toVisibility() + + // Use index, TabItems crashes with an id + val useFakeTempBasal = activePlugin.activePump.isFakingTempsByExtendedBoluses + binding.treatmentsTabs.getTabAt(1)?.view?.visibility = useFakeTempBasal.toVisibility() - binding.treatments.setOnClickListener { - setFragment(TreatmentsBolusCarbsFragment()) - setBackgroundColorOnSelected(it) - supportActionBar?.title = rh.gs(R.string.carbs_and_bolus) - } - binding.extendedBoluses.setOnClickListener { - setFragment(TreatmentsExtendedBolusesFragment()) - setBackgroundColorOnSelected(it) - supportActionBar?.title = rh.gs(R.string.extended_bolus) - } - binding.tempBasals.setOnClickListener { - setFragment(TreatmentsTemporaryBasalsFragment()) - setBackgroundColorOnSelected(it) - supportActionBar?.title = rh.gs(R.string.tempbasal_label) - } - binding.tempTargets.setOnClickListener { - setFragment(TreatmentsTempTargetFragment()) - setBackgroundColorOnSelected(it) - supportActionBar?.title = rh.gs(R.string.tempt_targets) - } - binding.profileSwitches.setOnClickListener { - setFragment(TreatmentsProfileSwitchFragment()) - setBackgroundColorOnSelected(it) - supportActionBar?.title = rh.gs(R.string.profile_changes) - } - binding.careportal.setOnClickListener { - setFragment(TreatmentsCareportalFragment()) - setBackgroundColorOnSelected(it) - supportActionBar?.title = rh.gs(R.string.careportal) - } - binding.userentry.setOnClickListener { - setFragment(TreatmentsUserEntryFragment()) - setBackgroundColorOnSelected(it) - supportActionBar?.title = rh.gs(R.string.user_action) - } setFragment(TreatmentsBolusCarbsFragment()) - setBackgroundColorOnSelected(binding.treatments) setSupportActionBar(binding.toolbar) supportActionBar?.setDisplayHomeAsUpEnabled(true) supportActionBar?.title = rh.gs(R.string.carbs_and_bolus) + + binding.treatmentsTabs.addOnTabSelectedListener(object : OnTabSelectedListener { + override fun onTabSelected(tab: TabLayout.Tab) { + val fragment = when (tab.position) { + 0 -> TreatmentsBolusCarbsFragment::class.java + 1 -> TreatmentsExtendedBolusesFragment::class.java + 2 -> TreatmentsTemporaryBasalsFragment::class.java + 3 -> TreatmentsTempTargetFragment::class.java + 4 -> TreatmentsProfileSwitchFragment::class.java + 5 -> TreatmentsCareportalFragment::class.java + else -> TreatmentsUserEntryFragment::class.java + } + setFragment(fragment.newInstance()) + supportActionBar?.title = tab.contentDescription + } + + override fun onTabUnselected(tab: TabLayout.Tab) {} + override fun onTabReselected(tab: TabLayout.Tab) {} + }) } override fun onOptionsItemSelected(item: MenuItem): Boolean { @@ -86,15 +73,4 @@ class TreatmentsActivity : NoSplashAppCompatActivity() { .commit() } - private fun setBackgroundColorOnSelected(selected: View) { - binding.treatments.setBackgroundColor(rh.gac(this, R.attr.defaultbackground)) - binding.extendedBoluses.setBackgroundColor(rh.gac(this, R.attr.defaultbackground)) - binding.tempBasals.setBackgroundColor(rh.gac(this, R.attr.defaultbackground)) - binding.tempTargets.setBackgroundColor(rh.gac(this, R.attr.defaultbackground)) - binding.profileSwitches.setBackgroundColor(rh.gac(this, R.attr.defaultbackground)) - binding.careportal.setBackgroundColor(rh.gac(this, R.attr.defaultbackground)) - binding.userentry.setBackgroundColor(rh.gac(this, R.attr.defaultbackground)) - selected.setBackgroundColor(rh.gac(this, R.attr.tabBgColorSelected)) - } - } diff --git a/app/src/main/res/layout/treatments_fragment.xml b/app/src/main/res/layout/treatments_fragment.xml index 91046fbe03..aec32c8e92 100644 --- a/app/src/main/res/layout/treatments_fragment.xml +++ b/app/src/main/res/layout/treatments_fragment.xml @@ -1,5 +1,4 @@ - + android:layout_height="wrap_content"> - + android:icon="@drawable/ic_cp_bolus_carbs" /> - + android:icon="@drawable/ic_actions_startextbolus" /> - + android:icon="@drawable/ic_actions_starttempbasal" /> - + android:icon="@drawable/ic_temptarget_high" /> - + android:icon="@drawable/ic_actions_profileswitch" /> - + android:icon="@drawable/ic_cp_note" /> - + android:icon="@drawable/ic_danar_useropt" /> - +