recruit-mp / LightCalendarView

A lightweight monthly calendar view for Android, fully written in Kotlin. Designed to meet the minimum demands for typical calendars.
Apache License 2.0
445 stars 67 forks source link

DayLayout.getDayView returning wrong DayView when first time invoked #14

Open TKolbusz opened 7 years ago

TKolbusz commented 7 years ago

As title says, Logcat // first invocation D/DayLayout: setSelectedDay Mon Jan 02 12:55:21 GMT 2017 D/DayLayout: setSelectedDay2 DayView(Sun Jan 01 12:55:21 GMT 2017) //2nd invocation D/DayLayout: setSelectedDay Mon Jan 02 12:55:21 GMT 2017 D/DayLayout: setSelectedDay2 DayView(Mon Jan 02 12:55:21 GMT 2017)

DayLayout.kt

    fun setSelectedDay(date: Date) {
        Log.d("DayLayout", "setSelectedDay " + date.toString())
        setSelectedDay(getDayView(date))
    }

    fun getDayView(date: Date): DayView? = childList.getOrNull(date.daysAfter(firstDate.time).toInt()) as? DayView

    private fun setSelectedDay(view: DayView?) {
        Log.d("DayLayout","setSelectedDay2 " + view.toString())
        selectedDayView?.apply {
            isSelected = false
            updateState()
        }
        selectedDayView = view?.apply {
            isSelected = true
            updateState()
            callback?.onDateSelected(date)
        }
    }

Edit:

After further investigation I came to conclusion that date.DaysAfter(firstDate.time) is returning wrong value if date and firstDate.time are the same. Essentially it means that when you select date when view is inflated(like me) you'll get wrong DayView selected. Solution for that is setting date and firstDate.time fields MINUTE and HOUR to 0, and adding 1 to the return value of daysAfter func

recruit-mahayash commented 7 years ago

Hi @TKolbusz,

I apologize for the delay in replying to you.

Thanks a lot for the issue and further investigations! We are now working on enhancements and bug-fixes including this in our private repository. The next version will be soon released.

Thank you.

recruit-mahayash commented 7 years ago

@TKolbusz,

While developing the release version 1.0.1, we have tested calling the same method in the same situation, but the problem could not be reproduced. This issue may be somehow solved in Release 1.0.1. Could you confirm if the same problem occurs with the latest library version?

Thanks!

TKolbusz commented 7 years ago

@recruit-mahayash Yep it works well in new release, the issue that I explained was really weird, it actually fixed itself by mistake, it was probably unreproducable because it was caused by my other changes in code.