Apply user preferred first day of week in date pickers

This commit is contained in:
Naveen 2023-07-22 13:45:01 +05:30
parent 8229492ae7
commit ed39bbfdda
No known key found for this signature in database
GPG Key ID: 0E155DAD31671DA3
5 changed files with 41 additions and 13 deletions

View File

@ -46,7 +46,6 @@ import kotlinx.android.synthetic.main.activity_event.view.event_reminder_3
import kotlinx.android.synthetic.main.item_attendee.view.* import kotlinx.android.synthetic.main.item_attendee.view.*
import org.joda.time.DateTime import org.joda.time.DateTime
import org.joda.time.DateTimeZone import org.joda.time.DateTimeZone
import java.util.Calendar
import java.util.TimeZone import java.util.TimeZone
import java.util.regex.Pattern import java.util.regex.Pattern
@ -1449,13 +1448,13 @@ class EventActivity : SimpleActivity() {
private fun setupStartDate() { private fun setupStartDate() {
hideKeyboard() hideKeyboard()
val datepicker = DatePickerDialog( val datePicker = DatePickerDialog(
this, getDatePickerDialogTheme(), startDateSetListener, mEventStartDateTime.year, mEventStartDateTime.monthOfYear - 1, this, getDatePickerDialogTheme(), startDateSetListener, mEventStartDateTime.year, mEventStartDateTime.monthOfYear - 1,
mEventStartDateTime.dayOfMonth mEventStartDateTime.dayOfMonth
) )
datepicker.datePicker.firstDayOfWeek = if (config.isSundayFirst) Calendar.SUNDAY else Calendar.MONDAY datePicker.datePicker.firstDayOfWeek = getJavaDayOfWeekFromJoda(config.firstDayOfWeek)
datepicker.show() datePicker.show()
} }
private fun setupStartTime() { private fun setupStartTime() {
@ -1493,13 +1492,13 @@ class EventActivity : SimpleActivity() {
private fun setupEndDate() { private fun setupEndDate() {
hideKeyboard() hideKeyboard()
val datepicker = DatePickerDialog( val datePicker = DatePickerDialog(
this, getDatePickerDialogTheme(), endDateSetListener, mEventEndDateTime.year, mEventEndDateTime.monthOfYear - 1, this, getDatePickerDialogTheme(), endDateSetListener, mEventEndDateTime.year, mEventEndDateTime.monthOfYear - 1,
mEventEndDateTime.dayOfMonth mEventEndDateTime.dayOfMonth
) )
datepicker.datePicker.firstDayOfWeek = if (config.isSundayFirst) Calendar.SUNDAY else Calendar.MONDAY datePicker.datePicker.firstDayOfWeek = getJavaDayOfWeekFromJoda(config.firstDayOfWeek)
datepicker.show() datePicker.show()
} }
private fun setupEndTime() { private fun setupEndTime() {

View File

@ -536,7 +536,7 @@ class TaskActivity : SimpleActivity() {
this, getDatePickerDialogTheme(), dateSetListener, mTaskDateTime.year, mTaskDateTime.monthOfYear - 1, mTaskDateTime.dayOfMonth this, getDatePickerDialogTheme(), dateSetListener, mTaskDateTime.year, mTaskDateTime.monthOfYear - 1, mTaskDateTime.dayOfMonth
) )
datePicker.datePicker.firstDayOfWeek = if (config.isSundayFirst) Calendar.SUNDAY else Calendar.MONDAY datePicker.datePicker.firstDayOfWeek = getJavaDayOfWeekFromJoda(config.firstDayOfWeek)
datePicker.show() datePicker.show()
} }

View File

@ -8,11 +8,11 @@ import com.simplemobiletools.calendar.pro.R
import com.simplemobiletools.calendar.pro.extensions.config import com.simplemobiletools.calendar.pro.extensions.config
import com.simplemobiletools.calendar.pro.extensions.seconds import com.simplemobiletools.calendar.pro.extensions.seconds
import com.simplemobiletools.calendar.pro.helpers.Formatter import com.simplemobiletools.calendar.pro.helpers.Formatter
import com.simplemobiletools.calendar.pro.helpers.getJavaDayOfWeekFromJoda
import com.simplemobiletools.calendar.pro.helpers.getNowSeconds import com.simplemobiletools.calendar.pro.helpers.getNowSeconds
import com.simplemobiletools.commons.extensions.* import com.simplemobiletools.commons.extensions.*
import kotlinx.android.synthetic.main.dialog_repeat_limit_type_picker.view.* import kotlinx.android.synthetic.main.dialog_repeat_limit_type_picker.view.*
import org.joda.time.DateTime import org.joda.time.DateTime
import java.util.*
class RepeatLimitTypePickerDialog(val activity: Activity, var repeatLimit: Long, val startTS: Long, val callback: (repeatLimit: Long) -> Unit) { class RepeatLimitTypePickerDialog(val activity: Activity, var repeatLimit: Long, val startTS: Long, val callback: (repeatLimit: Long) -> Unit) {
private var dialog: AlertDialog? = null private var dialog: AlertDialog? = null
@ -88,13 +88,13 @@ class RepeatLimitTypePickerDialog(val activity: Activity, var repeatLimit: Long,
private fun showRepetitionLimitDialog() { private fun showRepetitionLimitDialog() {
val repeatLimitDateTime = Formatter.getDateTimeFromTS(if (repeatLimit != 0L) repeatLimit else getNowSeconds()) val repeatLimitDateTime = Formatter.getDateTimeFromTS(if (repeatLimit != 0L) repeatLimit else getNowSeconds())
val datepicker = DatePickerDialog( val datePicker = DatePickerDialog(
activity, activity.getDatePickerDialogTheme(), repetitionLimitDateSetListener, repeatLimitDateTime.year, activity, activity.getDatePickerDialogTheme(), repetitionLimitDateSetListener, repeatLimitDateTime.year,
repeatLimitDateTime.monthOfYear - 1, repeatLimitDateTime.dayOfMonth repeatLimitDateTime.monthOfYear - 1, repeatLimitDateTime.dayOfMonth
) )
datepicker.datePicker.firstDayOfWeek = if (activity.config.isSundayFirst) Calendar.SUNDAY else Calendar.MONDAY datePicker.datePicker.firstDayOfWeek = getJavaDayOfWeekFromJoda(activity.config.firstDayOfWeek)
datepicker.show() datePicker.show()
} }
private val repetitionLimitDateSetListener = DatePickerDialog.OnDateSetListener { v, year, monthOfYear, dayOfMonth -> private val repetitionLimitDateSetListener = DatePickerDialog.OnDateSetListener { v, year, monthOfYear, dayOfMonth ->

View File

@ -30,7 +30,10 @@ class Config(context: Context) : BaseConfig(context) {
set(startWeekWithCurrentDay) = prefs.edit().putBoolean(START_WEEK_WITH_CURRENT_DAY, startWeekWithCurrentDay).apply() set(startWeekWithCurrentDay) = prefs.edit().putBoolean(START_WEEK_WITH_CURRENT_DAY, startWeekWithCurrentDay).apply()
var firstDayOfWeek: Int var firstDayOfWeek: Int
get() = prefs.getInt(FIRST_DAY_OF_WEEK, getDefaultFirstDayOfWeekJoda()) get() {
val defaultFirstDayOfWeek = Calendar.getInstance(Locale.getDefault()).firstDayOfWeek
return prefs.getInt(FIRST_DAY_OF_WEEK, getJodaDayOfWeekFromJava(defaultFirstDayOfWeek))
}
set(firstDayOfWeek) = prefs.edit().putInt(FIRST_DAY_OF_WEEK, firstDayOfWeek).apply() set(firstDayOfWeek) = prefs.edit().putInt(FIRST_DAY_OF_WEEK, firstDayOfWeek).apply()
var showMidnightSpanningEventsAtTop: Boolean var showMidnightSpanningEventsAtTop: Boolean

View File

@ -315,3 +315,29 @@ fun getDefaultFirstDayOfWeekJoda(): Int {
else -> DateTimeConstants.SUNDAY else -> DateTimeConstants.SUNDAY
} }
} }
fun getJodaDayOfWeekFromJava(dayOfWeek: Int): Int {
return when (dayOfWeek) {
Calendar.SUNDAY -> DateTimeConstants.SUNDAY
Calendar.MONDAY -> DateTimeConstants.MONDAY
Calendar.TUESDAY -> DateTimeConstants.TUESDAY
Calendar.WEDNESDAY -> DateTimeConstants.WEDNESDAY
Calendar.THURSDAY -> DateTimeConstants.THURSDAY
Calendar.FRIDAY -> DateTimeConstants.FRIDAY
Calendar.SATURDAY -> DateTimeConstants.SATURDAY
else -> throw IllegalArgumentException("Invalid day: $dayOfWeek")
}
}
fun getJavaDayOfWeekFromJoda(dayOfWeek: Int): Int {
return when (dayOfWeek) {
DateTimeConstants.SUNDAY -> Calendar.SUNDAY
DateTimeConstants.MONDAY -> Calendar.MONDAY
DateTimeConstants.TUESDAY -> Calendar.TUESDAY
DateTimeConstants.WEDNESDAY -> Calendar.WEDNESDAY
DateTimeConstants.THURSDAY -> Calendar.THURSDAY
DateTimeConstants.FRIDAY -> Calendar.FRIDAY
DateTimeConstants.SATURDAY -> Calendar.SATURDAY
else -> throw IllegalArgumentException("Invalid day: $dayOfWeek")
}
}