Skip to content

Commit

Permalink
Do not offset month adapter count by min date.
Browse files Browse the repository at this point in the history
  • Loading branch information
philliphsu committed May 7, 2017
1 parent 77f96d1 commit 9a1757f
Showing 1 changed file with 4 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
import com.philliphsu.bottomsheetpickers.date.MonthView.OnDayClickListener;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashMap;

import static android.view.ViewGroup.LayoutParams.MATCH_PARENT;
Expand Down Expand Up @@ -110,24 +109,9 @@ protected void init() {

@Override
public int getCount() {
final Calendar minDate = mController.getMinDate();
final Calendar maxDate = mController.getMaxDate();
final int minMonth = minDate == null ? 0 : minDate.get(Calendar.MONTH);

// The number of months remaining in minDate's year, from minMonth forward.
int monthsInMinYear = MONTHS_IN_YEAR - minMonth;
// The number of months in maxDate's year, up to maxDate's month.
// From Calendar.JANUARY (0) forward.
int monthsInMaxYear = maxDate == null ? MONTHS_IN_YEAR : maxDate.get(Calendar.MONTH) + 1;

int count = monthsInMinYear + monthsInMaxYear;
// Add the number of months in between the min and max dates.
// Subtract 2 from the year range because we already counted the min and max years.
// Add 1 to get the total number of years in the year range.
// e.g. There are 11 years in the range [2016, 2026].
// => net difference = -1
count += (mController.getMaxYear() - mController.getMinYear() - 1) * MONTHS_IN_YEAR;
return count;
return (mController.getMaxYear() - mController.getMinYear() + 1) * MONTHS_IN_YEAR;
}

@Override
Expand Down Expand Up @@ -267,26 +251,21 @@ protected void onDayTapped(CalendarDay day) {
* Returns the month (0 - 11) displayed at this position.
*/
final int getMonth(int position) {
return (position + getMonthOffset()) % MONTHS_IN_YEAR;
return position % MONTHS_IN_YEAR;
}

/**
* Returns the year displayed at this position.
*/
final int getYear(int position) {
return (position + getMonthOffset()) / MONTHS_IN_YEAR + mController.getMinYear();
return position / MONTHS_IN_YEAR + mController.getMinYear();
}

/**
* @return The page position at which the given day is located.
*/
final int getPosition(CalendarDay day) {
int positionOfYear = MONTHS_IN_YEAR * (day.year - mController.getMinYear()) - getMonthOffset();
int positionOfYear = MONTHS_IN_YEAR * (day.year - mController.getMinYear());
return positionOfYear + day.month;
}

private int getMonthOffset() {
Calendar minDate = mController.getMinDate();
return minDate == null ? 0 : minDate.get(Calendar.MONTH);
}
}

0 comments on commit 9a1757f

Please sign in to comment.