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

Add state builder method to override day to show when committing state #966

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -1799,6 +1799,7 @@ public class State {
private final CalendarDay maxDate;
private final boolean cacheCurrentPosition;
private final boolean showWeekDays;
private final CalendarDay dayToShow;

private State(final StateBuilder builder) {
calendarMode = builder.calendarMode;
Expand All @@ -1807,6 +1808,7 @@ private State(final StateBuilder builder) {
maxDate = builder.maxDate;
cacheCurrentPosition = builder.cacheCurrentPosition;
showWeekDays = builder.showWeekDays;
dayToShow = builder.dayToShow;
}

/**
Expand All @@ -1824,6 +1826,7 @@ public class StateBuilder {
private CalendarDay minDate = null;
private CalendarDay maxDate = null;
private boolean showWeekDays;
private CalendarDay dayToShow = null;

public StateBuilder() {
calendarMode = CalendarMode.MONTHS;
Expand Down Expand Up @@ -1853,6 +1856,15 @@ public StateBuilder setFirstDayOfWeek(DayOfWeek day) {
return this;
}

/**
* Sets the day the calendar should focus on when committing new state.
* @param dayToShow the day the calendar should focus on and display on screen
*/
public StateBuilder setDayToShow(CalendarDay dayToShow) {
this.dayToShow = dayToShow;
return this;
}

/**
* Set calendar display mode. The default mode is Months.
* When switching between modes will select todays date, or the selected date,
Expand Down Expand Up @@ -1926,7 +1938,12 @@ public void commit() {
private void commit(State state) {
// Use the calendarDayToShow to determine which date to focus on for the case of switching between month and week views
CalendarDay calendarDayToShow = null;
if (adapter != null && state.cacheCurrentPosition) {

if (state.dayToShow != null) {
calendarDayToShow = state.dayToShow;
}

if (adapter != null && calendarDayToShow == null && state.cacheCurrentPosition) {
calendarDayToShow = adapter.getItem(pager.getCurrentItem());
if (calendarMode != state.calendarMode) {
CalendarDay currentlySelectedDate = getSelectedDate();
Expand Down