Skip to content

Commit

Permalink
Fix timezone offset not accounted for on picker creation
Browse files Browse the repository at this point in the history
Only half the job done on last commit!
Small refactor to deduplicate listener code.
  • Loading branch information
maltaisn committed Dec 29, 2022
1 parent 28fcc31 commit 9520ab2
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import com.maltaisn.notes.sync.R
import com.maltaisn.notes.ui.navGraphViewModel
import com.maltaisn.notes.ui.observeEvent
import java.util.Calendar
import java.util.TimeZone
import javax.inject.Inject

class NotificationActivity : AppCompatActivity() {
Expand All @@ -55,16 +56,14 @@ class NotificationActivity : AppCompatActivity() {
val timePicker = supportFragmentManager.findFragmentByTag(TIME_DIALOG_TAG) as MaterialTimePicker?
if (timePicker != null) {
timePicker.clearOnPositiveButtonClickListeners()
timePicker.addOnPositiveButtonClickListener {
onTimeChanged(timePicker)
}
registerTimePickerListener(timePicker)
}

@Suppress("UNCHECKED_CAST")
val datePicker = supportFragmentManager.findFragmentByTag(DATE_DIALOG_TAG) as MaterialDatePicker<Long>?
if (datePicker != null) {
datePicker.clearOnPositiveButtonClickListeners()
datePicker.addOnPositiveButtonClickListener(::onDateChanged)
registerDatePickerListener(datePicker)
}
}

Expand All @@ -81,15 +80,13 @@ class NotificationActivity : AppCompatActivity() {

val datePicker = MaterialDatePicker.Builder.datePicker()
.setCalendarConstraints(calendarConstraints)
.setSelection(date)
.setSelection(date + TimeZone.getDefault().getOffset(date))
.build()

datePicker.addOnPositiveButtonClickListener(::onDateChanged)

registerDatePickerListener(datePicker)
datePicker.addOnNegativeButtonClickListener {
viewModel.cancelPostpone()
}

datePicker.show(supportFragmentManager, DATE_DIALOG_TAG)
}

Expand All @@ -106,14 +103,10 @@ class NotificationActivity : AppCompatActivity() {
.setMinute(calendar[Calendar.MINUTE])
.build()

timePicker.addOnPositiveButtonClickListener {
onTimeChanged(timePicker)
}

registerTimePickerListener(timePicker)
timePicker.addOnNegativeButtonClickListener {
viewModel.cancelPostpone()
}

timePicker.show(supportFragmentManager, TIME_DIALOG_TAG)
}

Expand All @@ -140,15 +133,21 @@ class NotificationActivity : AppCompatActivity() {
}
}

private fun onDateChanged(selection: Long) {
val calendar = Calendar.getInstance()
// MaterialDatePicker operates on UTC timezone... convert to local timezone (in UTC millis).
calendar.timeInMillis = selection - calendar.timeZone.getOffset(selection)
viewModel.setPostponeDate(calendar[Calendar.YEAR], calendar[Calendar.MONTH], calendar[Calendar.DAY_OF_MONTH])
private fun registerDatePickerListener(picker: MaterialDatePicker<Long>) {
picker.addOnPositiveButtonClickListener { selection ->
val calendar = Calendar.getInstance()
// MaterialDatePicker operates on UTC timezone... convert to local timezone (in UTC millis).
calendar.timeInMillis = selection - TimeZone.getDefault().getOffset(selection)
viewModel.setPostponeDate(calendar[Calendar.YEAR],
calendar[Calendar.MONTH],
calendar[Calendar.DAY_OF_MONTH])
}
}

private fun onTimeChanged(picker: MaterialTimePicker) {
viewModel.setPostponeTime(picker.hour, picker.minute)
private fun registerTimePickerListener(picker: MaterialTimePicker) {
picker.addOnPositiveButtonClickListener {
viewModel.setPostponeTime(picker.hour, picker.minute)
}
}

companion object {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ import com.maltaisn.recurpicker.picker.RecurrencePickerDialog
import debugCheck
import java.text.DateFormat
import java.util.Calendar
import java.util.TimeZone
import javax.inject.Inject
import javax.inject.Provider

Expand Down Expand Up @@ -119,16 +120,14 @@ class ReminderDialog : DialogFragment(), RecurrenceListCallback, RecurrencePicke
val timePicker = childFragmentManager.findFragmentByTag(TIME_DIALOG_TAG) as MaterialTimePicker?
if (timePicker != null) {
timePicker.clearOnPositiveButtonClickListeners()
timePicker.addOnPositiveButtonClickListener {
onTimeChanged(timePicker)
}
registerTimePickerListener(timePicker)
}

@Suppress("UNCHECKED_CAST")
val datePicker = childFragmentManager.findFragmentByTag(DATE_DIALOG_TAG) as MaterialDatePicker<Long>?
if (datePicker != null) {
datePicker.clearOnPositiveButtonClickListeners()
datePicker.addOnPositiveButtonClickListener(::onDateChanged)
registerDatePickerListener(datePicker)
}
}

Expand Down Expand Up @@ -169,11 +168,10 @@ class ReminderDialog : DialogFragment(), RecurrenceListCallback, RecurrencePicke

val datePicker = MaterialDatePicker.Builder.datePicker()
.setCalendarConstraints(calendarConstraints)
.setSelection(date)
.setSelection(date + TimeZone.getDefault().getOffset(date))
.build()

datePicker.addOnPositiveButtonClickListener(::onDateChanged)

registerDatePickerListener(datePicker)
datePicker.show(childFragmentManager, DATE_DIALOG_TAG)
}

Expand All @@ -190,10 +188,7 @@ class ReminderDialog : DialogFragment(), RecurrenceListCallback, RecurrencePicke
.setMinute(calendar[Calendar.MINUTE])
.build()

timePicker.addOnPositiveButtonClickListener {
onTimeChanged(timePicker)
}

registerTimePickerListener(timePicker)
timePicker.show(childFragmentManager, TIME_DIALOG_TAG)
}

Expand Down Expand Up @@ -262,15 +257,19 @@ class ReminderDialog : DialogFragment(), RecurrenceListCallback, RecurrencePicke
}
}

private fun onDateChanged(selection: Long) {
val calendar = Calendar.getInstance()
// MaterialDatePicker operates on UTC timezone... convert to local timezone (in UTC millis).
calendar.timeInMillis = selection - calendar.timeZone.getOffset(selection)
viewModel.changeDate(calendar[Calendar.YEAR], calendar[Calendar.MONTH], calendar[Calendar.DAY_OF_MONTH])
private fun registerDatePickerListener(picker: MaterialDatePicker<Long>) {
picker.addOnPositiveButtonClickListener { selection ->
val calendar = Calendar.getInstance()
// MaterialDatePicker operates on UTC timezone... convert to local timezone (in UTC millis).
calendar.timeInMillis = selection - TimeZone.getDefault().getOffset(selection)
viewModel.changeDate(calendar[Calendar.YEAR], calendar[Calendar.MONTH], calendar[Calendar.DAY_OF_MONTH])
}
}

private fun onTimeChanged(picker: MaterialTimePicker) {
viewModel.changeTime(picker.hour, picker.minute)
private fun registerTimePickerListener(picker: MaterialTimePicker) {
picker.addOnPositiveButtonClickListener {
viewModel.changeTime(picker.hour, picker.minute)
}
}

private fun onDialogShown(dialog: AlertDialog) {
Expand Down

0 comments on commit 9520ab2

Please sign in to comment.