Skip to content

Commit bfed03c

Browse files
committed
Merge branch 'feature/add_date_range'
2 parents 7e42c03 + f7d95b4 commit bfed03c

File tree

7 files changed

+145
-9
lines changed

7 files changed

+145
-9
lines changed

datetimepicker-library/res/values/colors.xml

+1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
<color name="blue">#ff33b5e5</color>
99
<color name="darker_blue">#ff0099cc</color>
1010
<color name="date_picker_text_normal">#ff999999</color>
11+
<color name="date_picker_text_disabled">#ffcccccc</color>
1112
<color name="calendar_header">#ff999999</color>
1213
<color name="date_picker_view_animator">#fff2f2f2</color>
1314

datetimepicker-library/src/com/fourmob/datetimepicker/date/DatePickerController.java

+8
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,15 @@ abstract interface DatePickerController {
66
public abstract int getMaxYear();
77

88
public abstract int getMinYear();
9+
10+
public abstract int getStartMonth();
911

12+
public abstract int getEndMonth();
13+
14+
public abstract int getStartDay();
15+
16+
public abstract int getEndDay();
17+
1018
public abstract SimpleMonthAdapter.CalendarDay getSelectedDay();
1119

1220
public abstract void onDayOfMonthSelected(int year, int month, int day);

datetimepicker-library/src/com/fourmob/datetimepicker/date/DatePickerDialog.java

+104-3
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,10 @@ public class DatePickerDialog extends DialogFragment implements View.OnClickList
4747
private HashSet<OnDateChangedListener> mListeners = new HashSet<OnDateChangedListener>();
4848
private int mMaxYear = MAX_YEAR;
4949
private int mMinYear = MIN_YEAR;
50+
private int mStartMonth = 0;
51+
private int mStartDay = 1;
52+
private int mEndMonth = 11;
53+
private int mEndDay = 31;
5054
private LinearLayout mMonthAndDayView;
5155
private String mSelectDay;
5256
private String mSelectYear;
@@ -67,6 +71,31 @@ private void adjustDayInMonthIfNeeded(int month, int year) {
6771
if (currentDay > day)
6872
this.mCalendar.set(Calendar.DAY_OF_MONTH, day);
6973
}
74+
75+
private void adjustDayAndMonthIfNeeded(int month, int year) {
76+
77+
if (year == this.mMinYear) {
78+
if (month < mStartMonth) {
79+
this.mCalendar.set(Calendar.MONTH, this.mStartMonth);
80+
this.mCalendar.set(Calendar.DAY_OF_MONTH, this.mStartDay);
81+
}
82+
if (month == mStartMonth && this.mCalendar.get(Calendar.DAY_OF_MONTH) < this.mStartDay) {
83+
this.mCalendar.set(Calendar.DAY_OF_MONTH, this.mStartDay);
84+
}
85+
}
86+
87+
if (year == this.mMaxYear) {
88+
if (month > mEndMonth) {
89+
this.mCalendar.set(Calendar.MONTH, this.mEndMonth);
90+
this.mCalendar.set(Calendar.DAY_OF_MONTH, this.mEndDay);
91+
}
92+
if (month == mEndMonth && this.mCalendar.get(Calendar.DAY_OF_MONTH) > this.mEndDay) {
93+
this.mCalendar.set(Calendar.DAY_OF_MONTH, this.mEndDay);
94+
}
95+
}
96+
adjustDayInMonthIfNeeded(month, year);
97+
98+
}
7099

71100
public DatePickerDialog() {
72101
// Empty constructor required for dialog fragment. DO NOT REMOVE
@@ -218,6 +247,10 @@ public View onCreateView(LayoutInflater layoutInflater, ViewGroup parent, Bundle
218247
this.mWeekStart = bundle.getInt("week_start");
219248
this.mMinYear = bundle.getInt("year_start");
220249
this.mMaxYear = bundle.getInt("year_end");
250+
this.mStartMonth = bundle.getInt("month_start");
251+
this.mEndMonth = bundle.getInt("month_end");
252+
this.mStartDay = bundle.getInt("day_start");
253+
this.mEndDay = bundle.getInt("day_end");
221254
currentView = bundle.getInt("current_view");
222255
listPosition = bundle.getInt("list_position");
223256
listPositionOffset = bundle.getInt("list_position_offset");
@@ -279,6 +312,10 @@ public void onSaveInstanceState(Bundle bundle) {
279312
bundle.putInt("week_start", this.mWeekStart);
280313
bundle.putInt("year_start", this.mMinYear);
281314
bundle.putInt("year_end", this.mMaxYear);
315+
bundle.putInt("month_start", this.mStartMonth);
316+
bundle.putInt("month_end", this.mEndMonth);
317+
bundle.putInt("day_start", this.mStartDay);
318+
bundle.putInt("day_end", this.mEndDay);
282319
bundle.putInt("current_view", this.mCurrentView);
283320
int mostVisiblePosition = -1;
284321
if (this.mCurrentView == 0)
@@ -292,7 +329,7 @@ public void onSaveInstanceState(Bundle bundle) {
292329
}
293330

294331
public void onYearSelected(int year) {
295-
adjustDayInMonthIfNeeded(this.mCalendar.get(Calendar.MONTH), year);
332+
adjustDayAndMonthIfNeeded(this.mCalendar.get(Calendar.MONTH), year);
296333
this.mCalendar.set(Calendar.YEAR, year);
297334
updatePickers();
298335
setCurrentView(0);
@@ -316,8 +353,6 @@ public void setOnDateSetListener(OnDateSetListener onDateSetListener) {
316353
}
317354

318355
public void setYearRange(int minYear, int maxYear) {
319-
if (maxYear <= minYear)
320-
throw new IllegalArgumentException("Year end must be larger than year start");
321356
if (maxYear > MAX_YEAR)
322357
throw new IllegalArgumentException("max year end must < " + MAX_YEAR);
323358
if (minYear < MIN_YEAR)
@@ -327,7 +362,72 @@ public void setYearRange(int minYear, int maxYear) {
327362
if (this.mDayPickerView != null)
328363
this.mDayPickerView.onChange();
329364
}
365+
366+
367+
/**
368+
* Sets the start date for the DatePickerDialog.
369+
*
370+
* @param startYear The minimum year.
371+
* @param startMonth The first valid month of the startYear. This must be a valid Calendar.Month. (0-11). Default is 0.
372+
* @param startDay The first valid day of startMont (inclusive). Default is 1.
373+
*/
374+
public void setStartDate(int startYear, int startMonth, int startDay) {
375+
setDateRange(startYear, startMonth, startDay, mMaxYear, mEndMonth, mEndDay);
376+
}
377+
378+
/**
379+
* Sets the end date for the DatePickerDialog.
380+
*
381+
* @param endYear The maximum year.
382+
* @param endMonth The last valid month of the endYear. This must be a valid Calendar.Month. (0-11). Default is 11.
383+
* @param endDay THe last valid day of endMonth (inclusive). Default is 31.
384+
*/
385+
public void setEndDate(int endYear, int endMonth, int endDay) {
386+
setDateRange(mMinYear, mStartMonth, mStartDay, endYear, endMonth, endDay);
387+
}
388+
389+
/**
390+
* Sets the time period for the DatePickerDialog.
391+
*
392+
* @param startYear The minimum year.
393+
* @param startMonth The first valid month of the startYear. This must be a valid Calendar.Month. (0-11). Default is 0.
394+
* @param startDay The first valid day of startMont (inclusive). Default is 1.
395+
* @param endYear The maximum year.
396+
* @param endMonth The last valid month of the endYear. This must be a valid Calendar.Month. (0-11). Default is 11.
397+
* @param endDay THe last valid day of endMonth (inclusive). Default is 31.
398+
*/
399+
public void setDateRange(int startYear, int startMonth, int startDay, int endYear, int endMonth, int endDay) {
400+
setYearRange(startYear, endYear);
401+
if (startMonth >= 12 || startMonth < 0)
402+
throw new IllegalArgumentException("startMonth must be between 0-11");
403+
if (endMonth >= 12 || endMonth < 0)
404+
throw new IllegalArgumentException("endMonth must be between 0-11");
405+
this.mStartMonth = startMonth;
406+
this.mStartDay = startDay;
407+
this.mEndMonth = endMonth;
408+
this.mEndDay = endDay;
409+
}
330410

411+
@Override
412+
public int getStartMonth() {
413+
return this.mStartMonth;
414+
}
415+
416+
@Override
417+
public int getEndMonth() {
418+
return this.mEndMonth;
419+
}
420+
421+
@Override
422+
public int getStartDay() {
423+
return this.mStartDay;
424+
}
425+
426+
@Override
427+
public int getEndDay() {
428+
return this.mEndDay;
429+
}
430+
331431
public void tryVibrate() {
332432
if (this.mVibrator != null && this.mVibrate) {
333433
long timeInMillis = SystemClock.uptimeMillis();
@@ -345,4 +445,5 @@ static abstract interface OnDateChangedListener {
345445
public static abstract interface OnDateSetListener {
346446
public abstract void onDateSet(DatePickerDialog datePickerDialog, int year, int month, int day);
347447
}
448+
348449
}

datetimepicker-library/src/com/fourmob/datetimepicker/date/DayPickerView.java

+4-2
Original file line numberDiff line numberDiff line change
@@ -63,11 +63,13 @@ public int getMostVisiblePosition() {
6363
}
6464

6565
public boolean goTo(SimpleMonthAdapter.CalendarDay calendarDay, boolean scrollToTop, boolean selectDay, boolean displayMonth) {
66-
if (selectDay)
66+
if (selectDay) {
6767
this.mSelectedDay.set(calendarDay);
68+
this.mAdapter.setSelectedDay(this.mSelectedDay);
69+
}
6870

6971
this.mTempDay.set(calendarDay);
70-
int monthIndex = 12 * (calendarDay.year - this.mController.getMinYear()) + calendarDay.month;
72+
int monthIndex = 12 * (calendarDay.year - this.mController.getMinYear()) - this.mController.getStartMonth() + calendarDay.month;
7173
postSetSelection(monthIndex);
7274

7375
// TODO improve

datetimepicker-library/src/com/fourmob/datetimepicker/date/SimpleMonthAdapter.java

+12-3
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@ private boolean isSelectedDayInMonth(int year, int month) {
2929
}
3030

3131
public int getCount() {
32-
return 12 * (1 + (this.mController.getMaxYear() - this.mController.getMinYear()));
32+
return 12 * (this.mController.getMaxYear() - this.mController.getMinYear())
33+
+ this.mController.getEndMonth() - this.mController.getStartMonth() + 1;
3334
}
3435

3536
public Object getItem(int position) {
@@ -54,17 +55,25 @@ public View getView(int position, View convertView, ViewGroup parent) {
5455
if (monthParams == null)
5556
monthParams = new HashMap<String, Integer>();
5657
monthParams.clear();
57-
int month = position % 12;
58-
int year = position / 12 + this.mController.getMinYear();
58+
int month = (position + this.mController.getStartMonth()) % 12;
59+
int year = (position + this.mController.getStartMonth()) / 12 + this.mController.getMinYear();
5960
Log.d("SimpleMonthAdapter", "Year: " + year + ", Month: " + month);
6061
int selectedDay = -1;
6162
if (isSelectedDayInMonth(year, month))
6263
selectedDay = this.mSelectedDay.day;
64+
int startDay = -1;
65+
int endDay = 31;
66+
if (this.mController.getStartMonth() == month && this.mController.getMinYear() == year)
67+
startDay = this.mController.getStartDay();
68+
if (this.mController.getEndMonth() == month && this.mController.getMaxYear() == year)
69+
endDay = this.mController.getEndDay();
6370
simpleMonthView.reuse();
6471
monthParams.put("selected_day", Integer.valueOf(selectedDay));
6572
monthParams.put("year", Integer.valueOf(year));
6673
monthParams.put("month", Integer.valueOf(month));
6774
monthParams.put("week_start", Integer.valueOf(this.mController.getFirstDayOfWeek()));
75+
monthParams.put("start_day", Integer.valueOf(startDay));
76+
monthParams.put("end_day", Integer.valueOf(endDay));
6877
simpleMonthView.setMonthParams(monthParams);
6978
simpleMonthView.invalidate();
7079
return simpleMonthView;

datetimepicker-library/src/com/fourmob/datetimepicker/date/SimpleMonthView.java

+12-1
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,9 @@ public class SimpleMonthView extends View {
6363
protected int mWeekStart = 1;
6464
protected int mWidth;
6565
protected int mYear;
66+
protected int mDayDisabledTextColor;
67+
protected int mStartDay;
68+
protected int mEndDay;
6669
private DateFormatSymbols mDateFormatSymbols = new DateFormatSymbols();
6770

6871
public SimpleMonthView(Context context) {
@@ -73,6 +76,7 @@ public SimpleMonthView(Context context) {
7376
this.mDayOfWeekTypeface = resources.getString(R.string.day_of_week_label_typeface);
7477
this.mMonthTitleTypeface = resources.getString(R.string.sans_serif);
7578
this.mDayTextColor = resources.getColor(R.color.date_picker_text_normal);
79+
this.mDayDisabledTextColor = resources.getColor(R.color.date_picker_text_disabled);
7680
this.mTodayNumberColor = resources.getColor(R.color.blue);
7781
this.mMonthTitleColor = resources.getColor(R.color.white);
7882
this.mMonthTitleBGColor = resources.getColor(R.color.circle_background);
@@ -149,8 +153,11 @@ protected void drawMonthNums(Canvas canvas) {
149153
int x = paddingDay * (1 + dayOffset * 2) + this.mPadding;
150154
if (this.mSelectedDay == day)
151155
canvas.drawCircle(x, y - MINI_DAY_NUMBER_TEXT_SIZE / 3, DAY_SELECTED_CIRCLE_SIZE, this.mSelectedCirclePaint);
156+
152157
if ((this.mHasToday) && (this.mToday == day))
153158
this.mMonthNumPaint.setColor(this.mTodayNumberColor);
159+
else if (day < this.mStartDay || day > this.mEndDay)
160+
this.mMonthNumPaint.setColor(this.mDayDisabledTextColor);
154161
else
155162
this.mMonthNumPaint.setColor(this.mDayTextColor);
156163
canvas.drawText(String.format("%d", day), x, y, this.mMonthNumPaint);
@@ -171,7 +178,9 @@ public SimpleMonthAdapter.CalendarDay getDayFromLocation(float x, float y) {
171178

172179
int yDay = (int) (y - MONTH_HEADER_SIZE) / this.mRowHeight;
173180
int day = 1 + ((int) ((x - padding) * this.mNumDays / (this.mWidth - padding - this.mPadding)) - findDayOffset()) + yDay * this.mNumDays;
174-
181+
// If day out of range
182+
if (day < this.mStartDay || day > this.mEndDay)
183+
return null;
175184
return new SimpleMonthAdapter.CalendarDay(this.mYear, this.mMonth, day);
176185
}
177186

@@ -254,6 +263,8 @@ public void setMonthParams(HashMap<String, Integer> monthParams) {
254263
this.mSelectedDay = ((Integer) monthParams.get("selected_day")).intValue();
255264
this.mMonth = ((Integer) monthParams.get("month")).intValue();
256265
this.mYear = ((Integer) monthParams.get("year")).intValue();
266+
this.mStartDay = ((Integer) monthParams.get("start_day")).intValue();
267+
this.mEndDay = ((Integer) monthParams.get("end_day")).intValue();
257268
Time time = new Time(Time.getCurrentTimezone());
258269
time.setToNow();
259270
this.mHasToday = false;

datetimepicker-sample/src/com/fourmob/datetimepicker/sample/MainActivity.java

+4
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,10 @@ protected void onCreate(Bundle savedInstanceState) {
3636
public void onClick(View v) {
3737
datePickerDialog.setVibrate(isVibrate());
3838
datePickerDialog.setYearRange(1985, 2028);
39+
40+
// Alternatively, you can set the date range of the Date Picker
41+
// datePickerDialog.setDateRange(startYear, startMonth, startDay, endYear, endMonth, endDay);
42+
3943
datePickerDialog.show(getSupportFragmentManager(), DATEPICKER_TAG);
4044
}
4145
});

0 commit comments

Comments
 (0)