From 2ee226d06b3ed4da55be534dc403788fcb823ce2 Mon Sep 17 00:00:00 2001 From: Naveen Date: Sun, 16 Jul 2023 02:54:51 +0530 Subject: [PATCH 1/2] Use local timezone to configure weekly views MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This addresses issues like https://github.com/SimpleMobileTools/Simple-Calendar/issues/2143 The weekly view originally used local timezone to configure everything but was modified to use UTC to fix a `Start week with Sunday` issue https://github.com/SimpleMobileTools/Simple-Calendar/issues/1213 where events were created on the wrong day. Since `getFirstDayOfWeek()` was rewritten to handle week preferences properly, there is no need to complicate things using UTC anymore (otherwise we'll have to handle UTC-to-local-timezone conversion in many places) This also fixes an issue with the drag-and-drop feature where the events were moved by ±1 day because the difference in timezone was not taken care of. --- .../simplemobiletools/calendar/pro/extensions/Context.kt | 7 +++---- .../calendar/pro/fragments/WeekFragment.kt | 4 ++-- .../calendar/pro/fragments/WeekFragmentsHolder.kt | 2 +- 3 files changed, 6 insertions(+), 7 deletions(-) diff --git a/app/src/main/kotlin/com/simplemobiletools/calendar/pro/extensions/Context.kt b/app/src/main/kotlin/com/simplemobiletools/calendar/pro/extensions/Context.kt index b7e79f5e3..9b1321279 100644 --- a/app/src/main/kotlin/com/simplemobiletools/calendar/pro/extensions/Context.kt +++ b/app/src/main/kotlin/com/simplemobiletools/calendar/pro/extensions/Context.kt @@ -20,7 +20,6 @@ import android.provider.CalendarContract import android.view.View import android.view.ViewGroup import android.widget.LinearLayout -import android.widget.Toast import androidx.constraintlayout.widget.ConstraintLayout import androidx.core.app.AlarmManagerCompat import androidx.core.app.NotificationCompat @@ -48,7 +47,6 @@ import com.simplemobiletools.commons.helpers.* import kotlinx.android.synthetic.main.day_monthly_event_view.view.* import org.joda.time.DateTime import org.joda.time.DateTimeConstants -import org.joda.time.DateTimeZone import org.joda.time.LocalDate import java.io.File import java.io.FileOutputStream @@ -327,7 +325,8 @@ fun Context.notifyEvent(originalEvent: Event) { val events = eventsHelper.getRepeatableEventsFor(currentSeconds - WEEK_SECONDS, currentSeconds + YEAR_SECONDS, event.id!!) for (currEvent in events) { eventStartTS = if (currEvent.getIsAllDay()) Formatter.getDayStartTS(Formatter.getDayCodeFromTS(currEvent.startTS)) else currEvent.startTS - val firstReminderMinutes = arrayOf(currEvent.reminder3Minutes, currEvent.reminder2Minutes, currEvent.reminder1Minutes).filter { it != REMINDER_OFF }.max() + val firstReminderMinutes = + arrayOf(currEvent.reminder3Minutes, currEvent.reminder2Minutes, currEvent.reminder1Minutes).filter { it != REMINDER_OFF }.max() if (eventStartTS - firstReminderMinutes * 60 > currentSeconds) { break } @@ -759,7 +758,7 @@ fun Context.editEvent(event: ListEvent) { } fun Context.getFirstDayOfWeek(date: DateTime): String { - var startOfWeek = date.withZoneRetainFields(DateTimeZone.UTC).withTimeAtStartOfDay() + var startOfWeek = date.withTimeAtStartOfDay() if (!config.startWeekWithCurrentDay) { startOfWeek = if (config.isSundayFirst) { // a workaround for Joda-time's Monday-as-first-day-of-the-week diff --git a/app/src/main/kotlin/com/simplemobiletools/calendar/pro/fragments/WeekFragment.kt b/app/src/main/kotlin/com/simplemobiletools/calendar/pro/fragments/WeekFragment.kt index 5433d94ef..d3e9525a6 100644 --- a/app/src/main/kotlin/com/simplemobiletools/calendar/pro/fragments/WeekFragment.kt +++ b/app/src/main/kotlin/com/simplemobiletools/calendar/pro/fragments/WeekFragment.kt @@ -196,14 +196,14 @@ class WeekFragment : Fragment(), WeeklyCalendar { mView.week_events_columns_holder.removeAllViews() (0 until config.weeklyViewDays).forEach { val column = inflater.inflate(R.layout.weekly_view_day_column, mView.week_events_columns_holder, false) as RelativeLayout - column.tag = Formatter.getUTCDayCodeFromTS(weekTimestamp + it * DAY_SECONDS) + column.tag = Formatter.getDayCodeFromTS(weekTimestamp + it * DAY_SECONDS) mView.week_events_columns_holder.addView(column) dayColumns.add(column) } } private fun setupDayLabels() { - var curDay = Formatter.getUTCDateTimeFromTS(weekTimestamp) + var curDay = Formatter.getDateTimeFromTS(weekTimestamp) val todayCode = Formatter.getDayCodeFromDateTime(DateTime()) val screenWidth = context?.usableScreenSize?.x ?: return val dayWidth = screenWidth / config.weeklyViewDays diff --git a/app/src/main/kotlin/com/simplemobiletools/calendar/pro/fragments/WeekFragmentsHolder.kt b/app/src/main/kotlin/com/simplemobiletools/calendar/pro/fragments/WeekFragmentsHolder.kt index e8e5a60a3..4204e360a 100644 --- a/app/src/main/kotlin/com/simplemobiletools/calendar/pro/fragments/WeekFragmentsHolder.kt +++ b/app/src/main/kotlin/com/simplemobiletools/calendar/pro/fragments/WeekFragmentsHolder.kt @@ -311,7 +311,7 @@ class WeekFragmentsHolder : MyFragmentHolder(), WeekFragmentListener { override fun getCurrentDate(): DateTime? { return if (currentWeekTS != 0L) { - Formatter.getUTCDateTimeFromTS(currentWeekTS) + Formatter.getDateTimeFromTS(currentWeekTS) } else { null } From f06b7864abaf71c22abcab279cbe587b687aa389 Mon Sep 17 00:00:00 2001 From: Naveen Date: Sun, 16 Jul 2023 03:07:28 +0530 Subject: [PATCH 2/2] Reuse `getFirstDayOfWeek()` in WeekFragment --- .../calendar/pro/extensions/Context.kt | 6 +++++- .../calendar/pro/fragments/WeekFragmentsHolder.kt | 15 ++------------- 2 files changed, 7 insertions(+), 14 deletions(-) diff --git a/app/src/main/kotlin/com/simplemobiletools/calendar/pro/extensions/Context.kt b/app/src/main/kotlin/com/simplemobiletools/calendar/pro/extensions/Context.kt index 9b1321279..e4ecdfafd 100644 --- a/app/src/main/kotlin/com/simplemobiletools/calendar/pro/extensions/Context.kt +++ b/app/src/main/kotlin/com/simplemobiletools/calendar/pro/extensions/Context.kt @@ -758,6 +758,10 @@ fun Context.editEvent(event: ListEvent) { } fun Context.getFirstDayOfWeek(date: DateTime): String { + return getFirstDayOfWeekDt(date).toString() +} + +fun Context.getFirstDayOfWeekDt(date: DateTime): DateTime { var startOfWeek = date.withTimeAtStartOfDay() if (!config.startWeekWithCurrentDay) { startOfWeek = if (config.isSundayFirst) { @@ -771,7 +775,7 @@ fun Context.getFirstDayOfWeek(date: DateTime): String { startOfWeek.withDayOfWeek(DateTimeConstants.MONDAY) } } - return startOfWeek.toString() + return startOfWeek } fun Context.isTaskCompleted(event: Event): Boolean { diff --git a/app/src/main/kotlin/com/simplemobiletools/calendar/pro/fragments/WeekFragmentsHolder.kt b/app/src/main/kotlin/com/simplemobiletools/calendar/pro/fragments/WeekFragmentsHolder.kt index 4204e360a..a06c89546 100644 --- a/app/src/main/kotlin/com/simplemobiletools/calendar/pro/fragments/WeekFragmentsHolder.kt +++ b/app/src/main/kotlin/com/simplemobiletools/calendar/pro/fragments/WeekFragmentsHolder.kt @@ -189,22 +189,11 @@ class WeekFragmentsHolder : MyFragmentHolder(), WeekFragmentListener { } private fun dateSelected(dateTime: DateTime, datePicker: DatePicker) { - val isSundayFirst = requireContext().config.isSundayFirst val month = datePicker.month + 1 val year = datePicker.year val day = datePicker.dayOfMonth - var newDateTime = dateTime.withDate(year, month, day) - - if (isSundayFirst) { - newDateTime = newDateTime.plusDays(1) - } - - var selectedWeek = newDateTime.withDayOfWeek(1).withTimeAtStartOfDay().minusDays(if (isSundayFirst) 1 else 0) - if (newDateTime.minusDays(7).seconds() > selectedWeek.seconds()) { - selectedWeek = selectedWeek.plusDays(7) - } - - currentWeekTS = selectedWeek.seconds() + val newDateTime = dateTime.withDate(year, month, day) + currentWeekTS = requireContext().getFirstDayOfWeekDt(newDateTime).seconds() setupFragment() }