TreatmentsCareportalFragmentBinding -> jetpack

This commit is contained in:
Milos Kozak 2021-02-03 15:30:05 +01:00
parent dbb2234236
commit 4476d0e0db
3 changed files with 74 additions and 69 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.TreatmentsCareportalFragmentBinding
import info.nightscout.androidaps.databinding.TreatmentsCareportalItemBinding
import info.nightscout.androidaps.db.CareportalEvent
import info.nightscout.androidaps.events.EventCareportalEventChange
import info.nightscout.androidaps.plugins.bus.RxBusWrapper
@ -27,10 +28,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_careportal_fragment.*
import javax.inject.Inject
class TreatmentsCareportalFragment : DaggerFragment() {
private val disposable = CompositeDisposable()
@Inject lateinit var rxBus: RxBusWrapper
@ -43,17 +44,21 @@ class TreatmentsCareportalFragment : 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_careportal_fragment, container, false)
}
private var _binding: TreatmentsCareportalFragmentBinding? = 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 =
TreatmentsCareportalFragmentBinding.inflate(inflater, container, false).also { _binding = it }.root
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
careportal_recyclerview.setHasFixedSize(true)
careportal_recyclerview.layoutManager = LinearLayoutManager(view.context)
careportal_recyclerview.adapter = RecyclerViewAdapter(MainApp.getDbHelper().getCareportalEvents(false))
careportal_refreshfromnightscout.setOnClickListener {
binding.recyclerview.setHasFixedSize(true)
binding.recyclerview.layoutManager = LinearLayoutManager(view.context)
binding.recyclerview.adapter = RecyclerViewAdapter(MainApp.getDbHelper().getCareportalEvents(false))
binding.refreshFromNightscout.setOnClickListener {
activity?.let { activity ->
OKDialog.showConfirmation(activity, resourceHelper.gs(R.string.careportal), resourceHelper.gs(R.string.refresheventsfromnightscout) + " ?", Runnable {
MainApp.getDbHelper().resetCareportalEvents()
@ -61,7 +66,7 @@ class TreatmentsCareportalFragment : DaggerFragment() {
})
}
}
careportal_removeandroidapsstartedevents.setOnClickListener {
binding.removeAndroidapsStartedEvents.setOnClickListener {
activity?.let { activity ->
OKDialog.showConfirmation(activity, resourceHelper.gs(R.string.careportal), resourceHelper.gs(R.string.careportal_removestartedevents), Runnable {
val events = MainApp.getDbHelper().getCareportalEvents(false)
@ -80,7 +85,7 @@ class TreatmentsCareportalFragment : DaggerFragment() {
}
val nsUploadOnly = sp.getBoolean(R.string.key_ns_upload_only, true) || !buildHelper.isEngineeringMode()
if (nsUploadOnly) careportal_refreshfromnightscout.visibility = View.GONE
if (nsUploadOnly) binding.refreshFromNightscout.visibility = View.GONE
}
@Synchronized override fun onResume() {
@ -88,7 +93,7 @@ class TreatmentsCareportalFragment : DaggerFragment() {
disposable.add(rxBus
.toObservable(EventCareportalEventChange::class.java)
.observeOn(AndroidSchedulers.mainThread())
.subscribe({ updateGui() }) { fabricPrivacy.logException(it) }
.subscribe({ updateGui() }, fabricPrivacy::logException)
)
updateGui()
}
@ -98,10 +103,19 @@ class TreatmentsCareportalFragment : DaggerFragment() {
disposable.clear()
}
private fun updateGui() =
careportal_recyclerview?.swapAdapter(RecyclerViewAdapter(MainApp.getDbHelper().getCareportalEvents(false)), false)
@Synchronized
override fun onDestroyView() {
super.onDestroyView()
_binding = null
}
private fun updateGui() {
if (_binding == null) return
binding.recyclerview.swapAdapter(RecyclerViewAdapter(MainApp.getDbHelper().getCareportalEvents(false)), false)
}
inner class RecyclerViewAdapter internal constructor(private var careportalEventList: List<CareportalEvent>) : RecyclerView.Adapter<CareportalEventsViewHolder>() {
override fun onCreateViewHolder(viewGroup: ViewGroup, viewType: Int): CareportalEventsViewHolder {
val v = LayoutInflater.from(viewGroup.context).inflate(R.layout.treatments_careportal_item, viewGroup, false)
return CareportalEventsViewHolder(v)
@ -109,28 +123,24 @@ class TreatmentsCareportalFragment : DaggerFragment() {
override fun onBindViewHolder(holder: CareportalEventsViewHolder, position: Int) {
val careportalEvent = careportalEventList[position]
holder.ns.visibility = if (NSUpload.isIdValid(careportalEvent._id)) View.VISIBLE else View.GONE
holder.date.text = dateUtil.dateAndTimeString(careportalEvent.date)
holder.duration.text = if (careportalEvent.durationInMsec() == 0L) "" else DateUtil.niceTimeScalar(careportalEvent.durationInMsec(), resourceHelper)
holder.note.text = careportalEvent.notes
holder.type.text = translator.translate(careportalEvent.eventType)
holder.remove.tag = careportalEvent
holder.binding.ns.visibility = if (NSUpload.isIdValid(careportalEvent._id)) View.VISIBLE else View.GONE
holder.binding.date.text = dateUtil.dateAndTimeString(careportalEvent.date)
holder.binding.duration.text = if (careportalEvent.durationInMsec() == 0L) "" else DateUtil.niceTimeScalar(careportalEvent.durationInMsec(), resourceHelper)
holder.binding.note.text = careportalEvent.notes
holder.binding.type.text = translator.translate(careportalEvent.eventType)
holder.binding.remove.tag = careportalEvent
}
override fun getItemCount(): Int {
return careportalEventList.size
}
inner class CareportalEventsViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
var date: TextView = itemView.findViewById(R.id.careportal_date)
var duration: TextView = itemView.findViewById(R.id.careportal_duration)
var type: TextView = itemView.findViewById(R.id.careportal_type)
var note: TextView = itemView.findViewById(R.id.careportal_note)
var remove: TextView = itemView.findViewById(R.id.careportal_remove)
var ns: TextView = itemView.findViewById(R.id.ns_sign)
inner class CareportalEventsViewHolder(view: View) : RecyclerView.ViewHolder(view) {
val binding = TreatmentsCareportalItemBinding.bind(view)
init {
remove.setOnClickListener { v: View ->
binding.remove.setOnClickListener { v: View ->
val careportalEvent = v.tag as CareportalEvent
activity?.let { activity ->
val text = resourceHelper.gs(R.string.eventtype) + ": " + translator.translate(careportalEvent.eventType) + "\n" +
@ -145,7 +155,7 @@ class TreatmentsCareportalFragment : DaggerFragment() {
}, null)
}
}
remove.paintFlags = remove.paintFlags or Paint.UNDERLINE_TEXT_FLAG
binding.remove.paintFlags = binding.remove.paintFlags or Paint.UNDERLINE_TEXT_FLAG
}
}

View file

@ -1,45 +1,40 @@
<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.TreatmentsCareportalFragment">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
android:layout_height="wrap_content"
android:orientation="horizontal">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:id="@+id/refresh_from_nightscout"
style="?android:attr/buttonStyle"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="center_horizontal"
android:layout_weight="1"
android:text="@string/refresheventsfromnightscout" />
<Button
android:id="@+id/careportal_refreshfromnightscout"
style="?android:attr/buttonStyle"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="center_horizontal"
android:layout_weight="1"
android:text="@string/refresheventsfromnightscout" />
<Button
android:id="@+id/remove_androidaps_started_events"
style="?android:attr/buttonStyle"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="center_horizontal"
android:layout_weight="1"
android:text="@string/careportal_removestartedevents" />
<Button
android:id="@+id/careportal_removeandroidapsstartedevents"
style="?android:attr/buttonStyle"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="center_horizontal"
android:layout_weight="1"
android:text="@string/careportal_removestartedevents" />
</LinearLayout>
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/careportal_recyclerview"
android:layout_width="match_parent"
android:layout_height="match_parent">
</androidx.recyclerview.widget.RecyclerView>
</LinearLayout>
</FrameLayout>
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recyclerview"
android:layout_width="match_parent"
android:layout_height="match_parent">
</androidx.recyclerview.widget.RecyclerView>
</LinearLayout>

View file

@ -29,7 +29,7 @@
android:text="{fa-clock-o}" />
<TextView
android:id="@+id/careportal_date"
android:id="@+id/date"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingEnd="10dp"
@ -37,14 +37,14 @@
android:textAppearance="?android:attr/textAppearanceSmall" />
<TextView
android:id="@+id/careportal_duration"
android:id="@+id/duration"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:paddingEnd="10dp"/>
<TextView
android:id="@+id/careportal_type"
android:id="@+id/type"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
@ -60,7 +60,7 @@
android:text="" />
<TextView
android:id="@+id/ns_sign"
android:id="@+id/ns"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="10dp"
@ -75,7 +75,7 @@
android:orientation="horizontal">
<TextView
android:id="@+id/careportal_note"
android:id="@+id/note"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
@ -85,7 +85,7 @@
android:text="Activity" />
<TextView
android:id="@+id/careportal_remove"
android:id="@+id/remove"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="10dp"