BGSourceFragment -> jetpack

This commit is contained in:
Milos Kozak 2021-02-03 15:43:47 +01:00
parent 4476d0e0db
commit fab3c870d7
3 changed files with 49 additions and 54 deletions

View file

@ -5,14 +5,14 @@ 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.ImageView
import android.widget.TextView
import androidx.recyclerview.widget.DiffUtil import androidx.recyclerview.widget.DiffUtil
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.BgsourceFragmentBinding
import info.nightscout.androidaps.databinding.BgsourceItemBinding
import info.nightscout.androidaps.db.BgReading import info.nightscout.androidaps.db.BgReading
import info.nightscout.androidaps.events.EventNewBG import info.nightscout.androidaps.events.EventNewBG
import info.nightscout.androidaps.interfaces.DatabaseHelperInterface import info.nightscout.androidaps.interfaces.DatabaseHelperInterface
@ -31,7 +31,6 @@ import info.nightscout.androidaps.utils.extensions.toVisibility
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.bgsource_fragment.*
import javax.inject.Inject import javax.inject.Inject
class BGSourceFragment : DaggerFragment() { class BGSourceFragment : DaggerFragment() {
@ -44,25 +43,24 @@ class BGSourceFragment : DaggerFragment() {
@Inject lateinit var databaseHelper: DatabaseHelperInterface @Inject lateinit var databaseHelper: DatabaseHelperInterface
private val disposable = CompositeDisposable() private val disposable = CompositeDisposable()
private val MILLS_TO_THE_PAST = T.hours(12).msecs() private val millsToThePast = T.hours(12).msecs()
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, private var _binding: BgsourceFragmentBinding? = null
savedInstanceState: Bundle?): View? {
return inflater.inflate(R.layout.bgsource_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 =
BgsourceFragmentBinding.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)
bgsource_recyclerview.setHasFixedSize(true) binding.recyclerview.setHasFixedSize(true)
bgsource_recyclerview.layoutManager = LinearLayoutManager(view.context) binding.recyclerview.layoutManager = LinearLayoutManager(view.context)
val now = System.currentTimeMillis() val now = System.currentTimeMillis()
bgsource_recyclerview.adapter = RecyclerViewAdapter(getBgData(now)) binding.recyclerview.adapter = RecyclerViewAdapter(getBgData(now))
}
override fun onDestroyView() {
super.onDestroyView()
bgsource_recyclerview?.adapter = null // avoid leaks
} }
@Synchronized @Synchronized
@ -87,17 +85,25 @@ class BGSourceFragment : DaggerFragment() {
super.onPause() super.onPause()
} }
@Synchronized
override fun onDestroyView() {
super.onDestroyView()
_binding = null
binding.recyclerview.adapter = null // avoid leaks
}
private fun updateGUI() { private fun updateGUI() {
if (_binding == null) return
val now = System.currentTimeMillis() val now = System.currentTimeMillis()
(bgsource_recyclerview?.adapter as? RecyclerViewAdapter)?.setData(getBgData(now)) (binding.recyclerview.adapter as? RecyclerViewAdapter)?.setData(getBgData(now))
} }
private fun getBgData(now: Long) = MainApp.getDbHelper() private fun getBgData(now: Long) = MainApp.getDbHelper()
.getAllBgreadingsDataFromTime(now - MILLS_TO_THE_PAST, false) .getAllBgreadingsDataFromTime(now - millsToThePast, false)
inner class RecyclerViewAdapter internal constructor(bgReadings: List<BgReading>) : RecyclerView.Adapter<BgReadingsViewHolder>() { inner class RecyclerViewAdapter internal constructor(bgReadings: List<BgReading>) : RecyclerView.Adapter<BgReadingsViewHolder>() {
private var callbackHelper = ListUpdateCallbackHelper(this) { bgsource_recyclerview?.smoothScrollToPosition(0) } private var callbackHelper = ListUpdateCallbackHelper(this) { binding.recyclerview.smoothScrollToPosition(0) }
private val currentData: MutableList<BgReading> = mutableListOf<BgReading>().also { it.addAll(bgReadings) } private val currentData: MutableList<BgReading> = mutableListOf<BgReading>().also { it.addAll(bgReadings) }
@ -130,30 +136,25 @@ class BGSourceFragment : DaggerFragment() {
override fun onBindViewHolder(holder: BgReadingsViewHolder, position: Int) { override fun onBindViewHolder(holder: BgReadingsViewHolder, position: Int) {
val bgReading = currentData[position] val bgReading = currentData[position]
holder.ns.visibility = (NSUpload.isIdValid(bgReading._id)).toVisibility() holder.binding.ns.visibility = (NSUpload.isIdValid(bgReading._id)).toVisibility()
holder.invalid.visibility = bgReading.isValid.not().toVisibility() holder.binding.invalid.visibility = bgReading.isValid.not().toVisibility()
holder.date.text = dateUtil.dateAndTimeString(bgReading.date) holder.binding.date.text = dateUtil.dateAndTimeString(bgReading.date)
holder.value.text = bgReading.valueToUnitsToString(profileFunction.getUnits()) holder.binding.value.text = bgReading.valueToUnitsToString(profileFunction.getUnits())
holder.direction.setImageResource(bgReading.directionToIcon(databaseHelper)) holder.binding.direction.setImageResource(bgReading.directionToIcon(databaseHelper))
holder.remove.tag = bgReading holder.binding.remove.tag = bgReading
holder.remove.visibility = bgReading.isValid.toVisibility() holder.binding.remove.visibility = bgReading.isValid.toVisibility()
} }
override fun getItemCount(): Int { override fun getItemCount(): Int {
return currentData.size return currentData.size
} }
inner class BgReadingsViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) { inner class BgReadingsViewHolder(view: View) : RecyclerView.ViewHolder(view) {
var date: TextView = itemView.findViewById(R.id.bgsource_date) val binding = BgsourceItemBinding.bind(view)
var value: TextView = itemView.findViewById(R.id.bgsource_value)
var direction: ImageView = itemView.findViewById(R.id.bgsource_direction)
var invalid: TextView = itemView.findViewById(R.id.invalid_sign)
var ns: TextView = itemView.findViewById(R.id.ns_sign)
var remove: TextView = itemView.findViewById(R.id.bgsource_remove)
init { init {
remove.setOnClickListener { v: View -> binding.remove.setOnClickListener { v: View ->
val bgReading = v.tag as BgReading val bgReading = v.tag as BgReading
activity?.let { activity -> activity?.let { activity ->
val text = dateUtil.dateAndTimeString(bgReading.date) + "\n" + bgReading.valueToUnitsToString(profileFunction.getUnits()) val text = dateUtil.dateAndTimeString(bgReading.date) + "\n" + bgReading.valueToUnitsToString(profileFunction.getUnits())
@ -163,7 +164,7 @@ class BGSourceFragment : DaggerFragment() {
}) })
} }
} }
remove.paintFlags = remove.paintFlags or Paint.UNDERLINE_TEXT_FLAG binding.remove.paintFlags = binding.remove.paintFlags or Paint.UNDERLINE_TEXT_FLAG
} }
} }
} }

