Skip to content

Commit

Permalink
Solve default time bug
Browse files Browse the repository at this point in the history
  • Loading branch information
J-Jamet committed Aug 13, 2017
1 parent 610bf0a commit 1481216
Show file tree
Hide file tree
Showing 6 changed files with 82 additions and 192 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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);
}
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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;
Expand Down Expand Up @@ -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);
Expand All @@ -142,34 +139,18 @@ 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 "
+ minimumDateTime.getTime() + " and " + maximumDateTime.getTime());

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));

Expand All @@ -193,6 +174,7 @@ public void onAnimationStart(Animation animation) {
@Override
public void onAnimationEnd(Animation animation) {
blockAnimationIn = false;
currentPosition = viewSwitcher.getDisplayedChild();
}

@Override
Expand All @@ -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);
Expand Down Expand Up @@ -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);

Expand All @@ -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));
}
});
Expand All @@ -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());
Expand All @@ -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());
}
}
Expand All @@ -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());
}
}
Expand All @@ -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());
}
Expand All @@ -368,45 +339,39 @@ 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();
}

/**
* Assign default year at start
* @param year
*/
public void setDefaultYear(int year) {
this.year = year;
this.dateTimeCalendar.set(Calendar.YEAR, year);
}

@Deprecated
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -492,7 +457,7 @@ public void setMinute(int minute) {
* @return
*/
public int getYear() {
return year;
return this.dateTimeCalendar.get(Calendar.YEAR);
}

/**
Expand All @@ -501,31 +466,31 @@ public int getYear() {
* @return
*/
public int getMonth() {
return month;
return this.dateTimeCalendar.get(Calendar.MONTH);
}

/**
* Get current day
* @return
*/
public int getDay() {
return day;
return this.dateTimeCalendar.get(Calendar.DAY_OF_MONTH);
}

/**
* Get current hour of day (hour in 24 hours)
* @return
*/
public int getHourOfDay() {
return hourOfDay;
return this.dateTimeCalendar.get(Calendar.HOUR_OF_DAY);
}

/**
* Get current minute
* @return
*/
public int getMinute() {
return minute;
return this.dateTimeCalendar.get(Calendar.MINUTE);
}

/**
Expand All @@ -534,7 +499,6 @@ public int getMinute() {
*/
public void setDefaultDateTime(Date date) {
this.dateTimeCalendar.setTime(date);
this.assignDefaultDateTimeCalendar = true;
}

/**
Expand Down Expand Up @@ -662,6 +626,8 @@ public void onClick(View view) {
Utils.animLabelElement(view);
if(viewSwitcher.getDisplayedChild() != positionView)
viewSwitcher.setDisplayedChild(positionView);

startAtPosition = positionView;
}
}
}
Expand Down
Loading

0 comments on commit 1481216

Please sign in to comment.