Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

wrong date retuned with gesture recognition. #1276

Open
alexvaiman opened this issue May 22, 2020 · 16 comments
Open

wrong date retuned with gesture recognition. #1276

alexvaiman opened this issue May 22, 2020 · 16 comments

Comments

@alexvaiman
Copy link

alexvaiman commented May 22, 2020

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..
#253 (comment)

@alexvaiman
Copy link
Author

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
Copy link
Author

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
Copy link
Author

alexvaiman commented May 22, 2020

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
Copy link
Author

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
Copy link
Author

you are welcome to upload this fix in the next version

@patchthecode
Copy link
Owner

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

@alexvaiman
Copy link
Author

alexvaiman commented May 22, 2020

"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
Copy link
Author

alexvaiman commented May 23, 2020

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
Copy link
Author

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
Copy link
Author

alexvaiman commented May 23, 2020

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)
    }

@patchthecode
Copy link
Owner

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

@alexvaiman
Copy link
Author

no problem sorry.

@alexvaiman
Copy link
Author

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

@patchthecode
Copy link
Owner

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
Copy link
Owner

patchthecode commented Sep 19, 2021

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

@patchthecode
Copy link
Owner

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants