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

DatePreference ClassCastException fixed and minor improvement #3

Open
wants to merge 2 commits 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 @@ -136,7 +136,7 @@ protected Parcelable onSaveInstanceState() {
protected void onRestoreInstanceState(Parcelable state) {
if (state == null || !state.getClass().equals(SavedState.class)) {
super.onRestoreInstanceState(state);
setTheDate(((SavedState) state).dateValue);
setTheDate(defaultValue());
} else {
SavedState s = (SavedState) state;
super.onRestoreInstanceState(s.getSuperState());
Expand Down Expand Up @@ -212,6 +212,35 @@ public void onClick(DialogInterface dialog, int which) {
onDialogClosed(which == DialogInterface.BUTTON1); // OK?
}

/**
* Produces the date the user has selected for the given preference, as a
* calendar.
*
* @param preferences
* the SharedPreferences to get the date from
* @param field
* the name of the preference to get the date from
* @param defaultDate
* the default date to use in case no preference is already saved
* @return a Calendar that the user has selected
*/
public static Calendar getDateFor(SharedPreferences preferences, String field, Date defaultDate) {
String defaultString = defaultDate != null ? formatter().format(defaultDate) : "";
String persisted = preferences.getString(field, defaultString);
Date date;
if ("".equals(persisted)) {
if (defaultDate == null) {
return null;
} // if
date = defaultDate;
} else {
date = stringToDate(persisted);
} // if/else
Calendar cal = Calendar.getInstance();
cal.setTime(date);
return cal;
}

/**
* Produces the date the user has selected for the given preference, as a
* calendar.
Expand Down