Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add ability set picker type at first launch #30

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ public class DatePickerDialog extends BottomSheetPickerDialog implements
private static final int UNINITIALIZED = -1;
private static final int MONTH_AND_DAY_VIEW = 0;
private static final int YEAR_VIEW = 1;
private static final int DEFAULT_VIEW = MONTH_AND_DAY_VIEW;

private static final String KEY_SELECTED_YEAR = "year";
private static final String KEY_SELECTED_MONTH = "month";
Expand Down Expand Up @@ -90,6 +91,7 @@ public class DatePickerDialog extends BottomSheetPickerDialog implements
private Button mDoneButton;
private Button mCancelButton;

private int mInitViewType = DEFAULT_VIEW; // MONTH_AND_DAY_VIEW or YEAR_VIEW
private int mCurrentView = UNINITIALIZED;

private int mWeekStart = mCalendar.getFirstDayOfWeek();
Expand Down Expand Up @@ -226,7 +228,7 @@ public View onCreateView(LayoutInflater inflater, ViewGroup container,

int listPosition = -1;
int listPositionOffset = 0;
int currentView = MONTH_AND_DAY_VIEW;
int currentView = mInitViewType;
int dayPickerCurrentView = DAY_PICKER_INDEX;
if (savedInstanceState != null) {
mWeekStart = savedInstanceState.getInt(KEY_WEEK_START);
Expand Down Expand Up @@ -391,6 +393,14 @@ public void onPause() {
mHapticFeedbackController.stop();
}

/**
* Set the init picker view type when dialog appear first time
* @param type MONTH_AND_DAY_VIEW or YEAR_VIEW
*/
private void setInitViewType(final int type) {
mInitViewType = type;
}

private void setCurrentView(final int viewIndex) {
long millis = mCalendar.getTimeInMillis();

Expand Down Expand Up @@ -839,6 +849,8 @@ public static class Builder extends BottomSheetPickerDialog.Builder {
private int mDayOfWeekHeaderTextColorSelected;
private int mDayOfWeekHeaderTextColorUnselected;

private int mInitViewType = DEFAULT_VIEW;

/**
* @param listener How the parent is notified that the date is set.
* @param year The initial year of the dialog.
Expand Down Expand Up @@ -965,6 +977,16 @@ protected final void super_build(@NonNull BottomSheetPickerDialog dialog) {
build(dialog);
}

public Builder startSelectionFromMonthAndDay() {
mInitViewType = MONTH_AND_DAY_VIEW;
return this;
}

public Builder startSelectionFromYear() {
mInitViewType = YEAR_VIEW;
return this;
}

@Override
public DatePickerDialog build() {
DatePickerDialog dialog = newInstance(mListener, mYear, mMonthOfYear, mDayOfMonth);
Expand All @@ -980,6 +1002,7 @@ private void build(@NonNull BottomSheetPickerDialog dialog) {
datePickerDialog.setDayOfWeekHeaderTextColorSelected(mDayOfWeekHeaderTextColorSelected);
datePickerDialog.setDayOfWeekHeaderTextColorUnselected(mDayOfWeekHeaderTextColorUnselected);
datePickerDialog.setFirstDayOfWeek(mWeekStart);
datePickerDialog.setInitViewType(mInitViewType);
if (mMinDate != null) {
datePickerDialog.setMinDate(mMinDate);
}
Expand Down