TreatmentsProfileSwitchFragment -> jetpack

This commit is contained in:
Milos Kozak 2021-02-03 20:08:53 +01:00
parent 8a15fe12ba
commit 327f1ca63b
3 changed files with 112 additions and 106 deletions

View file

@ -5,12 +5,13 @@ import android.os.Bundle
import android.view.LayoutInflater import android.view.LayoutInflater
import android.view.View import android.view.View
import android.view.ViewGroup import android.view.ViewGroup
import android.widget.TextView
import androidx.recyclerview.widget.LinearLayoutManager import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView import androidx.recyclerview.widget.RecyclerView
import dagger.android.support.DaggerFragment import dagger.android.support.DaggerFragment
import info.nightscout.androidaps.MainApp import info.nightscout.androidaps.MainApp
import info.nightscout.androidaps.R import info.nightscout.androidaps.R
import info.nightscout.androidaps.databinding.TreatmentsProfileswitchFragmentBinding
import info.nightscout.androidaps.databinding.TreatmentsProfileswitchItemBinding
import info.nightscout.androidaps.db.ProfileSwitch import info.nightscout.androidaps.db.ProfileSwitch
import info.nightscout.androidaps.db.Source import info.nightscout.androidaps.db.Source
import info.nightscout.androidaps.dialogs.ProfileViewerDialog import info.nightscout.androidaps.dialogs.ProfileViewerDialog
@ -32,10 +33,10 @@ import info.nightscout.androidaps.utils.resources.ResourceHelper
import info.nightscout.androidaps.utils.sharedPreferences.SP import info.nightscout.androidaps.utils.sharedPreferences.SP
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_profileswitch_fragment.*
import javax.inject.Inject import javax.inject.Inject
class TreatmentsProfileSwitchFragment : DaggerFragment() { class TreatmentsProfileSwitchFragment : DaggerFragment() {
private val disposable = CompositeDisposable() private val disposable = CompositeDisposable()
@Inject lateinit var rxBus: RxBusWrapper @Inject lateinit var rxBus: RxBusWrapper
@ -48,18 +49,22 @@ class TreatmentsProfileSwitchFragment : DaggerFragment() {
@Inject lateinit var dateUtil: DateUtil @Inject lateinit var dateUtil: DateUtil
@Inject lateinit var buildHelper: BuildHelper @Inject lateinit var buildHelper: BuildHelper
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, private var _binding: TreatmentsProfileswitchFragmentBinding? = null
savedInstanceState: Bundle?): View? {
return inflater.inflate(R.layout.treatments_profileswitch_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 =
TreatmentsProfileswitchFragmentBinding.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)
profileswitch_recyclerview.setHasFixedSize(true) binding.recyclerview.setHasFixedSize(true)
profileswitch_recyclerview.layoutManager = LinearLayoutManager(view.context) binding.recyclerview.layoutManager = LinearLayoutManager(view.context)
profileswitch_recyclerview.adapter = RecyclerProfileViewAdapter(MainApp.getDbHelper().getProfileSwitchData(DateUtil.now() - T.days(30).msecs(), false)) binding.recyclerview.adapter = RecyclerProfileViewAdapter(MainApp.getDbHelper().getProfileSwitchData(DateUtil.now() - T.days(30).msecs(), false))
profileswitch_refreshfromnightscout.setOnClickListener { binding.refreshFromNightscout.setOnClickListener {
activity?.let { activity -> activity?.let { activity ->
OKDialog.showConfirmation(activity, resourceHelper.gs(R.string.refresheventsfromnightscout) + "?") { OKDialog.showConfirmation(activity, resourceHelper.gs(R.string.refresheventsfromnightscout) + "?") {
MainApp.getDbHelper().resetProfileSwitch() MainApp.getDbHelper().resetProfileSwitch()
@ -67,7 +72,7 @@ class TreatmentsProfileSwitchFragment : DaggerFragment() {
} }
} }
} }
if (sp.getBoolean(R.string.key_ns_upload_only, true) || !buildHelper.isEngineeringMode()) profileswitch_refreshfromnightscout.visibility = View.GONE if (sp.getBoolean(R.string.key_ns_upload_only, true) || !buildHelper.isEngineeringMode()) binding.refreshFromNightscout.visibility = View.GONE
} }
@Synchronized @Synchronized
@ -76,7 +81,7 @@ class TreatmentsProfileSwitchFragment : DaggerFragment() {
disposable.add(rxBus disposable.add(rxBus
.toObservable(EventProfileNeedsUpdate::class.java) .toObservable(EventProfileNeedsUpdate::class.java)
.observeOn(AndroidSchedulers.mainThread()) .observeOn(AndroidSchedulers.mainThread())
.subscribe({ updateGUI() }) { fabricPrivacy.logException(it) } .subscribe({ updateGUI() }, fabricPrivacy::logException)
) )
updateGUI() updateGUI()
} }
@ -87,51 +92,53 @@ class TreatmentsProfileSwitchFragment : DaggerFragment() {
disposable.clear() disposable.clear()
} }
fun updateGUI() = @Synchronized
profileswitch_recyclerview?.swapAdapter(RecyclerProfileViewAdapter(MainApp.getDbHelper().getProfileSwitchData(DateUtil.now() - T.days(30).msecs(), false)), false) override fun onDestroyView() {
super.onDestroyView()
_binding = null
}
fun updateGUI() {
if (_binding == null) return
binding.recyclerview.swapAdapter(RecyclerProfileViewAdapter(MainApp.getDbHelper().getProfileSwitchData(DateUtil.now() - T.days(30).msecs(), false)), false)
}
inner class RecyclerProfileViewAdapter(private var profileSwitchList: List<ProfileSwitch>) : RecyclerView.Adapter<ProfileSwitchViewHolder>() { inner class RecyclerProfileViewAdapter(private var profileSwitchList: List<ProfileSwitch>) : RecyclerView.Adapter<ProfileSwitchViewHolder>() {
override fun onCreateViewHolder(viewGroup: ViewGroup, viewType: Int): ProfileSwitchViewHolder { override fun onCreateViewHolder(viewGroup: ViewGroup, viewType: Int): ProfileSwitchViewHolder {
return ProfileSwitchViewHolder(LayoutInflater.from(viewGroup.context).inflate(R.layout.treatments_profileswitch_item, viewGroup, false)) return ProfileSwitchViewHolder(LayoutInflater.from(viewGroup.context).inflate(R.layout.treatments_profileswitch_item, viewGroup, false))
} }
override fun onBindViewHolder(holder: ProfileSwitchViewHolder, position: Int) { override fun onBindViewHolder(holder: ProfileSwitchViewHolder, position: Int) {
val profileSwitch = profileSwitchList[position] val profileSwitch = profileSwitchList[position]
holder.ph.visibility = (profileSwitch.source == Source.PUMP).toVisibility() holder.binding.ph.visibility = (profileSwitch.source == Source.PUMP).toVisibility()
holder.ns.visibility = NSUpload.isIdValid(profileSwitch._id).toVisibility() holder.binding.ns.visibility = NSUpload.isIdValid(profileSwitch._id).toVisibility()
holder.date.text = dateUtil.dateAndTimeString(profileSwitch.date) holder.binding.date.text = dateUtil.dateAndTimeString(profileSwitch.date)
if (!profileSwitch.isEndingEvent) { if (!profileSwitch.isEndingEvent) {
holder.duration.text = resourceHelper.gs(R.string.format_mins, profileSwitch.durationInMinutes) holder.binding.duration.text = resourceHelper.gs(R.string.format_mins, profileSwitch.durationInMinutes)
} else { } else {
holder.duration.text = "" holder.binding.duration.text = ""
} }
holder.name.text = profileSwitch.customizedName holder.binding.name.text = profileSwitch.customizedName
if (profileSwitch.isInProgress) holder.date.setTextColor(resourceHelper.gc(R.color.colorActive)) else holder.date.setTextColor(holder.duration.currentTextColor) if (profileSwitch.isInProgress) holder.binding.date.setTextColor(resourceHelper.gc(R.color.colorActive)) else holder.binding.date.setTextColor(holder.binding.duration.currentTextColor)
holder.remove.tag = profileSwitch holder.binding.remove.tag = profileSwitch
holder.clone.tag = profileSwitch holder.binding.clone.tag = profileSwitch
holder.name.tag = profileSwitch holder.binding.name.tag = profileSwitch
holder.date.tag = profileSwitch holder.binding.date.tag = profileSwitch
holder.invalid.visibility = if (profileSwitch.isValid()) View.GONE else View.VISIBLE holder.binding.invalid.visibility = if (profileSwitch.isValid()) View.GONE else View.VISIBLE
} }
override fun getItemCount(): Int { override fun getItemCount(): Int {
return profileSwitchList.size return profileSwitchList.size
} }
inner class ProfileSwitchViewHolder internal constructor(itemView: View) : RecyclerView.ViewHolder(itemView), View.OnClickListener { inner class ProfileSwitchViewHolder internal constructor(itemView: View) : RecyclerView.ViewHolder(itemView){
var date: TextView = itemView.findViewById<View>(R.id.profileswitch_date) as TextView
var duration: TextView = itemView.findViewById<View>(R.id.profileswitch_duration) as TextView
var name: TextView = itemView.findViewById<View>(R.id.profileswitch_name) as TextView
var remove: TextView = itemView.findViewById<View>(R.id.profileswitch_remove) as TextView
var clone: TextView = itemView.findViewById<View>(R.id.profileswitch_clone) as TextView
var ph: TextView = itemView.findViewById<View>(R.id.pump_sign) as TextView
var ns: TextView = itemView.findViewById<View>(R.id.ns_sign) as TextView
var invalid: TextView = itemView.findViewById<View>(R.id.invalid_sign) as TextView
override fun onClick(v: View) { val binding = TreatmentsProfileswitchItemBinding.bind(itemView)
val profileSwitch = v.tag as ProfileSwitch
when (v.id) { init {
R.id.profileswitch_remove -> binding.remove.setOnClickListener {
val profileSwitch = it.tag as ProfileSwitch
activity?.let { activity -> activity?.let { activity ->
OKDialog.showConfirmation(activity, resourceHelper.gs(R.string.removerecord), OKDialog.showConfirmation(activity, resourceHelper.gs(R.string.removerecord),
resourceHelper.gs(R.string.careportal_profileswitch) + ": " + profileSwitch.profileName + resourceHelper.gs(R.string.careportal_profileswitch) + ": " + profileSwitch.profileName +
@ -142,8 +149,10 @@ class TreatmentsProfileSwitchFragment : DaggerFragment() {
MainApp.getDbHelper().delete(profileSwitch) MainApp.getDbHelper().delete(profileSwitch)
}) })
} }
R.id.profileswitch_clone -> }
binding.clone.setOnClickListener {
activity?.let { activity -> activity?.let { activity ->
val profileSwitch = it.tag as ProfileSwitch
OKDialog.showConfirmation(activity, resourceHelper.gs(R.string.careportal_profileswitch), resourceHelper.gs(R.string.copytolocalprofile) + "\n" + profileSwitch.customizedName + "\n" + dateUtil.dateAndTimeString(profileSwitch.date), Runnable { OKDialog.showConfirmation(activity, resourceHelper.gs(R.string.careportal_profileswitch), resourceHelper.gs(R.string.copytolocalprofile) + "\n" + profileSwitch.customizedName + "\n" + dateUtil.dateAndTimeString(profileSwitch.date), Runnable {
profileSwitch.profileObject?.let { profileSwitch.profileObject?.let {
val nonCustomized = it.convertToNonCustomizedProfile() val nonCustomized = it.convertToNonCustomizedProfile()
@ -156,26 +165,28 @@ class TreatmentsProfileSwitchFragment : DaggerFragment() {
} }
}) })
} }
}
R.id.profileswitch_date, R.id.profileswitch_name -> { binding.remove.paintFlags = binding.remove.paintFlags or Paint.UNDERLINE_TEXT_FLAG
val args = Bundle() binding.clone.paintFlags = binding.clone.paintFlags or Paint.UNDERLINE_TEXT_FLAG
args.putLong("time", (v.tag as ProfileSwitch).date) binding.name.setOnClickListener {
ProfileViewerDialog().also { pvd ->
pvd.arguments = Bundle().also { args ->
args.putLong("time", (it.tag as ProfileSwitch).date)
args.putInt("mode", ProfileViewerDialog.Mode.DB_PROFILE.ordinal) args.putInt("mode", ProfileViewerDialog.Mode.DB_PROFILE.ordinal)
val pvd = ProfileViewerDialog() }
pvd.arguments = args pvd.show(childFragmentManager, "ProfileViewDialog")
}
}
binding.date.setOnClickListener {
ProfileViewerDialog().also { pvd ->
pvd.arguments = Bundle().also { args ->
args.putLong("time", (it.tag as ProfileSwitch).date)
args.putInt("mode", ProfileViewerDialog.Mode.DB_PROFILE.ordinal)
}
pvd.show(childFragmentManager, "ProfileViewDialog") pvd.show(childFragmentManager, "ProfileViewDialog")
} }
} }
} }
init {
remove.setOnClickListener(this)
clone.setOnClickListener(this)
remove.paintFlags = remove.paintFlags or Paint.UNDERLINE_TEXT_FLAG
clone.paintFlags = clone.paintFlags or Paint.UNDERLINE_TEXT_FLAG
name.setOnClickListener(this)
date.setOnClickListener(this)
}
} }
} }
} }

