dates = getSelectedDates();
if (!dates.isEmpty()) {
setSelectedDate(getSelectedDate());
}
}
- }
- break;
- case SELECTION_MODE_NONE: {
+ break;
+ default:
+ case SELECTION_MODE_NONE:
this.selectionMode = SELECTION_MODE_NONE;
if (oldMode != SELECTION_MODE_NONE) {
//No selection! Clear out!
clearSelection();
}
- }
- break;
+ break;
}
adapter.setSelectionEnabled(selectionMode != SELECTION_MODE_NONE);
@@ -469,50 +482,6 @@ public void goToNext() {
}
}
- /**
- * Set calendar display mode. The default mode is Months.
- * When switching between modes will select todays date, or the selected date,
- * if selection mode is single.
- *
- * @param mode - calendar mode
- */
- @Experimental
- public void setCalendarDisplayMode(CalendarMode mode) {
- if (calendarMode != null && calendarMode.equals(mode)) {
- return;
- }
-
- CalendarPagerAdapter> newAdapter;
- switch (mode) {
- 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);
- setRangeDates(minDate, maxDate);
- calendarMode = mode;
-
- // Reset height params after mode change
- pager.setLayoutParams(new LayoutParams(calendarMode.visibleWeeksCount + DAY_NAMES_ROW));
-
- setCurrentDate(
- selectionMode == SELECTION_MODE_SINGLE && !adapter.getSelectedDates().isEmpty()
- ? adapter.getSelectedDates().get(0)
- : CalendarDay.today());
- invalidateDecorators();
- updateUi();
- }
-
/**
* Get the current selection mode. The default mode is {@linkplain #SELECTION_MODE_SINGLE}
*
@@ -521,6 +490,7 @@ public void setCalendarDisplayMode(CalendarMode mode) {
* @see #SELECTION_MODE_NONE
* @see #SELECTION_MODE_SINGLE
* @see #SELECTION_MODE_MULTIPLE
+ * @see #SELECTION_MODE_RANGE
*/
@SelectionMode
public int getSelectionMode() {
@@ -907,28 +877,6 @@ public CalendarDay getMinimumDate() {
return minDate;
}
- /**
- * @param calendar set the minimum selectable date, null for no minimum
- */
- public void setMinimumDate(@Nullable Calendar calendar) {
- setMinimumDate(CalendarDay.from(calendar));
- }
-
- /**
- * @param date set the minimum selectable date, null for no minimum
- */
- public void setMinimumDate(@Nullable Date date) {
- setMinimumDate(CalendarDay.from(date));
- }
-
- /**
- * @param calendar set the minimum selectable date, null for no minimum
- */
- public void setMinimumDate(@Nullable CalendarDay calendar) {
- minDate = calendar;
- setRangeDates(minDate, maxDate);
- }
-
/**
* @return the maximum selectable date for the calendar, if any
*/
@@ -936,28 +884,6 @@ public CalendarDay getMaximumDate() {
return maxDate;
}
- /**
- * @param calendar set the maximum selectable date, null for no maximum
- */
- public void setMaximumDate(@Nullable Calendar calendar) {
- setMaximumDate(CalendarDay.from(calendar));
- }
-
- /**
- * @param date set the maximum selectable date, null for no maximum
- */
- public void setMaximumDate(@Nullable Date date) {
- setMaximumDate(CalendarDay.from(date));
- }
-
- /**
- * @param calendar set the maximum selectable date, null for no maximum
- */
- public void setMaximumDate(@Nullable CalendarDay calendar) {
- maxDate = calendar;
- setRangeDates(minDate, maxDate);
- }
-
/**
* The default value is {@link #SHOW_DEFAULTS}, which currently is just {@link #SHOW_DECORATED_DISABLED}.
* This means that the default visible days are of the current month, in the min-max range.
@@ -1131,25 +1057,27 @@ protected Parcelable onSaveInstanceState() {
protected void onRestoreInstanceState(Parcelable state) {
SavedState ss = (SavedState) state;
super.onRestoreInstanceState(ss.getSuperState());
+ newState()
+ .setFirstDayOfWeek(ss.firstDayOfWeek)
+ .setCalendarDisplayMode(ss.calendarMode)
+ .setMinimumDate(ss.minDate)
+ .setMaximumDate(ss.maxDate)
+ .commit();
+
setSelectionColor(ss.color);
setDateTextAppearance(ss.dateTextAppearance);
setWeekDayTextAppearance(ss.weekDayTextAppearance);
setShowOtherDates(ss.showOtherDates);
setAllowClickDaysOutsideCurrentMonth(ss.allowClickDaysOutsideCurrentMonth);
- minDate = ss.minDate;
- maxDate = ss.maxDate;
- setRangeDates(ss.minDate, ss.maxDate);
clearSelection();
for (CalendarDay calendarDay : ss.selectedDates) {
setDateSelected(calendarDay, true);
}
- setFirstDayOfWeek(ss.firstDayOfWeek);
setTileWidth(ss.tileWidthPx);
setTileHeight(ss.tileHeightPx);
setTopbarVisible(ss.topbarVisible);
setSelectionMode(ss.selectionMode);
setDynamicHeightEnabled(ss.dynamicHeightEnabled);
- setCalendarDisplayMode(ss.calendarMode);
setCurrentDate(ss.currentMonth);
}
@@ -1167,6 +1095,9 @@ private void setRangeDates(CalendarDay min, CalendarDay max) {
CalendarDay c = currentMonth;
adapter.setRangeDates(min, max);
currentMonth = c;
+ if (min != null) {
+ currentMonth = min.isAfter(currentMonth) ? min : currentMonth;
+ }
int position = adapter.getIndexForDay(c);
pager.setCurrentItem(position, false);
updateUi();
@@ -1262,46 +1193,6 @@ private static int getThemeAccentColor(Context context) {
return outValue.data;
}
- /**
- * Sets the first day of the week.
- *
- * Uses the java.util.Calendar day constants.
- *
- * @param day The first day of the week as a java.util.Calendar day constant.
- * @see java.util.Calendar
- */
- public void setFirstDayOfWeek(int 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);
- setRangeDates(minDate, maxDate);
-
- 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.
*/
@@ -1312,7 +1203,7 @@ public int getFirstDayOfWeek() {
/**
* By default, the calendar will take up all the space needed to show any month (6 rows).
* By enabling dynamic height, the view will change height dependant on the visible month.
- *
+ *
* This means months that only need 5 or 4 rows to show the entire month will only take up
* that many rows, and will grow and shrink as necessary.
*
@@ -1414,6 +1305,15 @@ public void setOnMonthChangedListener(OnMonthChangedListener listener) {
this.monthListener = listener;
}
+ /**
+ * Sets the listener to be notified upon a range has been selected.
+ *
+ * @param listener thing to be notified
+ */
+ public void setOnRangeSelectedListener(OnRangeSelectedListener listener) {
+ this.rangeListener = listener;
+ }
+
/**
* Dispatch date change events to a listener, if set
*
@@ -1427,6 +1327,34 @@ protected void dispatchOnDateSelected(final CalendarDay day, final boolean selec
}
}
+ /**
+ * Dispatch a range of days to a listener, if set. First day must be before last Day.
+ *
+ * @param firstDay first day enclosing a range
+ * @param lastDay last day enclosing a range
+ */
+ protected void dispatchOnRangeSelected(final CalendarDay firstDay, final CalendarDay lastDay) {
+ final OnRangeSelectedListener listener = rangeListener;
+ final List days = new ArrayList<>();
+
+ final Calendar counter = Calendar.getInstance();
+ counter.setTime(firstDay.getDate()); // start from the first day and increment
+
+ final Calendar end = Calendar.getInstance();
+ end.setTime(lastDay.getDate()); // for comparison
+
+ while (counter.before(end) || counter.equals(end)) {
+ final CalendarDay current = CalendarDay.from(counter);
+ adapter.setDateSelected(current, true);
+ days.add(current);
+ counter.add(Calendar.DATE, 1);
+ }
+
+ if (listener != null) {
+ listener.onRangeSelected(MaterialCalendarView.this, days);
+ }
+ }
+
/**
* Dispatch date change events to a listener, if set
*
@@ -1453,6 +1381,25 @@ protected void onDateClicked(@NonNull CalendarDay date, boolean nowSelected) {
dispatchOnDateSelected(date, nowSelected);
}
break;
+ case SELECTION_MODE_RANGE: {
+ adapter.setDateSelected(date, nowSelected);
+ if (adapter.getSelectedDates().size() > 2) {
+ adapter.clearSelections();
+ adapter.setDateSelected(date, nowSelected); // re-set because adapter has been cleared
+ dispatchOnDateSelected(date, nowSelected);
+ } else if (adapter.getSelectedDates().size() == 2) {
+ final List dates = adapter.getSelectedDates();
+ if (dates.get(0).isAfter(dates.get(1))) {
+ dispatchOnRangeSelected(dates.get(1), dates.get(0));
+ } else {
+ dispatchOnRangeSelected(dates.get(0), dates.get(1));
+ }
+ } else {
+ adapter.setDateSelected(date, nowSelected);
+ dispatchOnDateSelected(date, nowSelected);
+ }
+ }
+ break;
default:
case SELECTION_MODE_SINGLE: {
adapter.clearSelections();
@@ -1463,6 +1410,21 @@ protected void onDateClicked(@NonNull CalendarDay date, boolean nowSelected) {
}
}
+ /**
+ * Select a fresh range of date including first day and last day.
+ *
+ * @param firstDay first day of the range to select
+ * @param lastDay last day of the range to select
+ */
+ public void selectRange(final CalendarDay firstDay, final CalendarDay lastDay) {
+ clearSelection();
+ if (firstDay.isAfter(lastDay)) {
+ dispatchOnRangeSelected(lastDay, firstDay);
+ } else {
+ dispatchOnRangeSelected(firstDay, lastDay);
+ }
+ }
+
/**
* Call by {@link CalendarPagerView} to indicate that a day was clicked and we should handle it
*
@@ -1772,4 +1734,174 @@ public void setPagingEnabled(boolean pagingEnabled) {
public boolean isPagingEnabled() {
return pager.isPagingEnabled();
}
+
+ /**
+ * Preserve the current parameters of the Material Calendar View.
+ */
+ public State state() {
+ return state;
+ }
+
+ /**
+ * Initialize the parameters from scratch.
+ */
+ public StateBuilder newState() {
+ return new StateBuilder();
+ }
+
+ public class State {
+ public final CalendarMode calendarMode;
+ public final int firstDayOfWeek;
+ public final CalendarDay minDate;
+ public final CalendarDay maxDate;
+
+ public State(StateBuilder builder) {
+ calendarMode = builder.calendarMode;
+ firstDayOfWeek = builder.firstDayOfWeek;
+ minDate = builder.minDate;
+ maxDate = builder.maxDate;
+ }
+
+ /**
+ * Modify parameters from current state.
+ */
+ public StateBuilder edit() {
+ return new StateBuilder(this);
+ }
+
+ }
+
+ public class StateBuilder {
+ private CalendarMode calendarMode = CalendarMode.MONTHS;
+ private int firstDayOfWeek = Calendar.getInstance().getFirstDayOfWeek();
+ public CalendarDay minDate = null;
+ public CalendarDay maxDate = null;
+
+ public StateBuilder() {
+ }
+
+ private StateBuilder(final State state) {
+ calendarMode = state.calendarMode;
+ firstDayOfWeek = state.firstDayOfWeek;
+ minDate = state.minDate;
+ maxDate = state.maxDate;
+ }
+
+ /**
+ * Sets the first day of the week.
+ *
+ * Uses the java.util.Calendar day constants.
+ *
+ * @param day The first day of the week as a java.util.Calendar day constant.
+ * @see java.util.Calendar
+ */
+ public StateBuilder setFirstDayOfWeek(int day) {
+ this.firstDayOfWeek = day;
+ return this;
+ }
+
+ /**
+ * Set calendar display mode. The default mode is Months.
+ * When switching between modes will select todays date, or the selected date,
+ * if selection mode is single.
+ *
+ * @param mode - calendar mode
+ */
+ public StateBuilder setCalendarDisplayMode(CalendarMode mode) {
+ this.calendarMode = mode;
+ return this;
+ }
+
+
+ /**
+ * @param calendar set the minimum selectable date, null for no minimum
+ */
+ public StateBuilder setMinimumDate(@Nullable Calendar calendar) {
+ setMinimumDate(CalendarDay.from(calendar));
+ return this;
+ }
+
+ /**
+ * @param date set the minimum selectable date, null for no minimum
+ */
+ public StateBuilder setMinimumDate(@Nullable Date date) {
+ setMinimumDate(CalendarDay.from(date));
+ return this;
+ }
+
+ /**
+ * @param calendar set the minimum selectable date, null for no minimum
+ */
+ public StateBuilder setMinimumDate(@Nullable CalendarDay calendar) {
+ minDate = calendar;
+ return this;
+ }
+
+ /**
+ * @param calendar set the maximum selectable date, null for no maximum
+ */
+ public StateBuilder setMaximumDate(@Nullable Calendar calendar) {
+ setMaximumDate(CalendarDay.from(calendar));
+ return this;
+ }
+
+ /**
+ * @param date set the maximum selectable date, null for no maximum
+ */
+ public StateBuilder setMaximumDate(@Nullable Date date) {
+ setMaximumDate(CalendarDay.from(date));
+ return this;
+ }
+
+ /**
+ * @param calendar set the maximum selectable date, null for no maximum
+ */
+ public StateBuilder setMaximumDate(@Nullable CalendarDay calendar) {
+ maxDate = calendar;
+ return this;
+ }
+
+ public void commit() {
+ MaterialCalendarView.this.commit(new State(this));
+ }
+ }
+
+ private void commit(State state) {
+ this.state = state;
+ // Save states parameters
+ calendarMode = state.calendarMode;
+ firstDayOfWeek = state.firstDayOfWeek;
+ minDate = state.minDate;
+ maxDate = state.maxDate;
+
+ // Recreate adapter
+ final 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);
+ setRangeDates(minDate, maxDate);
+
+ // Reset height params after mode change
+ pager.setLayoutParams(new LayoutParams(calendarMode.visibleWeeksCount + DAY_NAMES_ROW));
+
+ setCurrentDate(
+ selectionMode == SELECTION_MODE_SINGLE && !adapter.getSelectedDates().isEmpty()
+ ? adapter.getSelectedDates().get(0)
+ : CalendarDay.today());
+ invalidateDecorators();
+ updateUi();
+ }
}
diff --git a/library/src/main/java/com/prolificinteractive/materialcalendarview/OnRangeSelectedListener.java b/library/src/main/java/com/prolificinteractive/materialcalendarview/OnRangeSelectedListener.java
new file mode 100644
index 00000000..b157091a
--- /dev/null
+++ b/library/src/main/java/com/prolificinteractive/materialcalendarview/OnRangeSelectedListener.java
@@ -0,0 +1,20 @@
+package com.prolificinteractive.materialcalendarview;
+
+import android.support.annotation.NonNull;
+
+import java.util.List;
+
+/**
+ * The callback used to indicate a range has been selected
+ */
+public interface OnRangeSelectedListener {
+
+ /**
+ * Called when a user selects a range of days.
+ * There is no logic to prevent multiple calls for the same date and state.
+ *
+ * @param widget the view associated with this listener
+ * @param dates the dates in the range, in ascending order
+ */
+ void onRangeSelected(@NonNull MaterialCalendarView widget, @NonNull List dates);
+}
diff --git a/library/src/main/java/com/prolificinteractive/materialcalendarview/WeekPagerAdapter.java b/library/src/main/java/com/prolificinteractive/materialcalendarview/WeekPagerAdapter.java
index a1ab577b..62f77ebf 100644
--- a/library/src/main/java/com/prolificinteractive/materialcalendarview/WeekPagerAdapter.java
+++ b/library/src/main/java/com/prolificinteractive/materialcalendarview/WeekPagerAdapter.java
@@ -42,7 +42,7 @@ public static class Weekly implements DateRangeIndex {
public Weekly(@NonNull CalendarDay min, @NonNull CalendarDay max, int firstDayOfWeek) {
this.min = getFirstDayOfWeek(min, firstDayOfWeek);
- this.count = weekNumberDifference(min, max) + 1;
+ this.count = weekNumberDifference(this.min, max) + 1;
}
@Override
@@ -67,7 +67,11 @@ public CalendarDay getItem(int position) {
private int weekNumberDifference(@NonNull CalendarDay min, @NonNull CalendarDay max) {
long millisDiff = max.getDate().getTime() - min.getDate().getTime();
- long dayDiff = TimeUnit.DAYS.convert(millisDiff, TimeUnit.MILLISECONDS);
+
+ int dstOffsetMax = max.getCalendar().get(Calendar.DST_OFFSET);
+ int dstOffsetMin = min.getCalendar().get(Calendar.DST_OFFSET);
+
+ long dayDiff = TimeUnit.DAYS.convert(millisDiff + dstOffsetMax - dstOffsetMin, TimeUnit.MILLISECONDS);
return (int) (dayDiff / DAYS_IN_WEEK);
}
diff --git a/library/src/main/java/com/prolificinteractive/materialcalendarview/format/DateFormatTitleFormatter.java b/library/src/main/java/com/prolificinteractive/materialcalendarview/format/DateFormatTitleFormatter.java
index 3e1de558..b7eeae11 100644
--- a/library/src/main/java/com/prolificinteractive/materialcalendarview/format/DateFormatTitleFormatter.java
+++ b/library/src/main/java/com/prolificinteractive/materialcalendarview/format/DateFormatTitleFormatter.java
@@ -14,11 +14,11 @@ public class DateFormatTitleFormatter implements TitleFormatter {
private final DateFormat dateFormat;
/**
- * Format using "MMMM yyyy" for formatting
+ * Format using "LLLL yyyy" for formatting
*/
public DateFormatTitleFormatter() {
this.dateFormat = new SimpleDateFormat(
- "MMMM yyyy", Locale.getDefault()
+ "LLLL yyyy", Locale.getDefault()
);
}
diff --git a/sample/build.gradle b/sample/build.gradle
index 11d6b179..5d6be7e2 100644
--- a/sample/build.gradle
+++ b/sample/build.gradle
@@ -2,7 +2,7 @@ apply plugin: 'com.android.application'
android {
compileSdkVersion 23
- buildToolsVersion "23.0.2"
+ buildToolsVersion "23.0.3"
defaultConfig {
applicationId "com.prolificinteractive.materialcalendarview.sample"
@@ -16,8 +16,8 @@ android {
dependencies {
// You should use the commented out line below in you're application.
// We depend on the source directly here so that development is easier.
-// compile project(':library')
- compile 'com.prolificinteractive:material-calendarview:1.3.0'
+ compile project(':library')
+ //compile 'com.prolificinteractive:material-calendarview:1.4.0'
compile 'com.android.support:appcompat-v7:23.4.0'
compile 'com.android.support:recyclerview-v7:23.4.0'
diff --git a/sample/src/main/java/com/prolificinteractive/materialcalendarview/sample/BasicActivityDecorated.java b/sample/src/main/java/com/prolificinteractive/materialcalendarview/sample/BasicActivityDecorated.java
index 66e03b87..42f2eade 100644
--- a/sample/src/main/java/com/prolificinteractive/materialcalendarview/sample/BasicActivityDecorated.java
+++ b/sample/src/main/java/com/prolificinteractive/materialcalendarview/sample/BasicActivityDecorated.java
@@ -41,14 +41,19 @@ protected void onCreate(Bundle savedInstanceState) {
widget.setOnDateChangedListener(this);
widget.setShowOtherDates(MaterialCalendarView.SHOW_ALL);
- Calendar calendar = Calendar.getInstance();
- widget.setSelectedDate(calendar.getTime());
+ Calendar instance = Calendar.getInstance();
+ widget.setSelectedDate(instance.getTime());
- calendar.set(calendar.get(Calendar.YEAR), Calendar.JANUARY, 1);
- widget.setMinimumDate(calendar.getTime());
+ Calendar instance1 = Calendar.getInstance();
+ instance1.set(instance1.get(Calendar.YEAR), Calendar.JANUARY, 1);
- calendar.set(calendar.get(Calendar.YEAR), Calendar.DECEMBER, 31);
- widget.setMaximumDate(calendar.getTime());
+ Calendar instance2 = Calendar.getInstance();
+ instance2.set(instance2.get(Calendar.YEAR), Calendar.DECEMBER, 31);
+
+ widget.state().edit()
+ .setMinimumDate(instance1.getTime())
+ .setMaximumDate(instance2.getTime())
+ .commit();
widget.addDecorators(
new MySelectorDecorator(this),
diff --git a/sample/src/main/java/com/prolificinteractive/materialcalendarview/sample/CustomizeCodeActivity.java b/sample/src/main/java/com/prolificinteractive/materialcalendarview/sample/CustomizeCodeActivity.java
index 62588674..6e2e7b6d 100644
--- a/sample/src/main/java/com/prolificinteractive/materialcalendarview/sample/CustomizeCodeActivity.java
+++ b/sample/src/main/java/com/prolificinteractive/materialcalendarview/sample/CustomizeCodeActivity.java
@@ -42,10 +42,13 @@ protected void onCreate(Bundle savedInstanceState) {
widget.setCurrentDate(today);
widget.setSelectedDate(today);
- widget.setFirstDayOfWeek(Calendar.WEDNESDAY);
- widget.setMinimumDate(CalendarDay.from(2016, 4, 3));
- widget.setMaximumDate(CalendarDay.from(2016, 5, 12));
- widget.setCalendarDisplayMode(CalendarMode.WEEKS);
+ widget.state().edit()
+ .setFirstDayOfWeek(Calendar.WEDNESDAY)
+ .setMinimumDate(CalendarDay.from(2016, 4, 3))
+ .setMaximumDate(CalendarDay.from(2016, 5, 12))
+ .setCalendarDisplayMode(CalendarMode.WEEKS)
+ .commit();
+
}
}
diff --git a/sample/src/main/java/com/prolificinteractive/materialcalendarview/sample/DisableDaysActivity.java b/sample/src/main/java/com/prolificinteractive/materialcalendarview/sample/DisableDaysActivity.java
index d21e9fdd..5cd59589 100644
--- a/sample/src/main/java/com/prolificinteractive/materialcalendarview/sample/DisableDaysActivity.java
+++ b/sample/src/main/java/com/prolificinteractive/materialcalendarview/sample/DisableDaysActivity.java
@@ -36,11 +36,16 @@ protected void onCreate(Bundle savedInstanceState) {
Calendar calendar = Calendar.getInstance();
widget.setSelectedDate(calendar.getTime());
- calendar.set(calendar.get(Calendar.YEAR), Calendar.JANUARY, 1);
- widget.setMinimumDate(calendar.getTime());
+ Calendar instance1 = Calendar.getInstance();
+ instance1.set(instance1.get(Calendar.YEAR), Calendar.JANUARY, 1);
- calendar.set(calendar.get(Calendar.YEAR) + 2, Calendar.OCTOBER, 31);
- widget.setMaximumDate(calendar.getTime());
+ Calendar instance2 = Calendar.getInstance();
+ instance2.set(instance2.get(Calendar.YEAR) + 2, Calendar.OCTOBER, 31);
+
+ widget.state().edit()
+ .setMinimumDate(instance1.getTime())
+ .setMaximumDate(instance2.getTime())
+ .commit();
}
private static class PrimeDayDisableDecorator implements DayViewDecorator {
diff --git a/sample/src/main/java/com/prolificinteractive/materialcalendarview/sample/DynamicSettersActivity.java b/sample/src/main/java/com/prolificinteractive/materialcalendarview/sample/DynamicSettersActivity.java
index 06056825..cdb4d310 100644
--- a/sample/src/main/java/com/prolificinteractive/materialcalendarview/sample/DynamicSettersActivity.java
+++ b/sample/src/main/java/com/prolificinteractive/materialcalendarview/sample/DynamicSettersActivity.java
@@ -123,7 +123,9 @@ void onMinClicked() {
showDatePickerDialog(this, widget.getMinimumDate(), new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
- widget.setMinimumDate(CalendarDay.from(year, monthOfYear, dayOfMonth));
+ widget.state().edit()
+ .setMinimumDate(CalendarDay.from(year, monthOfYear, dayOfMonth))
+ .commit();
}
});
}
@@ -133,7 +135,9 @@ void onMaxClicked() {
showDatePickerDialog(this, widget.getMaximumDate(), new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
- widget.setMaximumDate(CalendarDay.from(year, monthOfYear, dayOfMonth));
+ widget.state().edit()
+ .setMaximumDate(CalendarDay.from(year, monthOfYear, dayOfMonth))
+ .commit();
}
});
}
@@ -226,7 +230,8 @@ void onChangeSelectionMode() {
CharSequence[] items = {
"No Selection",
"Single Date",
- "Multiple Dates"
+ "Multiple Dates",
+ "Range of Dates"
};
new AlertDialog.Builder(this)
.setTitle("Selection Mode")
@@ -253,17 +258,24 @@ public void onClick(DialogInterface dialog, int which) {
@OnClick(R.id.button_set_first_day)
void onFirstDayOfWeekClicked() {
int index = random.nextInt(DAYS_OF_WEEK.length);
- widget.setFirstDayOfWeek(DAYS_OF_WEEK[index]);
+ widget.state().edit()
+ .setFirstDayOfWeek(DAYS_OF_WEEK[index])
+ .commit();
+
}
@OnClick(R.id.button_weeks)
public void onSetWeekMode() {
- widget.setCalendarDisplayMode(CalendarMode.WEEKS);
+ widget.state().edit()
+ .setCalendarDisplayMode(CalendarMode.WEEKS)
+ .commit();
}
@OnClick(R.id.button_months)
public void onSetMonthMode() {
- widget.setCalendarDisplayMode(CalendarMode.MONTHS);
+ widget.state().edit()
+ .setCalendarDisplayMode(CalendarMode.MONTHS)
+ .commit();
}
diff --git a/sample/src/main/java/com/prolificinteractive/materialcalendarview/sample/SwappableBasicActivityDecorated.java b/sample/src/main/java/com/prolificinteractive/materialcalendarview/sample/SwappableBasicActivityDecorated.java
index b1a5e81f..8cd7517f 100644
--- a/sample/src/main/java/com/prolificinteractive/materialcalendarview/sample/SwappableBasicActivityDecorated.java
+++ b/sample/src/main/java/com/prolificinteractive/materialcalendarview/sample/SwappableBasicActivityDecorated.java
@@ -37,14 +37,20 @@ protected void onCreate(Bundle savedInstanceState) {
widget.setOnDateChangedListener(this);
widget.setShowOtherDates(MaterialCalendarView.SHOW_ALL);
- Calendar calendar = Calendar.getInstance();
- widget.setSelectedDate(calendar.getTime());
- calendar.set(calendar.get(Calendar.YEAR), Calendar.JANUARY, 1);
- widget.setMinimumDate(calendar.getTime());
+ Calendar instance = Calendar.getInstance();
+ widget.setSelectedDate(instance.getTime());
- calendar.set(calendar.get(Calendar.YEAR), Calendar.DECEMBER, 31);
- widget.setMaximumDate(calendar.getTime());
+ Calendar instance1 = Calendar.getInstance();
+ instance1.set(instance1.get(Calendar.YEAR), Calendar.JANUARY, 1);
+
+ Calendar instance2 = Calendar.getInstance();
+ instance2.set(instance2.get(Calendar.YEAR), Calendar.DECEMBER, 31);
+
+ widget.state().edit()
+ .setMinimumDate(instance1.getTime())
+ .setMaximumDate(instance2.getTime())
+ .commit();
widget.addDecorators(
new MySelectorDecorator(this),
@@ -62,11 +68,15 @@ public void onDateSelected(@NonNull MaterialCalendarView widget, @NonNull Calend
@OnClick(R.id.button_weeks)
public void onSetWeekMode() {
- widget.setCalendarDisplayMode(CalendarMode.WEEKS);
+ widget.state().edit()
+ .setCalendarDisplayMode(CalendarMode.WEEKS)
+ .commit();
}
@OnClick(R.id.button_months)
public void onSetMonthMode() {
- widget.setCalendarDisplayMode(CalendarMode.MONTHS);
+ widget.state().edit()
+ .setCalendarDisplayMode(CalendarMode.MONTHS)
+ .commit();
}
}