diff --git a/app/src/main/java/info/nightscout/androidaps/activities/HistoryBrowseActivity.kt b/app/src/main/java/info/nightscout/androidaps/activities/HistoryBrowseActivity.kt index d3736c6869..8abf3ff860 100644 --- a/app/src/main/java/info/nightscout/androidaps/activities/HistoryBrowseActivity.kt +++ b/app/src/main/java/info/nightscout/androidaps/activities/HistoryBrowseActivity.kt @@ -1,7 +1,6 @@ package info.nightscout.androidaps.activities import android.annotation.SuppressLint -import android.app.DatePickerDialog import android.content.Context import android.os.Bundle import android.util.DisplayMetrics @@ -9,6 +8,7 @@ import android.view.ViewGroup import android.widget.LinearLayout import android.widget.RelativeLayout import android.widget.TextView +import com.google.android.material.datepicker.MaterialDatePicker import com.jjoe64.graphview.GraphView import dagger.android.HasAndroidInjector import info.nightscout.androidaps.R @@ -148,33 +148,18 @@ class HistoryBrowseActivity : NoSplashAppCompatActivity() { true } - // create an OnDateSetListener - val dateSetListener = DatePickerDialog.OnDateSetListener { _, year, monthOfYear, dayOfMonth -> - Calendar.getInstance().also { calendar -> - calendar.timeInMillis = overviewData.fromTime - calendar[Calendar.YEAR] = year - calendar[Calendar.MONTH] = monthOfYear - calendar[Calendar.DAY_OF_MONTH] = dayOfMonth - calendar[Calendar.MILLISECOND] = 0 - calendar[Calendar.SECOND] = 0 - calendar[Calendar.MINUTE] = 0 - calendar[Calendar.HOUR_OF_DAY] = 0 - setTime(calendar.timeInMillis) - binding.date.text = dateUtil.dateAndTimeString(overviewData.fromTime) - } - loadAll("onClickDate") - } - binding.date.setOnClickListener { - val cal = Calendar.getInstance() - cal.timeInMillis = overviewData.fromTime - DatePickerDialog( - this, - dateSetListener, - cal.get(Calendar.YEAR), - cal.get(Calendar.MONTH), - cal.get(Calendar.DAY_OF_MONTH) - ).show() + MaterialDatePicker.Builder.datePicker() + .setSelection(dateUtil.timeStampToUtcDateMilis(overviewData.fromTime)) + .build() + .apply { + addOnPositiveButtonClickListener { selection -> + setTime(dateUtil.mergeUtcDateToTimestamp(overviewData.fromTime, selection)) + binding.date.text = dateUtil.dateAndTimeString(overviewData.fromTime) + loadAll("onClickDate") + } + } + .show(supportFragmentManager, "history_date_picker") } val dm = DisplayMetrics() @@ -421,4 +406,4 @@ class HistoryBrowseActivity : NoSplashAppCompatActivity() { binding.progressBar.progress = percent binding.progressBar.visibility = (percent != 100).toVisibilityKeepSpace() } -} \ No newline at end of file +} diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/general/overview/dialogs/EditQuickWizardDialog.kt b/app/src/main/java/info/nightscout/androidaps/plugins/general/overview/dialogs/EditQuickWizardDialog.kt index 41f210e7eb..9341f74dc7 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/general/overview/dialogs/EditQuickWizardDialog.kt +++ b/app/src/main/java/info/nightscout/androidaps/plugins/general/overview/dialogs/EditQuickWizardDialog.kt @@ -1,6 +1,5 @@ package info.nightscout.androidaps.plugins.general.overview.dialogs -import android.app.TimePickerDialog import android.os.Bundle import android.text.format.DateFormat import android.view.LayoutInflater @@ -8,6 +7,8 @@ import android.view.View import android.view.ViewGroup import android.view.Window import android.view.WindowManager +import com.google.android.material.timepicker.MaterialTimePicker +import com.google.android.material.timepicker.TimeFormat import dagger.android.support.DaggerDialogFragment import info.nightscout.androidaps.R import info.nightscout.androidaps.databinding.OverviewEditquickwizardDialogBinding @@ -24,6 +25,7 @@ import info.nightscout.androidaps.utils.wizard.QuickWizard import info.nightscout.androidaps.utils.wizard.QuickWizardEntry import info.nightscout.shared.sharedPreferences.SP import org.json.JSONException +import java.util.* import javax.inject.Inject class EditQuickWizardDialog : DaggerDialogFragment(), View.OnClickListener { @@ -40,8 +42,7 @@ class EditQuickWizardDialog : DaggerDialogFragment(), View.OnClickListener { private var _binding: OverviewEditquickwizardDialogBinding? = 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( @@ -96,41 +97,35 @@ class EditQuickWizardDialog : DaggerDialogFragment(), View.OnClickListener { } binding.okcancel.cancel.setOnClickListener { dismiss() } - // create an OnTimeSetListener - val fromTimeSetListener = TimePickerDialog.OnTimeSetListener { _, hour, minute -> - fromSeconds = (T.hours(hour.toLong()).secs() + T.mins(minute.toLong()).secs()).toInt() - binding.from.text = dateUtil.timeString(dateUtil.secondsOfTheDayToMilliseconds(fromSeconds)) + binding.from.setOnClickListener { + val clockFormat = if (DateFormat.is24HourFormat(context)) TimeFormat.CLOCK_24H else TimeFormat.CLOCK_12H + val timePicker = MaterialTimePicker.Builder() + .setTimeFormat(clockFormat) + .setHour(T.secs(fromSeconds.toLong()).hours().toInt()) + .setMinute(T.secs((fromSeconds % 3600).toLong()).mins().toInt()) + .build() + timePicker.addOnPositiveButtonClickListener { + fromSeconds = (T.hours(timePicker.hour.toLong()).secs() + T.mins(timePicker.minute.toLong()).secs()).toInt() + binding.from.text = dateUtil.timeString(dateUtil.secondsOfTheDayToMilliseconds(fromSeconds)) + } + timePicker.show(parentFragmentManager, "event_time_time_picker") } - binding.from.setOnClickListener { - context?.let { - TimePickerDialog( - it, - fromTimeSetListener, - T.secs(fromSeconds.toLong()).hours().toInt(), - T.secs((fromSeconds % 3600).toLong()).mins().toInt(), - DateFormat.is24HourFormat(context) - ).show() - } - } fromSeconds = entry.validFrom() binding.from.text = dateUtil.timeString(dateUtil.secondsOfTheDayToMilliseconds(fromSeconds)) - val toTimeSetListener = TimePickerDialog.OnTimeSetListener { _, hour, minute -> - toSeconds = (T.hours(hour.toLong()).secs() + T.mins(minute.toLong()).secs()).toInt() - binding.to.text = dateUtil.timeString(dateUtil.secondsOfTheDayToMilliseconds(toSeconds)) - } - binding.to.setOnClickListener { - context?.let { - TimePickerDialog( - it, - toTimeSetListener, - T.secs(toSeconds.toLong()).hours().toInt(), - T.secs((toSeconds % 3600).toLong()).mins().toInt(), - DateFormat.is24HourFormat(context) - ).show() + val clockFormat = if (DateFormat.is24HourFormat(context)) TimeFormat.CLOCK_24H else TimeFormat.CLOCK_12H + val timePicker = MaterialTimePicker.Builder() + .setTimeFormat(clockFormat) + .setHour(T.secs(toSeconds.toLong()).hours().toInt()) + .setMinute(T.secs((toSeconds % 3600).toLong()).mins().toInt()) + .build() + timePicker.addOnPositiveButtonClickListener { + toSeconds = (T.hours(timePicker.hour.toLong()).secs() + T.mins(timePicker.minute.toLong()).secs()).toInt() + binding.to.text = dateUtil.timeString(dateUtil.secondsOfTheDayToMilliseconds(toSeconds)) } + timePicker.show(parentFragmentManager, "event_time_time_picker") } fun usePercentage(custom: Boolean) { diff --git a/automation/src/main/java/info/nightscout/androidaps/plugins/general/automation/elements/InputDateTime.kt b/automation/src/main/java/info/nightscout/androidaps/plugins/general/automation/elements/InputDateTime.kt index fb1f779f0f..861cf8a194 100644 --- a/automation/src/main/java/info/nightscout/androidaps/plugins/general/automation/elements/InputDateTime.kt +++ b/automation/src/main/java/info/nightscout/androidaps/plugins/general/automation/elements/InputDateTime.kt @@ -1,12 +1,17 @@ package info.nightscout.androidaps.plugins.general.automation.elements -import android.app.DatePickerDialog -import android.app.TimePickerDialog +import android.content.Context import android.graphics.Typeface import android.text.format.DateFormat import android.view.ViewGroup import android.widget.LinearLayout import android.widget.TextView +import androidx.appcompat.app.AppCompatActivity +import android.view.ContextThemeWrapper +import androidx.fragment.app.FragmentManager +import com.google.android.material.datepicker.MaterialDatePicker +import com.google.android.material.timepicker.MaterialTimePicker +import com.google.android.material.timepicker.TimeFormat import info.nightscout.androidaps.automation.R import info.nightscout.androidaps.utils.DateUtil import info.nightscout.androidaps.utils.resources.ResourceHelper @@ -32,24 +37,17 @@ class InputDateTime(private val rh: ResourceHelper, private val dateUtil: DateUt text = dateUtil.dateString(value) setPadding(px, px, px, px) setOnClickListener { - root.context?.let { - val cal = Calendar.getInstance() - cal.timeInMillis = value - DatePickerDialog( - it, - { _, year, monthOfYear, dayOfMonth -> - value = Calendar.getInstance().apply { - timeInMillis = value - set(Calendar.YEAR, year) - set(Calendar.MONTH, monthOfYear) - set(Calendar.DAY_OF_MONTH, dayOfMonth) - }.timeInMillis - text = dateUtil.dateString(value) - }, - cal.get(Calendar.YEAR), - cal.get(Calendar.MONTH), - cal.get(Calendar.DAY_OF_MONTH) - ).show() + getFragmentManager(root.context)?.let { fm -> + MaterialDatePicker.Builder.datePicker() + .setSelection(dateUtil.timeStampToUtcDateMilis(value)) + .build() + .apply { + addOnPositiveButtonClickListener { selection -> + value = dateUtil.mergeUtcDateToTimestamp(value, selection) + text = dateUtil.dateString(value) + } + } + .show(fm, "input_date_picker") } } }) @@ -58,28 +56,31 @@ class InputDateTime(private val rh: ResourceHelper, private val dateUtil: DateUt text = dateUtil.timeString(value) setPadding(px, px, px, px) setOnClickListener { - root.context?.let { - val cal = Calendar.getInstance() - cal.timeInMillis = value - TimePickerDialog( - it, - { _, hour, minute -> - value = Calendar.getInstance().apply { - timeInMillis = value - set(Calendar.HOUR_OF_DAY, hour) - set(Calendar.MINUTE, minute) - set(Calendar.SECOND, 0) // randomize seconds to prevent creating record of the same time, if user choose time manually - }.timeInMillis - text = dateUtil.timeString(value) - }, - cal.get(Calendar.HOUR_OF_DAY), - cal.get(Calendar.MINUTE), - DateFormat.is24HourFormat(it) - ).show() + getFragmentManager(root.context)?.let { fm -> + val cal = Calendar.getInstance().apply { timeInMillis = value } + val clockFormat = if (DateFormat.is24HourFormat(context)) TimeFormat.CLOCK_24H else TimeFormat.CLOCK_12H + val timePicker = MaterialTimePicker.Builder() + .setTimeFormat(clockFormat) + .setHour(cal.get(Calendar.HOUR_OF_DAY)) + .setMinute(cal.get(Calendar.MINUTE)) + .build() + timePicker.addOnPositiveButtonClickListener { + value = dateUtil.mergeHourMinuteToTimestamp(value, timePicker.hour, timePicker.minute) + text = dateUtil.timeString(value) + } + timePicker.show(fm, "input_time_picker") } } } ) }) } -} \ No newline at end of file + + private fun getFragmentManager(context: Context?): FragmentManager? { + return when (context) { + is AppCompatActivity -> context.supportFragmentManager + is ContextThemeWrapper -> getFragmentManager(context.baseContext) + else -> null + } + } +} diff --git a/automation/src/main/java/info/nightscout/androidaps/plugins/general/automation/elements/InputTime.kt b/automation/src/main/java/info/nightscout/androidaps/plugins/general/automation/elements/InputTime.kt index fcee35b411..5ea6c66ff0 100644 --- a/automation/src/main/java/info/nightscout/androidaps/plugins/general/automation/elements/InputTime.kt +++ b/automation/src/main/java/info/nightscout/androidaps/plugins/general/automation/elements/InputTime.kt @@ -1,12 +1,16 @@ package info.nightscout.androidaps.plugins.general.automation.elements -import android.app.TimePickerDialog +import android.content.Context import android.graphics.Typeface import android.text.format.DateFormat -import android.view.Gravity +import android.view.ContextThemeWrapper import android.view.ViewGroup import android.widget.LinearLayout import android.widget.TextView +import androidx.appcompat.app.AppCompatActivity +import androidx.fragment.app.FragmentManager +import com.google.android.material.timepicker.MaterialTimePicker +import com.google.android.material.timepicker.TimeFormat import info.nightscout.androidaps.automation.R import info.nightscout.androidaps.interfaces.Profile import info.nightscout.androidaps.utils.DateUtil @@ -34,19 +38,19 @@ class InputTime(private val rh: ResourceHelper, private val dateUtil: DateUtil) val px = rh.dpToPx(10) setPadding(px, px, px, px) setOnClickListener { - root.context?.let { - val cal = Calendar.getInstance() - cal.timeInMillis = toMills(value) - TimePickerDialog( - it, - { _, hour, minute -> - value = 60 * hour + minute - text = dateUtil.timeString(toMills(value)) - }, - cal.get(Calendar.HOUR_OF_DAY), - cal.get(Calendar.MINUTE), - DateFormat.is24HourFormat(it) - ).show() + getFragmentManager(root.context)?.let { fm -> + val cal = Calendar.getInstance().apply { timeInMillis = toMills(value) } + val clockFormat = if (DateFormat.is24HourFormat(context)) TimeFormat.CLOCK_24H else TimeFormat.CLOCK_12H + val timePicker = MaterialTimePicker.Builder() + .setTimeFormat(clockFormat) + .setHour(cal.get(Calendar.HOUR_OF_DAY)) + .setMinute(cal.get(Calendar.MINUTE)) + .build() + timePicker.addOnPositiveButtonClickListener { + value = 60 * timePicker.hour + timePicker.minute + text = dateUtil.timeString(toMills(value)) + } + timePicker.show(fm, "input_time_picker") } } }) @@ -56,4 +60,12 @@ class InputTime(private val rh: ResourceHelper, private val dateUtil: DateUtil) private fun toMills(minutesSinceMidnight: Int): Long = MidnightTime.calcPlusMinutes(minutesSinceMidnight) private fun getMinSinceMidnight(time: Long): Int = Profile.secondsFromMidnight(time) / 60 -} \ No newline at end of file + + private fun getFragmentManager(context: Context?): FragmentManager? { + return when (context) { + is AppCompatActivity -> context.supportFragmentManager + is ContextThemeWrapper -> getFragmentManager(context.baseContext) + else -> null + } + } +} diff --git a/automation/src/main/java/info/nightscout/androidaps/plugins/general/automation/elements/InputTimeRange.kt b/automation/src/main/java/info/nightscout/androidaps/plugins/general/automation/elements/InputTimeRange.kt index 343f617a27..a417c1891b 100644 --- a/automation/src/main/java/info/nightscout/androidaps/plugins/general/automation/elements/InputTimeRange.kt +++ b/automation/src/main/java/info/nightscout/androidaps/plugins/general/automation/elements/InputTimeRange.kt @@ -1,12 +1,17 @@ package info.nightscout.androidaps.plugins.general.automation.elements -import android.app.TimePickerDialog +import android.content.Context import android.graphics.Typeface import android.text.format.DateFormat +import android.view.ContextThemeWrapper import android.view.Gravity import android.view.ViewGroup import android.widget.LinearLayout import android.widget.TextView +import androidx.appcompat.app.AppCompatActivity +import androidx.fragment.app.FragmentManager +import com.google.android.material.timepicker.MaterialTimePicker +import com.google.android.material.timepicker.TimeFormat import info.nightscout.androidaps.automation.R import info.nightscout.androidaps.interfaces.Profile import info.nightscout.androidaps.utils.DateUtil @@ -38,19 +43,19 @@ class InputTimeRange(private val rh: ResourceHelper, private val dateUtil: DateU text = dateUtil.timeString(toMills(start)) setPadding(px, px, px, px) setOnClickListener { - root.context?.let { - val cal = Calendar.getInstance() - cal.timeInMillis = toMills(start) - TimePickerDialog( - it, - { _, hour, minute -> - start = 60 * hour + minute - text = dateUtil.timeString(toMills(start)) - }, - cal.get(Calendar.HOUR_OF_DAY), - cal.get(Calendar.MINUTE), - DateFormat.is24HourFormat(it) - ).show() + getFragmentManager(root.context)?.let { fm -> + val cal = Calendar.getInstance().apply { timeInMillis = toMills(start) } + val clockFormat = if (DateFormat.is24HourFormat(context)) TimeFormat.CLOCK_24H else TimeFormat.CLOCK_12H + val timePicker = MaterialTimePicker.Builder() + .setTimeFormat(clockFormat) + .setHour(cal.get(Calendar.HOUR_OF_DAY)) + .setMinute(cal.get(Calendar.MINUTE)) + .build() + timePicker.addOnPositiveButtonClickListener { + start = 60 * timePicker.hour + timePicker.minute + text = dateUtil.timeString(toMills(start)) + } + timePicker.show(fm, "input_time_range_start_picker") } } }) @@ -59,19 +64,19 @@ class InputTimeRange(private val rh: ResourceHelper, private val dateUtil: DateU text = rh.gs(R.string.and) + " " + dateUtil.timeString(toMills(end)) setPadding(px, px, px, px) setOnClickListener { - root.context?.let { - val cal = Calendar.getInstance() - cal.timeInMillis = toMills(end) - TimePickerDialog( - it, - { _, hour, minute -> - end = 60 * hour + minute - text = dateUtil.timeString(toMills(end)) - }, - cal.get(Calendar.HOUR_OF_DAY), - cal.get(Calendar.MINUTE), - DateFormat.is24HourFormat(it) - ).show() + getFragmentManager(root.context)?.let { fm -> + val cal = Calendar.getInstance().apply { timeInMillis = toMills(end) } + val clockFormat = if (DateFormat.is24HourFormat(context)) TimeFormat.CLOCK_24H else TimeFormat.CLOCK_12H + val timePicker = MaterialTimePicker.Builder() + .setTimeFormat(clockFormat) + .setHour(cal.get(Calendar.HOUR_OF_DAY)) + .setMinute(cal.get(Calendar.MINUTE)) + .build() + timePicker.addOnPositiveButtonClickListener { + end = 60 * timePicker.hour + timePicker.minute + text = dateUtil.timeString(toMills(end)) + } + timePicker.show(fm, "input_time_range_end_picker") } } }) @@ -81,4 +86,12 @@ class InputTimeRange(private val rh: ResourceHelper, private val dateUtil: DateU private fun toMills(minutesSinceMidnight: Int): Long = MidnightTime.calcPlusMinutes(minutesSinceMidnight) private fun getMinSinceMidnight(time: Long): Int = Profile.secondsFromMidnight(time) / 60 -} \ No newline at end of file + + private fun getFragmentManager(context: Context?): FragmentManager? { + return when (context) { + is AppCompatActivity -> context.supportFragmentManager + is ContextThemeWrapper -> getFragmentManager(context.baseContext) + else -> null + } + } +} 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 a4cff0630e..7a56579093 100644 --- a/core/src/main/java/info/nightscout/androidaps/dialogs/DialogFragmentWithDate.kt +++ b/core/src/main/java/info/nightscout/androidaps/dialogs/DialogFragmentWithDate.kt @@ -1,7 +1,5 @@ package info.nightscout.androidaps.dialogs -import android.app.DatePickerDialog -import android.app.TimePickerDialog import android.os.Bundle import android.text.format.DateFormat import android.view.View @@ -11,12 +9,15 @@ import android.view.WindowManager import android.widget.Button import android.widget.TextView import androidx.fragment.app.FragmentManager +import com.google.android.material.datepicker.MaterialDatePicker +import com.google.android.material.timepicker.MaterialTimePicker +import com.google.android.material.timepicker.TimeFormat import dagger.android.support.DaggerDialogFragment import info.nightscout.androidaps.core.R +import info.nightscout.androidaps.extensions.toVisibility +import info.nightscout.androidaps.utils.DateUtil import info.nightscout.shared.logging.AAPSLogger import info.nightscout.shared.logging.LTag -import info.nightscout.androidaps.utils.DateUtil -import info.nightscout.androidaps.extensions.toVisibility import info.nightscout.shared.sharedPreferences.SP import java.util.* import java.util.concurrent.atomic.AtomicBoolean @@ -29,6 +30,7 @@ abstract class DialogFragmentWithDate : DaggerDialogFragment() { @Inject lateinit var dateUtil: DateUtil fun interface OnValueChangedListener { + fun onValueChanged(value: Long) } @@ -44,11 +46,6 @@ abstract class DialogFragmentWithDate : DaggerDialogFragment() { //one shot guards private var okClicked: AtomicBoolean = AtomicBoolean(false) - companion object { - - private var seconds: Int = (Math.random() * 59.0).toInt() - } - override fun onStart() { super.onStart() dialog?.window?.setLayout( @@ -78,69 +75,44 @@ abstract class DialogFragmentWithDate : DaggerDialogFragment() { } 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 = view.findViewById(R.id.eventdate) as TextView? eventDateView?.text = dateUtil.dateString(eventTime) - eventTimeView?.text = dateUtil.timeString(eventTime) + eventDateView?.setOnClickListener { + val selection = dateUtil.timeStampToUtcDateMilis(eventTime) + MaterialDatePicker.Builder.datePicker() + .setSelection(selection) + .build() + .apply { + addOnPositiveButtonClickListener { selection -> + eventTime = dateUtil.mergeUtcDateToTimestamp(eventTime, selection) + eventDateView?.text = dateUtil.dateString(eventTime) + callValueChangedListener() - // create an OnDateSetListener - val dateSetListener = - DatePickerDialog.OnDateSetListener { _, year, monthOfYear, dayOfMonth -> - val cal = Calendar.getInstance() - cal.timeInMillis = eventTime - cal.set(Calendar.YEAR, year) - cal.set(Calendar.MONTH, monthOfYear) - cal.set(Calendar.DAY_OF_MONTH, dayOfMonth) - eventTime = cal.timeInMillis - eventDateView?.text = dateUtil.dateString(eventTime) + } + } + .show(parentFragmentManager, "event_time_date_picker") + } + + eventTimeView = view.findViewById(R.id.eventtime) as TextView? + eventTimeView?.text = dateUtil.timeString(eventTime) + eventTimeView?.setOnClickListener { + val clockFormat = if (DateFormat.is24HourFormat(context)) TimeFormat.CLOCK_24H else TimeFormat.CLOCK_12H + val cal = Calendar.getInstance().apply { timeInMillis = eventTime } + val timePicker = MaterialTimePicker.Builder() + .setTimeFormat(clockFormat) + .setHour(cal.get(Calendar.HOUR_OF_DAY)) + .setMinute(cal.get(Calendar.MINUTE)) + .build() + timePicker.addOnPositiveButtonClickListener { + // Randomize seconds to prevent creating record of the same time, if user choose time manually + eventTime = dateUtil.mergeHourMinuteToTimestamp(eventTime, timePicker.hour, timePicker.minute, true) + eventTimeView?.text = dateUtil.timeString(eventTime) callValueChangedListener() } - - eventDateView?.setOnClickListener { - context?.let { - val cal = Calendar.getInstance() - cal.timeInMillis = eventTime - DatePickerDialog( - it, - dateSetListener, - cal.get(Calendar.YEAR), - cal.get(Calendar.MONTH), - cal.get(Calendar.DAY_OF_MONTH) - ).show() - } - } - - // create an OnTimeSetListener - val timeSetListener = TimePickerDialog.OnTimeSetListener { _, hour, minute -> - val cal = Calendar.getInstance() - cal.timeInMillis = eventTime - cal.set(Calendar.HOUR_OF_DAY, hour) - cal.set(Calendar.MINUTE, minute) - cal.set( - Calendar.SECOND, - seconds++ - ) // randomize seconds to prevent creating record of the same time, if user choose time manually - eventTime = cal.timeInMillis - eventTimeView?.text = dateUtil.timeString(eventTime) - callValueChangedListener() - } - - eventTimeView?.setOnClickListener { - context?.let { - val cal = Calendar.getInstance() - cal.timeInMillis = eventTime - TimePickerDialog( - it, - timeSetListener, - cal.get(Calendar.HOUR_OF_DAY), - cal.get(Calendar.MINUTE), - DateFormat.is24HourFormat(context) - ).show() - } + timePicker.show(parentFragmentManager, "event_time_time_picker") } (view.findViewById(R.id.notes_layout) as View?)?.visibility = @@ -189,4 +161,4 @@ abstract class DialogFragmentWithDate : DaggerDialogFragment() { } abstract fun submit(): Boolean -} \ No newline at end of file +} diff --git a/core/src/main/java/info/nightscout/androidaps/utils/DateUtil.kt b/core/src/main/java/info/nightscout/androidaps/utils/DateUtil.kt index 6b6f79f493..a4b072057b 100644 --- a/core/src/main/java/info/nightscout/androidaps/utils/DateUtil.kt +++ b/core/src/main/java/info/nightscout/androidaps/utils/DateUtil.kt @@ -386,12 +386,40 @@ class DateUtil @Inject constructor(private val context: Context) { .firstOrNull() ?: ZoneId.of("UTC") ) + fun timeStampToUtcDateMilis(timestamp: Long): Long { + val current = Calendar.getInstance().apply { timeInMillis = timestamp } + return Calendar.getInstance().apply { + set(Calendar.YEAR, current.get(Calendar.YEAR)) + set(Calendar.MONTH, current.get(Calendar.MONTH)) + set(Calendar.DAY_OF_MONTH, current.get(Calendar.DAY_OF_MONTH)) + }.timeInMillis + } + + fun mergeUtcDateToTimestamp(timestamp: Long, dateUtcMilis: Long): Long { + val selected = Calendar.getInstance().apply { timeInMillis = dateUtcMilis } + return Calendar.getInstance().apply { + timeInMillis = timestamp + set(Calendar.YEAR, selected.get(Calendar.YEAR)) + set(Calendar.MONTH, selected.get(Calendar.MONTH)) + set(Calendar.DAY_OF_MONTH, selected.get(Calendar.DAY_OF_MONTH)) + }.timeInMillis + } + + fun mergeHourMinuteToTimestamp(timestamp: Long, hour: Int, minute: Int, randomSecond: Boolean = false): Long { + return Calendar.getInstance().apply { + timeInMillis = timestamp + set(Calendar.HOUR_OF_DAY, hour) + set(Calendar.MINUTE, minute) + if (randomSecond) set(Calendar.SECOND, seconds++) + }.timeInMillis + } + companion object { private val timeStrings = LongSparseArray() - + private var seconds: Int = (Math.random() * 59.0).toInt() // singletons to avoid repeated allocation private var dfs: DecimalFormatSymbols? = null private var df: DecimalFormat? = null } -} \ No newline at end of file +} diff --git a/core/src/main/res/values-night/styles.xml b/core/src/main/res/values-night/styles.xml index f7b6521c01..400072835a 100644 --- a/core/src/main/res/values-night/styles.xml +++ b/core/src/main/res/values-night/styles.xml @@ -9,6 +9,8 @@ @color/dialog_title_color @color/dialog_title_icon_tint + @style/ThemeOverlay.MaterialComponents.TimePicker + @style/ThemeOverlay.MaterialComponents.MaterialCalendar @color/aaps_theme_dark_secondary @color/primaryLightColorDefault @color/secondaryLightColorDefault diff --git a/core/src/main/res/values/styles.xml b/core/src/main/res/values/styles.xml index d945378321..acb2b76ea6 100644 --- a/core/src/main/res/values/styles.xml +++ b/core/src/main/res/values/styles.xml @@ -19,6 +19,8 @@ @color/dialog_title_color @color/dialog_title_icon_tint + @style/ThemeOverlay.MaterialComponents.TimePicker + @style/ThemeOverlay.MaterialComponents.MaterialCalendar @color/primaryLightColorDefault @color/secondaryLightColorDefault @color/mtrl_scrim_color