Horizontal-Calendar icon indicating copy to clipboard operation
Horizontal-Calendar copied to clipboard

Cannot Programmatically Select Last Date In Range

Open benroaman opened this issue 7 years ago • 14 comments

Disclaimer: I am writing my app in Kotlin

For my app I need the last date in my range to be the current date, and for the current date to be the default selection when the view loads. When I provide the current date as the endDate for the calendar, the calendar selects the first date in the range as the default.

val startDate = Calendar.getInstance()
startDate.add(Calendar.MONTH, -1)

val endDate = Calendar.getInstance()

datePicker = HorizontalCalendar.Builder(activity, R.id.calendarView)
    .range(startDate, endDate)
    .datesNumberOnScreen(7)
    .build()

When the view loads, the calendar has selected the first date in the range. I have also tried adding the following after the above code:

datePicker?.selectDate(Calendar.getInstance(), true) datePicker?.goToday(true)

as well as adding the following to my build chain:

.defaultSelectedDate(Calendar.getInstance())

and none of those did the trick. if I increase the endDate by a single day everything works fine.

I am still able to manually select the final date in the range, everything still works fine with manual selection.

I saw in an old thread there had been issues with the number of dates displayed, and I tried changing that to 5. This also had no effect.

Thanks in advance!

Steps to Reproduce the Problem (sample code if possible)

  1. Initialize a HorizontalCalendar with Calendar.getInstance() as the end date.

Specifications

  • Android Version: 26
  • Horizontal-Calendar Version: 1.3.0

benroaman avatar Dec 31 '17 19:12 benroaman

@benroaman yeah I was able to reproduce this problem, it seems that time is also considered in the calculation so when you pass Calendar.getInstance() again it will be considered out of range!

I will be fixing this in the next release, thanks.

muraee avatar Jan 01 '18 14:01 muraee

@Mulham-Raee That's great news, thanks for being so proactive!

benroaman avatar Jan 02 '18 14:01 benroaman

So I just found another bug that seems like it's probably related to your above diagnosis so I thought I'd just point it out here. Using the exact same setup as my original post, when I manually select the final date in the range the onDateSelected method of the HorizontalCalendarListener is not invoked. (it works for every other date in the range).

Here's the code where I set the listener in case you're interested:

datePicker?.calendarListener = object: HorizontalCalendarListener() {
    override fun onDateSelected(date: Calendar?, position: Int) {
        selectDate(date)
    }
}

Thanks Again!

benroaman avatar Jan 02 '18 14:01 benroaman

Yes this is probably related to the same problem, thanks for pointing it out.

muraee avatar Jan 03 '18 14:01 muraee

@benroaman fixed in version 1.3.2.

muraee avatar Jan 15 '18 18:01 muraee

Hey , I still have an issue related to this bug.
onDateSelected doesnt gets triggered when i scroll from an past day to today (today is the last selectable day)

gvortel avatar Jan 26 '18 12:01 gvortel

@Mulham-Raee @gvortel I am also still getting this behavior after the update.

benroaman avatar Jan 29 '18 16:01 benroaman

@gvortel @benroaman can you please check with version 1.3.4, I can't reproduce this bug again on my end with this version.

muraee avatar Feb 02 '18 18:02 muraee

@Mulham-Raee I upgraded to 1.3.4 and I still have this behavior. The last date (today) is automatically selected when the view loads, but If I select another date and then select the last date (today) again, the ui shows my selection but onDateSelected is not invoked.

Here's my setup for your reference

        val startDate = Calendar.getInstance()
        startDate.add(Calendar.MONTH, -1)

        val endDate = Calendar.getInstance()

        datePicker = HorizontalCalendar.Builder(activity, R.id.calendarView)
                .range(startDate, endDate)
                .datesNumberOnScreen(7)
                .build()

        datePicker?.calendarListener = object: HorizontalCalendarListener() {
            override fun onDateSelected(date: Calendar?, position: Int) {
                selectDate(date)
            }
        }

Please let me know if there's any other info you would find helpful.

Thanks!

benroaman avatar Feb 05 '18 13:02 benroaman

@Mulham-Raee So I've done a bit more debugging, and while I can programmatically select any date in the range, I cannot manually (by scrolling or tapping) select the first or last date in the range. I have found that, for some reason, HorizontalSnapHelper.notifyCalendarListentener is not getting invoked when manually selecting the first or last date in the range. For every date that works, HorizontalSnapHelper.findSnapView is invoked twice, once returning on line 32 before notifyCalendarListener is invoked, and on the second time returning on line 40, after notifyCalendarListener is invoked. For the first and last dates in the range, findSnapView is only invoked once, returning on line 32.

I hope this is helpful.

-Ben

benroaman avatar Mar 01 '18 17:03 benroaman

@benroaman Thanks for the info, it is indeed helpful, I will investigate this problem further.

muraee avatar Mar 04 '18 10:03 muraee

@Mulham-Raee I debugged it and find out that it's happening because of following likes in findSnapView() method of HorizontalSnapHelper..

int[] snapDistance = calculateDistanceToFinalSnap(layoutManager, snapView);
if ((snapDistance[0] != 0) || (snapDistance[1] != 0)){
    return snapView;
}
selectedItemPosition = layoutManager.getPosition(snapView);

I think since calculateDistanceToFinalSnap() method of super class LinearSnapHelper always returns snapDistance[1] = 0 value for horizontal calendar, it returns without notifying calendar listeners.

Can we do like below:

                if ((snapDistance[0] == 0)){
                    return snapView;
                }

Check if snap distance on horizontal axis is 0. If yes, then simply return snapView. Else, notify listeners about change and return snapView..

What are your thoughts?

mahavir155 avatar Apr 30 '18 14:04 mahavir155

Hello @Mulham-Raee, I have an issue in Horizontal calendar, when ever i select a date it automatically goes to next date of selected Date..

Please let me know if there's any other info you would find helpful.

Thanks!

HARMAN2231 avatar Aug 22 '18 05:08 HARMAN2231

Hello @Mulham-Raee,

i just imported ur old version module in which first and last date was uneslectable what i have to change so it become selectable which u have done in 1.3.4 version. i find issue please chek it when datesNumberOnScreen(7) will be seven it will become selected but its listener will not call so please look in to this, it will helpfull for us

ashishgupta2510 avatar Aug 23 '18 10:08 ashishgupta2510