Merge pull request #1616 from Andries-Smit/style/material-date-time-picker

style: use material date and time ticker
This commit is contained in:
Milos Kozak 2022-04-14 12:07:45 +02:00 committed by GitHub
commit 1ba67d9f7e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
9 changed files with 220 additions and 210 deletions

View file

@ -1,7 +1,6 @@
package info.nightscout.androidaps.activities package info.nightscout.androidaps.activities
import android.annotation.SuppressLint import android.annotation.SuppressLint
import android.app.DatePickerDialog
import android.content.Context import android.content.Context
import android.os.Bundle import android.os.Bundle
import android.util.DisplayMetrics import android.util.DisplayMetrics
@ -9,6 +8,7 @@ import android.view.ViewGroup
import android.widget.LinearLayout import android.widget.LinearLayout
import android.widget.RelativeLayout import android.widget.RelativeLayout
import android.widget.TextView import android.widget.TextView
import com.google.android.material.datepicker.MaterialDatePicker
import com.jjoe64.graphview.GraphView import com.jjoe64.graphview.GraphView
import dagger.android.HasAndroidInjector import dagger.android.HasAndroidInjector
import info.nightscout.androidaps.R import info.nightscout.androidaps.R
@ -148,33 +148,18 @@ class HistoryBrowseActivity : NoSplashAppCompatActivity() {
true true
} }
// create an OnDateSetListener binding.date.setOnClickListener {
val dateSetListener = DatePickerDialog.OnDateSetListener { _, year, monthOfYear, dayOfMonth -> MaterialDatePicker.Builder.datePicker()
Calendar.getInstance().also { calendar -> .setSelection(dateUtil.timeStampToUtcDateMilis(overviewData.fromTime))
calendar.timeInMillis = overviewData.fromTime .build()
calendar[Calendar.YEAR] = year .apply {
calendar[Calendar.MONTH] = monthOfYear addOnPositiveButtonClickListener { selection ->
calendar[Calendar.DAY_OF_MONTH] = dayOfMonth setTime(dateUtil.mergeUtcDateToTimestamp(overviewData.fromTime, selection))
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) binding.date.text = dateUtil.dateAndTimeString(overviewData.fromTime)
}
loadAll("onClickDate") loadAll("onClickDate")
} }
}
binding.date.setOnClickListener { .show(supportFragmentManager, "history_date_picker")
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()
} }
val dm = DisplayMetrics() val dm = DisplayMetrics()

View file

