Skip to content

Commit

Permalink
Merge pull request #295 from prolificinteractive/week_mode_issues
Browse files Browse the repository at this point in the history
Fix - Issue with firstDayOfWeek showing wrong week
  • Loading branch information
ekchang committed May 12, 2016
2 parents c8c6e6d + de4c50b commit 9b221a8
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 48 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@

import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collections;
import java.util.List;

Expand Down Expand Up @@ -40,7 +39,6 @@ abstract class CalendarPagerAdapter<V extends CalendarPagerView> extends PagerAd
private DayFormatter dayFormatter = DayFormatter.DEFAULT;
private List<DayViewDecorator> decorators = new ArrayList<>();
private List<DecoratorResult> decoratorResults = null;
private int firstDayOfTheWeek = Calendar.SUNDAY;
private boolean selectionEnabled = true;

CalendarPagerAdapter(MaterialCalendarView mcv) {
Expand Down Expand Up @@ -91,7 +89,6 @@ public CalendarPagerAdapter<?> migrateStateAndReturn(CalendarPagerAdapter<?> new
newAdapter.minDate = minDate;
newAdapter.maxDate = maxDate;
newAdapter.selectedDates = selectedDates;
newAdapter.firstDayOfTheWeek = firstDayOfTheWeek;
newAdapter.selectionEnabled = selectionEnabled;
return newAdapter;
}
Expand Down Expand Up @@ -164,13 +161,6 @@ public Object instantiateItem(ViewGroup container, int position) {
return pagerView;
}

public void setFirstDayOfWeek(int day) {
firstDayOfTheWeek = day;
for (V pagerView : currentViews) {
pagerView.setFirstDayOfWeek(firstDayOfTheWeek);
}
}

public void setSelectionEnabled(boolean enabled) {
selectionEnabled = enabled;
for (V pagerView : currentViews) {
Expand Down Expand Up @@ -327,8 +317,4 @@ protected int getDateTextAppearance() {
protected int getWeekDayTextAppearance() {
return weekDayTextAppearance == null ? 0 : weekDayTextAppearance;
}

public int getFirstDayOfWeek() {
return firstDayOfTheWeek;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -92,26 +92,6 @@ protected int getFirstDayOfWeek() {
return firstDayOfWeek;
}

public void setFirstDayOfWeek(int dayOfWeek) {
this.firstDayOfWeek = dayOfWeek;

Calendar calendar = resetAndGetWorkingCalendar();
calendar.set(DAY_OF_WEEK, dayOfWeek);
for (WeekDayView dayView : weekDayViews) {
dayView.setDayOfWeek(calendar);
calendar.add(DATE, 1);
}

calendar = resetAndGetWorkingCalendar();
for (DayView dayView : dayViews) {
CalendarDay day = CalendarDay.from(calendar);
dayView.setDay(day);
calendar.add(DATE, 1);
}

updateUi();
}

protected abstract void buildDayViews(Collection<DayView> dayViews, Calendar calendar);

protected abstract boolean isDayEnabled(CalendarDay day);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,7 @@ public void onPageScrolled(int position, float positionOffset, int positionOffse
@SelectionMode
private int selectionMode = SELECTION_MODE_SINGLE;
private boolean allowClickDaysOutsideCurrentMonth = true;
private int firstDayOfWeek;

public MaterialCalendarView(Context context) {
this(context, null);
Expand Down Expand Up @@ -260,6 +261,15 @@ public void transformPage(View page, float position) {
R.styleable.MaterialCalendarView_mcv_calendarMode,
0
);
firstDayOfWeek = a.getInteger(
R.styleable.MaterialCalendarView_mcv_firstDayOfWeek,
-1
);

if (firstDayOfWeek < 0) {
//Allowing use of Calendar.getInstance() here as a performance optimization
firstDayOfWeek = Calendar.getInstance().getFirstDayOfWeek();
}
setCalendarDisplayMode(CalendarMode.values()[calendarModeIndex]);

final int tileSize = a.getDimensionPixelSize(R.styleable.MaterialCalendarView_mcv_tileSize, -1);
Expand Down Expand Up @@ -336,15 +346,6 @@ public void transformPage(View page, float position) {
true
));

int firstDayOfWeek = a.getInteger(
R.styleable.MaterialCalendarView_mcv_firstDayOfWeek,
-1
);
if (firstDayOfWeek < 0) {
//Allowing use of Calendar.getInstance() here as a performance optimization
firstDayOfWeek = Calendar.getInstance().getFirstDayOfWeek();
}
setFirstDayOfWeek(firstDayOfWeek);
} catch (Exception e) {
e.printStackTrace();
} finally {
Expand Down Expand Up @@ -1214,14 +1215,41 @@ private static int getThemeAccentColor(Context context) {
* @see java.util.Calendar
*/
public void setFirstDayOfWeek(int day) {
adapter.setFirstDayOfWeek(day);
firstDayOfWeek = day;
// TODO: 5/12/16 consider a less nuclear means of resetting the adapter when setting a new
// first day of week and how regular notifyDataSetChanged doesn't work (may require updating
// getItemPosition to flag current object and the ones to the left/right as changed)
CalendarPagerAdapter<?> newAdapter;
switch (calendarMode) {
case MONTHS:
newAdapter = new MonthPagerAdapter(this);
break;
case WEEKS:
newAdapter = new WeekPagerAdapter(this);
break;
default:
throw new IllegalArgumentException("Provided display mode which is not yet implemented");
}
if (adapter == null) {
adapter = newAdapter;
} else {
adapter = adapter.migrateStateAndReturn(newAdapter);
}
pager.setAdapter(adapter);

setCurrentDate(
selectionMode == SELECTION_MODE_SINGLE && !adapter.getSelectedDates().isEmpty()
? adapter.getSelectedDates().get(0)
: CalendarDay.today());
invalidateDecorators();
updateUi();
}

/**
* @return The first day of the week as a {@linkplain Calendar} day constant.
*/
public int getFirstDayOfWeek() {
return adapter.getFirstDayOfWeek();
return firstDayOfWeek;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class MonthPagerAdapter extends CalendarPagerAdapter<MonthView> {

@Override
protected MonthView createView(int position) {
return new MonthView(mcv, getItem(position), getFirstDayOfWeek());
return new MonthView(mcv, getItem(position), mcv.getFirstDayOfWeek());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public WeekPagerAdapter(MaterialCalendarView mcv) {

@Override
protected WeekView createView(int position) {
return new WeekView(mcv, getItem(position), getFirstDayOfWeek());
return new WeekView(mcv, getItem(position), mcv.getFirstDayOfWeek());
}

@Override
Expand All @@ -30,7 +30,7 @@ protected boolean isInstanceOfView(Object object) {

@Override
protected DateRangeIndex createRangeIndex(CalendarDay min, CalendarDay max) {
return new Weekly(min, max, getFirstDayOfWeek());
return new Weekly(min, max, mcv.getFirstDayOfWeek());
}

public static class Weekly implements DateRangeIndex {
Expand Down

0 comments on commit 9b221a8

Please sign in to comment.