From b86a81bdffb36f5396dfce8691a4f6c7ab5b8e35 Mon Sep 17 00:00:00 2001 From: Mark Breen Date: Tue, 19 Apr 2022 01:19:54 +0100 Subject: [PATCH] merge upstream changes --- .../plugins/source/BGSourceFragment.kt | 128 ++++++++++++------ 1 file changed, 85 insertions(+), 43 deletions(-) diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/source/BGSourceFragment.kt b/app/src/main/java/info/nightscout/androidaps/plugins/source/BGSourceFragment.kt index 00f3c57c25..ad6e0f867d 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/source/BGSourceFragment.kt +++ b/app/src/main/java/info/nightscout/androidaps/plugins/source/BGSourceFragment.kt @@ -1,10 +1,9 @@ package info.nightscout.androidaps.plugins.source -import android.graphics.Paint import android.os.Bundle -import android.view.LayoutInflater -import android.view.View -import android.view.ViewGroup +import android.util.SparseArray +import android.view.* +import androidx.core.util.forEach import androidx.recyclerview.widget.LinearLayoutManager import androidx.recyclerview.widget.RecyclerView import dagger.android.support.DaggerFragment @@ -26,6 +25,7 @@ import info.nightscout.androidaps.interfaces.PluginBase import info.nightscout.androidaps.interfaces.ProfileFunction import info.nightscout.androidaps.logging.UserEntryLogger import info.nightscout.androidaps.plugins.bus.RxBus +import info.nightscout.androidaps.utils.ActionModeHelper import info.nightscout.androidaps.utils.DateUtil import info.nightscout.androidaps.utils.FabricPrivacy import info.nightscout.androidaps.utils.T @@ -54,11 +54,10 @@ class BGSourceFragment : DaggerFragment() { private val disposable = CompositeDisposable() private val millsToThePast = T.hours(36).msecs() - + private lateinit var actionHelper: ActionModeHelper private var _binding: BgsourceFragmentBinding? = null - // This property is only valid between onCreateView and - // onDestroyView. + // This property is only valid between onCreateView and onDestroyView. private val binding get() = _binding!! override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View = @@ -66,6 +65,10 @@ class BGSourceFragment : DaggerFragment() { override fun onViewCreated(view: View, savedInstanceState: Bundle?) { super.onViewCreated(view, savedInstanceState) + actionHelper = ActionModeHelper(rh, activity) + actionHelper.setUpdateListHandler { binding.recyclerview.adapter?.notifyDataSetChanged() } + actionHelper.setOnRemoveHandler { removeSelected(it) } + setHasOptionsMenu(actionHelper.inMenu) binding.recyclerview.setHasFixedSize(true) binding.recyclerview.layoutManager = LinearLayoutManager(view.context) @@ -94,10 +97,21 @@ class BGSourceFragment : DaggerFragment() { @Synchronized override fun onPause() { + actionHelper.finish() disposable.clear() super.onPause() } + override fun onCreateOptionsMenu(menu: Menu, inflater: MenuInflater) { + super.onCreateOptionsMenu(menu, inflater) + actionHelper.onCreateOptionsMenu(menu, inflater) + } + + override fun onPrepareOptionsMenu(menu: Menu) { + super.onPrepareOptionsMenu(menu) + actionHelper.onPrepareOptionsMenu(menu) + } + @Synchronized override fun onDestroyView() { super.onDestroyView() @@ -105,6 +119,9 @@ class BGSourceFragment : DaggerFragment() { _binding = null } + override fun onOptionsItemSelected(item: MenuItem) = + actionHelper.onOptionsItemSelected(item) + inner class RecyclerViewAdapter internal constructor(private var glucoseValues: List) : RecyclerView.Adapter() { override fun onCreateViewHolder(viewGroup: ViewGroup, viewType: Int): GlucoseValuesViewHolder { @@ -116,59 +133,84 @@ class BGSourceFragment : DaggerFragment() { val glucoseValue = glucoseValues[position] holder.binding.ns.visibility = (glucoseValue.interfaceIDs.nightscoutId != null).toVisibility() holder.binding.invalid.visibility = (!glucoseValue.isValid).toVisibility() - val sameDayPrevious = position > 0 && dateUtil.isSameDay(glucoseValue.timestamp, glucoseValues[position-1].timestamp) - holder.binding.date.visibility = sameDayPrevious.not().toVisibility() - holder.binding.date.text = dateUtil.dateString(glucoseValue.timestamp) + val newDay = position == 0 || !dateUtil.isSameDay(glucoseValue.timestamp, glucoseValues[position - 1].timestamp) + holder.binding.date.visibility = newDay.toVisibility() + holder.binding.date.text = if (newDay) dateUtil.dateStringRelative(glucoseValue.timestamp, rh) else "" holder.binding.time.text = dateUtil.timeString(glucoseValue.timestamp) holder.binding.value.text = glucoseValue.valueToUnitsString(profileFunction.getUnits()) holder.binding.direction.setImageResource(glucoseValue.trendArrow.directionToIcon()) - holder.binding.remove.tag = glucoseValue if (position > 0) { val previous = glucoseValues[position - 1] val diff = previous.timestamp - glucoseValue.timestamp if (diff < T.secs(20).msecs()) - holder.binding.root.setBackgroundColor(rh.gc(R.color.errorAlertBackground)) + holder.binding.root.setBackgroundColor(rh.gac(context, R.attr.bgsourceError)) } + + holder.binding.root.setOnLongClickListener { + if (actionHelper.startRemove()) { + holder.binding.cbRemove.toggle() + actionHelper.updateSelection(position, glucoseValue, holder.binding.cbRemove.isChecked) + return@setOnLongClickListener true + } + false + } + holder.binding.root.setOnClickListener { + if (actionHelper.isRemoving) { + holder.binding.cbRemove.toggle() + actionHelper.updateSelection(position, glucoseValue, holder.binding.cbRemove.isChecked) + } + } + holder.binding.cbRemove.setOnCheckedChangeListener { _, value -> + actionHelper.updateSelection(position, glucoseValue, value) + } + holder.binding.cbRemove.isChecked = actionHelper.isSelected(position) + holder.binding.cbRemove.visibility = actionHelper.isRemoving.toVisibility() } - override fun getItemCount(): Int = glucoseValues.size + override fun getItemCount() = glucoseValues.size inner class GlucoseValuesViewHolder(view: View) : RecyclerView.ViewHolder(view) { val binding = BgsourceItemBinding.bind(view) + } + } - init { - binding.remove.paintFlags = binding.remove.paintFlags or Paint.UNDERLINE_TEXT_FLAG - binding.remove.setOnClickListener { v: View -> - val glucoseValue = v.tag as GlucoseValue - activity?.let { activity -> - val text = dateUtil.dateAndTimeString(glucoseValue.timestamp) + "\n" + glucoseValue.valueToUnitsString(profileFunction.getUnits()) - OKDialog.showConfirmation(activity, rh.gs(R.string.removerecord), text, Runnable { - val source = when ((activePlugin.activeBgSource as PluginBase).pluginDescription.pluginName) { - R.string.dexcom_app_patched -> Sources.Dexcom - R.string.eversense -> Sources.Eversense - R.string.Glimp -> Sources.Glimp - R.string.MM640g -> Sources.MM640g - R.string.nsclientbg -> Sources.NSClientSource - R.string.poctech -> Sources.PocTech - R.string.tomato -> Sources.Tomato - R.string.glunovo -> Sources.Glunovo - R.string.aidex -> Sources.Aidex - R.string.xdrip -> Sources.Xdrip - else -> Sources.Unknown - } - uel.log( - Action.BG_REMOVED, source, - ValueWithUnit.Timestamp(glucoseValue.timestamp) - ) - repository.runTransactionForResult(InvalidateGlucoseValueTransaction(glucoseValue.id)) - .doOnError { aapsLogger.error(LTag.DATABASE, "Error while invalidating BG value", it) } - .blockingGet() - .also { result -> result.invalidated.forEach { aapsLogger.debug(LTag.DATABASE, "Invalidated bg $it") } } - }) + private fun getConfirmationText(selectedItems: SparseArray): String { + if (selectedItems.size() == 1) { + val glucoseValue = selectedItems.valueAt(0) + return dateUtil.dateAndTimeString(glucoseValue.timestamp) + "\n" + glucoseValue.valueToUnitsString(profileFunction.getUnits()) + } + return rh.gs(R.string.confirm_remove_multiple_items, selectedItems.size()) + } + + private fun removeSelected(selectedItems: SparseArray) { + activity?.let { activity -> + OKDialog.showConfirmation(activity, rh.gs(R.string.removerecord), getConfirmationText(selectedItems), Runnable { + selectedItems.forEach { _, glucoseValue -> + val source = when ((activePlugin.activeBgSource as PluginBase).pluginDescription.pluginName) { + R.string.dexcom_app_patched -> Sources.Dexcom + R.string.eversense -> Sources.Eversense + R.string.Glimp -> Sources.Glimp + R.string.MM640g -> Sources.MM640g + R.string.nsclientbg -> Sources.NSClientSource + R.string.poctech -> Sources.PocTech + R.string.tomato -> Sources.Tomato + R.string.glunovo -> Sources.Glunovo + R.string.xdrip -> Sources.Xdrip + R.string.aidex -> Sources.Aidex + else -> Sources.Unknown } + uel.log( + Action.BG_REMOVED, source, + ValueWithUnit.Timestamp(glucoseValue.timestamp) + ) + repository.runTransactionForResult(InvalidateGlucoseValueTransaction(glucoseValue.id)) + .doOnError { aapsLogger.error(LTag.DATABASE, "Error while invalidating BG value", it) } + .blockingGet() + .also { result -> result.invalidated.forEach { aapsLogger.debug(LTag.DATABASE, "Invalidated bg $it") } } } - } + actionHelper.finish() + }) } } }