@ -1,6 +1,5 @@
package info.nightscout.androidaps.plugins.general.overview.dialogs package info.nightscout.androidaps.plugins.general.overview.dialogs
import android.app.TimePickerDialog
import android.os.Bundle import android.os.Bundle
import android.text.format.DateFormat import android.text.format.DateFormat
import android.view.LayoutInflater import android.view.LayoutInflater
@ -8,6 +7,8 @@ import android.view.View
import android.view.ViewGroup import android.view.ViewGroup
import android.view.Window import android.view.Window
import android.view.WindowManager import android.view.WindowManager
import com.google.android.material.timepicker.MaterialTimePicker
import com.google.android.material.timepicker.TimeFormat
import dagger.android.support.DaggerDialogFragment import dagger.android.support.DaggerDialogFragment
import info.nightscout.androidaps.R import info.nightscout.androidaps.R
import info.nightscout.androidaps.databinding.OverviewEditquickwizardDialogBinding 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.androidaps.utils.wizard.QuickWizardEntry
import info.nightscout.shared.sharedPreferences.SP import info.nightscout.shared.sharedPreferences.SP
import org.json.JSONException import org.json.JSONException
import java.util.*
import javax.inject.Inject import javax.inject.Inject
class EditQuickWizardDialog : DaggerDialogFragment(), View.OnClickListener { class EditQuickWizardDialog : DaggerDialogFragment(), View.OnClickListener {
@ -40,8 +42,7 @@ class EditQuickWizardDialog : DaggerDialogFragment(), View.OnClickListener {
private var _binding: OverviewEditquickwizardDialogBinding? = null private var _binding: OverviewEditquickwizardDialogBinding? = null
// This property is only valid between onCreateView and // This property is only valid between onCreateView and onDestroyView.
// onDestroyView.
private val binding get() = _binding!! private val binding get() = _binding!!
override fun onCreateView( override fun onCreateView(
@ -96,41 +97,35 @@ class EditQuickWizardDialog : DaggerDialogFragment(), View.OnClickListener {
} }
binding.okcancel.cancel.setOnClickListener { dismiss() } binding.okcancel.cancel.setOnClickListener { dismiss() }
// create an OnTimeSetListener binding.from.setOnClickListener {
val fromTimeSetListener = TimePickerDialog.OnTimeSetListener { _, hour, minute -> val clockFormat = if (DateFormat.is24HourFormat(context)) TimeFormat.CLOCK_24H else TimeFormat.CLOCK_12H
fromSeconds = (T.hours(hour.toLong()).secs() + T.mins(minute.toLong()).secs()).toInt() 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)) 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() fromSeconds = entry.validFrom()
binding.from.text = dateUtil.timeString(dateUtil.secondsOfTheDayToMilliseconds(fromSeconds)) binding.from.text = dateUtil.timeString(dateUtil.secondsOfTheDayToMilliseconds(fromSeconds))
val toTimeSetListener = TimePickerDialog.OnTimeSetListener { _, hour, minute -> binding.to.setOnClickListener {
toSeconds = (T.hours(hour.toLong()).secs() + T.mins(minute.toLong()).secs()).toInt() 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)) binding.to.text = dateUtil.timeString(dateUtil.secondsOfTheDayToMilliseconds(toSeconds))
} }
timePicker.show(parentFragmentManager, "event_time_time_picker")
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()
}
} }
fun usePercentage(custom: Boolean) { fun usePercentage(custom: Boolean) {

View file

@ -1,12 +1,17 @@
package info.nightscout.androidaps.plugins.general.automation.elements package info.nightscout.androidaps.plugins.general.automation.elements
import android.app.DatePickerDialog import android.content.Context
import android.app.TimePickerDialog
import android.graphics.Typeface import android.graphics.Typeface
import android.text.format.DateFormat import android.text.format.DateFormat
import android.view.ViewGroup import android.view.ViewGroup
import android.widget.LinearLayout import android.widget.LinearLayout
import android.widget.TextView 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.automation.R
import info.nightscout.androidaps.utils.DateUtil import info.nightscout.androidaps.utils.DateUtil
import info.nightscout.androidaps.utils.resources.ResourceHelper 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) text = dateUtil.dateString(value)
setPadding(px, px, px, px) setPadding(px, px, px, px)
setOnClickListener { setOnClickListener {
root.context?.let { getFragmentManager(root.context)?.let { fm ->
val cal = Calendar.getInstance() MaterialDatePicker.Builder.datePicker()
cal.timeInMillis = value .setSelection(dateUtil.timeStampToUtcDateMilis(value))
DatePickerDialog( .build()
it, .apply {
{ _, year, monthOfYear, dayOfMonth -> addOnPositiveButtonClickListener { selection ->
value = Calendar.getInstance().apply { value = dateUtil.mergeUtcDateToTimestamp(value, selection)
timeInMillis = value
set(Calendar.YEAR, year)
set(Calendar.MONTH, monthOfYear)
set(Calendar.DAY_OF_MONTH, dayOfMonth)
}.timeInMillis
text = dateUtil.dateString(value) text = dateUtil.dateString(value)
}, }
cal.get(Calendar.YEAR), }
cal.get(Calendar.MONTH), .show(fm, "input_date_picker")
cal.get(Calendar.DAY_OF_MONTH)
).show()
} }
} }
}) })
@ -58,28 +56,31 @@ class InputDateTime(private val rh: ResourceHelper, private val dateUtil: DateUt
text = dateUtil.timeString(value) text = dateUtil.timeString(value)
setPadding(px, px, px, px) setPadding(px, px, px, px)
setOnClickListener { setOnClickListener {
root.context?.let { getFragmentManager(root.context)?.let { fm ->
val cal = Calendar.getInstance() val cal = Calendar.getInstance().apply { timeInMillis = value }
cal.timeInMillis = value val clockFormat = if (DateFormat.is24HourFormat(context)) TimeFormat.CLOCK_24H else TimeFormat.CLOCK_12H
TimePickerDialog( val timePicker = MaterialTimePicker.Builder()
it, .setTimeFormat(clockFormat)
{ _, hour, minute -> .setHour(cal.get(Calendar.HOUR_OF_DAY))
value = Calendar.getInstance().apply { .setMinute(cal.get(Calendar.MINUTE))
timeInMillis = value .build()
set(Calendar.HOUR_OF_DAY, hour) timePicker.addOnPositiveButtonClickListener {
set(Calendar.MINUTE, minute) value = dateUtil.mergeHourMinuteToTimestamp(value, timePicker.hour, timePicker.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) text = dateUtil.timeString(value)
}, }
cal.get(Calendar.HOUR_OF_DAY), timePicker.show(fm, "input_time_picker")
cal.get(Calendar.MINUTE),
DateFormat.is24HourFormat(it)
).show()
} }
} }
} }
) )
}) })
} }
private fun getFragmentManager(context: Context?): FragmentManager? {
return when (context) {
is AppCompatActivity -> context.supportFragmentManager
is ContextThemeWrapper -> getFragmentManager(context.baseContext)
else -> null
}
}
} }

