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.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import dagger.android.support.DaggerFragment
import info.nightscout.androidaps.MainApp
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.Source
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 io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.disposables.CompositeDisposable
import kotlinx.android.synthetic.main.treatments_profileswitch_fragment.*
import javax.inject.Inject
class TreatmentsProfileSwitchFragment : DaggerFragment() {
private val disposable = CompositeDisposable()
@Inject lateinit var rxBus: RxBusWrapper
@ -48,18 +49,22 @@ class TreatmentsProfileSwitchFragment : DaggerFragment() {
@Inject lateinit var dateUtil: DateUtil
@Inject lateinit var buildHelper: BuildHelper
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?): View? {
return inflater.inflate(R.layout.treatments_profileswitch_fragment, container, false)
}
private var _binding: TreatmentsProfileswitchFragmentBinding? = 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 =
TreatmentsProfileswitchFragmentBinding.inflate(inflater, container, false).also { _binding = it }.root
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
profileswitch_recyclerview.setHasFixedSize(true)
profileswitch_recyclerview.layoutManager = LinearLayoutManager(view.context)
profileswitch_recyclerview.adapter = RecyclerProfileViewAdapter(MainApp.getDbHelper().getProfileSwitchData(DateUtil.now() - T.days(30).msecs(), false))
binding.recyclerview.setHasFixedSize(true)
binding.recyclerview.layoutManager = LinearLayoutManager(view.context)
binding.recyclerview.adapter = RecyclerProfileViewAdapter(MainApp.getDbHelper().getProfileSwitchData(DateUtil.now() - T.days(30).msecs(), false))
profileswitch_refreshfromnightscout.setOnClickListener {
binding.refreshFromNightscout.setOnClickListener {
activity?.let { activity ->
OKDialog.showConfirmation(activity, resourceHelper.gs(R.string.refresheventsfromnightscout) + "?") {
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
@ -76,7 +81,7 @@ class TreatmentsProfileSwitchFragment : DaggerFragment() {
disposable.add(rxBus
.toObservable(EventProfileNeedsUpdate::class.java)
.observeOn(AndroidSchedulers.mainThread())
.subscribe({ updateGUI() }) { fabricPrivacy.logException(it) }
.subscribe({ updateGUI() }, fabricPrivacy::logException)
)
updateGUI()
}
@ -87,95 +92,101 @@ class TreatmentsProfileSwitchFragment : DaggerFragment() {
disposable.clear()
}
fun updateGUI() =
profileswitch_recyclerview?.swapAdapter(RecyclerProfileViewAdapter(MainApp.getDbHelper().getProfileSwitchData(DateUtil.now() - T.days(30).msecs(), false)), false)
@Synchronized
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>() {
override fun onCreateViewHolder(viewGroup: ViewGroup, viewType: Int): ProfileSwitchViewHolder {
return ProfileSwitchViewHolder(LayoutInflater.from(viewGroup.context).inflate(R.layout.treatments_profileswitch_item, viewGroup, false))
}
override fun onBindViewHolder(holder: ProfileSwitchViewHolder, position: Int) {
val profileSwitch = profileSwitchList[position]
holder.ph.visibility = (profileSwitch.source == Source.PUMP).toVisibility()
holder.ns.visibility = NSUpload.isIdValid(profileSwitch._id).toVisibility()
holder.date.text = dateUtil.dateAndTimeString(profileSwitch.date)
holder.binding.ph.visibility = (profileSwitch.source == Source.PUMP).toVisibility()
holder.binding.ns.visibility = NSUpload.isIdValid(profileSwitch._id).toVisibility()
holder.binding.date.text = dateUtil.dateAndTimeString(profileSwitch.date)
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 {
holder.duration.text = ""
holder.binding.duration.text = ""
}
holder.name.text = profileSwitch.customizedName
if (profileSwitch.isInProgress) holder.date.setTextColor(resourceHelper.gc(R.color.colorActive)) else holder.date.setTextColor(holder.duration.currentTextColor)
holder.remove.tag = profileSwitch
holder.clone.tag = profileSwitch
holder.name.tag = profileSwitch
holder.date.tag = profileSwitch
holder.invalid.visibility = if (profileSwitch.isValid()) View.GONE else View.VISIBLE
holder.binding.name.text = profileSwitch.customizedName
if (profileSwitch.isInProgress) holder.binding.date.setTextColor(resourceHelper.gc(R.color.colorActive)) else holder.binding.date.setTextColor(holder.binding.duration.currentTextColor)
holder.binding.remove.tag = profileSwitch
holder.binding.clone.tag = profileSwitch
holder.binding.name.tag = profileSwitch
holder.binding.date.tag = profileSwitch
holder.binding.invalid.visibility = if (profileSwitch.isValid()) View.GONE else View.VISIBLE
}
override fun getItemCount(): Int {
return profileSwitchList.size
}
inner class ProfileSwitchViewHolder internal constructor(itemView: View) : RecyclerView.ViewHolder(itemView), View.OnClickListener {
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
inner class ProfileSwitchViewHolder internal constructor(itemView: View) : RecyclerView.ViewHolder(itemView){
override fun onClick(v: View) {
val profileSwitch = v.tag as ProfileSwitch
when (v.id) {
R.id.profileswitch_remove ->
activity?.let { activity ->
OKDialog.showConfirmation(activity, resourceHelper.gs(R.string.removerecord),
resourceHelper.gs(R.string.careportal_profileswitch) + ": " + profileSwitch.profileName +
"\n" + resourceHelper.gs(R.string.date) + ": " + dateUtil.dateAndTimeString(profileSwitch.date), Runnable {
val id = profileSwitch._id
if (NSUpload.isIdValid(id)) nsUpload.removeCareportalEntryFromNS(id)
else uploadQueue.removeID("dbAdd", id)
MainApp.getDbHelper().delete(profileSwitch)
})
}
R.id.profileswitch_clone ->
activity?.let { activity ->
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 {
val nonCustomized = it.convertToNonCustomizedProfile()
if (nonCustomized.isValid(resourceHelper.gs(R.string.careportal_profileswitch, false))) {
localProfilePlugin.addProfile(localProfilePlugin.copyFrom(nonCustomized, profileSwitch.customizedName + " " + dateUtil.dateAndTimeString(profileSwitch.date).replace(".", "_")))
rxBus.send(EventLocalProfileChanged())
} else {
OKDialog.show(activity, resourceHelper.gs(R.string.careportal_profileswitch), resourceHelper.gs(R.string.copytolocalprofile_invalid))
}
val binding = TreatmentsProfileswitchItemBinding.bind(itemView)
init {
binding.remove.setOnClickListener {
val profileSwitch = it.tag as ProfileSwitch
activity?.let { activity ->
OKDialog.showConfirmation(activity, resourceHelper.gs(R.string.removerecord),
resourceHelper.gs(R.string.careportal_profileswitch) + ": " + profileSwitch.profileName +
"\n" + resourceHelper.gs(R.string.date) + ": " + dateUtil.dateAndTimeString(profileSwitch.date), Runnable {
val id = profileSwitch._id
if (NSUpload.isIdValid(id)) nsUpload.removeCareportalEntryFromNS(id)
else uploadQueue.removeID("dbAdd", id)
MainApp.getDbHelper().delete(profileSwitch)
})
}
}
binding.clone.setOnClickListener {
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 {
profileSwitch.profileObject?.let {
val nonCustomized = it.convertToNonCustomizedProfile()
if (nonCustomized.isValid(resourceHelper.gs(R.string.careportal_profileswitch, false))) {
localProfilePlugin.addProfile(localProfilePlugin.copyFrom(nonCustomized, profileSwitch.customizedName + " " + dateUtil.dateAndTimeString(profileSwitch.date).replace(".", "_")))
rxBus.send(EventLocalProfileChanged())
} else {
OKDialog.show(activity, resourceHelper.gs(R.string.careportal_profileswitch), resourceHelper.gs(R.string.copytolocalprofile_invalid))
}
})
}
})
}
}
binding.remove.paintFlags = binding.remove.paintFlags or Paint.UNDERLINE_TEXT_FLAG
binding.clone.paintFlags = binding.clone.paintFlags or Paint.UNDERLINE_TEXT_FLAG
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)
}
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)
}
R.id.profileswitch_date, R.id.profileswitch_name -> {
val args = Bundle()
args.putLong("time", (v.tag as ProfileSwitch).date)
args.putInt("mode", ProfileViewerDialog.Mode.DB_PROFILE.ordinal)
val pvd = ProfileViewerDialog()
pvd.arguments = args
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,28 +1,23 @@
<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="info.nightscout.androidaps.plugins.treatments.fragments.TreatmentsProfileSwitchFragment">
<LinearLayout
<Button
android:id="@+id/refresh_from_nightscout"
style="?android:attr/buttonStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="@string/refresheventsfromnightscout" />
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recyclerview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
android:layout_height="match_parent">
<Button
android:id="@+id/profileswitch_refreshfromnightscout"
style="?android:attr/buttonStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="@string/refresheventsfromnightscout" />
</androidx.recyclerview.widget.RecyclerView>
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/profileswitch_recyclerview"
android:layout_width="match_parent"
android:layout_height="match_parent">
</androidx.recyclerview.widget.RecyclerView>
</LinearLayout>
</FrameLayout>
</LinearLayout>

View file

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