View file

@ -1,22 +1,16 @@
<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"
android:paddingTop="2dp" android:paddingTop="2dp"
tools:context="info.nightscout.androidaps.plugins.source.BGSourceFragment"> tools:context="info.nightscout.androidaps.plugins.source.BGSourceFragment">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<androidx.recyclerview.widget.RecyclerView <androidx.recyclerview.widget.RecyclerView
android:id="@+id/bgsource_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> </LinearLayout>
</FrameLayout>

View file

@ -21,14 +21,14 @@
android:orientation="horizontal"> android:orientation="horizontal">
<TextView <TextView
android:id="@+id/bgsource_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:text="16:55" android:text="16:55"
android:textStyle="bold" /> android:textStyle="bold" />
<TextView <TextView
android:id="@+id/bgsource_value" android:id="@+id/value"
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"
@ -36,14 +36,14 @@
android:textStyle="bold" /> android:textStyle="bold" />
<ImageView <ImageView
android:id="@+id/bgsource_direction" android:id="@+id/direction"
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="match_parent" android:layout_height="match_parent"
android:layout_gravity="center_vertical" android:layout_gravity="center_vertical"
card_view:srcCompat="@drawable/ic_flat" /> card_view:srcCompat="@drawable/ic_flat" />
<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:paddingStart="10dp" android:paddingStart="10dp"
@ -52,7 +52,7 @@
android:textColor="@color/colorSetTempButton" /> android:textColor="@color/colorSetTempButton" />
<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:paddingStart="10dp" android:paddingStart="10dp"
@ -60,7 +60,7 @@
android:textColor="@android:color/holo_red_light" /> android:textColor="@android:color/holo_red_light" />
<TextView <TextView
android:id="@+id/bgsource_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"