View file

@ -1,12 +1,16 @@
package info.nightscout.androidaps.plugins.general.automation.elements package info.nightscout.androidaps.plugins.general.automation.elements
import android.app.TimePickerDialog import android.content.Context
import android.graphics.Typeface import android.graphics.Typeface
import android.text.format.DateFormat import android.text.format.DateFormat
import android.view.Gravity import android.view.ContextThemeWrapper
import android.view.ViewGroup import android.view.ViewGroup
import android.widget.LinearLayout import android.widget.LinearLayout
import android.widget.TextView 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.automation.R
import info.nightscout.androidaps.interfaces.Profile import info.nightscout.androidaps.interfaces.Profile
import info.nightscout.androidaps.utils.DateUtil 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) val px = rh.dpToPx(10)
setPadding(px, px, px, px) setPadding(px, px, px, px)
setOnClickListener { setOnClickListener {
root.context?.let { getFragmentManager(root.context)?.let { fm ->
val cal = Calendar.getInstance() val cal = Calendar.getInstance().apply { timeInMillis = toMills(value) }
cal.timeInMillis = toMills(value) val clockFormat = if (DateFormat.is24HourFormat(context)) TimeFormat.CLOCK_24H else TimeFormat.CLOCK_12H
TimePickerDialog( val timePicker = MaterialTimePicker.Builder()
it, .setTimeFormat(clockFormat)
{ _, hour, minute -> .setHour(cal.get(Calendar.HOUR_OF_DAY))
value = 60 * hour + minute .setMinute(cal.get(Calendar.MINUTE))
.build()
timePicker.addOnPositiveButtonClickListener {
value = 60 * timePicker.hour + timePicker.minute
text = dateUtil.timeString(toMills(value)) text = dateUtil.timeString(toMills(value))
}, }
cal.get(Calendar.HOUR_OF_DAY), timePicker.show(fm, "input_time_picker")
cal.get(Calendar.MINUTE),
DateFormat.is24HourFormat(it)
).show()
} }
} }
}) })
@ -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 toMills(minutesSinceMidnight: Int): Long = MidnightTime.calcPlusMinutes(minutesSinceMidnight)
private fun getMinSinceMidnight(time: Long): Int = Profile.secondsFromMidnight(time) / 60 private fun getMinSinceMidnight(time: Long): Int = Profile.secondsFromMidnight(time) / 60
private fun getFragmentManager(context: Context?): FragmentManager? {
return when (context) {
is AppCompatActivity -> context.supportFragmentManager
is ContextThemeWrapper -> getFragmentManager(context.baseContext)
else -> null
}
}
} }

