From 36287b8dfe1cc65b6d8f25ac67144953b4757d73 Mon Sep 17 00:00:00 2001 From: Artur Schens Date: Mon, 22 Oct 2018 19:10:49 +0200 Subject: [PATCH] refactored setter --- .../date/DatePickerDialog.java | 179 ++++++++++-------- 1 file changed, 95 insertions(+), 84 deletions(-) diff --git a/library/src/main/java/com/borax12/materialdaterangepicker/date/DatePickerDialog.java b/library/src/main/java/com/borax12/materialdaterangepicker/date/DatePickerDialog.java index 9346616..7e85704 100644 --- a/library/src/main/java/com/borax12/materialdaterangepicker/date/DatePickerDialog.java +++ b/library/src/main/java/com/borax12/materialdaterangepicker/date/DatePickerDialog.java @@ -51,7 +51,6 @@ import java.util.Locale; - /** * Dialog allowing users to select a date. */ @@ -161,23 +160,26 @@ public class DatePickerDialog extends DialogFragment implements private com.borax12.materialdaterangepicker.date.SimpleDayPickerView mDayPickerViewEnd; private com.borax12.materialdaterangepicker.date.YearPickerView mYearPickerViewEnd; private com.borax12.materialdaterangepicker.date.AccessibleDateAnimator mAnimatorEnd; - private int tabTag=1; + private int tabTag = 1; private String startTitle; private String endTitle; + private int cancelButtonBackground; + private int okButtonBackground; + /** * The callback used to indicate the user is done filling in the date. */ public interface OnDateSetListener { /** - * @param view The view associated with this listener. - * @param year The year that was set. + * @param view The view associated with this listener. + * @param year The year that was set. * @param monthOfYear The month that was set (0-11) for compatibility - * with {@link java.util.Calendar}. - * @param dayOfMonth The day of the month that was set. + * with {@link java.util.Calendar}. + * @param dayOfMonth The day of the month that was set. */ - void onDateSet(DatePickerDialog view, int year, int monthOfYear, int dayOfMonth,int yearEnd, int monthOfYearEnd, int dayOfMonthEnd); + void onDateSet(DatePickerDialog view, int year, int monthOfYear, int dayOfMonth, int yearEnd, int monthOfYearEnd, int dayOfMonthEnd); } /** @@ -194,28 +196,27 @@ public DatePickerDialog() { } /** - * @param callBack How the parent is notified that the date is set. - * @param year The initial year of the dialog. + * @param callBack How the parent is notified that the date is set. + * @param year The initial year of the dialog. * @param monthOfYear The initial month of the dialog. - * @param dayOfMonth The initial day of the dialog. + * @param dayOfMonth The initial day of the dialog. */ public static DatePickerDialog newInstance(OnDateSetListener callBack, int year, - int monthOfYear, - int dayOfMonth) { + int monthOfYear, + int dayOfMonth) { DatePickerDialog ret = new DatePickerDialog(); ret.initialize(callBack, year, monthOfYear, dayOfMonth); return ret; } /** - * - * @param callBack How the parent is notified that the date is set. - * @param year The initial year of the dialog. - * @param monthOfYear The initial month of the dialog. - * @param dayOfMonth The initial day of the dialog. - * @param yearEnd The end year of the dialog. + * @param callBack How the parent is notified that the date is set. + * @param year The initial year of the dialog. + * @param monthOfYear The initial month of the dialog. + * @param dayOfMonth The initial day of the dialog. + * @param yearEnd The end year of the dialog. * @param montOfYearEnd The end month of the dialog. - * @param dayOfMonthEnd The end day of the dialog. + * @param dayOfMonthEnd The end day of the dialog. */ public static DatePickerDialog newInstance(OnDateSetListener callBack, int year, int monthOfYear, @@ -284,11 +285,11 @@ public void onSaveInstanceState(@NonNull Bundle outState) { outState.putInt(KEY_CURRENT_VIEW_END, mCurrentViewEnd); int listPosition = -1; int listPositionEnd = -1; - if (mCurrentView == MONTH_AND_DAY_VIEW||mCurrentViewEnd==MONTH_AND_DAY_VIEW) { + if (mCurrentView == MONTH_AND_DAY_VIEW || mCurrentViewEnd == MONTH_AND_DAY_VIEW) { listPosition = mDayPickerView.getMostVisiblePosition(); listPositionEnd = mDayPickerViewEnd.getMostVisiblePosition(); - } else if (mCurrentView == YEAR_VIEW||mCurrentViewEnd==YEAR_VIEW) { + } else if (mCurrentView == YEAR_VIEW || mCurrentViewEnd == YEAR_VIEW) { listPosition = mYearPickerView.getFirstVisiblePosition(); listPositionEnd = mYearPickerViewEnd.getFirstVisiblePosition(); outState.putInt(KEY_LIST_POSITION_OFFSET, mYearPickerView.getFirstPositionOffset()); @@ -312,7 +313,7 @@ public void onSaveInstanceState(@NonNull Bundle outState) { @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, - Bundle savedInstanceState) { + Bundle savedInstanceState) { getDialog().getWindow().requestFeature(Window.FEATURE_NO_TITLE); View view = inflater.inflate(R.layout.range_date_picker_dialog, null); @@ -328,7 +329,7 @@ public View onCreateView(LayoutInflater inflater, ViewGroup container, TabHost.TabSpec endDatePage = tabHost.newTabSpec("end"); endDatePage.setContent(R.id.end_date_group); - endDatePage.setIndicator((endTitle!=null&&!endTitle.isEmpty())?endTitle:activity.getResources().getString(R.string.mdtp_to)); + endDatePage.setIndicator((endTitle != null && !endTitle.isEmpty()) ? endTitle : activity.getResources().getString(R.string.mdtp_to)); tabHost.addTab(startDatePage); tabHost.addTab(endDatePage); @@ -367,14 +368,14 @@ public View onCreateView(LayoutInflater inflater, ViewGroup container, listPositionOffset = savedInstanceState.getInt(KEY_LIST_POSITION_OFFSET); listPositionEnd = savedInstanceState.getInt(KEY_LIST_POSITION_END); listPositionOffsetEnd = savedInstanceState.getInt(KEY_LIST_POSITION_OFFSET_END); - mMinDate = (Calendar)savedInstanceState.getSerializable(KEY_MIN_DATE); - mMaxDate = (Calendar)savedInstanceState.getSerializable(KEY_MAX_DATE); - mMinDateEnd = (Calendar)savedInstanceState.getSerializable(KEY_MIN_DATE_END); - mMaxDateEnd= (Calendar)savedInstanceState.getSerializable(KEY_MAX_DATE_END); - highlightedDays = (Calendar[])savedInstanceState.getSerializable(KEY_HIGHLIGHTED_DAYS); - selectableDays = (Calendar[])savedInstanceState.getSerializable(KEY_SELECTABLE_DAYS); - highlightedDaysEnd = (Calendar[])savedInstanceState.getSerializable(KEY_HIGHLIGHTED_DAYS_END); - selectableDaysEnd = (Calendar[])savedInstanceState.getSerializable(KEY_SELECTABLE_DAYS_END); + mMinDate = (Calendar) savedInstanceState.getSerializable(KEY_MIN_DATE); + mMaxDate = (Calendar) savedInstanceState.getSerializable(KEY_MAX_DATE); + mMinDateEnd = (Calendar) savedInstanceState.getSerializable(KEY_MIN_DATE_END); + mMaxDateEnd = (Calendar) savedInstanceState.getSerializable(KEY_MAX_DATE_END); + highlightedDays = (Calendar[]) savedInstanceState.getSerializable(KEY_HIGHLIGHTED_DAYS); + selectableDays = (Calendar[]) savedInstanceState.getSerializable(KEY_SELECTABLE_DAYS); + highlightedDaysEnd = (Calendar[]) savedInstanceState.getSerializable(KEY_HIGHLIGHTED_DAYS_END); + selectableDaysEnd = (Calendar[]) savedInstanceState.getSerializable(KEY_SELECTABLE_DAYS_END); mThemeDark = savedInstanceState.getBoolean(KEY_THEME_DARK); mAccentColor = savedInstanceState.getInt(KEY_ACCENT); mVibrate = savedInstanceState.getBoolean(KEY_VIBRATE); @@ -431,12 +432,13 @@ public void onClick(View v) { tryVibrate(); if (mCallBack != null) { mCallBack.onDateSet(DatePickerDialog.this, mCalendar.get(Calendar.YEAR), - mCalendar.get(Calendar.MONTH), mCalendar.get(Calendar.DAY_OF_MONTH),mCalendarEnd.get(Calendar.YEAR), + mCalendar.get(Calendar.MONTH), mCalendar.get(Calendar.DAY_OF_MONTH), mCalendarEnd.get(Calendar.YEAR), mCalendarEnd.get(Calendar.MONTH), mCalendarEnd.get(Calendar.DAY_OF_MONTH)); } dismiss(); } }); + okButton.setBackgroundColor(okButtonBackground); okButton.setTypeface(TypefaceHelper.get(activity, "Roboto-Medium")); Button cancelButton = (Button) view.findViewById(R.id.cancel); @@ -444,10 +446,11 @@ public void onClick(View v) { @Override public void onClick(View v) { tryVibrate(); - if(getDialog() != null) getDialog().cancel(); + if (getDialog() != null) getDialog().cancel(); } }); - cancelButton.setTypeface(TypefaceHelper.get(activity,"Roboto-Medium")); + cancelButton.setBackgroundColor(cancelButtonBackground); + cancelButton.setTypeface(TypefaceHelper.get(activity, "Roboto-Medium")); cancelButton.setVisibility(isCancelable() ? View.VISIBLE : View.GONE); //If an accent color has not been set manually, try and get it from the context @@ -458,7 +461,8 @@ public void onClick(View v) { } } if (mAccentColor != -1) { - if(mDayOfWeekView != null) mDayOfWeekView.setBackgroundColor(Utils.darkenColor(mAccentColor)); + if (mDayOfWeekView != null) + mDayOfWeekView.setBackgroundColor(Utils.darkenColor(mAccentColor)); view.findViewById(R.id.day_picker_selected_date_layout).setBackgroundColor(mAccentColor); view.findViewById(R.id.day_picker_selected_date_layout_end).setBackgroundColor(mAccentColor); okButton.setTextColor(mAccentColor); @@ -494,13 +498,12 @@ public void onClick(View v) { @Override public void onTabChanged(String tabId) { com.borax12.materialdaterangepicker.date.MonthAdapter.CalendarDay calendarDay; - if(tabId.equals("start")){ + if (tabId.equals("start")) { calendarDay = new com.borax12.materialdaterangepicker.date.MonthAdapter.CalendarDay(mCalendar.getTimeInMillis()); - mDayPickerView.goTo(calendarDay,true,true,false); - } - else{ + mDayPickerView.goTo(calendarDay, true, true, false); + } else { calendarDay = new com.borax12.materialdaterangepicker.date.MonthAdapter.CalendarDay(mCalendarEnd.getTimeInMillis()); - mDayPickerViewEnd.goTo(calendarDay,true,true,false); + mDayPickerViewEnd.goTo(calendarDay, true, true, false); } } @@ -518,23 +521,24 @@ public void onResume() { public void onPause() { super.onPause(); mHapticFeedbackController.stop(); - if(mDismissOnPause) dismiss(); + if (mDismissOnPause) dismiss(); } @Override public void onCancel(DialogInterface dialog) { super.onCancel(dialog); - if(mOnCancelListener != null) mOnCancelListener.onCancel(dialog); + if (mOnCancelListener != null) mOnCancelListener.onCancel(dialog); } @Override public void onDismiss(DialogInterface dialog) { super.onDismiss(dialog); - if(mOnDismissListener != null) mOnDismissListener.onDismiss(dialog); + if (mOnDismissListener != null) mOnDismissListener.onDismiss(dialog); } /** * Get whether auto highlighting is turned on or not + * * @return true if on, false otherwise */ @SuppressWarnings("unused") @@ -545,12 +549,13 @@ public boolean isAutoHighlight() { /** * If set to true, all days between {@link #highlightedDays} and {@link #highlightedDaysEnd} will be highlighted. * This will reset manually inserted days to highlight using {@link #setHighlightedDays(Calendar[], Calendar[])} + * * @param autoHighlight Set true to turn on auto highlighting, false otherwise */ @SuppressWarnings("unused") public void setAutoHighlight(boolean autoHighlight) { this.mAutoHighlight = autoHighlight; - if(autoHighlight) { + if (autoHighlight) { calculateHighlightedDays(); } else { highlightedDays = null; @@ -589,8 +594,8 @@ private void setCurrentView(final int viewIndex) { int flags = DateUtils.FORMAT_SHOW_DATE; String dayString = DateUtils.formatDateTime(getActivity(), millis, flags); String dayStringEnd = DateUtils.formatDateTime(getActivity(), millisEnd, flags); - mAnimator.setContentDescription(mDayPickerDescription+": "+dayString); - mAnimatorEnd.setContentDescription(mDayPickerDescription+": "+dayStringEnd); + mAnimator.setContentDescription(mDayPickerDescription + ": " + dayString); + mAnimatorEnd.setContentDescription(mDayPickerDescription + ": " + dayStringEnd); Utils.tryAccessibilityAnnounce(mAnimator, mSelectDay); Utils.tryAccessibilityAnnounce(mAnimatorEnd, mSelectDay); break; @@ -620,8 +625,8 @@ private void setCurrentView(final int viewIndex) { CharSequence yearString = YEAR_FORMAT.format(millis); CharSequence yearStringEnd = YEAR_FORMAT.format(millisEnd); - mAnimator.setContentDescription(mYearPickerDescription+": "+yearString); - mAnimatorEnd.setContentDescription(mYearPickerDescription+": "+yearStringEnd); + mAnimator.setContentDescription(mYearPickerDescription + ": " + yearString); + mAnimatorEnd.setContentDescription(mYearPickerDescription + ": " + yearStringEnd); Utils.tryAccessibilityAnnounce(mAnimator, mSelectYear); Utils.tryAccessibilityAnnounce(mAnimatorEnd, mSelectYear); break; @@ -665,6 +670,7 @@ private void updateDisplay(boolean announce) { /** * Set whether the device should vibrate when touching fields + * * @param vibrate true if the device should vibrate when touching a field */ public void vibrate(boolean vibrate) { @@ -673,6 +679,7 @@ public void vibrate(boolean vibrate) { /** * Set whether the picker should dismiss itself when being paused or whether it should try to survive an orientation change + * * @param dismissOnPause true if the dialog should dismiss itself when it's pausing */ public void dismissOnPause(boolean dismissOnPause) { @@ -681,6 +688,7 @@ public void dismissOnPause(boolean dismissOnPause) { /** * Set whether the dark theme should be used + * * @param themeDark true if the dark theme should be used, false if the default theme should be used */ public void setThemeDark(boolean themeDark) { @@ -689,6 +697,7 @@ public void setThemeDark(boolean themeDark) { /** * Returns true when the dark theme should be used + * * @return true if the dark theme should be used, false if the default theme should be used */ @Override @@ -698,6 +707,7 @@ public boolean isThemeDark() { /** * Set the accent color of this dialog + * * @param accentColor the accent color you want */ public void setAccentColor(int accentColor) { @@ -706,6 +716,7 @@ public void setAccentColor(int accentColor) { /** * Get the accent color of this dialog + * * @return accent color */ public int getAccentColor() { @@ -713,7 +724,7 @@ public int getAccentColor() { } @SuppressWarnings("unused") - public void setFirstDayOfWeek(int startOfWeek,int startWeekEnd) { + public void setFirstDayOfWeek(int startOfWeek, int startWeekEnd) { if (startOfWeek < Calendar.SUNDAY || startOfWeek > Calendar.SATURDAY) { throw new IllegalArgumentException("Value must be between Calendar.SUNDAY and " + "Calendar.SATURDAY"); @@ -738,7 +749,7 @@ public void setYearRange(int startYear, int endYear) { mMinYear = startYear; mMaxYear = endYear; - if (mDayPickerView != null&&mDayPickerViewEnd!=null) { + if (mDayPickerView != null && mDayPickerViewEnd != null) { mDayPickerView.onChange(); mDayPickerViewEnd.onChange(); } @@ -747,13 +758,14 @@ public void setYearRange(int startYear, int endYear) { /** * Sets the minimal date supported by this DatePicker. Dates before (but not including) the * specified date will be disallowed from being selected. + * * @param calendar a Calendar object set to the year, month, day desired as the mindate. */ @SuppressWarnings("unused") public void setMinDate(Calendar calendar) { mMinDate = calendar; - if (mDayPickerView != null&&mDayPickerViewEnd!=null) { + if (mDayPickerView != null && mDayPickerViewEnd != null) { mDayPickerView.onChange(); mDayPickerViewEnd.onChange(); } @@ -770,13 +782,14 @@ public Calendar getMinDate() { /** * Sets the minimal date supported by this DatePicker. Dates after (but not including) the * specified date will be disallowed from being selected. + * * @param calendar a Calendar object set to the year, month, day desired as the maxdate. */ @SuppressWarnings("unused") public void setMaxDate(Calendar calendar) { mMaxDate = calendar; - if (mDayPickerView != null&&mDayPickerViewEnd!=null) { + if (mDayPickerView != null && mDayPickerViewEnd != null) { mDayPickerView.onChange(); mDayPickerViewEnd.onChange(); } @@ -793,10 +806,11 @@ public Calendar getMaxDate() { /** * Sets an array of dates which should be highlighted when the picker is drawn * This will turn off auto highlighting. + * * @param highlightedDays an Array of Calendar objects containing the dates to be highlighted */ @SuppressWarnings("unused") - public void setHighlightedDays(Calendar[] highlightedDays,Calendar[] highlightedDaysEnd) { + public void setHighlightedDays(Calendar[] highlightedDays, Calendar[] highlightedDaysEnd) { mAutoHighlight = false; // Sort the array to optimize searching over it later on @@ -817,6 +831,7 @@ public Calendar[] getHighlightedDays() { /** * Set's a list of days which are the only valid selections. * Setting this value will take precedence over using setMinDate() and setMaxDate() + * * @param selectableDays an Array of Calendar Objects containing the selectable dates */ @SuppressWarnings("unused") @@ -861,7 +876,7 @@ public void setOnDismissListener(DialogInterface.OnDismissListener onDismissList // change the selected day number to the last day of the selected month or year. // e.g. Switching from Mar to Apr when Mar 31 is selected -> Apr 30 // e.g. Switching from 2012 to 2013 when Feb 29, 2012 is selected -> Feb 28, 2013 - private void adjustDayInMonthIfNeeded( Calendar calendar ) { + private void adjustDayInMonthIfNeeded(Calendar calendar) { int day = calendar.get(Calendar.DAY_OF_MONTH); int daysInMonth = calendar.getActualMaximum(Calendar.DAY_OF_MONTH); if (day > daysInMonth) { @@ -872,9 +887,9 @@ private void adjustDayInMonthIfNeeded( Calendar calendar ) { @Override public void onClick(View v) { tryVibrate(); - if (v.getId() == R.id.date_picker_year||v.getId() == R.id.date_picker_year_end) { + if (v.getId() == R.id.date_picker_year || v.getId() == R.id.date_picker_year_end) { setCurrentView(YEAR_VIEW); - } else if (v.getId() == R.id.date_picker_month_and_day||v.getId() == R.id.date_picker_month_and_day_end) { + } else if (v.getId() == R.id.date_picker_month_and_day || v.getId() == R.id.date_picker_month_and_day_end) { setCurrentView(MONTH_AND_DAY_VIEW); } } @@ -884,9 +899,9 @@ public void onYearSelected(int year) { adjustDayInMonthIfNeeded(mCalendar); adjustDayInMonthIfNeeded(mCalendarEnd); - if(tabHost.getCurrentTab()==0){ + if (tabHost.getCurrentTab() == 0) { mCalendar.set(Calendar.YEAR, year); - }else{ + } else { mCalendarEnd.set(Calendar.YEAR, year); } updatePickers(); @@ -897,18 +912,17 @@ public void onYearSelected(int year) { @Override public void onDayOfMonthSelected(int year, int month, int day) { - if(tabHost.getCurrentTab()==0){ + if (tabHost.getCurrentTab() == 0) { mCalendar.set(Calendar.YEAR, year); mCalendar.set(Calendar.MONTH, month); mCalendar.set(Calendar.DAY_OF_MONTH, day); - } - else{ + } else { mCalendarEnd.set(Calendar.YEAR, year); mCalendarEnd.set(Calendar.MONTH, month); mCalendarEnd.set(Calendar.DAY_OF_MONTH, day); } - if(mAutoHighlight) { + if (mAutoHighlight) { calculateHighlightedDays(); } @@ -917,41 +931,41 @@ public void onDayOfMonthSelected(int year, int month, int day) { } private void calculateHighlightedDays() { - int numDays = (int)Math.round( + int numDays = (int) Math.round( (mCalendarEnd.getTimeInMillis() - mCalendar.getTimeInMillis()) / 86400000d); // In case user chooses an end day before the start day. int dir = 1; - if(numDays < 0) { + if (numDays < 0) { dir = -1; } numDays = Math.abs(numDays); // +1 to account for the end day which should be highlighted as well - highlightedDays = new Calendar[numDays+1]; + highlightedDays = new Calendar[numDays + 1]; for (int i = 0; i < numDays; i++) { highlightedDays[i] = new GregorianCalendar( mCalendar.get(Calendar.YEAR), mCalendar.get(Calendar.MONTH), mCalendar.get(Calendar.DAY_OF_MONTH)); - highlightedDays[i].add(Calendar.DAY_OF_MONTH, i*dir); + highlightedDays[i].add(Calendar.DAY_OF_MONTH, i * dir); } highlightedDays[numDays] = mCalendarEnd; highlightedDaysEnd = highlightedDays; } private void updatePickers() { - for(OnDateChangedListener listener : mListeners) listener.onDateChanged(); + for (OnDateChangedListener listener : mListeners) listener.onDateChanged(); } @Override public com.borax12.materialdaterangepicker.date.MonthAdapter.CalendarDay getSelectedDay() { - if(tabHost.getCurrentTab()==0){ + if (tabHost.getCurrentTab() == 0) { return new com.borax12.materialdaterangepicker.date.MonthAdapter.CalendarDay(mCalendar); - }else{ + } else { return new com.borax12.materialdaterangepicker.date.MonthAdapter.CalendarDay(mCalendarEnd); } @@ -959,14 +973,15 @@ public com.borax12.materialdaterangepicker.date.MonthAdapter.CalendarDay getSele @Override public int getMinYear() { - if(selectableDays != null) return selectableDays[0].get(Calendar.YEAR); + if (selectableDays != null) return selectableDays[0].get(Calendar.YEAR); // Ensure no years can be selected outside of the given minimum date return mMinDate != null && mMinDate.get(Calendar.YEAR) > mMinYear ? mMinDate.get(Calendar.YEAR) : mMinYear; } @Override public int getMaxYear() { - if(selectableDays != null) return selectableDays[selectableDays.length-1].get(Calendar.YEAR); + if (selectableDays != null) + return selectableDays[selectableDays.length - 1].get(Calendar.YEAR); // Ensure no years can be selected outside of the given maximum date return mMaxDate != null && mMaxDate.get(Calendar.YEAR) < mMaxYear ? mMaxDate.get(Calendar.YEAR) : mMaxYear; } @@ -988,36 +1003,32 @@ public void unregisterOnDateChangedListener(OnDateChangedListener listener) { @Override public void tryVibrate() { - if(mVibrate) mHapticFeedbackController.tryVibrate(); + if (mVibrate) mHapticFeedbackController.tryVibrate(); } public void setCancelBackgroundColor(int color) { - Button cancelButton = (Button) getView().findViewById(R.id.cancel); - if (cancelButton != null) { - cancelButton.setBackgroundColor(color); - } + cancelButtonBackground = color; } - public void setPositiveButtonBackgroundColor(int color) { - Button okButton = (Button) getView().findViewById(R.id.ok); - if (okButton != null) { - okButton.setBackgroundColor(color); - } + public void setOkButtonBackgroundColor(int color) { + okButtonBackground = color; } /** * setStartTitle + * * @param String the title to display for start panel - */ + */ public void setStartTitle(String startTitle) { this.startTitle = startTitle; } - + /** * setEndTitle + * * @param String the title to display for end panel - */ + */ public void setEndTitle(String endTitle) { this.endTitle = endTitle; }