From c3047427f9106024c9529b5af947a57f724de36c Mon Sep 17 00:00:00 2001 From: jonatansalas Date: Wed, 11 Jan 2017 10:39:26 -0300 Subject: [PATCH] #2 Added support for min and max date --- .../internal/fragment/DatePickerFragment.java | 28 +++++++++++++------ .../view/DatePickerEditText.java | 24 ++++------------ .../view/DatePickerInputEditText.java | 24 ++++------------ 3 files changed, 32 insertions(+), 44 deletions(-) diff --git a/datetimepickeredittext/src/main/java/io/blackbox_vision/datetimepickeredittext/internal/fragment/DatePickerFragment.java b/datetimepickeredittext/src/main/java/io/blackbox_vision/datetimepickeredittext/internal/fragment/DatePickerFragment.java index c00784a..2f69115 100644 --- a/datetimepickeredittext/src/main/java/io/blackbox_vision/datetimepickeredittext/internal/fragment/DatePickerFragment.java +++ b/datetimepickeredittext/src/main/java/io/blackbox_vision/datetimepickeredittext/internal/fragment/DatePickerFragment.java @@ -8,15 +8,21 @@ import java.util.Calendar; +import io.blackbox_vision.datetimepickeredittext.internal.utils.DateUtils; + import static android.app.DatePickerDialog.*; public final class DatePickerFragment extends DialogFragment { + private static final String DEFAULT_TEMPLATE = "dd/MM/yyyy"; + private static final String DEFAULT_MIN_DATE = "01/01/1980"; + private static final String DEFAULT_MAX_DATE = "01/01/2100"; + private OnDateSetListener onDateSetListener; private Calendar date; - private Long minDate; - private Long maxDate; + private String minDate; + private String maxDate; private Integer themeId; @@ -45,8 +51,14 @@ public Dialog onCreateDialog(Bundle savedInstanceState) { datePickerDialog = new DatePickerDialog(getActivity(), onDateSetListener, year, month, day); } - datePickerDialog.getDatePicker().setMinDate(minDate); - datePickerDialog.getDatePicker().setMaxDate(maxDate); + final String minDateStr = null != minDate? minDate : DEFAULT_MIN_DATE; + final String maxDateStr = null != maxDate? maxDate : DEFAULT_MAX_DATE; + + final long min = DateUtils.parse(minDateStr, DEFAULT_TEMPLATE).getTime(); + final long max = DateUtils.parse(maxDateStr, DEFAULT_TEMPLATE).getTime(); + + datePickerDialog.getDatePicker().setMinDate(min); + datePickerDialog.getDatePicker().setMaxDate(max); return datePickerDialog; } @@ -78,20 +90,20 @@ public DatePickerFragment setThemeId(Integer themeId) { return this; } - public Long getMaxDate() { + public String getMaxDate() { return maxDate; } - public DatePickerFragment setMaxDate(Long maxDate) { + public DatePickerFragment setMaxDate(String maxDate) { this.maxDate = maxDate; return this; } - public Long getMinDate() { + public String getMinDate() { return minDate; } - public DatePickerFragment setMinDate(Long minDate) { + public DatePickerFragment setMinDate(String minDate) { this.minDate = minDate; return this; } diff --git a/datetimepickeredittext/src/main/java/io/blackbox_vision/datetimepickeredittext/view/DatePickerEditText.java b/datetimepickeredittext/src/main/java/io/blackbox_vision/datetimepickeredittext/view/DatePickerEditText.java index 2bde688..992be96 100644 --- a/datetimepickeredittext/src/main/java/io/blackbox_vision/datetimepickeredittext/view/DatePickerEditText.java +++ b/datetimepickeredittext/src/main/java/io/blackbox_vision/datetimepickeredittext/view/DatePickerEditText.java @@ -25,10 +25,6 @@ public final class DatePickerEditText extends EditText implements OnFocusChangeListener, OnDateSetListener { private static final String TAG = DatePickerEditText.class.getSimpleName(); - private static final String DEFAULT_TEMPLATE = "dd/MM/yyyy"; - private static final String DEFAULT_MIN_DATE = "01/01/1980"; - private static final String DEFAULT_MAX_DATE = "01/01/2100"; - private OnFocusChangeListener onFocusChangedListener; private FragmentManager manager; @@ -85,20 +81,12 @@ public void onFocusChange(View view, boolean isFocused) { imm.hideSoftInputFromWindow(getWindowToken(), 0); if (isFocused) { - final String minDateStr = null != minDate? minDate : DEFAULT_MIN_DATE; - final String maxDateStr = null != maxDate? maxDate : DEFAULT_MAX_DATE; - - final long min = DateUtils.parse(minDateStr, null != minDate? dateFormat : DEFAULT_TEMPLATE).getTime(); - final long max = DateUtils.parse(maxDateStr, null != maxDate? dateFormat : DEFAULT_TEMPLATE).getTime(); - - final DatePickerFragment datePickerFragment = new DatePickerFragment(); - - datePickerFragment.setDate(date); - datePickerFragment.setThemeId(themeId); - datePickerFragment.setOnDateSetListener(this); - - datePickerFragment.setMinDate(min); - datePickerFragment.setMaxDate(max); + final DatePickerFragment datePickerFragment = new DatePickerFragment() + .setDate(date) + .setThemeId(themeId) + .setOnDateSetListener(this) + .setMinDate(minDate) + .setMaxDate(maxDate); datePickerFragment.show(manager, TAG); } diff --git a/datetimepickeredittext/src/main/java/io/blackbox_vision/datetimepickeredittext/view/DatePickerInputEditText.java b/datetimepickeredittext/src/main/java/io/blackbox_vision/datetimepickeredittext/view/DatePickerInputEditText.java index 7145d81..6646c0e 100644 --- a/datetimepickeredittext/src/main/java/io/blackbox_vision/datetimepickeredittext/view/DatePickerInputEditText.java +++ b/datetimepickeredittext/src/main/java/io/blackbox_vision/datetimepickeredittext/view/DatePickerInputEditText.java @@ -25,10 +25,6 @@ public final class DatePickerInputEditText extends TextInputEditText implements OnFocusChangeListener, OnDateSetListener { private static final String TAG = DatePickerInputEditText.class.getSimpleName(); - private static final String DEFAULT_TEMPLATE = "dd/MM/yyyy"; - private static final String DEFAULT_MIN_DATE = "01/01/1980"; - private static final String DEFAULT_MAX_DATE = "01/01/2100"; - private OnFocusChangeListener onFocusChangedListener; private FragmentManager manager; @@ -85,20 +81,12 @@ public void onFocusChange(View view, boolean isFocused) { imm.hideSoftInputFromWindow(getWindowToken(), 0); if (isFocused) { - final String minDateStr = null != minDate? minDate : DEFAULT_MIN_DATE; - final String maxDateStr = null != maxDate? maxDate : DEFAULT_MAX_DATE; - - final long min = DateUtils.parse(minDateStr, null != minDate? dateFormat : DEFAULT_TEMPLATE).getTime(); - final long max = DateUtils.parse(maxDateStr, null != maxDate? dateFormat : DEFAULT_TEMPLATE).getTime(); - - final DatePickerFragment datePickerFragment = new DatePickerFragment(); - - datePickerFragment.setDate(date); - datePickerFragment.setThemeId(themeId); - datePickerFragment.setOnDateSetListener(this); - - datePickerFragment.setMinDate(min); - datePickerFragment.setMaxDate(max); + final DatePickerFragment datePickerFragment = new DatePickerFragment() + .setDate(date) + .setThemeId(themeId) + .setOnDateSetListener(this) + .setMinDate(minDate) + .setMaxDate(maxDate); datePickerFragment.show(manager, TAG); }