Add "Start week on" preference

This commit is contained in:
Naveen 2023-07-22 13:21:51 +05:30
parent 38ae6bcb14
commit 8229492ae7
No known key found for this signature in database
GPG Key ID: 0E155DAD31671DA3
6 changed files with 98 additions and 35 deletions

View File

@ -73,7 +73,7 @@ class MainActivity : SimpleActivity(), RefreshRecyclerViewListener {
private var mStoredBackgroundColor = 0
private var mStoredPrimaryColor = 0
private var mStoredDayCode = ""
private var mStoredIsSundayFirst = false
private var mStoredFirstDayOfWeek = 0
private var mStoredMidnightSpan = true
private var mStoredUse24HourFormat = false
private var mStoredDimPastEvents = true
@ -183,7 +183,7 @@ class MainActivity : SimpleActivity(), RefreshRecyclerViewListener {
}
if (config.storedView == WEEKLY_VIEW) {
if (mStoredIsSundayFirst != config.isSundayFirst || mStoredUse24HourFormat != config.use24HourFormat
if (mStoredFirstDayOfWeek != config.firstDayOfWeek || mStoredUse24HourFormat != config.use24HourFormat
|| mStoredMidnightSpan != config.showMidnightSpanningEventsAtTop || mStoredStartWeekWithCurrentDay != config.startWeekWithCurrentDay
) {
updateViewPager()
@ -318,7 +318,7 @@ class MainActivity : SimpleActivity(), RefreshRecyclerViewListener {
mStoredPrimaryColor = getProperPrimaryColor()
mStoredBackgroundColor = getProperBackgroundColor()
config.apply {
mStoredIsSundayFirst = isSundayFirst
mStoredFirstDayOfWeek = firstDayOfWeek
mStoredUse24HourFormat = use24HourFormat
mStoredDimPastEvents = dimPastEvents
mStoredDimCompletedTasks = dimCompletedTasks

View File

@ -26,6 +26,7 @@ import com.simplemobiletools.commons.models.AlarmSound
import com.simplemobiletools.commons.models.RadioItem
import kotlinx.android.synthetic.main.activity_settings.*
import org.joda.time.DateTime
import org.joda.time.DateTimeConstants
import java.io.File
import java.io.InputStream
import java.text.SimpleDateFormat
@ -63,7 +64,7 @@ class SettingsActivity : SimpleActivity() {
setupManageQuickFilterEventTypes()
setupHourFormat()
setupAllowCreatingTasks()
setupSundayFirst()
setupStartWeekOn()
setupHighlightWeekends()
setupHighlightWeekendsColor()
setupDeleteAllEvents()
@ -343,11 +344,24 @@ class SettingsActivity : SimpleActivity() {
}
}
private fun setupSundayFirst() {
settings_sunday_first.isChecked = config.isSundayFirst
settings_sunday_first_holder.setOnClickListener {
settings_sunday_first.toggle()
config.isSundayFirst = settings_sunday_first.isChecked
private fun setupStartWeekOn() {
val items = arrayListOf(
RadioItem(DateTimeConstants.SUNDAY, getString(R.string.sunday)),
RadioItem(DateTimeConstants.MONDAY, getString(R.string.monday)),
RadioItem(DateTimeConstants.TUESDAY, getString(R.string.tuesday)),
RadioItem(DateTimeConstants.WEDNESDAY, getString(R.string.wednesday)),
RadioItem(DateTimeConstants.THURSDAY, getString(R.string.thursday)),
RadioItem(DateTimeConstants.FRIDAY, getString(R.string.friday)),
RadioItem(DateTimeConstants.SATURDAY, getString(R.string.saturday)),
)
settings_start_week_on.text = getDayOfWeekString(config.firstDayOfWeek)
settings_start_week_on_holder.setOnClickListener {
RadioGroupDialog(this, items, config.firstDayOfWeek) { any ->
val firstDayOfWeek = any as Int
config.firstDayOfWeek = firstDayOfWeek
settings_start_week_on.text = getDayOfWeekString(config.firstDayOfWeek)
}
}
}

View File

@ -532,6 +532,7 @@ fun Context.getNewEventTimestampFromCode(dayCode: String, allowChangingDay: Bool
val currMinutes = calendar.get(Calendar.MINUTE)
dateTime.withMinuteOfHour(currMinutes).seconds()
}
DEFAULT_START_TIME_NEXT_FULL_HOUR -> newDateTime.seconds()
else -> {
val hours = defaultStartTime / 60
@ -692,11 +693,13 @@ fun Context.handleEventDeleting(eventIds: List<Long>, timestamps: List<Long>, ac
eventsHelper.deleteRepeatingEventOccurrence(value, timestamps[index], true)
}
}
DELETE_FUTURE_OCCURRENCES -> {
eventIds.forEachIndexed { index, value ->
eventsHelper.addEventRepeatLimit(value, timestamps[index])
}
}
DELETE_ALL_OCCURRENCES -> {
eventsHelper.deleteEvents(eventIds.toMutableList(), true)
}
@ -762,20 +765,38 @@ fun Context.getFirstDayOfWeek(date: DateTime): String {
}
fun Context.getFirstDayOfWeekDt(date: DateTime): DateTime {
var startOfWeek = date.withTimeAtStartOfDay()
if (!config.startWeekWithCurrentDay) {
startOfWeek = if (config.isSundayFirst) {
// a workaround for Joda-time's Monday-as-first-day-of-the-week
if (startOfWeek.dayOfWeek == DateTimeConstants.SUNDAY) {
startOfWeek
} else {
startOfWeek.minusWeeks(1).withDayOfWeek(DateTimeConstants.SUNDAY)
}
val currentDate = date.withTimeAtStartOfDay()
if (config.startWeekWithCurrentDay) {
return currentDate
} else {
val firstDayOfWeek = config.firstDayOfWeek
val currentDayOfWeek = currentDate.dayOfWeek
return if (currentDayOfWeek == firstDayOfWeek) {
currentDate
} else {
startOfWeek.withDayOfWeek(DateTimeConstants.MONDAY)
// Joda-time's weeks always starts on Monday but user preferred firstDayOfWeek could be any week day
if (firstDayOfWeek < currentDayOfWeek) {
currentDate.withDayOfWeek(firstDayOfWeek)
} else {
currentDate.minusWeeks(1).withDayOfWeek(firstDayOfWeek)
}
}
}
return startOfWeek
}
fun Context.getDayOfWeekString(dayOfWeek: Int): String {
val dayOfWeekResId = when (dayOfWeek) {
DateTimeConstants.MONDAY -> R.string.monday
DateTimeConstants.TUESDAY -> R.string.tuesday
DateTimeConstants.WEDNESDAY -> R.string.wednesday
DateTimeConstants.THURSDAY -> R.string.thursday
DateTimeConstants.FRIDAY -> R.string.friday
DateTimeConstants.SATURDAY -> R.string.saturday
DateTimeConstants.SUNDAY -> R.string.sunday
else -> throw IllegalArgumentException("Invalid day: $dayOfWeek")
}
return getString(dayOfWeekResId)
}
fun Context.isTaskCompleted(event: Event): Boolean {

View File

@ -29,6 +29,10 @@ class Config(context: Context) : BaseConfig(context) {
get() = prefs.getBoolean(START_WEEK_WITH_CURRENT_DAY, false)
set(startWeekWithCurrentDay) = prefs.edit().putBoolean(START_WEEK_WITH_CURRENT_DAY, startWeekWithCurrentDay).apply()
var firstDayOfWeek: Int
get() = prefs.getInt(FIRST_DAY_OF_WEEK, getDefaultFirstDayOfWeekJoda())
set(firstDayOfWeek) = prefs.edit().putInt(FIRST_DAY_OF_WEEK, firstDayOfWeek).apply()
var showMidnightSpanningEventsAtTop: Boolean
get() = prefs.getBoolean(SHOW_MIDNIGHT_SPANNING_EVENTS_AT_TOP, true)
set(midnightSpanning) = prefs.edit().putBoolean(SHOW_MIDNIGHT_SPANNING_EVENTS_AT_TOP, midnightSpanning).apply()

View File

@ -4,6 +4,7 @@ import com.simplemobiletools.calendar.pro.activities.EventActivity
import com.simplemobiletools.calendar.pro.activities.TaskActivity
import com.simplemobiletools.commons.helpers.MONTH_SECONDS
import org.joda.time.DateTime
import org.joda.time.DateTimeConstants
import java.util.*
const val STORED_LOCALLY_ONLY = 0
@ -82,6 +83,7 @@ const val EVENT_LIST_PERIOD = "event_list_period"
const val WEEK_NUMBERS = "week_numbers"
const val START_WEEKLY_AT = "start_weekly_at"
const val START_WEEK_WITH_CURRENT_DAY = "start_week_with_current_day"
const val FIRST_DAY_OF_WEEK = "start_week_on"
const val SHOW_MIDNIGHT_SPANNING_EVENTS_AT_TOP = "show_midnight_spanning_events_at_top"
const val ALLOW_CUSTOMIZE_DAY_COUNT = "allow_customise_day_count"
const val VIBRATE = "vibrate"
@ -299,3 +301,17 @@ fun getPreviousAutoBackupTime(): DateTime {
val nextBackupTime = getNextAutoBackupTime()
return nextBackupTime.minusDays(AUTO_BACKUP_INTERVAL_IN_DAYS)
}
fun getDefaultFirstDayOfWeekJoda(): Int {
val calendar = Calendar.getInstance(Locale.getDefault())
return when (calendar.firstDayOfWeek) {
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 -> DateTimeConstants.SUNDAY
}
}

View File

@ -148,6 +148,29 @@
</RelativeLayout>
<RelativeLayout
android:id="@+id/settings_start_week_on_holder"
style="@style/SettingsHolderTextViewStyle"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<com.simplemobiletools.commons.views.MyTextView
android:id="@+id/settings_start_week_on_label"
style="@style/SettingsTextLabelStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/start_week_on" />
<com.simplemobiletools.commons.views.MyTextView
android:id="@+id/settings_start_week_on"
style="@style/SettingsTextValueStyle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/settings_start_week_on_label"
tools:text="@string/monday" />
</RelativeLayout>
<RelativeLayout
android:id="@+id/settings_hour_format_holder"
style="@style/SettingsHolderCheckboxStyle"
@ -163,21 +186,6 @@
</RelativeLayout>
<RelativeLayout
android:id="@+id/settings_sunday_first_holder"
style="@style/SettingsHolderCheckboxStyle"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<com.simplemobiletools.commons.views.MyAppCompatCheckbox
android:id="@+id/settings_sunday_first"
style="@style/SettingsCheckboxStyle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/sunday_first" />
</RelativeLayout>
<RelativeLayout
android:id="@+id/settings_highlight_weekends_holder"
style="@style/SettingsHolderCheckboxStyle"