View file

@ -1,16 +1,12 @@
<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="info.nightscout.androidaps.plugins.treatments.fragments.TreatmentsProfileSwitchFragment"> tools:context="info.nightscout.androidaps.plugins.treatments.fragments.TreatmentsProfileSwitchFragment">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<Button <Button
android:id="@+id/profileswitch_refreshfromnightscout" android:id="@+id/refresh_from_nightscout"
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"
@ -18,11 +14,10 @@
android:text="@string/refresheventsfromnightscout" /> android:text="@string/refresheventsfromnightscout" />
<androidx.recyclerview.widget.RecyclerView <androidx.recyclerview.widget.RecyclerView
android:id="@+id/profileswitch_recyclerview" android:id="@+id/recyclerview"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="match_parent"> android:layout_height="match_parent">
</androidx.recyclerview.widget.RecyclerView> </androidx.recyclerview.widget.RecyclerView>
</LinearLayout>
</FrameLayout> </LinearLayout>

View file

@ -29,7 +29,7 @@
android:text="{fa-clock-o}" /> android:text="{fa-clock-o}" />
<TextView <TextView
android:id="@+id/profileswitch_date" android:id="@+id/date"
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:paddingStart="10dp" android:paddingStart="10dp"
@ -37,7 +37,7 @@
android:textAppearance="?android:attr/textAppearanceSmall" /> android:textAppearance="?android:attr/textAppearanceSmall" />
<TextView <TextView
android:id="@+id/profileswitch_name" android:id="@+id/name"
android:layout_width="0dp" android:layout_width="0dp"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_weight="1" android:layout_weight="1"
@ -54,8 +54,8 @@
android:orientation="horizontal"> android:orientation="horizontal">
<TextView <TextView
android:id="@+id/profileswitch_duration" android:id="@+id/duration"
android:layout_width="wrap_content" android:layout_width="0dp"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_weight="1" android:layout_weight="1"
android:paddingStart="10dp" android:paddingStart="10dp"
@ -63,7 +63,7 @@
android:textAppearance="?android:attr/textAppearanceSmall" /> android:textAppearance="?android:attr/textAppearanceSmall" />
<TextView <TextView
android:id="@+id/invalid_sign" android:id="@+id/invalid"
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:paddingEnd="10dp" android:paddingEnd="10dp"
@ -72,7 +72,7 @@
android:textColor="@android:color/holo_red_light" /> android:textColor="@android:color/holo_red_light" />
<TextView <TextView
android:id="@+id/pump_sign" android:id="@+id/ph"
android:layout_width="0dp" android:layout_width="0dp"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:paddingEnd="10dp" android:paddingEnd="10dp"
@ -81,7 +81,7 @@
android:textColor="@color/colorSetTempButton" /> android:textColor="@color/colorSetTempButton" />
<TextView <TextView
android:id="@+id/ns_sign" android:id="@+id/ns"
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:paddingEnd="10dp" android:paddingEnd="10dp"
@ -89,7 +89,7 @@
android:textColor="@color/colorSetTempButton" /> android:textColor="@color/colorSetTempButton" />
<TextView <TextView
android:id="@+id/profileswitch_clone" android:id="@+id/clone"
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:paddingEnd="5dp" android:paddingEnd="5dp"
@ -99,7 +99,7 @@
android:textColor="@android:color/holo_blue_light" /> android:textColor="@android:color/holo_blue_light" />
<TextView <TextView
android:id="@+id/profileswitch_remove" android:id="@+id/remove"
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:paddingEnd="5dp" android:paddingEnd="5dp"