Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Day Of Week Offset Does Not Work Correctly #17

Open
richardortiz84 opened this issue Aug 1, 2024 · 3 comments
Open

Day Of Week Offset Does Not Work Correctly #17

richardortiz84 opened this issue Aug 1, 2024 · 3 comments

Comments

@richardortiz84
Copy link

richardortiz84 commented Aug 1, 2024

When attempting to set the dayOfWeekOffset to be Sunday like a typical work week calendar, I noticed that the start of the week would indeed be set to the given day, but the calendar then began 6 days in advance. For instance this month August 2024 setting the dayOfWeekOffset to Sunday resulted in a calendar having a first visible day of 7. If you turned on showPreviousMonthDays, the 3 days that show at the beginning of the calendar would be visually set to be the previous month, but would in fact be days from this month (4, 5, and 6).

Screenshot 2024-08-01 at 12 07 48 AM

My assumption is that setting this field would set the beginning of the week to be the given day but the months calendar would still begin on the 1st.

This seems to be because calculateVisibleDaysOfPreviousMonth and calculateVisibleDaysOfNextMonth do not take into account the offset and adjust the numbers accordingly.

Here is an update to those functions to account for the offset:

private fun calculateVisibleDaysOfPreviousMonth(
	monthYear: MonthYear,
	dayOfWeekOffset: Int,
): Int {
	val (month, year) = monthYear
	val dayOfWeekMonthBegins = LocalDate(year = year, month = month, dayOfMonth = 1).dayOfWeek.ordinal // Monday = 0

	return when {
		dayOfWeekOffset > dayOfWeekMonthBegins -> 7 - dayOfWeekOffset + dayOfWeekMonthBegins
		dayOfWeekOffset < dayOfWeekMonthBegins -> dayOfWeekMonthBegins - dayOfWeekOffset
		else -> 0
	}
}

private fun calculateVisibleDaysOfNextMonth(
	monthYear: MonthYear,
	dayOfWeekOffset: Int,
): Int {
	val (month, year) = monthYear
	val daysInMonth = monthLength(month, year)
	val dayOfWeekMonthEnds = LocalDate(year = year, month = month, dayOfMonth = daysInMonth).dayOfWeek.ordinal

	return when {
		dayOfWeekOffset > dayOfWeekMonthEnds -> dayOfWeekOffset - 1 - dayOfWeekMonthEnds
		dayOfWeekOffset < dayOfWeekMonthEnds -> 6 - (dayOfWeekMonthEnds - dayOfWeekOffset)
		else -> 6
	}
}

In CalendarView.kt in function private fun CalendarView()
Update the function calls in lines 73-77

	val previousMonthDays = calculateVisibleDaysOfPreviousMonth(yearMonth, config.value.dayOfWeekOffset)
	val nextMonthDays = if (config.value.showNextMonthDays) {
		calculateVisibleDaysOfNextMonth(yearMonth, config.value.dayOfWeekOffset)
	} else {
		0
	}

Delete Line 152 in function private fun Item()

	newDate = newDate.plus(state.dayOfWeekOffset, DateTimeUnit.DAY)

Here is the after:
Screenshot 2024-08-01 at 12 10 00 AM
Screenshot 2024-08-01 at 12 10 47 AM

@ismai117
Copy link

@WojciechOsak can you update this please?

@ismai117
Copy link

ismai117 commented Aug 29, 2024

@richardortiz84 what else did you change? I updated those functions but now it's updated to

Screenshot 2024-08-29 at 06 59 53

val startDate = LocalDate.today()
val config = rememberCalendarState(
	startDate = startDate,
	monthOffset = 0,
	dayOfWeekOffset = 6
)
CalendarView(
	config = config,
)

@richardortiz84
Copy link
Author

richardortiz84 commented Aug 30, 2024

@ismai117 My apologies I forgot that I had also modified the Item composable in the calendar view. I have updated my original comment but here is the additional changes.

In CalendarView.kt in function private fun CalendarView()
Update the function calls in lines 73-77

	val previousMonthDays = calculateVisibleDaysOfPreviousMonth(yearMonth, config.value.dayOfWeekOffset)
	val nextMonthDays = if (config.value.showNextMonthDays) {
		calculateVisibleDaysOfNextMonth(yearMonth, config.value.dayOfWeekOffset)
	} else {
		0
	}

Delete Line 152 in function private fun Item()

	newDate = newDate.plus(state.dayOfWeekOffset, DateTimeUnit.DAY)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants