Skip to content

Instantly share code, notes, and snippets.

@alorma
Created June 21, 2017 06:16
Show Gist options
  • Save alorma/13b62df70f57d307b2c3b32be95a1992 to your computer and use it in GitHub Desktop.
Save alorma/13b62df70f57d307b2c3b32be95a1992 to your computer and use it in GitHub Desktop.
Date preference
package com.schibsted.android.rocket.features.navigation.profile.edit.preference;
import android.content.Context;
import android.content.res.TypedArray;
import android.os.Parcel;
import android.os.Parcelable;
import android.support.v7.preference.DialogPreference;
import android.util.AttributeSet;
import com.schibsted.android.rocket.RocketApplication;
import com.schibsted.android.rocket.RocketComponent;
import com.schibsted.android.rocket.features.navigation.profile.edit.DateMapper;
import com.schibsted.android.rocket.features.navigation.profile.edit.EditProfileModule;
import javax.inject.Inject;
import org.joda.time.DateTime;
import static com.schibsted.android.rocket.features.navigation.profile.edit.DateMapper.HUMAN_DATE_FORMAT;
public class DatePickerPreference extends DialogPreference {
@Inject DateMapper dateMapper;
private DateTime value;
public DatePickerPreference(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
init();
}
public DatePickerPreference(Context context, AttributeSet attrs, int defStyleAttr) {
this(context, attrs, defStyleAttr, 0);
}
public DatePickerPreference(Context context, AttributeSet attrs) {
this(context, attrs, android.R.attr.dialogPreferenceStyle);
}
public DatePickerPreference(Context context) {
this(context, null);
}
private void init() {
setLayoutResource(android.support.v7.preference.R.layout.preference);
RocketComponent rocketComponent = ((RocketApplication) getContext().getApplicationContext()).getComponent();
rocketComponent.editProfileComponent(new EditProfileModule()).inject(this);
}
public DateTime getValue() {
return value;
}
public void setValue(String date) {
setValue(dateMapper.getDateTime(date, HUMAN_DATE_FORMAT));
}
public void setValue(DateTime date) {
this.value = date;
persistString(dateMapper.mapToHumanReadableDate(date));
notifyChanged();
}
public void clearValue() {
value = null;
persistString(null);
notifyChanged();
}
@Override
public CharSequence getSummary() {
return value != null ? dateMapper.mapToHumanReadableDate(value) : null;
}
@Override
protected DateTime onGetDefaultValue(TypedArray a, int index) {
return dateMapper.getDateTime(a.getString(index), DateMapper.HUMAN_DATE_FORMAT);
}
@Override
protected void onSetInitialValue(boolean restorePersistedValue, Object defaultValue) {
super.onSetInitialValue(restorePersistedValue, defaultValue);
String defaultReturnValue = null;
if (value != null) {
defaultReturnValue = dateMapper.mapToHumanReadableDate(value);
}
setValue(restorePersistedValue ? getPersistedString(defaultReturnValue) : defaultReturnValue);
}
@Override
protected Parcelable onSaveInstanceState() {
final Parcelable superState = super.onSaveInstanceState();
if (isPersistent()) {
// No need to save instance state since it's persistent
return superState;
}
final DatePickerPreference.SavedState myState = new DatePickerPreference.SavedState(superState);
myState.date = getValue();
return myState;
}
@Override
protected void onRestoreInstanceState(Parcelable state) {
if (state == null || !state.getClass().equals(DatePickerPreference.SavedState.class)) {
// Didn't save state for us in onSaveInstanceState
super.onRestoreInstanceState(state);
return;
}
DatePickerPreference.SavedState myState = (DatePickerPreference.SavedState) state;
super.onRestoreInstanceState(myState.getSuperState());
setValue(myState.date);
}
private static class SavedState extends BaseSavedState {
DateTime date;
SavedState(Parcel source) {
super(source);
date = (DateTime) source.readSerializable();
}
@Override
public void writeToParcel(Parcel dest, int flags) {
super.writeToParcel(dest, flags);
dest.writeSerializable(date);
}
SavedState(Parcelable superState) {
super(superState);
}
public static final Parcelable.Creator<DatePickerPreference.SavedState> CREATOR =
new Parcelable.Creator<DatePickerPreference.SavedState>() {
@Override
public DatePickerPreference.SavedState createFromParcel(Parcel in) {
return new DatePickerPreference.SavedState(in);
}
@Override
public DatePickerPreference.SavedState[] newArray(int size) {
return new DatePickerPreference.SavedState[size];
}
};
}
}
package com.schibsted.android.rocket.features.navigation.profile.edit.preference;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.preference.PreferenceDialogFragmentCompat;
import android.view.View;
import android.widget.DatePicker;
import com.schibsted.android.rocket.R;
import org.joda.time.DateTime;
public class DatePickerPreferenceDialogFragmentCompat extends PreferenceDialogFragmentCompat {
private static final String SAVE_STATE_DATE = "DatePickerPreferenceDialogFragmentCompat.Date";
private DateTime date;
private DatePicker datePicker;
public static DatePickerPreferenceDialogFragmentCompat newInstance(String key) {
final DatePickerPreferenceDialogFragmentCompat fragment = new DatePickerPreferenceDialogFragmentCompat();
Bundle args = new Bundle(1);
args.putString(ARG_KEY, key);
fragment.setArguments(args);
return fragment;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (savedInstanceState == null) {
date = getDatePreference().getValue();
} else {
date = (DateTime) savedInstanceState.getSerializable(SAVE_STATE_DATE);
}
}
@Override
protected View onCreateDialogView(Context context) {
return new DatePicker(context, null, R.style.DatePicker);
}
@Override
protected void onBindDialogView(View view) {
super.onBindDialogView(view);
if (view instanceof DatePicker) {
datePicker = (DatePicker) view;
datePicker.setCalendarViewShown(false);
if (date != null) {
datePicker.updateDate(date.getYear(), date.getMonthOfYear(), date.getDayOfMonth());
}
}
}
private DatePickerPreference getDatePreference() {
return (DatePickerPreference) getPreference();
}
@Override
public void onDialogClosed(boolean positiveResult) {
if (positiveResult) {
int dayOfMonth = datePicker.getDayOfMonth();
int month = datePicker.getMonth();
int year = datePicker.getYear();
DateTime dateTime = new DateTime(year, month, dayOfMonth, 0, 0);
if (getDatePreference().callChangeListener(dateTime)) {
getDatePreference().setValue(dateTime);
}
}
}
@Override
public void onSaveInstanceState(@NonNull Bundle outState) {
super.onSaveInstanceState(outState);
if (date != null) {
outState.putSerializable(SAVE_STATE_DATE, date);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment