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 android.widget.ArrayAdapter
import dagger.android.support.DaggerFragment import dagger.android.support.DaggerFragment
import info.nightscout.androidaps.R 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.interfaces.ProfileFunction
import info.nightscout.androidaps.plugins.bus.RxBusWrapper
import info.nightscout.androidaps.plugins.profile.ns.events.EventNSProfileUpdateGUI import info.nightscout.androidaps.plugins.profile.ns.events.EventNSProfileUpdateGUI
import info.nightscout.androidaps.plugins.treatments.TreatmentsPlugin import info.nightscout.androidaps.plugins.treatments.TreatmentsPlugin
import info.nightscout.androidaps.utils.DateUtil 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 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.close.*
import kotlinx.android.synthetic.main.nsprofile_fragment.*
import kotlinx.android.synthetic.main.profileviewer_fragment.*
import javax.inject.Inject import javax.inject.Inject
class NSProfileFragment : DaggerFragment() { class NSProfileFragment : DaggerFragment() {
@ -36,18 +34,25 @@ class NSProfileFragment : DaggerFragment() {
private var disposable: CompositeDisposable = CompositeDisposable() 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?, override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?): View? { savedInstanceState: Bundle?): View {
return inflater.inflate(R.layout.nsprofile_fragment, container, false) _binding = NsprofileFragmentBinding.inflate(inflater, container, false)
return binding.root
} }
override fun onViewCreated(view: View, savedInstanceState: Bundle?) { override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState) 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 { binding.profileswitch.setOnClickListener {
val name = nsprofile_spinner.selectedItem?.toString() ?: "" val name = binding.spinner.selectedItem?.toString() ?: ""
nsProfilePlugin.profile?.let { store -> nsProfilePlugin.profile?.let { store ->
store.getSpecificProfile(name)?.let { store.getSpecificProfile(name)?.let {
activity?.let { activity -> 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<*>?) { override fun onNothingSelected(parent: AdapterView<*>?) {
profileview_invalidprofile.visibility = View.VISIBLE if (_binding == null) return
profileview_noprofile.visibility = View.VISIBLE binding.profileviewer.invalidprofile.visibility = View.VISIBLE
profileview_units.text = "" binding.profileviewer.noprofile.visibility = View.VISIBLE
profileview_dia.text = "" binding.profileviewer.units.text = ""
profileview_activeprofile.text = "" binding.profileviewer.dia.text = ""
profileview_ic.text = "" binding.profileviewer.activeprofile.text = ""
profileview_isf.text = "" binding.profileviewer.ic.text = ""
profileview_basal.text = "" binding.profileviewer.isf.text = ""
profileview_basaltotal.text = "" binding.profileviewer.basal.text = ""
profileview_target.text = "" binding.profileviewer.basaltotal.text = ""
nsprofile_profileswitch.visibility = View.GONE binding.profileviewer.target.text = ""
binding.profileswitch.visibility = View.GONE
} }
override fun onItemSelected(parent: AdapterView<*>?, view: View?, position: Int, id: Long) { 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 -> nsProfilePlugin.profile?.let { store ->
store.getSpecificProfile(name)?.let { profile -> store.getSpecificProfile(name)?.let { profile ->
profileview_units.text = profile.units if (_binding == null) return
profileview_dia.text = resourceHelper.gs(R.string.format_hours, profile.dia) binding.profileviewer.units.text = profile.units
profileview_activeprofile.text = name binding.profileviewer.dia.text = resourceHelper.gs(R.string.format_hours, profile.dia)
profileview_ic.text = profile.icList binding.profileviewer.activeprofile.text = name
profileview_isf.text = profile.isfList binding.profileviewer.ic.text = profile.icList
profileview_basal.text = profile.basalList binding.profileviewer.isf.text = profile.isfList
profileview_basaltotal.text = String.format(resourceHelper.gs(R.string.profile_total), DecimalFormatter.to2Decimal(profile.baseBasalSum())) binding.profileviewer.basal.text = profile.basalList
profileview_target.text = profile.targetList binding.profileviewer.basaltotal.text = String.format(resourceHelper.gs(R.string.profile_total), DecimalFormatter.to2Decimal(profile.baseBasalSum()))
basal_graph.show(profile) binding.profileviewer.target.text = profile.targetList
binding.profileviewer.basalGraph.show(profile)
if (profile.isValid("NSProfileFragment")) { if (profile.isValid("NSProfileFragment")) {
profileview_invalidprofile.visibility = View.GONE binding.profileviewer.invalidprofile.visibility = View.GONE
nsprofile_profileswitch.visibility = View.VISIBLE binding.profileswitch.visibility = View.VISIBLE
} else { } else {
profileview_invalidprofile.visibility = View.VISIBLE binding.profileviewer.invalidprofile.visibility = View.VISIBLE
nsprofile_profileswitch.visibility = View.GONE binding.profileswitch.visibility = View.GONE
} }
} }
} }
@ -120,21 +128,29 @@ class NSProfileFragment : DaggerFragment() {
disposable.clear() disposable.clear()
} }
@Synchronized
override fun onDestroyView() {
super.onDestroyView()
_binding = null
}
@Synchronized @Synchronized
fun updateGUI() { fun updateGUI() {
if (profileview_noprofile == null) return if (_binding == null) return
profileview_noprofile.visibility = View.VISIBLE binding.profileviewer.noprofile.visibility = View.VISIBLE
nsProfilePlugin.profile?.let { profileStore -> nsProfilePlugin.profile?.let { profileStore ->
context?.let { context ->
val profileList = profileStore.getProfileList() val profileList = profileStore.getProfileList()
val adapter = ArrayAdapter(context!!, R.layout.spinner_centered, profileList) val adapter = ArrayAdapter(context, R.layout.spinner_centered, profileList)
nsprofile_spinner.adapter = adapter binding.spinner.adapter = adapter
// set selected to actual profile // set selected to actual profile
for (p in profileList.indices) { for (p in profileList.indices) {
if (profileList[p] == profileFunction.getProfileName()) 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"> android:layout_height="match_parent">
<Spinner <Spinner
android:id="@+id/nsprofile_spinner" android:id="@+id/spinner"
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_gravity="center_horizontal" android:layout_gravity="center_horizontal"
android:layout_marginBottom="5dp" /> android:layout_marginBottom="5dp" />
<Button <Button
android:id="@+id/nsprofile_profileswitch" android:id="@+id/profileswitch"
style="?android:attr/buttonStyle" style="?android:attr/buttonStyle"
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
@ -20,6 +20,7 @@
android:textColor="@color/colorProfileSwitchButton" /> android:textColor="@color/colorProfileSwitchButton" />
<include <include
android:id="@+id/profileviewer"
layout="@layout/profileviewer_fragment" layout="@layout/profileviewer_fragment"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="match_parent" /> android:layout_height="match_parent" />

View file

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