View file

@ -1,12 +1,17 @@
package info.nightscout.androidaps.plugins.general.automation.elements package info.nightscout.androidaps.plugins.general.automation.elements
import android.app.TimePickerDialog import android.content.Context
import android.graphics.Typeface import android.graphics.Typeface
import android.text.format.DateFormat import android.text.format.DateFormat
import android.view.ContextThemeWrapper
import android.view.Gravity import android.view.Gravity
import android.view.ViewGroup import android.view.ViewGroup
import android.widget.LinearLayout import android.widget.LinearLayout
import android.widget.TextView 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.automation.R
import info.nightscout.androidaps.interfaces.Profile import info.nightscout.androidaps.interfaces.Profile
import info.nightscout.androidaps.utils.DateUtil 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)) text = dateUtil.timeString(toMills(start))
setPadding(px, px, px, px) setPadding(px, px, px, px)
setOnClickListener { setOnClickListener {
root.context?.let { getFragmentManager(root.context)?.let { fm ->
val cal = Calendar.getInstance() val cal = Calendar.getInstance().apply { timeInMillis = toMills(start) }
cal.timeInMillis = toMills(start) val clockFormat = if (DateFormat.is24HourFormat(context)) TimeFormat.CLOCK_24H else TimeFormat.CLOCK_12H
TimePickerDialog( val timePicker = MaterialTimePicker.Builder()
it, .setTimeFormat(clockFormat)
{ _, hour, minute -> .setHour(cal.get(Calendar.HOUR_OF_DAY))
start = 60 * hour + minute .setMinute(cal.get(Calendar.MINUTE))
.build()
timePicker.addOnPositiveButtonClickListener {
start = 60 * timePicker.hour + timePicker.minute
text = dateUtil.timeString(toMills(start)) text = dateUtil.timeString(toMills(start))
}, }
cal.get(Calendar.HOUR_OF_DAY), timePicker.show(fm, "input_time_range_start_picker")
cal.get(Calendar.MINUTE),
DateFormat.is24HourFormat(it)
).show()
} }
} }
}) })
@ -59,19 +64,19 @@ class InputTimeRange(private val rh: ResourceHelper, private val dateUtil: DateU
text = rh.gs(R.string.and) + " " + dateUtil.timeString(toMills(end)) text = rh.gs(R.string.and) + " " + dateUtil.timeString(toMills(end))
setPadding(px, px, px, px) setPadding(px, px, px, px)
setOnClickListener { setOnClickListener {
root.context?.let { getFragmentManager(root.context)?.let { fm ->
val cal = Calendar.getInstance() val cal = Calendar.getInstance().apply { timeInMillis = toMills(end) }
cal.timeInMillis = toMills(end) val clockFormat = if (DateFormat.is24HourFormat(context)) TimeFormat.CLOCK_24H else TimeFormat.CLOCK_12H
TimePickerDialog( val timePicker = MaterialTimePicker.Builder()
it, .setTimeFormat(clockFormat)
{ _, hour, minute -> .setHour(cal.get(Calendar.HOUR_OF_DAY))
end = 60 * hour + minute .setMinute(cal.get(Calendar.MINUTE))
.build()
timePicker.addOnPositiveButtonClickListener {
end = 60 * timePicker.hour + timePicker.minute
text = dateUtil.timeString(toMills(end)) text = dateUtil.timeString(toMills(end))
}, }
cal.get(Calendar.HOUR_OF_DAY), timePicker.show(fm, "input_time_range_end_picker")
cal.get(Calendar.MINUTE),
DateFormat.is24HourFormat(it)
).show()
} }
} }
}) })
@ -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 toMills(minutesSinceMidnight: Int): Long = MidnightTime.calcPlusMinutes(minutesSinceMidnight)
private fun getMinSinceMidnight(time: Long): Int = Profile.secondsFromMidnight(time) / 60 private fun getMinSinceMidnight(time: Long): Int = Profile.secondsFromMidnight(time) / 60
private fun getFragmentManager(context: Context?): FragmentManager? {
return when (context) {
is AppCompatActivity -> context.supportFragmentManager
is ContextThemeWrapper -> getFragmentManager(context.baseContext)
else -> null
}
}
} }

