JTAppleCalendar icon indicating copy to clipboard operation
JTAppleCalendar copied to clipboard

calendar.currentSection() skips the multiple of 10th section

Open kushsolitary opened this issue 5 years ago • 2 comments

Version: 8.0.3

So, I have only tested this with the weekly view, but when I check for calendar.currentSection in the didScrollToDateSegmentWith delegate method, it skips the multiples of 10th (id: 9) section. So if I print these values and start scrolling from the first section, I get the following:

0
1
2
3
4
5
6
7
8
10
11
12
13
14
15
16
17
18
20

As you can see, it misses the 9th and 19th section. This is not a major issue, but I found it while debugging calendarView.scrollToDate function when it's invoked with triggerScrollToDateDelegate: true, animateScroll: true. The problem is that for certain sections, the triggerScrollToDateDelegate triggers before the animation completes.

I narrowed it down to this function:

    func calendarOffsetIsAlreadyAtScrollPosition(forOffset offset: CGPoint) -> Bool {
        var retval = false
        // If the scroll is set to animate, and the target content
        // offset is already on the screen, then the
        // didFinishScrollingAnimation
        // delegate will not get called. Once animation is on let's
        // force a scroll so the delegate MUST get caalled
        let theOffset = scrollDirection == .horizontal ? offset.x : offset.y
        let divValue = scrollDirection == .horizontal ? frame.width : frame.height
        let sectionForOffset = Int(theOffset / divValue)
        let calendarCurrentOffset = scrollDirection == .horizontal ? contentOffset.x : contentOffset.y
        if calendarCurrentOffset == theOffset || (scrollingMode.pagingIsEnabled() && (sectionForOffset ==  currentSection())) {
            retval = true
        }
        return retval
    }

The currentSection() call in the above function returns the wrong section in some cases and hence it satisfies the condition which invokes the didScrollToDateSegmentWith prematurely.

I couldn't find a way to fix it but I am hoping someone here can point me to the right direction. Thank you!

kushsolitary avatar May 09 '20 08:05 kushsolitary

For now, I have exposed a new function in the delegate scrollDidEndScrollingAnimation which fires after the didScrollToDateSegmentWith. This is a small hack to go around the issue but it seems to be working fine for me.

https://github.com/kushsolitary/JTAppleCalendar/commit/a79b7af081be68c14a325bd042625f1e1d020361

kushsolitary avatar May 09 '20 09:05 kushsolitary

Thank you. Will look at this in more detail once the covid19 situation is less.

patchthecode avatar May 10 '20 14:05 patchthecode