WenchaoD / FSCalendar

A fully customizable iOS calendar library, compatible with Objective-C and Swift
MIT License
10.61k stars 1.94k forks source link

Weird April Issue #885

Open ninobc opened 6 years ago

ninobc commented 6 years ago

Hi everyone.

First, thank you for this incredible library! However, I am experiencing a very weird issue ONLY in April. So what I am trying to accomplish is to select a certain number of dates AFTER the date selected by the user. Below is my didSelectDate code that I am using... and if you try it out yourselves, you can see that it is completely functional and works as intended. However, when selecting a date in April, this is where things start to get messy. If you even try to programmatically select a date in April, it will only set the PREVIOUS date to white text color and won't configure the cell properly (won't be highlighted as selected).

Any help would be much appreciated!

func calendar(_ calendar: FSCalendar, didSelect date: Date, at monthPosition: FSCalendarMonthPosition) {
        print("did select date \(self.formatter.string(from: date))")
        //Deselect all other dates
        for selectedDate in self.calendar.selectedDates {
            self.calendar.deselect(selectedDate)
        }
        //Prepare dates to be selected (selected date + 2 days)
        let dates = [
            date, self.gregorian.date(byAdding: .day, value: 1, to: date),
            self.gregorian.date(byAdding: .day, value: 2, to: date)
        ]

        //Select the dates
        print("Dates to select: \(dates)")
        dates.forEach { (date) in
            self.calendar.select(date, scrollToDate: false)
        }

        self.configureVisibleCells()

        //Printing out the dates that have been selected
        var selectedDates = ""
        for selectedDate in self.calendar.selectedDates {
            selectedDates.append("\(self.formatter.string(from: selectedDate)) ")
        }
        print(selectedDates)

        //Scrolling to the next/prev months if applicable
        if monthPosition == .previous || monthPosition == .next {
            calendar.setCurrentPage(date, animated: true)
        }
    }

Screenshots:

screen shot 2018-01-31 at 12 34 42 pm screen shot 2018-01-31 at 12 35 16 pm
fparkar commented 6 years ago

That might be happening because of below lines that are there in the programming.

    let dates = [
        self.gregorian.date(byAdding: .day, value: -1, to: Date()),
        Date(),
        self.gregorian.date(byAdding: .day, value: 1, to: Date())
    ]

What developer is doing, by default selecting yesterday today tomorrow date when you open calendar.