JTAppleCalendar icon indicating copy to clipboard operation
JTAppleCalendar copied to clipboard

wrong date retuned with gesture recognition.

Open alexvaiman opened this issue 5 years ago • 16 comments

let cellState = calendarView.cellStatus(at: gesture.location(in: gesture.view!)) cellState.date, will always return the first section month, instead of the real month.

*I can see now that this is long time issue.. https://github.com/patchthecode/JTAppleCalendar/issues/253#issuecomment-439784680

alexvaiman avatar May 22 '20 13:05 alexvaiman

the code used.

 longPressGesture.minimumPressDuration = 1    // User has the flexibility to set the time
        calendarView.addGestureRecognizer(longPressGesture)
        self.addGestureRecognizer(longPressGesture)
@objc private func didStartLongPressing(gesture: UILongPressGestureRecognizer) {
        if gesture.state == .began {
            if let cellState = calendarView.cellStatus(at: gesture.location(in: gesture.view!)) {
                
                let dateType = cellState.date.getDateType(ranges: self.savedDatesRanges)
                if dateType.case != .none {
                    return
                }
                
                self.unsavedRange = PrivateCalendarRange(startDate: cellState.date, endDate: cellState.date)
                firstUserSelectedDate = nil
                secondUserSelectedDate = nil
                calendarView.deselectAllDates()
                calendarView.selectDates([cellState.date], triggerSelectionDelegate: false, keepSelectionIfMultiSelectionAllowed: true)
            }
        }
    }

alexvaiman avatar May 22 '20 14:05 alexvaiman

for me this is a very important thing - as in my implementation, long click on a date meaning the user wants this date as a "single date range"

alexvaiman avatar May 22 '20 14:05 alexvaiman

ok i can see the problem

   public func cellStatus(at point: CGPoint) -> CellState? {
        if let indexPath = indexPathForItem(at: point) {
            let cell = cellForItem(at: indexPath) as? JTACDayCell
            return cellStateFromIndexPath(indexPath, cell: cell)
        }
        return nil
    }

this function does not take the current month into the account what so ever... i will fix this. (i hope this will be fast...)

alexvaiman avatar May 22 '20 14:05 alexvaiman

ok it was actually easy

    public func cellStatus(at point: CGPoint) -> CellState? {
        guard let indexPath = indexPathForItem(at: point) else {
            return nil
        }
        
        let i = indexPath.item
        let row =  i / maxNumberOfDaysInWeek
        let col = i % maxNumberOfDaysInWeek
        return cellStatusForDate(at: row, column: col)
    }

alexvaiman avatar May 22 '20 17:05 alexvaiman

you are welcome to upload this fix in the next version

alexvaiman avatar May 22 '20 17:05 alexvaiman

Can you let me know what date you are expecting, and what date you are receiving? Is it an off by 1 date error?

patchthecode avatar May 22 '20 20:05 patchthecode

"Can you let me know what date you are expecting, and what date you are receiving? Is it an off by 1 date error?"

I am expecting the day i am pressing on utc-0, start of day. I get totally wrong result - the offset depends on the month, each month farther from the calendar start date, the offset is bigger - but I have already fixed the issue - and sent a pull request.

alexvaiman avatar May 22 '20 21:05 alexvaiman

for me my solution works - it gives me the correct date, but the cellState.cell is still nil. I dont care about cell, at this stage. (I will handle the ui on shouldSelect if needed)

the problem is: let cell = cellForItem(at: indexPath) as? JTACDayCell will return nil - in any section other then section 0 (start month of the calendar). maybe later I will fix this as well. for now at least you get the correct statusCell info.

alexvaiman avatar May 23 '20 09:05 alexvaiman

ok i solved this as well.

 public func cellStatus(at point: CGPoint) -> CellState? {
        guard let indexPath = indexPathForItem(at: point) else {
            return nil
        }
        
        guard let section = currentSection() else {
            return nil
        }

        let i = indexPath.item
        let row =  i / maxNumberOfDaysInWeek
        let column = i % maxNumberOfDaysInWeek
        let convertedRow = (row * maxNumberOfDaysInWeek) + column
        let indexPathToFind = IndexPath(item: convertedRow, section: section)
        
        if let date = dateOwnerInfoFromPath(indexPathToFind) {
            let cell = cellForItem(at: indexPathToFind) as? JTACDayCell
            let stateOfCell = cellStateFromIndexPath(indexPathToFind, withDateInfo: date,cell: cell)
            return stateOfCell
        }
        
        return nil
    }

alexvaiman avatar May 23 '20 10:05 alexvaiman

also added new function, for those who care only for the cell data. (like my case of use)

 public func cellStatusForDate(at point: CGPoint) -> CellState? {
        guard let indexPath = indexPathForItem(at: point) else {
            return nil
        }
        
        return cellStatusForDate(at: indexPath.item / maxNumberOfDaysInWeek , column: indexPath.item % maxNumberOfDaysInWeek)
    }

alexvaiman avatar May 23 '20 10:05 alexvaiman

leave open. i'll take a loo once i get the chance.

patchthecode avatar May 26 '20 12:05 patchthecode

no problem sorry.

alexvaiman avatar May 26 '20 12:05 alexvaiman

i am still using my fork, more then a year.. no new version?)

alexvaiman avatar Sep 19 '21 14:09 alexvaiman

time is short these days. But i thought i had fixed this.

Can you tell me if the tutorial here fixes your issue? https://patchthecode.com/jtapplecalendar-home/ranged-selection/

patchthecode avatar Sep 19 '21 15:09 patchthecode

It has code in there for getting the point of the gesture. Scroll down to the Dragging your finger to Ranged Selection section.

patchthecode avatar Sep 19 '21 15:09 patchthecode

Or do you have a discord address? typing here is a bit slow.

patchthecode avatar Sep 19 '21 15:09 patchthecode