View file

@ -1,7 +1,5 @@
package info.nightscout.androidaps.dialogs package info.nightscout.androidaps.dialogs
import android.app.DatePickerDialog
import android.app.TimePickerDialog
import android.os.Bundle import android.os.Bundle
import android.text.format.DateFormat import android.text.format.DateFormat
import android.view.View import android.view.View
@ -11,12 +9,15 @@ import android.view.WindowManager
import android.widget.Button import android.widget.Button
import android.widget.TextView import android.widget.TextView
import androidx.fragment.app.FragmentManager 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 dagger.android.support.DaggerDialogFragment
import info.nightscout.androidaps.core.R 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.AAPSLogger
import info.nightscout.shared.logging.LTag 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 info.nightscout.shared.sharedPreferences.SP
import java.util.* import java.util.*
import java.util.concurrent.atomic.AtomicBoolean import java.util.concurrent.atomic.AtomicBoolean
@ -29,6 +30,7 @@ abstract class DialogFragmentWithDate : DaggerDialogFragment() {
@Inject lateinit var dateUtil: DateUtil @Inject lateinit var dateUtil: DateUtil
fun interface OnValueChangedListener { fun interface OnValueChangedListener {
fun onValueChanged(value: Long) fun onValueChanged(value: Long)
} }
@ -44,11 +46,6 @@ abstract class DialogFragmentWithDate : DaggerDialogFragment() {
//one shot guards //one shot guards
private var okClicked: AtomicBoolean = AtomicBoolean(false) private var okClicked: AtomicBoolean = AtomicBoolean(false)
companion object {
private var seconds: Int = (Math.random() * 59.0).toInt()
}
override fun onStart() { override fun onStart() {
super.onStart() super.onStart()
dialog?.window?.setLayout( dialog?.window?.setLayout(
@ -78,69 +75,44 @@ abstract class DialogFragmentWithDate : DaggerDialogFragment() {
} }
override fun onViewCreated(view: View, savedInstanceState: Bundle?) { 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() eventTimeOriginal = savedInstanceState?.getLong("eventTimeOriginal") ?: dateUtil.nowWithoutMilliseconds()
eventTime = savedInstanceState?.getLong("eventTime") ?: eventTimeOriginal eventTime = savedInstanceState?.getLong("eventTime") ?: eventTimeOriginal
eventDateView = view.findViewById(R.id.eventdate) as TextView?
eventDateView?.text = dateUtil.dateString(eventTime) eventDateView?.text = dateUtil.dateString(eventTime)
eventTimeView?.text = dateUtil.timeString(eventTime)
// 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)
callValueChangedListener()
}
eventDateView?.setOnClickListener { eventDateView?.setOnClickListener {
context?.let { val selection = dateUtil.timeStampToUtcDateMilis(eventTime)
val cal = Calendar.getInstance() MaterialDatePicker.Builder.datePicker()
cal.timeInMillis = eventTime .setSelection(selection)
DatePickerDialog( .build()
it, .apply {
dateSetListener, addOnPositiveButtonClickListener { selection ->
cal.get(Calendar.YEAR), eventTime = dateUtil.mergeUtcDateToTimestamp(eventTime, selection)
cal.get(Calendar.MONTH), eventDateView?.text = dateUtil.dateString(eventTime)
cal.get(Calendar.DAY_OF_MONTH) callValueChangedListener()
).show()
} }
} }
.show(parentFragmentManager, "event_time_date_picker")
}
// create an OnTimeSetListener eventTimeView = view.findViewById(R.id.eventtime) as TextView?
val timeSetListener = TimePickerDialog.OnTimeSetListener { _, hour, minute -> eventTimeView?.text = dateUtil.timeString(eventTime)
val cal = Calendar.getInstance() eventTimeView?.setOnClickListener {
cal.timeInMillis = eventTime val clockFormat = if (DateFormat.is24HourFormat(context)) TimeFormat.CLOCK_24H else TimeFormat.CLOCK_12H
cal.set(Calendar.HOUR_OF_DAY, hour) val cal = Calendar.getInstance().apply { timeInMillis = eventTime }
cal.set(Calendar.MINUTE, minute) val timePicker = MaterialTimePicker.Builder()
cal.set( .setTimeFormat(clockFormat)
Calendar.SECOND, .setHour(cal.get(Calendar.HOUR_OF_DAY))
seconds++ .setMinute(cal.get(Calendar.MINUTE))
) // randomize seconds to prevent creating record of the same time, if user choose time manually .build()
eventTime = cal.timeInMillis 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) eventTimeView?.text = dateUtil.timeString(eventTime)
callValueChangedListener() callValueChangedListener()
} }
timePicker.show(parentFragmentManager, "event_time_time_picker")
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()
}
} }
(view.findViewById(R.id.notes_layout) as View?)?.visibility = (view.findViewById(R.id.notes_layout) as View?)?.visibility =

View file

@ -386,10 +386,38 @@ class DateUtil @Inject constructor(private val context: Context) {
.firstOrNull() ?: ZoneId.of("UTC") .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 { companion object {
private val timeStrings = LongSparseArray<String>() private val timeStrings = LongSparseArray<String>()
private var seconds: Int = (Math.random() * 59.0).toInt()
// singletons to avoid repeated allocation // singletons to avoid repeated allocation
private var dfs: DecimalFormatSymbols? = null private var dfs: DecimalFormatSymbols? = null
private var df: DecimalFormat? = null private var df: DecimalFormat? = null

View file

@ -9,6 +9,8 @@
<item name="dialogTitleColor">@color/dialog_title_color</item> <item name="dialogTitleColor">@color/dialog_title_color</item>
<item name="dialogTitleIconTint">@color/dialog_title_icon_tint</item> <item name="dialogTitleIconTint">@color/dialog_title_icon_tint</item>
<!-- New MaterialComponents attributes. --> <!-- New MaterialComponents attributes. -->
<item name="materialTimePickerTheme">@style/ThemeOverlay.MaterialComponents.TimePicker</item>
<item name="materialCalendarTheme">@style/ThemeOverlay.MaterialComponents.MaterialCalendar</item>
<item name="colorSecondary">@color/aaps_theme_dark_secondary</item> <item name="colorSecondary">@color/aaps_theme_dark_secondary</item>
<item name="colorPrimaryVariant">@color/primaryLightColorDefault</item> <item name="colorPrimaryVariant">@color/primaryLightColorDefault</item>
<item name="colorSecondaryVariant">@color/secondaryLightColorDefault</item> <item name="colorSecondaryVariant">@color/secondaryLightColorDefault</item>

View file

@ -19,6 +19,8 @@
<item name="dialogTitleColor">@color/dialog_title_color</item> <item name="dialogTitleColor">@color/dialog_title_color</item>
<item name="dialogTitleIconTint">@color/dialog_title_icon_tint</item> <item name="dialogTitleIconTint">@color/dialog_title_icon_tint</item>
<!-- New MaterialComponents attributes. --> <!-- New MaterialComponents attributes. -->
<item name="materialTimePickerTheme">@style/ThemeOverlay.MaterialComponents.TimePicker</item>
<item name="materialCalendarTheme">@style/ThemeOverlay.MaterialComponents.MaterialCalendar</item>
<item name="colorPrimaryVariant">@color/primaryLightColorDefault</item> <item name="colorPrimaryVariant">@color/primaryLightColorDefault</item>
<item name="colorSecondaryVariant">@color/secondaryLightColorDefault</item> <item name="colorSecondaryVariant">@color/secondaryLightColorDefault</item>
<item name="scrimBackground">@color/mtrl_scrim_color</item> <item name="scrimBackground">@color/mtrl_scrim_color</item>