From 54386c6ff0fed0e46aba7998f1154cbc6e1c42de Mon Sep 17 00:00:00 2001 From: Akhunzaada Date: Wed, 11 Jul 2018 16:02:49 +0500 Subject: [PATCH 1/5] Fixes #148 The crash was caused because of the options secondDateAfterFirst which was added in the commit db112c9 The issue was only reproduced when the flag secondDateAfterFirst was set to true and the WheelDayOfMonthPicker view was visible. The listener addOnDateChangedListener was updating the second picker with the minimum date whereas when the WheelDayOfMonthPicker's visibility was gone, the picker had no items resulting in a crash in getItem() method of the WheelPicker adapter. --- .../florent37/singledateandtimepicker/widget/WheelPicker.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 0cc894c8..f2407828 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 @@ -1068,7 +1068,7 @@ public int getItemCount() { @Override public V getItem(int position) { final int itemCount = getItemCount(); - return data.get((position + itemCount) % itemCount); + return itemCount != 0 ? data.get((position + itemCount) % itemCount) : null; } @Override From 32b9b69d687a486c778b220b0878686f9c99ac61 Mon Sep 17 00:00:00 2001 From: Akhunzaada Date: Thu, 12 Jul 2018 16:01:43 +0500 Subject: [PATCH 2/5] Fixes #124, #127 & #136 The crash was caused when today's label was set using todayText() method or from xml. The WheelDayPicker was considering the label from resources for today position instead of the custom set label. --- .../widget/WheelDayPicker.java | 20 +++++++++++++------ .../widget/WheelPicker.java | 2 +- 2 files changed, 15 insertions(+), 7 deletions(-) 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 91265446..7663c6db 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 @@ -2,6 +2,7 @@ import android.content.Context; import android.support.annotation.NonNull; +import android.text.TextUtils; import android.util.AttributeSet; import com.github.florent37.singledateandtimepicker.DateHelper; @@ -22,6 +23,8 @@ public class WheelDayPicker extends WheelPicker { private OnDaySelectedListener onDaySelectedListener; + private String todayText; + public WheelDayPicker(Context context) { super(context); } @@ -40,11 +43,6 @@ protected String initDefault() { return getTodayText(); } - @NonNull - private String getTodayText() { - return getResources().getString(R.string.picker_today); - } - public WheelDayPicker setDayFormatter(SimpleDateFormat simpleDateFormat){ this.simpleDateFormat = simpleDateFormat; adapter.setData(generateAdapterValues()); @@ -126,11 +124,21 @@ private Date convertItemToDate(int itemPosition) { return date; } + public int getTodayTextPosition() { + return adapter.getData().indexOf(getTodayText()); + } + + @NonNull + private String getTodayText() { + return TextUtils.isEmpty(todayText) ? getResources().getString(R.string.picker_today) : todayText; + } + public void setTodayText(String todayText) { - int index = adapter.getData().indexOf(getTodayText()); + int index = getTodayTextPosition(); if (index != -1) { adapter.getData().set(index, todayText); notifyDatasetChanged(); + this.todayText = todayText; } } 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 f2407828..3ca87546 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 @@ -927,7 +927,7 @@ public int findIndexOfDate(@NonNull Date date) { if (this instanceof WheelDayPicker) { String today = getFormattedValue(new Date()); if (today.equals(formatItem)) { - return getDefaultItemPosition(); + return ((WheelDayPicker)this).getTodayTextPosition(); } } From 1533b11328a3c4874016974c559aa48f5aa94886 Mon Sep 17 00:00:00 2001 From: Abdul Khaliq Date: Fri, 6 Nov 2020 23:20:17 +0500 Subject: [PATCH 3/5] 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) { From 7902c56c1d0ce46b9a10e7a1187b7e87f9d6cf2b Mon Sep 17 00:00:00 2001 From: Abdul Khaliq Date: Fri, 6 Nov 2020 23:38:35 +0500 Subject: [PATCH 4/5] Gradle wrapper and plugin versions updated targetSdkVersion set to API 30 Dependency versions updated --- app/build.gradle | 4 ++-- build.gradle | 4 ++-- gradle/wrapper/gradle-wrapper.properties | 2 +- singledateandtimepicker/build.gradle | 2 +- 4 files changed, 6 insertions(+), 6 deletions(-) diff --git a/app/build.gradle b/app/build.gradle index afec38dd..38ef7cf4 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -28,8 +28,8 @@ android { dependencies { implementation fileTree(include: ['*.jar'], dir: 'libs') - implementation "androidx.appcompat:appcompat:1.1.0" - implementation 'com.google.android.material:material:1.1.0' + implementation "androidx.appcompat:appcompat:1.2.0" + implementation 'com.google.android.material:material:1.2.1' implementation 'com.jakewharton:butterknife:10.2.1' annotationProcessor 'com.jakewharton:butterknife-compiler:10.2.1' implementation project(':singledateandtimepicker') diff --git a/build.gradle b/build.gradle index ba2ea3fd..4f9311c3 100644 --- a/build.gradle +++ b/build.gradle @@ -6,7 +6,7 @@ buildscript { google() } dependencies { - classpath 'com.android.tools.build:gradle:3.6.2' + classpath 'com.android.tools.build:gradle:4.0.2' classpath 'com.github.dcendents:android-maven-gradle-plugin:2.1' classpath 'com.jfrog.bintray.gradle:gradle-bintray-plugin:1.8.4' // NOTE: Do not place your application dependencies here; they belong @@ -23,7 +23,7 @@ allprojects { } ext { - sdk = 28 + sdk = 30 minSdk = 14 github = 'https://github.com/florent37/SingleDateAndTimePicker' diff --git a/gradle/wrapper/gradle-wrapper.properties b/gradle/wrapper/gradle-wrapper.properties index a47afbd2..15b18e5d 100644 --- a/gradle/wrapper/gradle-wrapper.properties +++ b/gradle/wrapper/gradle-wrapper.properties @@ -3,4 +3,4 @@ distributionBase=GRADLE_USER_HOME distributionPath=wrapper/dists zipStoreBase=GRADLE_USER_HOME zipStorePath=wrapper/dists -distributionUrl=https\://services.gradle.org/distributions/gradle-5.6.4-all.zip +distributionUrl=https\://services.gradle.org/distributions/gradle-6.1.1-all.zip diff --git a/singledateandtimepicker/build.gradle b/singledateandtimepicker/build.gradle index a19bbffe..0a917a2b 100644 --- a/singledateandtimepicker/build.gradle +++ b/singledateandtimepicker/build.gradle @@ -15,7 +15,7 @@ android { } dependencies { - implementation "androidx.appcompat:appcompat:1.1.0" + implementation "androidx.appcompat:appcompat:1.2.0" } ext { From 86dd95700cc3a72e7da62fbab610295f3f4e183b Mon Sep 17 00:00:00 2001 From: Abdul Khaliq Date: Sat, 7 Nov 2020 00:20:55 +0500 Subject: [PATCH 5/5] #307 New translations added for resource picker_today --- singledateandtimepicker/src/main/res/values-pl/strings.xml | 4 ++++ singledateandtimepicker/src/main/res/values-sk/strings.xml | 4 ++++ 2 files changed, 8 insertions(+) create mode 100644 singledateandtimepicker/src/main/res/values-pl/strings.xml create mode 100644 singledateandtimepicker/src/main/res/values-sk/strings.xml diff --git a/singledateandtimepicker/src/main/res/values-pl/strings.xml b/singledateandtimepicker/src/main/res/values-pl/strings.xml new file mode 100644 index 00000000..2233d3aa --- /dev/null +++ b/singledateandtimepicker/src/main/res/values-pl/strings.xml @@ -0,0 +1,4 @@ + + + dzisiaj + \ No newline at end of file diff --git a/singledateandtimepicker/src/main/res/values-sk/strings.xml b/singledateandtimepicker/src/main/res/values-sk/strings.xml new file mode 100644 index 00000000..00c23b38 --- /dev/null +++ b/singledateandtimepicker/src/main/res/values-sk/strings.xml @@ -0,0 +1,4 @@ + + + dnes + \ No newline at end of file