NSProfileFragment -> jetpack

This commit is contained in:
Milos Kozak 2021-01-21 12:48:30 +01:00
parent c6438f5df9
commit 99bf1567e2
3 changed files with 116 additions and 97 deletions

View file

@ -8,8 +8,9 @@ import android.widget.AdapterView
import android.widget.ArrayAdapter
import dagger.android.support.DaggerFragment
import info.nightscout.androidaps.R
import info.nightscout.androidaps.plugins.bus.RxBusWrapper
import info.nightscout.androidaps.databinding.NsprofileFragmentBinding
import info.nightscout.androidaps.interfaces.ProfileFunction
import info.nightscout.androidaps.plugins.bus.RxBusWrapper
import info.nightscout.androidaps.plugins.profile.ns.events.EventNSProfileUpdateGUI
import info.nightscout.androidaps.plugins.treatments.TreatmentsPlugin
import info.nightscout.androidaps.utils.DateUtil
@ -20,9 +21,6 @@ import info.nightscout.androidaps.utils.extensions.plusAssign
import info.nightscout.androidaps.utils.resources.ResourceHelper
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.disposables.CompositeDisposable
import kotlinx.android.synthetic.main.close.*
import kotlinx.android.synthetic.main.nsprofile_fragment.*
import kotlinx.android.synthetic.main.profileviewer_fragment.*
import javax.inject.Inject
class NSProfileFragment : DaggerFragment() {
@ -36,18 +34,25 @@ class NSProfileFragment : DaggerFragment() {
private var disposable: CompositeDisposable = CompositeDisposable()
private var _binding: NsprofileFragmentBinding? = 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? {
return inflater.inflate(R.layout.nsprofile_fragment, container, false)
savedInstanceState: Bundle?): View {
_binding = NsprofileFragmentBinding.inflate(inflater, container, false)
return binding.root
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
close.visibility = View.GONE // not needed for fragment
binding.profileviewer.closeLayout.close.visibility = View.GONE // not needed for fragment
nsprofile_profileswitch.setOnClickListener {
val name = nsprofile_spinner.selectedItem?.toString() ?: ""
binding.profileswitch.setOnClickListener {
val name = binding.spinner.selectedItem?.toString() ?: ""
nsProfilePlugin.profile?.let { store ->
store.getSpecificProfile(name)?.let {
activity?.let { activity ->
@ -60,43 +65,46 @@ class NSProfileFragment : DaggerFragment() {
}
}
nsprofile_spinner.onItemSelectedListener = object : AdapterView.OnItemSelectedListener {
binding.spinner.onItemSelectedListener = object : AdapterView.OnItemSelectedListener {
override fun onNothingSelected(parent: AdapterView<*>?) {
profileview_invalidprofile.visibility = View.VISIBLE
profileview_noprofile.visibility = View.VISIBLE
profileview_units.text = ""
profileview_dia.text = ""
profileview_activeprofile.text = ""
profileview_ic.text = ""
profileview_isf.text = ""
profileview_basal.text = ""
profileview_basaltotal.text = ""
profileview_target.text = ""
nsprofile_profileswitch.visibility = View.GONE
if (_binding == null) return
binding.profileviewer.invalidprofile.visibility = View.VISIBLE
binding.profileviewer.noprofile.visibility = View.VISIBLE
binding.profileviewer.units.text = ""
binding.profileviewer.dia.text = ""
binding.profileviewer.activeprofile.text = ""
binding.profileviewer.ic.text = ""
binding.profileviewer.isf.text = ""
binding.profileviewer.basal.text = ""
binding.profileviewer.basaltotal.text = ""
binding.profileviewer.target.text = ""
binding.profileswitch.visibility = View.GONE
}
override fun onItemSelected(parent: AdapterView<*>?, view: View?, position: Int, id: Long) {
val name = nsprofile_spinner.getItemAtPosition(position).toString()
if (_binding == null) return
val name = binding.spinner.getItemAtPosition(position).toString()
nsprofile_profileswitch.visibility = View.GONE
binding.profileswitch.visibility = View.GONE
nsProfilePlugin.profile?.let { store ->
store.getSpecificProfile(name)?.let { profile ->
profileview_units.text = profile.units
profileview_dia.text = resourceHelper.gs(R.string.format_hours, profile.dia)
profileview_activeprofile.text = name
profileview_ic.text = profile.icList
profileview_isf.text = profile.isfList
profileview_basal.text = profile.basalList
profileview_basaltotal.text = String.format(resourceHelper.gs(R.string.profile_total), DecimalFormatter.to2Decimal(profile.baseBasalSum()))
profileview_target.text = profile.targetList
basal_graph.show(profile)
if (_binding == null) return
binding.profileviewer.units.text = profile.units
binding.profileviewer.dia.text = resourceHelper.gs(R.string.format_hours, profile.dia)
binding.profileviewer.activeprofile.text = name
binding.profileviewer.ic.text = profile.icList
binding.profileviewer.isf.text = profile.isfList
binding.profileviewer.basal.text = profile.basalList
binding.profileviewer.basaltotal.text = String.format(resourceHelper.gs(R.string.profile_total), DecimalFormatter.to2Decimal(profile.baseBasalSum()))
binding.profileviewer.target.text = profile.targetList
binding.profileviewer.basalGraph.show(profile)
if (profile.isValid("NSProfileFragment")) {
profileview_invalidprofile.visibility = View.GONE
nsprofile_profileswitch.visibility = View.VISIBLE
binding.profileviewer.invalidprofile.visibility = View.GONE
binding.profileswitch.visibility = View.VISIBLE
} else {
profileview_invalidprofile.visibility = View.VISIBLE
nsprofile_profileswitch.visibility = View.GONE
binding.profileviewer.invalidprofile.visibility = View.VISIBLE
binding.profileswitch.visibility = View.GONE
}
}
}
@ -120,21 +128,29 @@ class NSProfileFragment : DaggerFragment() {
disposable.clear()
}
@Synchronized
override fun onDestroyView() {
super.onDestroyView()
_binding = null
}
@Synchronized
fun updateGUI() {
if (profileview_noprofile == null) return
profileview_noprofile.visibility = View.VISIBLE
if (_binding == null) return
binding.profileviewer.noprofile.visibility = View.VISIBLE
nsProfilePlugin.profile?.let { profileStore ->
context?.let { context ->
val profileList = profileStore.getProfileList()
val adapter = ArrayAdapter(context!!, R.layout.spinner_centered, profileList)
nsprofile_spinner.adapter = adapter
val adapter = ArrayAdapter(context, R.layout.spinner_centered, profileList)
binding.spinner.adapter = adapter
// set selected to actual profile
for (p in profileList.indices) {
if (profileList[p] == profileFunction.getProfileName())
nsprofile_spinner.setSelection(p)
binding.spinner.setSelection(p)
}
binding.profileviewer.noprofile.visibility = View.GONE
}
profileview_noprofile.visibility = View.GONE
}
}
}

View file

@ -4,14 +4,14 @@
android:layout_height="match_parent">
<Spinner
android:id="@+id/nsprofile_spinner"
android:id="@+id/spinner"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginBottom="5dp" />
<Button
android:id="@+id/nsprofile_profileswitch"
android:id="@+id/profileswitch"
style="?android:attr/buttonStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
@ -20,6 +20,7 @@
android:textColor="@color/colorProfileSwitchButton" />
<include
android:id="@+id/profileviewer"
layout="@layout/profileviewer_fragment"
android:layout_width="match_parent"
android:layout_height="match_parent" />

View file

@ -10,7 +10,7 @@
android:orientation="vertical">
<TextView
android:id="@+id/profileview_invalidprofile"
android:id="@+id/invalidprofile"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
@ -21,7 +21,7 @@
android:visibility="gone" />
<TextView
android:id="@+id/profileview_noprofile"
android:id="@+id/noprofile"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
@ -48,20 +48,20 @@
<TextView
android:layout_width="5dp"
android:layout_height="wrap_content"
android:layout_weight="0"
android:gravity="center_horizontal"
android:layout_marginStart="2dp"
android:layout_marginEnd="2dp"
android:layout_weight="0"
android:gravity="center_horizontal"
android:text=":"
android:textSize="14sp" />
<TextView
android:id="@+id/profileview_activeprofile"
android:id="@+id/activeprofile"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="5dp"
android:layout_weight="1"
android:gravity="start"
android:layout_marginStart="5dp"
android:textColor="@android:color/white"
android:textSize="14sp" />
@ -78,7 +78,7 @@
android:visibility="gone" />
<LinearLayout
android:id="@+id/profileview_datelayout"
android:id="@+id/datelayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
@ -87,29 +87,29 @@
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginEnd="5dp"
android:layout_weight="2"
android:gravity="end"
android:layout_marginEnd="5dp"
android:text="@string/date"
android:textSize="14sp" />
<TextView
android:layout_width="5dp"
android:layout_height="wrap_content"
android:layout_weight="0"
android:gravity="center_horizontal"
android:layout_marginStart="2dp"
android:layout_marginEnd="2dp"
android:layout_weight="0"
android:gravity="center_horizontal"
android:text=":"
android:textSize="14sp" />
<TextView
android:id="@+id/profileview_date"
android:id="@+id/date"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="5dp"
android:layout_weight="1"
android:gravity="start"
android:layout_marginStart="5dp"
android:textColor="@android:color/white"
android:textSize="14sp" />
@ -132,29 +132,29 @@
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginEnd="5dp"
android:layout_weight="2"
android:gravity="end"
android:layout_marginEnd="5dp"
android:text="@string/units_label"
android:textSize="14sp" />
<TextView
android:layout_width="5dp"
android:layout_height="wrap_content"
android:layout_weight="0"
android:gravity="center_horizontal"
android:layout_marginStart="2dp"
android:layout_marginEnd="2dp"
android:layout_weight="0"
android:gravity="center_horizontal"
android:text=":"
android:textSize="14sp" />
<TextView
android:id="@+id/profileview_units"
android:id="@+id/units"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="5dp"
android:layout_weight="1"
android:gravity="start"
android:layout_marginStart="5dp"
android:textColor="@android:color/white"
android:textSize="14sp" />
@ -177,29 +177,29 @@
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginEnd="5dp"
android:layout_weight="2"
android:gravity="end"
android:layout_marginEnd="5dp"
android:text="@string/dia_label"
android:textSize="14sp" />
<TextView
android:layout_width="5dp"
android:layout_height="wrap_content"
android:layout_weight="0"
android:gravity="center_horizontal"
android:layout_marginStart="2dp"
android:layout_marginEnd="2dp"
android:layout_weight="0"
android:gravity="center_horizontal"
android:text=":"
android:textSize="14sp" />
<TextView
android:id="@+id/profileview_dia"
android:id="@+id/dia"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="5dp"
android:layout_weight="1"
android:gravity="start"
android:layout_marginStart="5dp"
android:textColor="@android:color/white"
android:textSize="14sp" />
@ -222,29 +222,29 @@
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginEnd="5dp"
android:layout_weight="2"
android:gravity="end"
android:layout_marginEnd="5dp"
android:text="@string/ic_label"
android:textSize="14sp" />
<TextView
android:layout_width="5dp"
android:layout_height="wrap_content"
android:layout_weight="0"
android:gravity="center_horizontal"
android:layout_marginStart="2dp"
android:layout_marginEnd="2dp"
android:layout_weight="0"
android:gravity="center_horizontal"
android:text=":"
android:textSize="14sp" />
<TextView
android:id="@+id/profileview_ic"
android:id="@+id/ic"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="5dp"
android:layout_weight="1"
android:gravity="start"
android:layout_marginStart="5dp"
android:textColor="@android:color/white"
android:textSize="14sp" />
@ -267,29 +267,29 @@
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginEnd="5dp"
android:layout_weight="2"
android:gravity="end"
android:layout_marginEnd="5dp"
android:text="@string/isf_label"
android:textSize="14sp" />
<TextView
android:layout_width="5dp"
android:layout_height="wrap_content"
android:layout_weight="0"
android:gravity="center_horizontal"
android:layout_marginStart="2dp"
android:layout_marginEnd="2dp"
android:layout_weight="0"
android:gravity="center_horizontal"
android:text=":"
android:textSize="14sp" />
<TextView
android:id="@+id/profileview_isf"
android:id="@+id/isf"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="5dp"
android:layout_weight="1"
android:gravity="start"
android:layout_marginStart="5dp"
android:textColor="@android:color/white"
android:textSize="14sp" />
@ -312,29 +312,29 @@
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginEnd="5dp"
android:layout_weight="2"
android:gravity="end"
android:layout_marginEnd="5dp"
android:text="@string/basal_label"
android:textSize="14sp" />
<TextView
android:layout_width="5dp"
android:layout_height="wrap_content"
android:layout_weight="0"
android:gravity="center_horizontal"
android:layout_marginStart="2dp"
android:layout_marginEnd="2dp"
android:layout_weight="0"
android:gravity="center_horizontal"
android:text=":"
android:textSize="14sp" />
<TextView
android:id="@+id/profileview_basal"
android:id="@+id/basal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="5dp"
android:layout_weight="1"
android:gravity="start"
android:layout_marginStart="5dp"
android:textColor="@android:color/white"
android:textSize="14sp" />
@ -348,29 +348,29 @@
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginEnd="5dp"
android:layout_weight="2"
android:gravity="end"
android:layout_marginEnd="5dp"
android:text=""
android:textSize="14sp" />
<TextView
android:layout_width="5dp"
android:layout_height="wrap_content"
android:layout_weight="0"
android:gravity="center_horizontal"
android:layout_marginStart="2dp"
android:layout_marginEnd="2dp"
android:layout_weight="0"
android:gravity="center_horizontal"
android:text=""
android:textSize="14sp" />
<TextView
android:id="@+id/profileview_basaltotal"
android:id="@+id/basaltotal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="17dp"
android:layout_weight="1"
android:gravity="start"
android:layout_marginStart="17dp"
android:textColor="@android:color/white"
android:textSize="14sp" />
@ -399,29 +399,29 @@
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginEnd="5dp"
android:layout_weight="2"
android:gravity="end"
android:layout_marginEnd="5dp"
android:text="@string/target_label"
android:textSize="14sp" />
<TextView
android:layout_width="5dp"
android:layout_height="wrap_content"
android:layout_weight="0"
android:gravity="center_horizontal"
android:layout_marginStart="2dp"
android:layout_marginEnd="2dp"
android:layout_weight="0"
android:gravity="center_horizontal"
android:text=":"
android:textSize="14sp" />
<TextView
android:id="@+id/profileview_target"
android:id="@+id/target"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="5dp"
android:layout_weight="1"
android:gravity="start"
android:layout_marginStart="5dp"
android:textColor="@android:color/white"
android:textSize="14sp" />
@ -437,14 +437,16 @@
android:background="@color/listdelimiter" />
<Button
android:id="@+id/profileview_reload"
android:id="@+id/reload"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="@string/reloadprofile"
android:visibility="gone" />
<include layout="@layout/close" />
<include
android:id="@+id/close_layout"
layout="@layout/close" />
</LinearLayout>