Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Other florent37#306, florent37#303, florent37#269, florent37#290
The issues were mainly caused because of incorrect retrieving of today item index.
Optimized today item index search on scroll.
  • Loading branch information
Akhunzaada committed Nov 6, 2020
1 parent d77606c commit 1533b11
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 54 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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();
Expand All @@ -243,8 +243,9 @@ public void doubleClicked() {
//.bottomSheet()
//.curved()

// .backgroundColor(Color.BLACK)
// .mainColor(Color.GREEN)
//.backgroundColor(Color.BLACK)
//.mainColor(Color.GREEN)

.minutesStep(15)
.mustBeOnFuture()

Expand All @@ -257,6 +258,7 @@ public void doubleClicked() {
.tab0Date(now)
.tab1Date(new Date(now.getTime() + TimeUnit.HOURS.toMillis(1)))

//.todayText("Today")
.title("Double")

.tab0Text("Depart")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@

public class DoubleDateAndTimePickerDialog extends BaseDialog {


private Listener listener;
private BottomSheetHelper bottomSheetHelper;
private TextView buttonTab0;
Expand Down Expand Up @@ -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);
Expand All @@ -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);
Expand All @@ -282,6 +281,9 @@ public void onDateChanged(String displayed, Date date) {
}
});
}

pickerTab0.checkPickersMinMax();
pickerTab1.checkPickersMinMax();
}

@NonNull
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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<DateWithLabel> 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) {
Expand All @@ -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;
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ public abstract class WheelPicker<V> extends View {
protected int lastScrollPosition;
protected Listener<WheelPicker, V> listener;
protected Adapter<V> adapter = new Adapter<>();
protected DateWithLabel today;
protected int todayItemPosition = -1;
private Locale customLocale;
private Paint paint;
private Scroller scroller;
Expand Down Expand Up @@ -664,7 +666,6 @@ protected void onItemSelected(int position, V item) {
}
}


protected void onItemCurrentScroll(int position, V item) {
if (lastScrollPosition != position) {
if (listener != null) {
Expand Down Expand Up @@ -733,16 +734,26 @@ public int getDefaultItemPosition() {
}

public int getTodayItemPosition() {
List<V> 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<V> 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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit 1533b11

Please sign in to comment.