From 14812161755292b9bdabb553ebd7d5727227a8c7 Mon Sep 17 00:00:00 2001 From: JJamet Date: Sun, 13 Aug 2017 19:36:47 +0200 Subject: [PATCH] Solve default time bug --- .../switchdatetimesample/Sample.java | 14 +- .../SwitchDateTimeDialogFragment.java | 126 +++++++----------- .../time/RadialPickerLayout.java | 31 +++-- .../switchdatetime/time/SwitchTimePicker.java | 13 +- .../layout-ldrtl-v17/time_header_label.xml | 41 +----- .../src/main/res/layout/time_header_label.xml | 49 +------ 6 files changed, 82 insertions(+), 192 deletions(-) diff --git a/sample/src/main/java/com/kunzisoft/switchdatetimesample/Sample.java b/sample/src/main/java/com/kunzisoft/switchdatetimesample/Sample.java index 5d81a1c..9b0034c 100644 --- a/sample/src/main/java/com/kunzisoft/switchdatetimesample/Sample.java +++ b/sample/src/main/java/com/kunzisoft/switchdatetimesample/Sample.java @@ -52,19 +52,12 @@ protected void onCreate(Bundle savedInstanceState) { ); } - // Assign values we want + // Init format final SimpleDateFormat myDateFormat = new SimpleDateFormat("d MMM yyyy HH:mm", java.util.Locale.getDefault()); - dateTimeFragment.startAtCalendarView(); + // Assign unmodifiable values dateTimeFragment.set24HoursMode(false); dateTimeFragment.setMinimumDateTime(new GregorianCalendar(2015, Calendar.JANUARY, 1).getTime()); dateTimeFragment.setMaximumDateTime(new GregorianCalendar(2025, Calendar.DECEMBER, 31).getTime()); - dateTimeFragment.setDefaultDateTime(new GregorianCalendar(2017, Calendar.MARCH, 4, 15, 20).getTime()); - // Or assign each element, default element is the current moment - // dateTimeFragment.setDefaultHourOfDay(15); - // dateTimeFragment.setDefaultMinute(20); - // dateTimeFragment.setDefaultDay(4); - // dateTimeFragment.setDefaultMonth(Calendar.MARCH); - // dateTimeFragment.setDefaultYear(2017); // Define new day and month format try { @@ -97,6 +90,9 @@ public void onNeutralButtonClick(Date date) { buttonView.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { + // Re-init each time + dateTimeFragment.startAtCalendarView(); + dateTimeFragment.setDefaultDateTime(new GregorianCalendar(2017, Calendar.MARCH, 4, 15, 20).getTime()); dateTimeFragment.show(getSupportFragmentManager(), TAG_DATETIME_FRAGMENT); } }); diff --git a/switchdatetime/src/main/java/com/kunzisoft/switchdatetime/SwitchDateTimeDialogFragment.java b/switchdatetime/src/main/java/com/kunzisoft/switchdatetime/SwitchDateTimeDialogFragment.java index 8618e84..9f6e7d5 100644 --- a/switchdatetime/src/main/java/com/kunzisoft/switchdatetime/SwitchDateTimeDialogFragment.java +++ b/switchdatetime/src/main/java/com/kunzisoft/switchdatetime/SwitchDateTimeDialogFragment.java @@ -40,6 +40,9 @@ public class SwitchDateTimeDialogFragment extends DialogFragment { private static final String TAG = "SwitchDateTimeDialogFrg"; private static final String STATE_DATETIME = "STATE_DATETIME"; + private static final String STATE_CURRENT_POSITION = "STATE_CURRENT_POSITION"; + + private static final int UNDEFINED_POSITION = -1; private Calendar dateTimeCalendar = Calendar.getInstance(); private Calendar minimumDateTime = new GregorianCalendar(1970, 1, 1); private Calendar maximumDateTime = new GregorianCalendar(2200, 1, 1); @@ -55,16 +58,9 @@ public class SwitchDateTimeDialogFragment extends DialogFragment { private String mNeutralButton; private OnButtonClickListener mListener; - private final static int UNDEFINED_TIME_VALUE = -1; - private int year = UNDEFINED_TIME_VALUE; - private int month = UNDEFINED_TIME_VALUE; - private int day = UNDEFINED_TIME_VALUE; - private int hourOfDay = UNDEFINED_TIME_VALUE; - private int minute = UNDEFINED_TIME_VALUE; - private boolean assignDefaultDateTimeCalendar; - private boolean is24HoursMode = false; - private int startAtPosition = 0; + private int startAtPosition = UNDEFINED_POSITION; + private int currentPosition = 0; private int alertStyleId; private SimpleDateFormat dayAndMonthSimpleDate; @@ -124,8 +120,9 @@ public void setOnButtonClickListener(OnButtonClickListener onButtonClickListener @Override public void onSaveInstanceState(Bundle savedInstanceState) { - // Save the current datetime + // Save the current datetime and position savedInstanceState.putLong(STATE_DATETIME, dateTimeCalendar.getTimeInMillis()); + savedInstanceState.putInt(STATE_CURRENT_POSITION, currentPosition); timePicker.onSaveInstanceState(savedInstanceState); super.onSaveInstanceState(savedInstanceState); @@ -142,26 +139,11 @@ public void onSaveInstanceState(Bundle savedInstanceState) { mNeutralButton = getArguments().getString(TAG_NEUTRAL_BUTTON); } - if (!assignDefaultDateTimeCalendar && savedInstanceState != null) { - // Restore value from saved state + if(savedInstanceState != null) { + currentPosition = savedInstanceState.getInt(STATE_CURRENT_POSITION); dateTimeCalendar.setTime(new Date(savedInstanceState.getLong(STATE_DATETIME))); } - // Init values with current time if setDefault is not used - if(assignDefaultDateTimeCalendar || year == UNDEFINED_TIME_VALUE) - year = dateTimeCalendar.get(Calendar.YEAR); - if(assignDefaultDateTimeCalendar || month == UNDEFINED_TIME_VALUE) - month = dateTimeCalendar.get(Calendar.MONTH); - if(assignDefaultDateTimeCalendar || day == UNDEFINED_TIME_VALUE) - day = dateTimeCalendar.get(Calendar.DAY_OF_MONTH); - if(assignDefaultDateTimeCalendar || hourOfDay == UNDEFINED_TIME_VALUE) - hourOfDay = dateTimeCalendar.get(Calendar.HOUR_OF_DAY); - if(assignDefaultDateTimeCalendar || minute == UNDEFINED_TIME_VALUE) - minute = dateTimeCalendar.get(Calendar.MINUTE); - assignAllValuesToCalendar(); - - assignDefaultDateTimeCalendar = false; - // Throw exception if default select date isn't between minimumDateTime and maximumDateTime if(dateTimeCalendar.before(minimumDateTime) || dateTimeCalendar.after(maximumDateTime)) throw new RuntimeException("Default date " + dateTimeCalendar.getTime() + " must be between " @@ -169,7 +151,6 @@ public void onSaveInstanceState(Bundle savedInstanceState) { LayoutInflater inflater = LayoutInflater.from(getActivity()); getActivity().getTheme().applyStyle(R.style.Theme_SwitchDateTime, false); - //getActivity().setTheme(R.style.Theme_SwitchDateTime); View dateTimeLayout = inflater.inflate(R.layout.dialog_switch_datetime_picker, (ViewGroup) getActivity().findViewById(R.id.datetime_picker)); @@ -193,6 +174,7 @@ public void onAnimationStart(Animation animation) { @Override public void onAnimationEnd(Animation animation) { blockAnimationIn = false; + currentPosition = viewSwitcher.getDisplayedChild(); } @Override @@ -212,7 +194,11 @@ public void onAnimationEnd(Animation animation) { @Override public void onAnimationRepeat(Animation animation) {} }); - viewSwitcher.setDisplayedChild(startAtPosition); + + // Defined the start position + if(startAtPosition != UNDEFINED_POSITION) + currentPosition = startAtPosition; + viewSwitcher.setDisplayedChild(currentPosition); // Button for switch between Hours/Minutes, Calendar and YearList ImageButton buttonSwitch = (ImageButton) dateTimeLayout.findViewById(R.id.button_switch); @@ -249,27 +235,22 @@ public void onClick(View view) { yearSimpleDate = new SimpleDateFormat("yyyy", Locale.getDefault()); // Init headers - yearHeaderValues.setText(String.valueOf(year)); + yearHeaderValues.setText(String.valueOf(dateTimeCalendar.get(Calendar.YEAR))); monthAndDayHeaderValues.setText(dayAndMonthSimpleDate.format(dateTimeCalendar.getTime())); // Construct TimePicker SwitchTimePicker.OnTimeSelectedListener onTimeSelectedListener = new SwitchTimePicker.OnTimeSelectedListener() { @Override public void onTimeSelected(int hourOfDayTime, int minuteTime) { - hourOfDay = hourOfDayTime; - minute = minuteTime; dateTimeCalendar.set(Calendar.HOUR_OF_DAY, hourOfDayTime); dateTimeCalendar.set(Calendar.MINUTE, minuteTime); } }; // Init time with saved elements - if(savedInstanceState == null) - timePicker = new SwitchTimePicker(getContext(), onTimeSelectedListener); - else - timePicker = new SwitchTimePicker(getContext(), onTimeSelectedListener, savedInstanceState); + timePicker = new SwitchTimePicker(getContext(), onTimeSelectedListener, savedInstanceState); timePicker.setIs24HourMode(is24HoursMode); - timePicker.setHourOfDay(hourOfDay); - timePicker.setMinute(minute); + timePicker.setHourOfDay(dateTimeCalendar.get(Calendar.HOUR_OF_DAY)); + timePicker.setMinute(dateTimeCalendar.get(Calendar.MINUTE)); timePicker.onCreateView(dateTimeLayout, savedInstanceState); timePicker.setOnClickTimeListener(onTimeClickListener); @@ -285,16 +266,11 @@ public void onTimeSelected(int hourOfDayTime, int minuteTime) { @Override public void onDateSelected(@NonNull MaterialCalendarView widget, @NonNull CalendarDay calendarDay, boolean selected) { Date currentDate = calendarDay.getDate(); - year = calendarDay.getYear(); - month = calendarDay.getMonth(); - day = calendarDay.getDay(); - - dateTimeCalendar.set(Calendar.YEAR, year); - dateTimeCalendar.set(Calendar.MONTH, month); - dateTimeCalendar.set(Calendar.DAY_OF_MONTH, day); - listPickerYearView.assignCurrentYear(year); - - yearHeaderValues.setText(String.valueOf(year)); + dateTimeCalendar.set(Calendar.YEAR, calendarDay.getYear()); + dateTimeCalendar.set(Calendar.MONTH, calendarDay.getMonth()); + dateTimeCalendar.set(Calendar.DAY_OF_MONTH, calendarDay.getDay()); + listPickerYearView.assignCurrentYear(calendarDay.getYear()); + yearHeaderValues.setText(String.valueOf(calendarDay.getYear())); monthAndDayHeaderValues.setText(dayAndMonthSimpleDate.format(currentDate)); } }); @@ -304,14 +280,12 @@ public void onDateSelected(@NonNull MaterialCalendarView widget, @NonNull Calend listPickerYearView = (ListPickerYearView) dateTimeLayout.findViewById(R.id.yearPicker); listPickerYearView.setMinYear(minimumDateTime.get(Calendar.YEAR)); listPickerYearView.setMaxYear(maximumDateTime.get(Calendar.YEAR)); - listPickerYearView.assignCurrentYear(year); + listPickerYearView.assignCurrentYear(dateTimeCalendar.get(Calendar.YEAR)); listPickerYearView.setDatePickerListener(new OnYearSelectedListener() { @Override public void onYearSelected(View view, int yearPicker) { - year = yearPicker; - - dateTimeCalendar.set(Calendar.YEAR, year); - yearHeaderValues.setText(String.valueOf(year)); + dateTimeCalendar.set(Calendar.YEAR, yearPicker); + yearHeaderValues.setText(String.valueOf(yearPicker)); // Unfortunately, we have lags here and thread isn't a solution :/ materialCalendarView.setCurrentDate(dateTimeCalendar.getTime()); @@ -336,7 +310,6 @@ public void onYearSelected(View view, int yearPicker) { DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { if(mListener !=null) { - assignAllValuesToCalendar(); mListener.onPositiveButtonClick(dateTimeCalendar.getTime()); } } @@ -348,7 +321,6 @@ public void onClick(DialogInterface dialog, int which) { public void onClick(DialogInterface dialog, int which) { // Close dialog if(mListener !=null) { - assignAllValuesToCalendar(); mListener.onNegativeButtonClick(dateTimeCalendar.getTime()); } } @@ -358,7 +330,6 @@ public void onClick(DialogInterface dialog, int which) { @Override public void onClick(DialogInterface dialog, int which) { if (mListener != null) { - assignAllValuesToCalendar(); if(mListener instanceof OnButtonWithNeutralClickListener) ((OnButtonWithNeutralClickListener) mListener).onNeutralButtonClick(dateTimeCalendar.getTime()); } @@ -368,37 +339,31 @@ public void onClick(DialogInterface dialog, int which) { return db.create(); } - /** - * Assign each value of time in calendar - */ - private void assignAllValuesToCalendar() { - dateTimeCalendar.set(Calendar.YEAR, year); - dateTimeCalendar.set(Calendar.MONTH, month); - dateTimeCalendar.set(Calendar.DAY_OF_MONTH, day); - dateTimeCalendar.set(Calendar.HOUR_OF_DAY, hourOfDay); - dateTimeCalendar.set(Calendar.MINUTE, minute); - dateTimeCalendar.set(Calendar.SECOND, 0); + @Override + public void onDismiss(DialogInterface dialog) { + super.onDismiss(dialog); + startAtPosition = UNDEFINED_POSITION; } /** * Define "Time" as the first view to show */ public void startAtTimeView() { - startAtPosition = 0; + startAtPosition = HeaderViewsPosition.VIEW_HOURS_AND_MINUTES.getPosition(); } /** * Define "Calendar" as the first view to show */ public void startAtCalendarView() { - startAtPosition = 1; + startAtPosition = HeaderViewsPosition.VIEW_MONTH_AND_DAY.getPosition(); } /** * Define "Year" as the first view to show */ public void startAtYearView() { - startAtPosition = 2; + startAtPosition = HeaderViewsPosition.VIEW_YEAR.getPosition(); } /** @@ -406,7 +371,7 @@ public void startAtYearView() { * @param year */ public void setDefaultYear(int year) { - this.year = year; + this.dateTimeCalendar.set(Calendar.YEAR, year); } @Deprecated @@ -424,7 +389,7 @@ public void setYear(int year) { * @param month */ public void setDefaultMonth(int month) { - this.month = month; + this.dateTimeCalendar.set(Calendar.MONTH, month); } @Deprecated @@ -441,7 +406,7 @@ public void setMonth(int month) { * @param day */ public void setDefaultDay(int day) { - this.day = day; + this.dateTimeCalendar.set(Calendar.DAY_OF_MONTH, day); } @Deprecated @@ -458,7 +423,7 @@ public void setDay(int day) { * @param hourOfDay */ public void setDefaultHourOfDay(int hourOfDay) { - this.hourOfDay = hourOfDay; + this.dateTimeCalendar.set(Calendar.HOUR_OF_DAY, hourOfDay); } @Deprecated @@ -475,7 +440,7 @@ public void setHour(int hour) { * @param minute */ public void setDefaultMinute(int minute) { - this.minute = minute; + this.dateTimeCalendar.set(Calendar.MINUTE, minute); } @Deprecated @@ -492,7 +457,7 @@ public void setMinute(int minute) { * @return */ public int getYear() { - return year; + return this.dateTimeCalendar.get(Calendar.YEAR); } /** @@ -501,7 +466,7 @@ public int getYear() { * @return */ public int getMonth() { - return month; + return this.dateTimeCalendar.get(Calendar.MONTH); } /** @@ -509,7 +474,7 @@ public int getMonth() { * @return */ public int getDay() { - return day; + return this.dateTimeCalendar.get(Calendar.DAY_OF_MONTH); } /** @@ -517,7 +482,7 @@ public int getDay() { * @return */ public int getHourOfDay() { - return hourOfDay; + return this.dateTimeCalendar.get(Calendar.HOUR_OF_DAY); } /** @@ -525,7 +490,7 @@ public int getHourOfDay() { * @return */ public int getMinute() { - return minute; + return this.dateTimeCalendar.get(Calendar.MINUTE); } /** @@ -534,7 +499,6 @@ public int getMinute() { */ public void setDefaultDateTime(Date date) { this.dateTimeCalendar.setTime(date); - this.assignDefaultDateTimeCalendar = true; } /** @@ -662,6 +626,8 @@ public void onClick(View view) { Utils.animLabelElement(view); if(viewSwitcher.getDisplayedChild() != positionView) viewSwitcher.setDisplayedChild(positionView); + + startAtPosition = positionView; } } } diff --git a/switchdatetime/src/main/java/com/kunzisoft/switchdatetime/time/RadialPickerLayout.java b/switchdatetime/src/main/java/com/kunzisoft/switchdatetime/time/RadialPickerLayout.java index 4cd6c2b..2220756 100644 --- a/switchdatetime/src/main/java/com/kunzisoft/switchdatetime/time/RadialPickerLayout.java +++ b/switchdatetime/src/main/java/com/kunzisoft/switchdatetime/time/RadialPickerLayout.java @@ -16,6 +16,7 @@ * limitations under the License. */ +import android.animation.Animator; import android.animation.AnimatorSet; import android.animation.ObjectAnimator; import android.annotation.SuppressLint; @@ -48,6 +49,8 @@ import com.kunzisoft.switchdatetime.time.widget.RadialSelectorView; import com.kunzisoft.switchdatetime.time.widget.RadialTextsView; +import java.util.ArrayList; +import java.util.List; import java.util.Locale; public class RadialPickerLayout extends FrameLayout implements OnTouchListener { @@ -578,24 +581,26 @@ public void setCurrentItemShowing(int index, boolean animate) { mCurrentItemShowing = index; if (animate && (index != lastIndex)) { - ObjectAnimator[] anims = new ObjectAnimator[4]; - if (index == MINUTE_INDEX) { - anims[0] = mHourRadialTextsView.getDisappearAnimator(); - anims[1] = mHourRadialSelectorView.getDisappearAnimator(); - anims[2] = mMinuteRadialTextsView.getReappearAnimator(); - anims[3] = mMinuteRadialSelectorView.getReappearAnimator(); - } else { - anims[0] = mHourRadialTextsView.getReappearAnimator(); - anims[1] = mHourRadialSelectorView.getReappearAnimator(); - anims[2] = mMinuteRadialTextsView.getDisappearAnimator(); - anims[3] = mMinuteRadialSelectorView.getDisappearAnimator(); + List animators = new ArrayList<>(); + switch(index) { + case MINUTE_INDEX : + animators.add(mHourRadialTextsView.getDisappearAnimator()); + animators.add(mHourRadialSelectorView.getDisappearAnimator()); + animators.add(mMinuteRadialTextsView.getReappearAnimator()); + animators.add(mMinuteRadialSelectorView.getReappearAnimator()); + break; + case HOUR_INDEX : + animators.add(mHourRadialTextsView.getReappearAnimator()); + animators.add(mHourRadialSelectorView.getReappearAnimator()); + animators.add(mMinuteRadialTextsView.getDisappearAnimator()); + animators.add(mMinuteRadialSelectorView.getDisappearAnimator()); + break; } - if (mTransition != null && mTransition.isRunning()) { mTransition.end(); } mTransition = new AnimatorSet(); - mTransition.playTogether(anims); + mTransition.playTogether(animators); mTransition.start(); } else { if (Build.VERSION.SDK_INT >= 11) { diff --git a/switchdatetime/src/main/java/com/kunzisoft/switchdatetime/time/SwitchTimePicker.java b/switchdatetime/src/main/java/com/kunzisoft/switchdatetime/time/SwitchTimePicker.java index dae58d3..211a71f 100644 --- a/switchdatetime/src/main/java/com/kunzisoft/switchdatetime/time/SwitchTimePicker.java +++ b/switchdatetime/src/main/java/com/kunzisoft/switchdatetime/time/SwitchTimePicker.java @@ -73,9 +73,7 @@ public class SwitchTimePicker implements RadialPickerLayout.OnValueSelectedListe private int mCurrentViewShow; private TextView mHourView; - private TextView mHourSpaceView; private TextView mMinuteView; - private TextView mMinuteSpaceView; private TextView mAmPmTextView; private View mAmPmHitspace; private RadialPickerLayout mTimePicker; @@ -180,15 +178,16 @@ public View onCreateView(final View view, final Resources resources = mContext.getResources(); mHourPickerDescription = resources.getString(R.string.hour_picker_description); mSelectHours = resources.getString(R.string.select_hours); + mMinutePickerDescription = resources.getString(R.string.minute_picker_description); mSelectMinutes = resources.getString(R.string.select_minutes); mHourView = (TextView) view.findViewById(R.id.hours); mHourView.setOnKeyListener(keyboardListener); - mHourSpaceView = (TextView) view.findViewById(R.id.hour_space); - mMinuteSpaceView = (TextView) view.findViewById(R.id.minutes_space); + mMinuteView = (TextView) view.findViewById(R.id.minutes); mMinuteView.setOnKeyListener(keyboardListener); + mAmPmTextView = (TextView) view.findViewById(R.id.ampm_label); mAmPmTextView.setOnKeyListener(keyboardListener); @@ -238,7 +237,7 @@ public void onClick(View v) { @Override public void onClick(View v) { // TODO bug - //setCurrentItemShowing(MINUTE_INDEX, true, false, true); + setCurrentItemShowing(MINUTE_INDEX, true, false, true); mTimePicker.tryVibrate(); if(onClickTimeListener != null) onClickTimeListener.onClick(mMinuteView); @@ -366,7 +365,6 @@ private void attributeHour(int value, boolean announce) { CharSequence text = String.format(format, value); mHourView.setText(text); - mHourSpaceView.setText(text); if (announce) { Utils.tryAccessibilityAnnounce(mTimePicker, text); } @@ -385,7 +383,6 @@ private void attributeMinute(int value) { CharSequence text = String.format(Locale.getDefault(), "%02d", value); Utils.tryAccessibilityAnnounce(mTimePicker, text); mMinuteView.setText(text); - mMinuteSpaceView.setText(text); } // Show either Hours or Minutes. @@ -627,10 +624,8 @@ private void updateDisplay(boolean allowEmptyDisplay) { String minuteStr = (values[1] == -1) ? mDoublePlaceholderText : String.format(minuteFormat, values[1]).replace(' ', mPlaceholderText); mHourView.setText(hourStr); - mHourSpaceView.setText(hourStr); //mHourView.setTextColor(mColor); mMinuteView.setText(minuteStr); - mMinuteSpaceView.setText(minuteStr); //mMinuteView.setTextColor(mColor); if (!mIs24HourMode) { updateAmPmDisplay(values[2]); diff --git a/switchdatetime/src/main/res/layout-ldrtl-v17/time_header_label.xml b/switchdatetime/src/main/res/layout-ldrtl-v17/time_header_label.xml index d9b44a7..4956222 100644 --- a/switchdatetime/src/main/res/layout-ldrtl-v17/time_header_label.xml +++ b/switchdatetime/src/main/res/layout-ldrtl-v17/time_header_label.xml @@ -29,7 +29,7 @@ android:importantForAccessibility="no" /> - - - - - - + diff --git a/switchdatetime/src/main/res/layout/time_header_label.xml b/switchdatetime/src/main/res/layout/time_header_label.xml index 1b8b7be..e51db04 100644 --- a/switchdatetime/src/main/res/layout/time_header_label.xml +++ b/switchdatetime/src/main/res/layout/time_header_label.xml @@ -27,37 +27,16 @@ android:layout_centerInParent="true" android:visibility="invisible" android:importantForAccessibility="no" /> - - - - - - - +