From 2705f3004a6f6f496e445e4c55ce9c4241782c79 Mon Sep 17 00:00:00 2001 From: Andries Smit Date: Thu, 27 Jan 2022 20:55:28 +0100 Subject: [PATCH] carbs dialog event time allign with time offset --- .../androidaps/dialogs/CarbsDialog.kt | 19 ++++++--- .../dialogs/DialogFragmentWithDate.kt | 42 +++++++++++++++---- 2 files changed, 47 insertions(+), 14 deletions(-) diff --git a/app/src/main/java/info/nightscout/androidaps/dialogs/CarbsDialog.kt b/app/src/main/java/info/nightscout/androidaps/dialogs/CarbsDialog.kt index 07f066d47c..6f57680102 100644 --- a/app/src/main/java/info/nightscout/androidaps/dialogs/CarbsDialog.kt +++ b/app/src/main/java/info/nightscout/androidaps/dialogs/CarbsDialog.kt @@ -105,6 +105,12 @@ class CarbsDialog : DialogFragmentWithDate() { ): View { onCreateViewGeneral() _binding = DialogCarbsBinding.inflate(inflater, container, false) + binding.time.setOnValueChangedListener { timeOffset: Double -> + run { + val newTime = eventTimeOriginal + timeOffset.toLong() * 1000 * 60 + updateDateTime(newTime) + } + } return binding.root } @@ -159,6 +165,13 @@ class CarbsDialog : DialogFragmentWithDate() { validateInputs() } + setOnValueChangedListener { eventTime: Long -> + run { + val timeOffset = ((eventTime - eventTimeOriginal) / (1000 * 60)).toDouble() + binding.time.value = timeOffset + } + } + iobCobCalculator.ads.actualBg()?.let { bgReading -> if (bgReading.value < 72) binding.hypoTt.isChecked = true @@ -229,10 +242,6 @@ class CarbsDialog : DialogFragmentWithDate() { ) val timeOffset = binding.time.value.toInt() - eventTime -= eventTime % 1000 - val time = eventTime + timeOffset * 1000 * 60 - if (timeOffset != 0) - actions.add(rh.gs(R.string.time) + ": " + dateUtil.dateAndTimeString(time)) if (useAlarm && carbs > 0 && timeOffset > 0) actions.add(rh.gs(R.string.alarminxmin, timeOffset).formatColor(rh, R.color.info)) val duration = binding.duration.value.toInt() @@ -330,7 +339,7 @@ class CarbsDialog : DialogFragmentWithDate() { detailedBolusInfo.context = context detailedBolusInfo.notes = notes detailedBolusInfo.carbsDuration = T.hours(duration.toLong()).msecs() - detailedBolusInfo.carbsTimestamp = time + detailedBolusInfo.carbsTimestamp = eventTime uel.log(if (duration == 0) Action.CARBS else Action.EXTENDED_CARBS, Sources.CarbDialog, notes, ValueWithUnit.Timestamp(eventTime).takeIf { eventTimeChanged }, diff --git a/core/src/main/java/info/nightscout/androidaps/dialogs/DialogFragmentWithDate.kt b/core/src/main/java/info/nightscout/androidaps/dialogs/DialogFragmentWithDate.kt index 729c57eeeb..1adf7ed00f 100644 --- a/core/src/main/java/info/nightscout/androidaps/dialogs/DialogFragmentWithDate.kt +++ b/core/src/main/java/info/nightscout/androidaps/dialogs/DialogFragmentWithDate.kt @@ -27,8 +27,18 @@ abstract class DialogFragmentWithDate : DaggerDialogFragment() { @Inject lateinit var sp: SP @Inject lateinit var dateUtil: DateUtil + fun interface OnValueChangedListener { + fun onValueChanged(value: Long) + } + var eventTime: Long = 0 - var eventTimeChanged = false + var eventTimeOriginal: Long = 0 + val eventTimeChanged: Boolean + get() = eventTime != eventTimeOriginal + + var eventDateView: TextView? = null + var eventTimeView: TextView? = null + private var mOnValueChangedListener: OnValueChangedListener? = null //one shot guards private var okClicked: Boolean = false @@ -50,7 +60,7 @@ abstract class DialogFragmentWithDate : DaggerDialogFragment() { override fun onSaveInstanceState(savedInstanceState: Bundle) { super.onSaveInstanceState(savedInstanceState) savedInstanceState.putLong("eventTime", eventTime) - savedInstanceState.putBoolean("eventTimeChanged", eventTimeChanged) + savedInstanceState.putLong("eventTimeOriginal", eventTimeOriginal) } fun onCreateViewGeneral() { @@ -60,12 +70,18 @@ abstract class DialogFragmentWithDate : DaggerDialogFragment() { dialog?.setCanceledOnTouchOutside(false) } - override fun onViewCreated(view: View, savedInstanceState: Bundle?) { - val eventDateView = view.findViewById(R.id.eventdate) as TextView? - val eventTimeView = view.findViewById(R.id.eventtime) as TextView? + fun updateDateTime(timeMs: Long) { + eventTime = timeMs + eventDateView?.text = dateUtil.dateString(eventTime) + eventTimeView?.text = dateUtil.timeString(eventTime) + } - eventTime = savedInstanceState?.getLong("eventTime") ?: dateUtil.nowWithoutMilliseconds() - eventTimeChanged = savedInstanceState?.getBoolean("eventTimeChanged") ?: false + override fun onViewCreated(view: View, savedInstanceState: Bundle?) { + eventDateView = view.findViewById(R.id.eventdate) as TextView? + eventTimeView = view.findViewById(R.id.eventtime) as TextView? + + eventTimeOriginal = savedInstanceState?.getLong("eventTimeOriginal") ?: dateUtil.nowWithoutMilliseconds() + eventTime = savedInstanceState?.getLong("eventTime") ?: eventTimeOriginal eventDateView?.text = dateUtil.dateString(eventTime) eventTimeView?.text = dateUtil.timeString(eventTime) @@ -79,8 +95,8 @@ abstract class DialogFragmentWithDate : DaggerDialogFragment() { cal.set(Calendar.MONTH, monthOfYear) cal.set(Calendar.DAY_OF_MONTH, dayOfMonth) eventTime = cal.timeInMillis - eventTimeChanged = true eventDateView?.text = dateUtil.dateString(eventTime) + callValueChangedListener() } eventDateView?.setOnClickListener { @@ -107,8 +123,8 @@ abstract class DialogFragmentWithDate : DaggerDialogFragment() { seconds++ ) // randomize seconds to prevent creating record of the same time, if user choose time manually eventTime = cal.timeInMillis - eventTimeChanged = true eventTimeView?.text = dateUtil.timeString(eventTime) + callValueChangedListener() } eventTimeView?.setOnClickListener { @@ -150,6 +166,14 @@ abstract class DialogFragmentWithDate : DaggerDialogFragment() { } + private fun callValueChangedListener() { + mOnValueChangedListener?.onValueChanged(eventTime) + } + + fun setOnValueChangedListener(onValueChangedListener: OnValueChangedListener?) { + mOnValueChangedListener = onValueChangedListener + } + override fun show(manager: FragmentManager, tag: String?) { try { manager.beginTransaction().let {