JTAppleCalendar icon indicating copy to clipboard operation
JTAppleCalendar copied to clipboard

Trying to show calendar starting at current month causes flashing

Open iamZoltanVaradi opened this issue 6 years ago • 18 comments

(Required) Version Number: 8.0.0

Description

When showing my ViewController I want the calendar to show the current day's month from the beginning. I achieve this by calling self.calendarView.scrollToDate(Date(), animateScroll: false) from ViewDidLoad.

However, when seeing the ViewController you can notice a quick flash when the calendar switches from the start month to the current month. I would like to be able to overcome this.

Steps To Reproduce

Create a JTACMonthView, and configure it the following way:

func configureCalendar(_ calendar: JTACMonthView) -> ConfigurationParameters {    
    let startDate: Date = self.calendarStart ?? Date()
    let endDate: Date = self.calendarEnd ?? Date()
    
    let parameters = ConfigurationParameters(startDate: startDate,
                                             endDate: endDate,
                                             numberOfRows: 6,
                                             calendar: Calendar.current,
                                             generateInDates: .forAllMonths,
                                             generateOutDates: .tillEndOfGrid,
                                             firstDayOfWeek: .monday,
                                             hasStrictBoundaries: true)
    
    
    return parameters
  }

From your ViewController's ViewDidLoad, call self.calendarView.scrollToDate(Date(), animateScroll: false)

Expected Behavior

When the calendar becomes visible, it should already start at the selected month.

Additional Context

If in the config parameters, numberOfRows is 1, then the can't be noticed. Most likely the more rendering the calendar needs to do, the more time it takes to scroll to the desired month.

iamZoltanVaradi avatar Jul 30 '19 14:07 iamZoltanVaradi

  1. Can you let me know you startDate and endDate
  2. Is your calendar inside any weird complex view hierarchy? or is it just a simply view hierarchy?

patchthecode avatar Jul 30 '19 17:07 patchthecode

  1. Start date is 2019.03.01 and end date is 2019.07.31
  2. It's in a ViewController which is the root of a NavigationViewController. That NavigationViewController is in a TabBarController. Not too simple, but nothing special either.

iamZoltanVaradi avatar Jul 30 '19 18:07 iamZoltanVaradi

ok, then it should not be flashing. I have tested with 50 years + range. I wrote code to remove flashing since version 7 let me check my sample app.

patchthecode avatar Jul 30 '19 18:07 patchthecode

Can you also let me know if you are using Xcode version 11 beta?

patchthecode avatar Jul 30 '19 18:07 patchthecode

I use Xcode 10.2

iamZoltanVaradi avatar Jul 30 '19 18:07 iamZoltanVaradi

thanks. checking it out.

patchthecode avatar Jul 30 '19 18:07 patchthecode

hmm.. I cannot reproduce what you are seeing. Below I have a TabView -> NavController -> ViewController with calendar scrolling to date and animation = false

calendararchitecture

I tested i Xcode 11 because i currently have upgraded to Xcode 11 Beta. Is it possible you can create a new sample app?

Just slap together a UITabView with UINavigation with UIViewController with a barebones calendar which scrolls to date? Maybe you are doing something in code which I am not doing? With a sample file i can solve faster.

patchthecode avatar Jul 30 '19 18:07 patchthecode

Yes, thank you, let me do it.

iamZoltanVaradi avatar Jul 30 '19 18:07 iamZoltanVaradi

The current version of the sample project is requiring Xcode 11, so I can't run it. Can you please check without animating the segue that happens after tapping the "Calendar Layouts demonstration" button? My feeling is the flashing happens there too, only it's not noticeable because of the animation

iamZoltanVaradi avatar Jul 30 '19 19:07 iamZoltanVaradi

The GIF image i showed above is a recording what what you just asked me to do. The animation was set to false. And in the recording we can see there is no flash.

The reason i asked for a sample app from you was because i tested the sample app

  1. in a tab bar with navigation controller
  2. outside of a tabbar with nav controller

scrolling animations was set to false in both cases. And there was no flash. Which led me to believe that maybe you were doing something else to what I am doing. If version 8.0 of the sample app cannot be opened by you, then can you try version 7.1.7 ? --> https://github.com/patchthecode/JTAppleCalendar/archive/7.1.7.zip

But i would really like if you can provide a quick small sample app with the problem.

patchthecode avatar Jul 31 '19 02:07 patchthecode

In the demo app, I can't reproduce the issue. Unfortunately, I can't provide the code I'm using.

In my code, I am able to mitigate the effect if I make the shared function to configure cells less complex. I do lots of coloring there. If I leave that shared function empty, then scrolling to the specific date's month happens without flashing.

I measured what happens in Instruments, most time is spent at JTAppleCalendarView layoutSubviews. In there, most time used was for preparing cells. My gut feeling is my UI is too complex, that's why I notice the flashing.

iamZoltanVaradi avatar Jul 31 '19 09:07 iamZoltanVaradi

hmm.. i have created many complex cells before, and see no flashing. So let me ask you this:

Are you by any chance initializing any heavy classes in the cells? for example:

  1. DateFormatter (you should never do this in there)
  2. Colors initialized on the fly
  3. And other classes initialized in the cell
  4. heavy computations done in loops in the cells (try to avoid loops, remember this is done fro 42 cells). For example i use Dictionaries where-ever possible for instant cellData access.

Heavy classes, that should be initialized on the outside, saved in a variable, and then used in the cells.

Keep in mind that your calendar is a UICollectionView with ~42 cells on the screen at a time. And heavy initializations or computations should not there in there, because you should want a buttery smooth scrolling experience.

So is there any heavy classes or computations done in there?

patchthecode avatar Jul 31 '19 11:07 patchthecode

Not really, I also made sure of I wouldn't do any heavy lifting there. Even after I commented out everything from the cell initializing code, the jumping still happened.

What I find strange is if I don't try scrolling, just see the start month, my custom UI renders instantly, without any delay.

iamZoltanVaradi avatar Jul 31 '19 13:07 iamZoltanVaradi

just to clarify, you commented every thing out and it still flashed? BUT when you tried creating a empty sample project there was no flash?

Ok, now i am confused. Because the commented out project should behave exactly the same as a newly created empty sample project...

It is sort of hard to debug without anything to work on. And without the issue happening on both your sample app and also my sample app. I am trying to think of what the problem might be.. hmm..

patchthecode avatar Jul 31 '19 14:07 patchthecode

I'm had a similar issue and solved it by removing all DispatchQueue.main.async in InternalActionFunctions.swift so scrolling happens immediately.

Thomasch avatar Aug 01 '19 13:08 Thomasch

I just did that, but it didn't help. Luckily. I would have been really worried if my bug was related to threads. Unfortunately, at this stage of development, I can't spend time on trying to sandbox the problem. Feel free to close this ticket and I'll get back to you what was wrong when I my time allows.

iamZoltanVaradi avatar Aug 01 '19 13:08 iamZoltanVaradi

hi, is it possible that you are seeing this issue? --> https://github.com/patchthecode/JTAppleCalendar/issues/1002#issuecomment-520588627 Scroll to the end and let me know if that version removes the flashing for you.

If so, i will try to release a 7.1.8 version this weekend.

patchthecode avatar Aug 13 '19 16:08 patchthecode

Unfortunately not. I the part this modification affects doesn't get called for me, even though I pretty much do the same thing. (Call scrollToDate from viewDidLoad)

iamZoltanVaradi avatar Aug 14 '19 12:08 iamZoltanVaradi