From 2c602467ef5c032ec59f105c6a789252acf0cc7f Mon Sep 17 00:00:00 2001 From: Yen Date: Wed, 26 Sep 2018 10:00:31 -0700 Subject: [PATCH] Added: OnWeekChangedListener --- .../MaterialCalendarView.java | 28 ++++++++++++++++++- .../OnWeekChangedListener.java | 15 ++++++++++ .../SwappableBasicActivityDecorated.java | 23 ++++++++++++++- 3 files changed, 64 insertions(+), 2 deletions(-) create mode 100644 library/src/main/java/com/prolificinteractive/materialcalendarview/OnWeekChangedListener.java diff --git a/library/src/main/java/com/prolificinteractive/materialcalendarview/MaterialCalendarView.java b/library/src/main/java/com/prolificinteractive/materialcalendarview/MaterialCalendarView.java index 6205f2a5..fe882860 100644 --- a/library/src/main/java/com/prolificinteractive/materialcalendarview/MaterialCalendarView.java +++ b/library/src/main/java/com/prolificinteractive/materialcalendarview/MaterialCalendarView.java @@ -208,7 +208,12 @@ public void onPageSelected(int position) { currentMonth = adapter.getItem(position); updateUi(); - dispatchOnMonthChanged(currentMonth); + if (adapter instanceof MonthPagerAdapter) { + dispatchOnMonthChanged(currentMonth); + } + if (adapter instanceof WeekPagerAdapter) { + dispatchOnWeekChanged(currentMonth); + } } @Override @@ -226,6 +231,7 @@ public void onPageScrolled(int position, float positionOffset, int positionOffse private OnDateSelectedListener listener; private OnDateLongClickListener longClickListener; private OnMonthChangedListener monthListener; + private OnWeekChangedListener weekListener; private OnRangeSelectedListener rangeListener; CharSequence calendarContentDescription; @@ -1328,6 +1334,15 @@ public void setOnMonthChangedListener(OnMonthChangedListener listener) { this.monthListener = listener; } + /** + * Sets the listener to be notified upon week changes. + * + * @param listener thing to be notified + */ + public void setOnWeekChangedListener(OnWeekChangedListener listener) { + this.weekListener = listener; + } + /** * Sets the listener to be notified upon a range has been selected. * @@ -1380,6 +1395,17 @@ protected void dispatchOnMonthChanged(final CalendarDay day) { } } + /** + * Dispatch date change events to a listener, if set + * + * @param day first day of the new week + */ + protected void dispatchOnWeekChanged(final CalendarDay day) { + if (weekListener != null) { + weekListener.onWeekChanged(MaterialCalendarView.this, day); + } + } + /** * Call by {@link CalendarPagerView} to indicate that a day was clicked and we should handle it. * This method will always process the click to the selected date. diff --git a/library/src/main/java/com/prolificinteractive/materialcalendarview/OnWeekChangedListener.java b/library/src/main/java/com/prolificinteractive/materialcalendarview/OnWeekChangedListener.java new file mode 100644 index 00000000..36a398df --- /dev/null +++ b/library/src/main/java/com/prolificinteractive/materialcalendarview/OnWeekChangedListener.java @@ -0,0 +1,15 @@ +package com.prolificinteractive.materialcalendarview; + +/** + * The callback used to indicate the user changes the displayed week + */ +public interface OnWeekChangedListener { + + /** + * Called upon change of the selected day + * + * @param widget the view associated with this listener + * @param date the week picked, as the first day of the week + */ + void onWeekChanged(MaterialCalendarView widget, CalendarDay date); +} 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 aaa5ba6f..e8ddcc87 100644 --- a/sample/src/main/java/com/prolificinteractive/materialcalendarview/sample/SwappableBasicActivityDecorated.java +++ b/sample/src/main/java/com/prolificinteractive/materialcalendarview/sample/SwappableBasicActivityDecorated.java @@ -3,6 +3,7 @@ import android.os.Bundle; import android.support.annotation.NonNull; import android.support.v7.app.AppCompatActivity; +import android.widget.Toast; import butterknife.BindView; import butterknife.ButterKnife; import butterknife.OnClick; @@ -10,17 +11,22 @@ import com.prolificinteractive.materialcalendarview.CalendarMode; import com.prolificinteractive.materialcalendarview.MaterialCalendarView; import com.prolificinteractive.materialcalendarview.OnDateSelectedListener; +import com.prolificinteractive.materialcalendarview.OnMonthChangedListener; +import com.prolificinteractive.materialcalendarview.OnWeekChangedListener; import com.prolificinteractive.materialcalendarview.sample.decorators.HighlightWeekendsDecorator; import com.prolificinteractive.materialcalendarview.sample.decorators.MySelectorDecorator; import com.prolificinteractive.materialcalendarview.sample.decorators.OneDayDecorator; import org.threeten.bp.LocalDate; import org.threeten.bp.Month; +import org.threeten.bp.format.DateTimeFormatter; /** * Shows off the most basic usage */ public class SwappableBasicActivityDecorated extends AppCompatActivity - implements OnDateSelectedListener { + implements OnDateSelectedListener, OnMonthChangedListener, OnWeekChangedListener { + + private static final DateTimeFormatter FORMATTER = DateTimeFormatter.ofPattern("EEE, d MMM yyyy"); private final OneDayDecorator oneDayDecorator = new OneDayDecorator(); @@ -31,6 +37,8 @@ public class SwappableBasicActivityDecorated extends AppCompatActivity setContentView(R.layout.activity_basic_modes); ButterKnife.bind(this); + widget.setOnMonthChangedListener(this); + widget.setOnWeekChangedListener(this); widget.setOnDateChangedListener(this); widget.setShowOtherDates(MaterialCalendarView.SHOW_ALL); @@ -72,4 +80,17 @@ public void onSetMonthMode() { .setCalendarDisplayMode(CalendarMode.MONTHS) .commit(); } + + @Override + public void onMonthChanged(MaterialCalendarView widget, CalendarDay date) { + Toast.makeText(this, "First day of Month: " + FORMATTER.format(date.getDate()), + Toast.LENGTH_SHORT).show(); + } + + @Override + public void onWeekChanged(MaterialCalendarView widget, CalendarDay date) { + Toast.makeText(this, "First day of Week: " + FORMATTER.format(date.getDate()), + Toast.LENGTH_SHORT).show(); + } + }