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.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.TreatmentsCareportalFragmentBinding
import info.nightscout.androidaps.databinding.TreatmentsCareportalItemBinding
import info.nightscout.androidaps.db.CareportalEvent import info.nightscout.androidaps.db.CareportalEvent
import info.nightscout.androidaps.events.EventCareportalEventChange import info.nightscout.androidaps.events.EventCareportalEventChange
import info.nightscout.androidaps.plugins.bus.RxBusWrapper 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 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_careportal_fragment.*
import javax.inject.Inject import javax.inject.Inject
class TreatmentsCareportalFragment : DaggerFragment() { class TreatmentsCareportalFragment : DaggerFragment() {
private val disposable = CompositeDisposable() private val disposable = CompositeDisposable()
@Inject lateinit var rxBus: RxBusWrapper @Inject lateinit var rxBus: RxBusWrapper
@ -43,17 +44,21 @@ class TreatmentsCareportalFragment : 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: TreatmentsCareportalFragmentBinding? = null
savedInstanceState: Bundle?): View? {
return inflater.inflate(R.layout.treatments_careportal_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 =
TreatmentsCareportalFragmentBinding.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)
careportal_recyclerview.setHasFixedSize(true) binding.recyclerview.setHasFixedSize(true)
careportal_recyclerview.layoutManager = LinearLayoutManager(view.context) binding.recyclerview.layoutManager = LinearLayoutManager(view.context)
careportal_recyclerview.adapter = RecyclerViewAdapter(MainApp.getDbHelper().getCareportalEvents(false)) binding.recyclerview.adapter = RecyclerViewAdapter(MainApp.getDbHelper().getCareportalEvents(false))
careportal_refreshfromnightscout.setOnClickListener { binding.refreshFromNightscout.setOnClickListener {
activity?.let { activity -> activity?.let { activity ->
OKDialog.showConfirmation(activity, resourceHelper.gs(R.string.careportal), resourceHelper.gs(R.string.refresheventsfromnightscout) + " ?", Runnable { OKDialog.showConfirmation(activity, resourceHelper.gs(R.string.careportal), resourceHelper.gs(R.string.refresheventsfromnightscout) + " ?", Runnable {
MainApp.getDbHelper().resetCareportalEvents() MainApp.getDbHelper().resetCareportalEvents()
@ -61,7 +66,7 @@ class TreatmentsCareportalFragment : DaggerFragment() {
}) })
} }
} }
careportal_removeandroidapsstartedevents.setOnClickListener { binding.removeAndroidapsStartedEvents.setOnClickListener {
activity?.let { activity -> activity?.let { activity ->
OKDialog.showConfirmation(activity, resourceHelper.gs(R.string.careportal), resourceHelper.gs(R.string.careportal_removestartedevents), Runnable { OKDialog.showConfirmation(activity, resourceHelper.gs(R.string.careportal), resourceHelper.gs(R.string.careportal_removestartedevents), Runnable {
val events = MainApp.getDbHelper().getCareportalEvents(false) 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() 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() { @Synchronized override fun onResume() {
@ -88,7 +93,7 @@ class TreatmentsCareportalFragment : DaggerFragment() {
disposable.add(rxBus disposable.add(rxBus
.toObservable(EventCareportalEventChange::class.java) .toObservable(EventCareportalEventChange::class.java)
.observeOn(AndroidSchedulers.mainThread()) .observeOn(AndroidSchedulers.mainThread())
.subscribe({ updateGui() }) { fabricPrivacy.logException(it) } .subscribe({ updateGui() }, fabricPrivacy::logException)
) )
updateGui() updateGui()
} }
@ -98,10 +103,19 @@ class TreatmentsCareportalFragment : DaggerFragment() {
disposable.clear() disposable.clear()
} }
private fun updateGui() = @Synchronized
careportal_recyclerview?.swapAdapter(RecyclerViewAdapter(MainApp.getDbHelper().getCareportalEvents(false)), false) 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>() { inner class RecyclerViewAdapter internal constructor(private var careportalEventList: List<CareportalEvent>) : RecyclerView.Adapter<CareportalEventsViewHolder>() {
override fun onCreateViewHolder(viewGroup: ViewGroup, viewType: Int): CareportalEventsViewHolder { override fun onCreateViewHolder(viewGroup: ViewGroup, viewType: Int): CareportalEventsViewHolder {
val v = LayoutInflater.from(viewGroup.context).inflate(R.layout.treatments_careportal_item, viewGroup, false) val v = LayoutInflater.from(viewGroup.context).inflate(R.layout.treatments_careportal_item, viewGroup, false)
return CareportalEventsViewHolder(v) return CareportalEventsViewHolder(v)
@ -109,28 +123,24 @@ class TreatmentsCareportalFragment : DaggerFragment() {
override fun onBindViewHolder(holder: CareportalEventsViewHolder, position: Int) { override fun onBindViewHolder(holder: CareportalEventsViewHolder, position: Int) {
val careportalEvent = careportalEventList[position] val careportalEvent = careportalEventList[position]
holder.ns.visibility = if (NSUpload.isIdValid(careportalEvent._id)) View.VISIBLE else View.GONE holder.binding.ns.visibility = if (NSUpload.isIdValid(careportalEvent._id)) View.VISIBLE else View.GONE
holder.date.text = dateUtil.dateAndTimeString(careportalEvent.date) holder.binding.date.text = dateUtil.dateAndTimeString(careportalEvent.date)
holder.duration.text = if (careportalEvent.durationInMsec() == 0L) "" else DateUtil.niceTimeScalar(careportalEvent.durationInMsec(), resourceHelper) holder.binding.duration.text = if (careportalEvent.durationInMsec() == 0L) "" else DateUtil.niceTimeScalar(careportalEvent.durationInMsec(), resourceHelper)
holder.note.text = careportalEvent.notes holder.binding.note.text = careportalEvent.notes
holder.type.text = translator.translate(careportalEvent.eventType) holder.binding.type.text = translator.translate(careportalEvent.eventType)
holder.remove.tag = careportalEvent holder.binding.remove.tag = careportalEvent
} }
override fun getItemCount(): Int { override fun getItemCount(): Int {
return careportalEventList.size return careportalEventList.size
} }
inner class CareportalEventsViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) { inner class CareportalEventsViewHolder(view: View) : RecyclerView.ViewHolder(view) {
var date: TextView = itemView.findViewById(R.id.careportal_date)
var duration: TextView = itemView.findViewById(R.id.careportal_duration) val binding = TreatmentsCareportalItemBinding.bind(view)
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)
init { init {
remove.setOnClickListener { v: View -> binding.remove.setOnClickListener { v: View ->
val careportalEvent = v.tag as CareportalEvent val careportalEvent = v.tag as CareportalEvent
activity?.let { activity -> activity?.let { activity ->
val text = resourceHelper.gs(R.string.eventtype) + ": " + translator.translate(careportalEvent.eventType) + "\n" + val text = resourceHelper.gs(R.string.eventtype) + ": " + translator.translate(careportalEvent.eventType) + "\n" +
@ -145,7 +155,7 @@ class TreatmentsCareportalFragment : DaggerFragment() {
}, null) }, 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" 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.TreatmentsCareportalFragment"> tools:context="info.nightscout.androidaps.plugins.treatments.fragments.TreatmentsCareportalFragment">
<LinearLayout <LinearLayout
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="match_parent" android:layout_height="wrap_content"
android:orientation="vertical"> android:orientation="horizontal">
<LinearLayout <Button
android:layout_width="match_parent" android:id="@+id/refresh_from_nightscout"
android:layout_height="wrap_content" style="?android:attr/buttonStyle"
android:orientation="horizontal"> android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="center_horizontal"
android:layout_weight="1"
android:text="@string/refresheventsfromnightscout" />
<Button <Button
android:id="@+id/careportal_refreshfromnightscout" android:id="@+id/remove_androidaps_started_events"
style="?android:attr/buttonStyle" style="?android:attr/buttonStyle"
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="match_parent" android:layout_height="match_parent"
android:layout_gravity="center_horizontal" android:layout_gravity="center_horizontal"
android:layout_weight="1" android:layout_weight="1"
android:text="@string/refresheventsfromnightscout" /> 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> </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}" /> android:text="{fa-clock-o}" />
<TextView <TextView
android:id="@+id/careportal_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:paddingEnd="10dp" android:paddingEnd="10dp"
@ -37,14 +37,14 @@
android:textAppearance="?android:attr/textAppearanceSmall" /> android:textAppearance="?android:attr/textAppearanceSmall" />
<TextView <TextView
android:id="@+id/careportal_duration" android:id="@+id/duration"
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_gravity="center_vertical" android:layout_gravity="center_vertical"
android:paddingEnd="10dp"/> android:paddingEnd="10dp"/>
<TextView <TextView
android:id="@+id/careportal_type" android:id="@+id/type"
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_gravity="center_vertical" android:layout_gravity="center_vertical"
@ -60,7 +60,7 @@
android:text="" /> android:text="" />
<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:layout_marginEnd="10dp" android:layout_marginEnd="10dp"
@ -75,7 +75,7 @@
android:orientation="horizontal"> android:orientation="horizontal">
<TextView <TextView
android:id="@+id/careportal_note" android:id="@+id/note"
android:layout_width="0dp" android:layout_width="0dp"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_gravity="center_vertical" android:layout_gravity="center_vertical"
@ -85,7 +85,7 @@
android:text="Activity" /> android:text="Activity" />
<TextView <TextView
android:id="@+id/careportal_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:layout_marginEnd="10dp" android:layout_marginEnd="10dp"