JTAppleCalendar icon indicating copy to clipboard operation
JTAppleCalendar copied to clipboard

Initial selected date getting ignored

Open wesgood opened this issue 6 years ago • 10 comments

v8.0.0 (working on checking this in 8.0.2)

Description

Setting a selected date inside viewDidLoad will not be added to the selectedDate array. The use case is to set the current date as preselected but then selecting another date will clear today. However when i set a breakpoint after selectedDates() the array is empty, and then two dates get highlighted.

Steps To Reproduce

  1. Create a calendar view
  2. Inside viewDidLoad assign other settings (delegate, etc) and include selectDates
  3. When the view loads, click another date in the calendar to clear today's date selection but instead 2 dates are highlighted

Expected Behavior

Selecting the second date in the month should clear the other dates (I have allow only 1 date selected)

wesgood avatar Sep 30 '19 18:09 wesgood

just checking, but are you sure you renamed the didSelect and didDeselect to the correct names as shown on the 8.0 migration page?

patchthecode avatar Sep 30 '19 18:09 patchthecode

Yes, I believe I corrected those during the last update

wesgood avatar Sep 30 '19 18:09 wesgood

i cannot recreate it on the sample project attached to this repo. Can you paste me relevant code in your

  1. cellForItem functions
  2. willDisplayCell functions

patchthecode avatar Sep 30 '19 18:09 patchthecode

I was able to update to 8.0.2 but it is crashing on assigning the datasource so I haven't confirmed if it has the same issue. Here are the functions you're requesting

func calendar(_ calendar: JTACMonthView, cellForItemAt date: Date, cellState: CellState, indexPath: IndexPath) -> JTACDayCell {
        let cell = calendar.dequeueReusableJTAppleCell(withReuseIdentifier: "CalendarDateCollectionViewCell", for: indexPath) as! CalendarDateCollectionViewCell
        cell.render(cellState)

        // set the secondary properties
        cell.hasEvents = delegate?.calendarDateHasActivities(self, selectedDate: cellState.date) ?? false
        cell.needsAttention = delegate?.calendarDateRequiresAttention(self, selectedDate: cellState.date) ?? false

        return cell
    }

    func calendar(_ calendar: JTACMonthView, willDisplay cell: JTACDayCell, forItemAt date: Date, cellState: CellState, indexPath: IndexPath) {
        let cell = cell as! CalendarDateCollectionViewCell
        cell.render(cellState)
    }

wesgood avatar Sep 30 '19 20:09 wesgood

ok, those functions look ok. There are no conflicts there.

Ok the final question I would have is, can i see your viewDidLoad function?

patchthecode avatar Sep 30 '19 20:09 patchthecode

That's going to be a little trickier as my calendar is embedded inside another view on the controller. The container is populated from the NIB and the code below is run inside the setup method.

 // calendar
        calendarView.calendarDelegate = self
        calendarView.calendarDataSource = self
        calendarView.register(UINib(nibName: "CalendarDateCollectionViewCell", bundle: nil), forCellWithReuseIdentifier: "CalendarDateCollectionViewCell")
        calendarView.scrollingMode = .stopAtEachCalendarFrame
        calendarView.scrollDirection = .horizontal
        calendarView.scrollToDate(selectedDate, triggerScrollToDateDelegate: false, animateScroll: false, preferredScrollPosition: .top, extraAddedOffset: 0) {
            self.calendarView.selectDates([self.selectedDate])
            print(self.calendarView.selectedDates.description)
        }
        calendarView.reloadData()

If i put a breakpoint at the print statement, the calendarView.selectedDates array is empty

wesgood avatar Sep 30 '19 20:09 wesgood

will get back to this over the weekend

patchthecode avatar Oct 04 '19 10:10 patchthecode

Im back at this. Kindly create a minimal sample project (just the bare minimum to get this error to happen). I do not need any of your secret company code, so you can create a new empty non designed project. Once it recreates the issue, then all will be ok.

patchthecode avatar Oct 19 '19 15:10 patchthecode

Thank you for taking an interest in this. One of the other developers on our project looked into it more closely and found that if we call selectDates() and scrollToDate() too close together, the animation/scroll has not completed and the selection gets out of sync. He solved it by adding a delay to the date selection after scrolling.

His suggestion was to have a callback in the scrollToDate method that would allow you to set the correct selection on the correct thread.

Hopefully that provides enough info without the entire test project but otherwise the library is working great for us.

Follow up: He provided more info to say that it seems that the issue is related to these three lines together:

calendarView.selectDates([selectedDate])
calendarView.scrollToDate(selectedDate, triggerScrollToDateDelegate: false, animateScroll: false, preferredScrollPosition: .top, extraAddedOffset: 0)
calendarView.reloadData()

where reloadData() specifically causes an issue (meaning remove the call and things work correctly)

wesgood avatar Oct 21 '19 17:10 wesgood

this help very much. If will rethink the logic in that section. Call backs makes sense. Tell your dev i said thanks.

patchthecode avatar Oct 21 '19 20:10 patchthecode