From 1533b11328a3c4874016974c559aa48f5aa94886 Mon Sep 17 00:00:00 2001 From: Abdul Khaliq Date: Fri, 6 Nov 2020 23:20:17 +0500 Subject: [PATCH] Fixes #301, #292 Other #306, #303, #269, #290 The issues were mainly caused because of incorrect retrieving of today item index. Optimized today item index search on scroll. --- ...atePickerMainActivityWithDoublePicker.java | 18 +++++++------ .../dialog/DoubleDateAndTimePickerDialog.java | 24 +++++++++-------- .../widget/WheelDayPicker.java | 26 +++--------------- .../widget/WheelPicker.java | 27 +++++++++++++------ .../widget/WheelYearPicker.java | 5 ---- 5 files changed, 46 insertions(+), 54 deletions(-) diff --git a/app/src/main/java/com/github/florent37/sample/singledateandtimepicker/SingleDatePickerMainActivityWithDoublePicker.java b/app/src/main/java/com/github/florent37/sample/singledateandtimepicker/SingleDatePickerMainActivityWithDoublePicker.java index c6846449..057211b7 100644 --- a/app/src/main/java/com/github/florent37/sample/singledateandtimepicker/SingleDatePickerMainActivityWithDoublePicker.java +++ b/app/src/main/java/com/github/florent37/sample/singledateandtimepicker/SingleDatePickerMainActivityWithDoublePicker.java @@ -55,13 +55,13 @@ protected void onCreate(Bundle savedInstanceState) { setContentView(R.layout.single_date_picker_activity_main_double_picker); ButterKnife.bind(this); - this.simpleDateFormat = new SimpleDateFormat("EEE d MMM HH:mm", Locale.getDefault()); + this.simpleDateFormat = new SimpleDateFormat("EEE d MMM yyyy", Locale.getDefault()); this.simpleTimeFormat = new SimpleDateFormat("hh:mm aa", Locale.getDefault()); - this.simpleDateOnlyFormat = new SimpleDateFormat("EEE d MMM", Locale.getDefault()); + this.simpleDateOnlyFormat = new SimpleDateFormat("EEE d MMM yyyy", Locale.getDefault()); - this.simpleDateLocaleFormat = new SimpleDateFormat("EEE d MMM", Locale.GERMAN); + this.simpleDateLocaleFormat = new SimpleDateFormat("EEE d MMM yyyy", Locale.GERMAN); } @Override @@ -196,12 +196,11 @@ public void simpleClicked() { .displayMonthNumbers(true) //.mustBeOnFuture() - //.minutesStep(15) //.mustBeOnFuture() //.defaultDate(defaultDate) - // .minDateRange(minDate) - // .maxDateRange(maxDate) + //.minDateRange(minDate) + //.maxDateRange(maxDate) .displayListener(new SingleDateAndTimePickerDialog.DisplayListener() { @Override @@ -233,6 +232,7 @@ public void doubleClicked() { final Calendar calendarMax = Calendar.getInstance(); calendarMin.setTime(now); // Set min now + calendarMin.set(Calendar.DAY_OF_MONTH, calendarMin.get(Calendar.DAY_OF_MONTH) + 2); calendarMax.setTime(new Date(now.getTime() + TimeUnit.DAYS.toMillis(150))); // Set max now + 150 days final Date minDate = calendarMin.getTime(); @@ -243,8 +243,9 @@ public void doubleClicked() { //.bottomSheet() //.curved() -// .backgroundColor(Color.BLACK) -// .mainColor(Color.GREEN) + //.backgroundColor(Color.BLACK) + //.mainColor(Color.GREEN) + .minutesStep(15) .mustBeOnFuture() @@ -257,6 +258,7 @@ public void doubleClicked() { .tab0Date(now) .tab1Date(new Date(now.getTime() + TimeUnit.HOURS.toMillis(1))) + //.todayText("Today") .title("Double") .tab0Text("Depart") diff --git a/singledateandtimepicker/src/main/java/com/github/florent37/singledateandtimepicker/dialog/DoubleDateAndTimePickerDialog.java b/singledateandtimepicker/src/main/java/com/github/florent37/singledateandtimepicker/dialog/DoubleDateAndTimePickerDialog.java index 54d3c8c3..1f450c67 100644 --- a/singledateandtimepicker/src/main/java/com/github/florent37/singledateandtimepicker/dialog/DoubleDateAndTimePickerDialog.java +++ b/singledateandtimepicker/src/main/java/com/github/florent37/singledateandtimepicker/dialog/DoubleDateAndTimePickerDialog.java @@ -29,7 +29,6 @@ public class DoubleDateAndTimePickerDialog extends BaseDialog { - private Listener listener; private BottomSheetHelper bottomSheetHelper; private TextView buttonTab0; @@ -234,16 +233,6 @@ public void onClick(View view) { pickerTab1.setSelectedTextColor(mainColor); } - if (minDate != null) { - pickerTab0.setMinDate(minDate); - pickerTab1.setMinDate(minDate); - } - - if (maxDate != null) { - pickerTab0.setMaxDate(maxDate); - pickerTab1.setMaxDate(maxDate); - } - if (defaultDate != null) { Calendar calendar = Calendar.getInstance(); calendar.setTime(defaultDate); @@ -263,6 +252,16 @@ public void onClick(View view) { pickerTab1.selectDate(calendar); } + if (minDate != null) { + pickerTab0.setMinDate(minDate); + pickerTab1.setMinDate(minDate); + } + + if (maxDate != null) { + pickerTab0.setMaxDate(maxDate); + pickerTab1.setMaxDate(maxDate); + } + if (dayFormatter != null) { pickerTab0.setDayFormatter(dayFormatter); pickerTab1.setDayFormatter(dayFormatter); @@ -282,6 +281,9 @@ public void onDateChanged(String displayed, Date date) { } }); } + + pickerTab0.checkPickersMinMax(); + pickerTab1.checkPickersMinMax(); } @NonNull diff --git a/singledateandtimepicker/src/main/java/com/github/florent37/singledateandtimepicker/widget/WheelDayPicker.java b/singledateandtimepicker/src/main/java/com/github/florent37/singledateandtimepicker/widget/WheelDayPicker.java index c64dca48..fb6251ea 100644 --- a/singledateandtimepicker/src/main/java/com/github/florent37/singledateandtimepicker/widget/WheelDayPicker.java +++ b/singledateandtimepicker/src/main/java/com/github/florent37/singledateandtimepicker/widget/WheelDayPicker.java @@ -52,11 +52,6 @@ protected DateWithLabel initDefault() { return new DateWithLabel(getTodayText(), new Date()); } - @NonNull - private String getTodayText() { - return getLocalizedString(R.string.picker_today); - } - @Override protected void onItemSelected(int position, DateWithLabel item) { if (onDaySelectedListener != null) { @@ -124,28 +119,14 @@ private SimpleDateFormat getDateFormat() { } private Date convertItemToDate(int itemPosition) { - Date date; final String itemText = adapter.getItemText(itemPosition); final Calendar todayCalendar = Calendar.getInstance(); todayCalendar.setTimeZone(dateHelper.getTimeZone()); - int todayPosition = -1; - final List data = adapter.getData(); - - for (int i = 0; i < data.size(); i++) { - if (data.get(i).label.equals(getTodayText())) { - todayPosition = i; - break; - } - } - - if (getTodayText().equals(itemText)) { - date = todayCalendar.getTime(); - } else { - todayCalendar.add(Calendar.DAY_OF_YEAR, (itemPosition - todayPosition)); - date = todayCalendar.getTime(); + if (!getTodayText().equals(itemText)) { + todayCalendar.add(Calendar.DAY_OF_YEAR, (itemPosition - getTodayItemPosition())); } - return date; + return todayCalendar.getTime(); } public void setTodayText(DateWithLabel today) { @@ -154,6 +135,7 @@ public void setTodayText(DateWithLabel today) { if (data.get(i).label.equals(getTodayText())) { adapter.getData().set(i, today); notifyDatasetChanged(); + this.today = today; } } } diff --git a/singledateandtimepicker/src/main/java/com/github/florent37/singledateandtimepicker/widget/WheelPicker.java b/singledateandtimepicker/src/main/java/com/github/florent37/singledateandtimepicker/widget/WheelPicker.java index 10f96f96..10ef13ab 100644 --- a/singledateandtimepicker/src/main/java/com/github/florent37/singledateandtimepicker/widget/WheelPicker.java +++ b/singledateandtimepicker/src/main/java/com/github/florent37/singledateandtimepicker/widget/WheelPicker.java @@ -52,6 +52,8 @@ public abstract class WheelPicker extends View { protected int lastScrollPosition; protected Listener listener; protected Adapter adapter = new Adapter<>(); + protected DateWithLabel today; + protected int todayItemPosition = -1; private Locale customLocale; private Paint paint; private Scroller scroller; @@ -664,7 +666,6 @@ protected void onItemSelected(int position, V item) { } } - protected void onItemCurrentScroll(int position, V item) { if (lastScrollPosition != position) { if (listener != null) { @@ -733,16 +734,26 @@ public int getDefaultItemPosition() { } public int getTodayItemPosition() { - List list = adapter.getData(); - for (int i = 0; i < list.size(); i++) { - if (list.get(i) instanceof DateWithLabel) { - DateWithLabel dwl = (DateWithLabel) list.get(i); - if (dwl.label.equals(getLocalizedString(R.string.picker_today))) { - return i; + if (todayItemPosition == -1) { + todayItemPosition = 0; + List list = adapter.getData(); + String todayText = getTodayText(); + for (int i = 0; i < list.size(); i++) { + if (list.get(i) instanceof DateWithLabel) { + DateWithLabel dwl = (DateWithLabel) list.get(i); + if (dwl.label.equals(todayText)) { + todayItemPosition = i; + break; + } } } } - return 0; + return todayItemPosition; + } + + @NonNull + public String getTodayText() { + return today == null || TextUtils.isEmpty(today.label) ? getLocalizedString(R.string.picker_today) : today.label; } public void setAdapter(Adapter adapter) { diff --git a/singledateandtimepicker/src/main/java/com/github/florent37/singledateandtimepicker/widget/WheelYearPicker.java b/singledateandtimepicker/src/main/java/com/github/florent37/singledateandtimepicker/widget/WheelYearPicker.java index 7412e8b2..1063f9c1 100644 --- a/singledateandtimepicker/src/main/java/com/github/florent37/singledateandtimepicker/widget/WheelYearPicker.java +++ b/singledateandtimepicker/src/main/java/com/github/florent37/singledateandtimepicker/widget/WheelYearPicker.java @@ -45,11 +45,6 @@ protected String initDefault() { return getTodayText(); } - @NonNull - private String getTodayText() { - return getLocalizedString(R.string.picker_today); - } - @Override protected void onItemSelected(int position, String item) { if (onYearSelectedListener != null) {