Skip to content

Commit

Permalink
Fix - Range selection ordering (#818)
Browse files Browse the repository at this point in the history
  • Loading branch information
quentin41500 authored Jul 24, 2018
1 parent 000b184 commit e3d157e
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 43 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import org.threeten.bp.LocalDate;

/**
* Pager adapter backing the calendar view
Expand Down Expand Up @@ -294,6 +295,13 @@ public void clearSelections() {
invalidateSelectedDates();
}

/**
* Select or un-select a day.
*
* @param day Day to select or un-select
* @param selected Whether to select or un-select the day from the list.
* @see CalendarPagerAdapter#selectRange(CalendarDay, CalendarDay)
*/
public void setDateSelected(CalendarDay day, boolean selected) {
if (selected) {
if (!selectedDates.contains(day)) {
Expand All @@ -308,6 +316,31 @@ public void setDateSelected(CalendarDay day, boolean selected) {
}
}

/**
* Clear the previous selection, select the range of days from first to last, and finally
* invalidate. First day should be before last day, otherwise the selection won't happen.
*
* @param first The first day of the range.
* @param last The last day in the range.
* @see CalendarPagerAdapter#setDateSelected(CalendarDay, boolean)
*/
public void selectRange(final CalendarDay first, final CalendarDay last) {
selectedDates.clear();

// Copy to start from the first day and increment
LocalDate temp = LocalDate.of(first.getYear(), first.getMonth(), first.getDay());

// for comparison
final LocalDate end = last.getDate();

while (temp.isBefore(end) || temp.equals(end)) {
selectedDates.add(CalendarDay.from(temp));
temp = temp.plusDays(1);
}

invalidateSelectedDates();
}

private void invalidateSelectedDates() {
validateSelectedDates();
for (V pagerView : currentViews) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Locale;
import org.threeten.bp.DayOfWeek;
import org.threeten.bp.LocalDate;
import org.threeten.bp.temporal.TemporalField;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -780,10 +780,19 @@ public void setWeekDayTextAppearance(int resourceId) {
}

/**
* @return the selected day, or null if no selection. If in multiple selection mode, this
* will return the last selected date
* Get the currently selected date, or null if no selection. Depending on the selection mode,
* you might get different results.
*
* <p>For {@link #SELECTION_MODE_SINGLE}, returns the selected date.</p>
* <p>For {@link #SELECTION_MODE_MULTIPLE}, returns the last date selected.</p>
* <p>For {@link #SELECTION_MODE_RANGE}, returns the last date of the range. In most cases, you should probably be using {@link #getSelectedDates()}.</p>
* <p>For {@link #SELECTION_MODE_NONE}, returns null.</p>
*
* @return The selected day, or null if no selection. If in multiple selection mode, this
* will return the last date of the list of selected dates.
* @see MaterialCalendarView#getSelectedDates()
*/
public CalendarDay getSelectedDate() {
@Nullable public CalendarDay getSelectedDate() {
List<CalendarDay> dates = adapter.getSelectedDates();
if (dates.isEmpty()) {
return null;
Expand All @@ -793,10 +802,16 @@ public CalendarDay getSelectedDate() {
}

/**
* @return all of the currently selected dates
* Return the list of currently selected dates. Mostly useful for {@link #SELECTION_MODE_MULTIPLE}
* and {@link #SELECTION_MODE_RANGE}. For the other modes, check {@link #getSelectedDate()}.
*
* <p>For {@link #SELECTION_MODE_MULTIPLE}, returns the list in the order of selection.</p>
* <p>For {@link #SELECTION_MODE_RANGE}, returns the range of dates ordered chronologically.</p>
*
* @return All of the currently selected dates.
* @see MaterialCalendarView#getSelectedDate()
*/
@NonNull
public List<CalendarDay> getSelectedDates() {
@NonNull public List<CalendarDay> getSelectedDates() {
return adapter.getSelectedDates();
}

Expand Down Expand Up @@ -1395,30 +1410,13 @@ 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.
* Dispatch a range of days to a range listener, if set, ordered chronologically.
*
* @param firstDay first day enclosing a range
* @param lastDay last day enclosing a range
* @param days Enclosing days ordered from first to last day.
*/
protected void dispatchOnRangeSelected(final CalendarDay firstDay, final CalendarDay lastDay) {
final OnRangeSelectedListener listener = rangeListener;
final List<CalendarDay> days = new ArrayList<>();

// Copy to start from the first day and increment
LocalDate temp = LocalDate.of(firstDay.getYear(), firstDay.getMonth(), firstDay.getDay());

// for comparison
final LocalDate end = lastDay.getDate();

while (temp.isBefore(end) || temp.equals(end)) {
final CalendarDay current = CalendarDay.from(temp);
adapter.setDateSelected(current, true);
days.add(current);
temp = temp.plusDays(1);
}

if (listener != null) {
listener.onRangeSelected(MaterialCalendarView.this, days);
protected void dispatchOnRangeSelected(@NonNull final List<CalendarDay> days) {
if (rangeListener != null) {
rangeListener.onRangeSelected(MaterialCalendarView.this, days);
}
}

Expand Down Expand Up @@ -1457,16 +1455,18 @@ protected void onDateClicked(@NonNull CalendarDay date, boolean nowSelected) {
} else if (currentSelection.size() == 1) {
// Selecting the second date of a range
final CalendarDay firstDaySelected = currentSelection.get(0);
adapter.setDateSelected(date, nowSelected);
if (firstDaySelected.equals(date)) {
// Right now, we are not supporting a range of one day, so we are removing the day instead.
adapter.setDateSelected(date, nowSelected);
dispatchOnDateSelected(date, nowSelected);
} else if (firstDaySelected.isAfter(date)) {
// Selecting a range, dispatching...
dispatchOnRangeSelected(date, firstDaySelected);
} else {
// Selecting a range, dispatching in reverse order...
dispatchOnRangeSelected(firstDaySelected, date);
adapter.selectRange(date, firstDaySelected);
dispatchOnRangeSelected(adapter.getSelectedDates());
} else {
// Selecting a range, dispatching in order...
adapter.selectRange(firstDaySelected, date);
dispatchOnRangeSelected(adapter.getSelectedDates());
}
} else {
// Clearing selection and making a selection of the new date.
Expand All @@ -1493,13 +1493,14 @@ protected void onDateClicked(@NonNull CalendarDay date, boolean nowSelected) {
* @param lastDay last day of the range to select
*/
public void selectRange(final CalendarDay firstDay, final CalendarDay lastDay) {
clearSelection();
if (firstDay == null || lastDay == null) {
return;
} else if (firstDay.isAfter(lastDay)) {
dispatchOnRangeSelected(lastDay, firstDay);
adapter.selectRange(lastDay, firstDay);
dispatchOnRangeSelected(adapter.getSelectedDates());
} else {
dispatchOnRangeSelected(firstDay, lastDay);
adapter.selectRange(firstDay, lastDay);
dispatchOnRangeSelected(adapter.getSelectedDates());
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,17 @@
import android.content.Context;
import android.content.DialogInterface;
import android.graphics.Color;
import android.icu.util.Calendar;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.DatePicker;
import android.widget.LinearLayout;
import android.widget.NumberPicker;
import android.widget.Toast;
import com.prolificinteractive.materialcalendarview.CalendarDay;
import com.prolificinteractive.materialcalendarview.CalendarMode;
import com.prolificinteractive.materialcalendarview.MaterialCalendarView;
import java.sql.Date;
import java.util.Random;
import butterknife.BindView;
import butterknife.ButterKnife;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.widget.Toast;
import butterknife.BindView;
import butterknife.ButterKnife;
Expand All @@ -13,6 +14,7 @@
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.List;
import org.threeten.bp.format.DateTimeFormatter;

/**
* An activity that demonstrate the multiple selection mode that the calendar offers.
Expand All @@ -23,7 +25,7 @@
public class SelectionModesActivity extends AppCompatActivity
implements OnDateSelectedListener, OnRangeSelectedListener {

private static final DateFormat FORMATTER = SimpleDateFormat.getDateInstance();
private static final DateTimeFormatter FORMATTER = DateTimeFormatter.ofPattern("EEE, d MMM yyyy");

@BindView(R.id.calendar_view_single) MaterialCalendarView single;
@BindView(R.id.calendar_view_multi) MaterialCalendarView multi;
Expand Down Expand Up @@ -72,5 +74,4 @@ void onCalendarModeChanged(boolean checked) {
range.state().edit().setCalendarDisplayMode(mode).commit();
none.state().edit().setCalendarDisplayMode(mode).commit();
}

}

0 comments on commit e3d157e

Please sign in to comment.